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