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 org.hibernate.HibernateException;
019    import org.kuali.rice.core.api.util.type.KualiDecimal;
020    
021    import java.math.BigDecimal;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    import java.sql.SQLException;
025    import java.sql.Types;
026    
027    /**
028     * This is a description of what this class does - g1zhang don't forget to fill this in. 
029     * 
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     *
032     */
033    public class HibernateKualiDecimalFieldType extends
034    HibernateImmutableValueUserType {
035    
036            /**
037             * This overridden method ...
038             * 
039             * @see HibernateImmutableValueUserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
040             */
041            @Override
042            public Object nullSafeGet(ResultSet rs, String[] names, Object source)
043            throws HibernateException, SQLException {
044                    BigDecimal objectToConvert = rs.getBigDecimal(names[0]);
045    
046                    if (objectToConvert != null) {
047                            return new KualiDecimal(objectToConvert);
048                    }
049    
050                    return null;
051            }
052    
053            /**
054             * This overridden method ...
055             * 
056             * @see HibernateImmutableValueUserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
057             */
058            @Override
059            public void nullSafeSet(PreparedStatement st, Object source, int index)
060            throws HibernateException, SQLException {
061                    
062                    Object converted =getConverted(source);
063    
064                    if (converted == null) {
065                            st.setNull(index, Types.DECIMAL);
066                    } else {
067                            st.setBigDecimal(index, (BigDecimal)converted);
068                    }
069    
070            }
071    
072            public Object getConverted(Object value){
073    
074                    Object source = null;
075    
076                    if (value instanceof KualiDecimal) {
077                            source = ((KualiDecimal) value).bigDecimalValue();
078                    } else if (value instanceof BigDecimal) {
079                            source = value;
080                    }
081                    return source;
082            }
083    
084            /**
085             * Returns String.class
086             * 
087             * @see org.hibernate.usertype.UserType#returnedClass()
088             */
089            public Class returnedClass() {
090                    return BigDecimal.class;
091            }
092    
093            /**
094             * Returns an array with the SQL VARCHAR type as the single member
095             * 
096             * @see org.hibernate.usertype.UserType#sqlTypes()
097             */
098            public int[] sqlTypes() {
099                    return new int[] { Types.DECIMAL };
100            }
101            
102            //for sub types
103            public Object getConvertedToKualiDecimal(Object value){
104                    Object converted = null;
105                    if (value instanceof BigDecimal) 
106                            converted = new KualiDecimal((BigDecimal) value);
107                    return converted;
108            }
109            public Object getConvertedToBigDecimal(Object value){
110    
111                    Object converted = null;
112    
113                    if (value instanceof KualiDecimal) {
114                            converted = ((KualiDecimal) value).bigDecimalValue();
115                    }
116                    return converted;
117            }
118    
119    }