1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.dao.orm.hibernate;
21  
22  import java.io.Serializable;
23  
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.sql.Types;
28  
29  import org.hibernate.Hibernate;
30  import org.hibernate.HibernateException;
31  import org.hibernate.usertype.UserType;
32  
33  /**
34   * <a href="IntegerType.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Bruno Farache
38   *
39   */
40  public class IntegerType implements UserType {
41  
42      public final static int DEFAULT_VALUE = 0;
43  
44      public final static int[] SQL_TYPES = new int[] {Types.INTEGER};
45  
46      public Object assemble(Serializable cached, Object owner) {
47          return cached;
48      }
49  
50      public Object deepCopy(Object obj) {
51          return obj;
52      }
53  
54      public Serializable disassemble(Object value) {
55          return (Serializable)value;
56      }
57  
58      public boolean equals(Object x, Object y) {
59          if (x == y) {
60              return true;
61          }
62          else if (x == null || y == null) {
63              return false;
64          }
65          else {
66              return x.equals(y);
67          }
68      }
69  
70      public int hashCode(Object x) {
71          return x.hashCode();
72      }
73  
74      public boolean isMutable() {
75          return false;
76      }
77  
78      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
79          throws HibernateException {
80  
81          Integer value = null;
82  
83          try {
84              value = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
85          }
86          catch (SQLException sqle) {
87          }
88  
89          if (value == null) {
90              return new Integer(DEFAULT_VALUE);
91          }
92          else {
93              return value;
94          }
95      }
96  
97      public void nullSafeSet(PreparedStatement ps, Object obj, int index)
98          throws HibernateException, SQLException {
99  
100         if (obj == null) {
101             obj = new Integer(DEFAULT_VALUE);
102         }
103 
104         Hibernate.INTEGER.nullSafeSet(ps, obj, index);
105     }
106 
107     public Object replace(Object original, Object target, Object owner) {
108         return original;
109     }
110 
111     public Class<Integer> returnedClass() {
112         return Integer.class;
113     }
114 
115     public int[] sqlTypes() {
116         return SQL_TYPES;
117     }
118 
119 }