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 com.liferay.portal.kernel.util.GetterUtil;
26  
27  import java.io.Serializable;
28  
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import java.sql.Types;
33  
34  import org.hibernate.Hibernate;
35  import org.hibernate.HibernateException;
36  import org.hibernate.usertype.UserType;
37  
38  /**
39   * <a href="LongType.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class LongType implements UserType {
45  
46      public final static long DEFAULT_VALUE = 0;
47  
48      public final static int[] SQL_TYPES = new int[] {Types.BIGINT};
49  
50      public Object assemble(Serializable cached, Object owner) {
51          return cached;
52      }
53  
54      public Object deepCopy(Object obj) {
55          return obj;
56      }
57  
58      public Serializable disassemble(Object value) {
59          return (Serializable)value;
60      }
61  
62      public boolean equals(Object x, Object y) {
63          if (x == y) {
64              return true;
65          }
66          else if (x == null || y == null) {
67              return false;
68          }
69          else {
70              return x.equals(y);
71          }
72      }
73  
74      public int hashCode(Object x) {
75          return x.hashCode();
76      }
77  
78      public boolean isMutable() {
79          return false;
80      }
81  
82      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
83          throws HibernateException, SQLException {
84  
85          Object value = null;
86  
87          try {
88              value = Hibernate.LONG.nullSafeGet(rs, names[0]);
89          }
90          catch (SQLException sqle1) {
91  
92              // Some JDBC drivers do not know how to convert a VARCHAR column
93              // with a blank entry into a BIGINT
94  
95              try {
96                  value = new Long(GetterUtil.getLong(
97                      (String)Hibernate.STRING.nullSafeGet(rs, names[0])));
98              }
99              catch (SQLException sqle2) {
100                 throw sqle1;
101             }
102         }
103 
104         if (value == null) {
105             return new Long(DEFAULT_VALUE);
106         }
107         else {
108             return value;
109         }
110     }
111 
112     public void nullSafeSet(PreparedStatement ps, Object obj, int index)
113         throws HibernateException, SQLException {
114 
115         if (obj == null) {
116             obj = new Long(DEFAULT_VALUE);
117         }
118 
119         Hibernate.LONG.nullSafeSet(ps, obj, index);
120     }
121 
122     public Object replace(Object original, Object target, Object owner) {
123         return original;
124     }
125 
126     public Class<Long> returnedClass() {
127         return Long.class;
128     }
129 
130     public int[] sqlTypes() {
131         return SQL_TYPES;
132     }
133 
134 }