001: //$Id: AbstractType.java 7793 2005-08-10 05:06:40Z oneovthafew $
002: package org.hibernate.type;
003:
004: import java.io.Serializable;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.util.Map;
008:
009: import org.dom4j.Element;
010: import org.dom4j.Node;
011: import org.hibernate.EntityMode;
012: import org.hibernate.HibernateException;
013: import org.hibernate.engine.SessionFactoryImplementor;
014: import org.hibernate.engine.SessionImplementor;
015: import org.hibernate.util.EqualsHelper;
016:
017: /**
018: * Abstract superclass of the built in Type hierarchy.
019: * @author Gavin King
020: */
021: public abstract class AbstractType implements Type {
022:
023: public boolean isAssociationType() {
024: return false;
025: }
026:
027: public boolean isCollectionType() {
028: return false;
029: }
030:
031: public boolean isComponentType() {
032: return false;
033: }
034:
035: public boolean isEntityType() {
036: return false;
037: }
038:
039: public boolean isXMLElement() {
040: return false;
041: }
042:
043: public int compare(Object x, Object y, EntityMode entityMode) {
044: return ((Comparable) x).compareTo(y);
045: }
046:
047: public Serializable disassemble(Object value,
048: SessionImplementor session, Object owner)
049: throws HibernateException {
050:
051: if (value == null) {
052: return null;
053: } else {
054: return (Serializable) deepCopy(value, session
055: .getEntityMode(), session.getFactory());
056: }
057: }
058:
059: public Object assemble(Serializable cached,
060: SessionImplementor session, Object owner)
061: throws HibernateException {
062: if (cached == null) {
063: return null;
064: } else {
065: return deepCopy(cached, session.getEntityMode(), session
066: .getFactory());
067: }
068: }
069:
070: public boolean isDirty(Object old, Object current,
071: SessionImplementor session) throws HibernateException {
072: return !isSame(old, current, session.getEntityMode());
073: }
074:
075: public Object hydrate(ResultSet rs, String[] names,
076: SessionImplementor session, Object owner)
077: throws HibernateException, SQLException {
078: // TODO: this is very suboptimal for some subclasses (namely components),
079: // since it does not take advantage of two-phase-load
080: return nullSafeGet(rs, names, session, owner);
081: }
082:
083: public Object resolve(Object value, SessionImplementor session,
084: Object owner) throws HibernateException {
085: return value;
086: }
087:
088: public Object semiResolve(Object value, SessionImplementor session,
089: Object owner) throws HibernateException {
090: return value;
091: }
092:
093: public boolean isAnyType() {
094: return false;
095: }
096:
097: public boolean isModified(Object old, Object current,
098: boolean[] checkable, SessionImplementor session)
099: throws HibernateException {
100: return isDirty(old, current, session);
101: }
102:
103: public boolean isSame(Object x, Object y, EntityMode entityMode)
104: throws HibernateException {
105: return isEqual(x, y, entityMode);
106: }
107:
108: public boolean isEqual(Object x, Object y, EntityMode entityMode) {
109: return EqualsHelper.equals(x, y);
110: }
111:
112: public int getHashCode(Object x, EntityMode entityMode) {
113: return x.hashCode();
114: }
115:
116: public boolean isEqual(Object x, Object y, EntityMode entityMode,
117: SessionFactoryImplementor factory) {
118: return isEqual(x, y, entityMode);
119: }
120:
121: public int getHashCode(Object x, EntityMode entityMode,
122: SessionFactoryImplementor factory) {
123: return getHashCode(x, entityMode);
124: }
125:
126: protected static void replaceNode(Node container, Element value) {
127: if (container != value) { //not really necessary, I guess...
128: Element parent = container.getParent();
129: container.detach();
130: value.setName(container.getName());
131: value.detach();
132: parent.add(value);
133: }
134: }
135:
136: public Type getSemiResolvedType(SessionFactoryImplementor factory) {
137: return this ;
138: }
139:
140: public Object replace(Object original, Object target,
141: SessionImplementor session, Object owner, Map copyCache,
142: ForeignKeyDirection foreignKeyDirection)
143: throws HibernateException {
144: boolean include;
145: if (isAssociationType()) {
146: AssociationType atype = (AssociationType) this ;
147: include = atype.getForeignKeyDirection() == foreignKeyDirection;
148: } else {
149: include = ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT == foreignKeyDirection;
150: }
151: return include ? replace(original, target, session, owner,
152: copyCache) : target;
153: }
154:
155: public void beforeAssemble(Serializable cached,
156: SessionImplementor session) {
157: }
158:
159: /*public Object copy(Object original, Object target, SessionImplementor session, Object owner, Map copyCache)
160: throws HibernateException {
161: if (original==null) return null;
162: return assemble( disassemble(original, session), session, owner );
163: }*/
164:
165: }
|