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.KualiPercent;
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 HibernateKualiIntegerPercentFieldType 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            // Check for null, and verify object type.
045            // Do conversion if our type is correct (BigDecimal).
046            if (source != null && source instanceof BigDecimal) {
047                BigDecimal converted = (BigDecimal) source;
048    
049                // Once we have converted, we need to convert again to KualiPercent.
050                KualiPercent percentConverted = new KualiPercent((BigDecimal) converted);
051    
052                return percentConverted;
053    
054            }
055            else {
056                return null;
057            }
058            }
059    
060            /**
061             * This overridden method ...
062             * 
063             * @see HibernateImmutableValueUserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
064             */
065            @Override
066            public void nullSafeSet(PreparedStatement st, Object source, int index)
067                            throws HibernateException, SQLException {
068                   Object converted = source;
069    
070                    if (source instanceof KualiPercent) {
071                        converted = ((KualiPercent) source).bigDecimalValue();
072                    }
073    
074                    if (converted == null) {
075                            st.setNull(index, Types.DECIMAL); 
076                    } else {
077                            st.setBigDecimal(index, ((BigDecimal)converted));
078                    }
079            }
080    
081            /**
082             * This overridden method ...
083             * 
084             * @see HibernateImmutableValueUserType#returnedClass()
085             */
086            public Class returnedClass() {
087                    return BigDecimal.class;
088            }
089    
090            /**
091             * Returns an array with the SQL VARCHAR type as the single member
092             * 
093             * @see org.hibernate.usertype.UserType#sqlTypes()
094             */
095            public int[] sqlTypes() {
096                    return new int[] { Types.DECIMAL };
097            }
098    
099    }