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