001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.dao.hibernate;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024:
025: import java.io.Serializable;
026:
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import java.sql.Types;
031:
032: import org.hibernate.Hibernate;
033: import org.hibernate.HibernateException;
034: import org.hibernate.usertype.UserType;
035:
036: /**
037: * <a href="LongType.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class LongType implements UserType {
043:
044: public final static long DEFAULT_VALUE = 0;
045:
046: public final static int[] SQL_TYPES = new int[] { Types.BIGINT };
047:
048: public Object assemble(Serializable cached, Object owner) {
049: return cached;
050: }
051:
052: public Object deepCopy(Object obj) {
053: return obj;
054: }
055:
056: public Serializable disassemble(Object value) {
057: return (Serializable) value;
058: }
059:
060: public boolean equals(Object x, Object y) {
061: if (x == y) {
062: return true;
063: } else if (x == null || y == null) {
064: return false;
065: } else {
066: return x.equals(y);
067: }
068: }
069:
070: public int hashCode(Object x) {
071: return x.hashCode();
072: }
073:
074: public boolean isMutable() {
075: return false;
076: }
077:
078: public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
079: throws HibernateException, SQLException {
080:
081: Object value = null;
082:
083: try {
084: value = Hibernate.LONG.nullSafeGet(rs, names[0]);
085: } catch (SQLException sqle1) {
086:
087: // Some JDBC drivers do not know how to convert a VARCHAR column
088: // with a blank entry into a BIGINT
089:
090: try {
091: value = new Long(GetterUtil
092: .getLong((String) Hibernate.STRING.nullSafeGet(
093: rs, names[0])));
094: } catch (SQLException sqle2) {
095: throw sqle1;
096: }
097: }
098:
099: if (value == null) {
100: return new Long(DEFAULT_VALUE);
101: } else {
102: return value;
103: }
104: }
105:
106: public void nullSafeSet(PreparedStatement ps, Object obj, int index)
107: throws HibernateException, SQLException {
108:
109: if (obj == null) {
110: obj = new Long(DEFAULT_VALUE);
111: }
112:
113: Hibernate.LONG.nullSafeSet(ps, obj, index);
114: }
115:
116: public Object replace(Object original, Object target, Object owner) {
117: return original;
118: }
119:
120: public Class returnedClass() {
121: return Long.class;
122: }
123:
124: public int[] sqlTypes() {
125: return SQL_TYPES;
126: }
127:
128: }
|