1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class IntegerType implements Serializable, UserType {
43  
44      public final static int DEFAULT_VALUE = 0;
45  
46      public final static int[] SQL_TYPES = new int[] {Types.INTEGER};
47  
48      public Object assemble(Serializable cached, Object owner) {
49          return cached;
50      }
51  
52      public Object deepCopy(Object obj) {
53          return obj;
54      }
55  
56      public Serializable disassemble(Object value) {
57          return (Serializable)value;
58      }
59  
60      public boolean equals(Object x, Object y) {
61          if (x == y) {
62              return true;
63          }
64          else if (x == null || y == null) {
65              return false;
66          }
67          else {
68              return x.equals(y);
69          }
70      }
71  
72      public int hashCode(Object x) {
73          return x.hashCode();
74      }
75  
76      public boolean isMutable() {
77          return false;
78      }
79  
80      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
81          throws HibernateException {
82  
83          Integer value = null;
84  
85          try {
86              value = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
87          }
88          catch (SQLException sqle) {
89          }
90  
91          if (value == null) {
92              return new Integer(DEFAULT_VALUE);
93          }
94          else {
95              return value;
96          }
97      }
98  
99      public void nullSafeSet(PreparedStatement ps, Object obj, int index)
100         throws HibernateException, SQLException {
101 
102         if (obj == null) {
103             obj = new Integer(DEFAULT_VALUE);
104         }
105 
106         Hibernate.INTEGER.nullSafeSet(ps, obj, index);
107     }
108 
109     public Object replace(Object original, Object target, Object owner) {
110         return original;
111     }
112 
113     public Class<Integer> returnedClass() {
114         return Integer.class;
115     }
116 
117     public int[] sqlTypes() {
118         return SQL_TYPES;
119     }
120 
121 }