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 java.io.Serializable;
024:
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Types;
029:
030: import org.hibernate.Hibernate;
031: import org.hibernate.HibernateException;
032: import org.hibernate.usertype.UserType;
033:
034: /**
035: * <a href="IntegerType.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: * @author Bruno Farache
039: *
040: */
041: public class IntegerType implements UserType {
042:
043: public final static int DEFAULT_VALUE = 0;
044:
045: public final static int[] SQL_TYPES = new int[] { Types.INTEGER };
046:
047: public Object assemble(Serializable cached, Object owner) {
048: return cached;
049: }
050:
051: public Object deepCopy(Object obj) {
052: return obj;
053: }
054:
055: public Serializable disassemble(Object value) {
056: return (Serializable) value;
057: }
058:
059: public boolean equals(Object x, Object y) {
060: if (x == y) {
061: return true;
062: } else if (x == null || y == null) {
063: return false;
064: } else {
065: return x.equals(y);
066: }
067: }
068:
069: public int hashCode(Object x) {
070: return x.hashCode();
071: }
072:
073: public boolean isMutable() {
074: return false;
075: }
076:
077: public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
078: throws HibernateException, SQLException {
079:
080: Integer value = null;
081:
082: try {
083: value = (Integer) Hibernate.INTEGER.nullSafeGet(rs,
084: names[0]);
085: } catch (SQLException sqle) {
086: }
087:
088: if (value == null) {
089: return new Integer(DEFAULT_VALUE);
090: } else {
091: return value;
092: }
093: }
094:
095: public void nullSafeSet(PreparedStatement ps, Object obj, int index)
096: throws HibernateException, SQLException {
097:
098: if (obj == null) {
099: obj = new Integer(DEFAULT_VALUE);
100: }
101:
102: Hibernate.INTEGER.nullSafeSet(ps, obj, index);
103: }
104:
105: public Object replace(Object original, Object target, Object owner) {
106: return original;
107: }
108:
109: public Class returnedClass() {
110: return Integer.class;
111: }
112:
113: public int[] sqlTypes() {
114: return SQL_TYPES;
115: }
116:
117: }
|