1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  
19  import java.io.Serializable;
20  
21  import java.sql.PreparedStatement;
22  import java.sql.ResultSet;
23  import java.sql.SQLException;
24  import java.sql.Types;
25  
26  import org.hibernate.Hibernate;
27  import org.hibernate.HibernateException;
28  import org.hibernate.usertype.UserType;
29  
30  /**
31   * <a href="LongType.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class LongType implements Serializable, UserType {
36  
37      public final static long DEFAULT_VALUE = 0;
38  
39      public final static int[] SQL_TYPES = new int[] {Types.BIGINT};
40  
41      public Object assemble(Serializable cached, Object owner) {
42          return cached;
43      }
44  
45      public Object deepCopy(Object obj) {
46          return obj;
47      }
48  
49      public Serializable disassemble(Object value) {
50          return (Serializable)value;
51      }
52  
53      public boolean equals(Object x, Object y) {
54          if (x == y) {
55              return true;
56          }
57          else if (x == null || y == null) {
58              return false;
59          }
60          else {
61              return x.equals(y);
62          }
63      }
64  
65      public int hashCode(Object x) {
66          return x.hashCode();
67      }
68  
69      public boolean isMutable() {
70          return false;
71      }
72  
73      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
74          throws HibernateException, SQLException {
75  
76          Object value = null;
77  
78          try {
79              value = Hibernate.LONG.nullSafeGet(rs, names[0]);
80          }
81          catch (SQLException sqle1) {
82  
83              // Some JDBC drivers do not know how to convert a VARCHAR column
84              // with a blank entry into a BIGINT
85  
86              try {
87                  value = new Long(GetterUtil.getLong(
88                      (String)Hibernate.STRING.nullSafeGet(rs, names[0])));
89              }
90              catch (SQLException sqle2) {
91                  throw sqle1;
92              }
93          }
94  
95          if (value == null) {
96              return new Long(DEFAULT_VALUE);
97          }
98          else {
99              return value;
100         }
101     }
102 
103     public void nullSafeSet(PreparedStatement ps, Object obj, int index)
104         throws HibernateException, SQLException {
105 
106         if (obj == null) {
107             obj = new Long(DEFAULT_VALUE);
108         }
109 
110         Hibernate.LONG.nullSafeSet(ps, obj, index);
111     }
112 
113     public Object replace(Object original, Object target, Object owner) {
114         return original;
115     }
116 
117     public Class<Long> returnedClass() {
118         return Long.class;
119     }
120 
121     public int[] sqlTypes() {
122         return SQL_TYPES;
123     }
124 
125 }