1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.type;
26
27 import java.io.Serializable;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.util.Map;
31
32 import org.dom4j.Element;
33 import org.dom4j.Node;
34 import org.hibernate.EntityMode;
35 import org.hibernate.HibernateException;
36 import org.hibernate.engine.SessionFactoryImplementor;
37 import org.hibernate.engine.SessionImplementor;
38 import org.hibernate.util.EqualsHelper;
39
40 /**
41 * Abstract superclass of the built in Type hierarchy.
42 *
43 * @author Gavin King
44 */
45 public abstract class AbstractType implements Type {
46
47 public boolean isAssociationType() {
48 return false;
49 }
50
51 public boolean isCollectionType() {
52 return false;
53 }
54
55 public boolean isComponentType() {
56 return false;
57 }
58
59 public boolean isEntityType() {
60 return false;
61 }
62
63 public boolean isXMLElement() {
64 return false;
65 }
66
67 public int compare(Object x, Object y, EntityMode entityMode) {
68 return ( (Comparable) x ).compareTo(y);
69 }
70
71 public Serializable disassemble(Object value, SessionImplementor session, Object owner)
72 throws HibernateException {
73
74 if (value==null) {
75 return null;
76 }
77 else {
78 return (Serializable) deepCopy( value, session.getEntityMode(), session.getFactory() );
79 }
80 }
81
82 public Object assemble(Serializable cached, SessionImplementor session, Object owner)
83 throws HibernateException {
84 if ( cached==null ) {
85 return null;
86 }
87 else {
88 return deepCopy( cached, session.getEntityMode(), session.getFactory() );
89 }
90 }
91
92 public boolean isDirty(Object old, Object current, SessionImplementor session)
93 throws HibernateException {
94 return !isSame( old, current, session.getEntityMode() );
95 }
96
97 public Object hydrate(
98 ResultSet rs,
99 String[] names,
100 SessionImplementor session,
101 Object owner)
102 throws HibernateException, SQLException {
103 // TODO: this is very suboptimal for some subclasses (namely components),
104 // since it does not take advantage of two-phase-load
105 return nullSafeGet(rs, names, session, owner);
106 }
107
108 public Object resolve(Object value, SessionImplementor session, Object owner)
109 throws HibernateException {
110 return value;
111 }
112
113 public Object semiResolve(Object value, SessionImplementor session, Object owner)
114 throws HibernateException {
115 return value;
116 }
117
118 public boolean isAnyType() {
119 return false;
120 }
121
122 public boolean isModified(Object old, Object current, boolean[] checkable, SessionImplementor session)
123 throws HibernateException {
124 return isDirty(old, current, session);
125 }
126
127 public boolean isSame(Object x, Object y, EntityMode entityMode) throws HibernateException {
128 return isEqual(x, y, entityMode);
129 }
130
131 public boolean isEqual(Object x, Object y, EntityMode entityMode) {
132 return EqualsHelper.equals(x, y);
133 }
134
135 public int getHashCode(Object x, EntityMode entityMode) {
136 return x.hashCode();
137 }
138
139 public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory) {
140 return isEqual(x, y, entityMode);
141 }
142
143 public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory) {
144 return getHashCode(x, entityMode);
145 }
146
147 protected static void replaceNode(Node container, Element value) {
148 if ( container!=value ) { //not really necessary, I guess...
149 Element parent = container.getParent();
150 container.detach();
151 value.setName( container.getName() );
152 value.detach();
153 parent.add(value);
154 }
155 }
156
157 public Type getSemiResolvedType(SessionFactoryImplementor factory) {
158 return this;
159 }
160
161 public Object replace(
162 Object original,
163 Object target,
164 SessionImplementor session,
165 Object owner,
166 Map copyCache,
167 ForeignKeyDirection foreignKeyDirection)
168 throws HibernateException {
169 boolean include;
170 if ( isAssociationType() ) {
171 AssociationType atype = (AssociationType) this;
172 include = atype.getForeignKeyDirection()==foreignKeyDirection;
173 }
174 else {
175 include = ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT==foreignKeyDirection;
176 }
177 return include ? replace(original, target, session, owner, copyCache) : target;
178 }
179
180 public void beforeAssemble(Serializable cached, SessionImplementor session) {}
181
182 /*public Object copy(Object original, Object target, SessionImplementor session, Object owner, Map copyCache)
183 throws HibernateException {
184 if (original==null) return null;
185 return assemble( disassemble(original, session), session, owner );
186 }*/
187
188 }