001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.core.framework.persistence.jpa.type;
017    
018    import java.io.Serializable;
019    import java.sql.PreparedStatement;
020    import java.sql.ResultSet;
021    import java.sql.SQLException;
022    
023    import org.hibernate.HibernateException;
024    import org.hibernate.usertype.UserType;
025    
026    /**
027     * This is a description of what this class does - jksmith don't forget to fill this in. 
028     * 
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     *
031     */
032    public abstract class HibernateImmutableValueUserType implements UserType {
033    
034            /**
035             * As the object is immutable, the immutable value will simply stay in the cache
036             * 
037             * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
038             */
039            public Object assemble(Serializable cached, Object owner) throws HibernateException {
040                    return cached;
041            }
042    
043            /**
044             * As the object is immutable, simply returns the value
045             * 
046             * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
047             */
048            public Object deepCopy(Object value) throws HibernateException {
049                    return value;
050            }
051    
052            /**
053             * As the object is immutable, returns the value if it is Serializable - otherwise returns null
054             * 
055             * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
056             */
057            public Serializable disassemble(Object value) throws HibernateException {
058                    return (value instanceof Serializable) ? (Serializable)value : null;
059            }
060    
061            /**
062             * As the object is immutable, assumes it has a good equals method on it
063             * 
064             * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
065             */
066            public boolean equals(Object x, Object y) throws HibernateException {
067                    return (x == y) || (x != null && x.equals(y));
068            }
069    
070            /**
071             * Returns the hashcode of the passed in value
072             * 
073             * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
074             */
075            public int hashCode(Object value) throws HibernateException {
076                    return value.hashCode();
077            }
078    
079            /**
080             * Immutable types aren't mutable...kind of by definintion
081             * 
082             * @see org.hibernate.usertype.UserType#isMutable()
083             */
084            public boolean isMutable() {
085                    return false;
086            }
087    
088            /**
089             * Abstract method for children classes to override - this returns the SQL value from the given ResultSet into a Java value
090             * 
091             * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
092             */
093            public abstract Object nullSafeGet(ResultSet rs, String[] names, Object owner)throws HibernateException, SQLException;
094    
095            /**
096             * Abstract method for children to override - takes a Java value and changes it to be correctly added to the given PreparedStatement
097             * 
098             * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
099             */
100            public abstract void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException;
101    
102            /**
103             * Since the original is immutable, returns the original
104             * 
105             * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
106             */
107            public Object replace(Object original, Object target, Object owner) throws HibernateException {
108                    return original;
109            }
110    
111            /**
112             * Abstract method for children to override - returns the class of the Java value returned by nullSafeGet
113             * 
114             * @see org.hibernate.usertype.UserType#returnedClass()
115             */
116            public abstract Class returnedClass();
117    
118            /**
119             * Abstract method for children to override - returns the SQL type set in nullSafeSet
120             * 
121             * @see org.hibernate.usertype.UserType#sqlTypes()
122             */
123            public abstract int[] sqlTypes();
124    }