1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.orm.hibernate;
24  
25  import java.io.Serializable;
26  
27  import java.sql.PreparedStatement;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Types;
31  
32  import org.hibernate.Hibernate;
33  import org.hibernate.HibernateException;
34  import org.hibernate.usertype.UserType;
35  
36  /**
37   * <a href="IntegerType.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Bruno Farache
41   *
42   */
43  public class IntegerType implements UserType {
44  
45      public final static int DEFAULT_VALUE = 0;
46  
47      public final static int[] SQL_TYPES = new int[] {Types.INTEGER};
48  
49      public Object assemble(Serializable cached, Object owner) {
50          return cached;
51      }
52  
53      public Object deepCopy(Object obj) {
54          return obj;
55      }
56  
57      public Serializable disassemble(Object value) {
58          return (Serializable)value;
59      }
60  
61      public boolean equals(Object x, Object y) {
62          if (x == y) {
63              return true;
64          }
65          else if (x == null || y == null) {
66              return false;
67          }
68          else {
69              return x.equals(y);
70          }
71      }
72  
73      public int hashCode(Object x) {
74          return x.hashCode();
75      }
76  
77      public boolean isMutable() {
78          return false;
79      }
80  
81      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
82          throws HibernateException {
83  
84          Integer value = null;
85  
86          try {
87              value = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
88          }
89          catch (SQLException sqle) {
90          }
91  
92          if (value == null) {
93              return new Integer(DEFAULT_VALUE);
94          }
95          else {
96              return value;
97          }
98      }
99  
100     public void nullSafeSet(PreparedStatement ps, Object obj, int index)
101         throws HibernateException, SQLException {
102 
103         if (obj == null) {
104             obj = new Integer(DEFAULT_VALUE);
105         }
106 
107         Hibernate.INTEGER.nullSafeSet(ps, obj, index);
108     }
109 
110     public Object replace(Object original, Object target, Object owner) {
111         return original;
112     }
113 
114     public Class<Integer> returnedClass() {
115         return Integer.class;
116     }
117 
118     public int[] sqlTypes() {
119         return SQL_TYPES;
120     }
121 
122 }