001: package org.hibernate.intercept;
002:
003: import org.hibernate.engine.SessionImplementor;
004: import org.hibernate.LazyInitializationException;
005:
006: import java.util.Set;
007: import java.io.Serializable;
008:
009: /**
010: * @author Steve Ebersole
011: */
012: public abstract class AbstractFieldInterceptor implements
013: FieldInterceptor, Serializable {
014:
015: private transient SessionImplementor session;
016: private Set uninitializedFields;
017: private final String entityName;
018:
019: private transient boolean initializing;
020: private boolean dirty;
021:
022: protected AbstractFieldInterceptor(SessionImplementor session,
023: Set uninitializedFields, String entityName) {
024: this .session = session;
025: this .uninitializedFields = uninitializedFields;
026: this .entityName = entityName;
027: }
028:
029: // FieldInterceptor impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
030:
031: public final void setSession(SessionImplementor session) {
032: this .session = session;
033: }
034:
035: public final boolean isInitialized() {
036: return uninitializedFields == null
037: || uninitializedFields.size() == 0;
038: }
039:
040: public final boolean isInitialized(String field) {
041: return uninitializedFields == null
042: || !uninitializedFields.contains(field);
043: }
044:
045: public final void dirty() {
046: dirty = true;
047: }
048:
049: public final boolean isDirty() {
050: return dirty;
051: }
052:
053: public final void clearDirty() {
054: dirty = false;
055: }
056:
057: // subclass accesses ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
058:
059: protected final Object intercept(Object target, String fieldName,
060: Object value) {
061: if (initializing) {
062: return value;
063: }
064:
065: if (uninitializedFields != null
066: && uninitializedFields.contains(fieldName)) {
067: if (session == null) {
068: throw new LazyInitializationException(
069: "entity with lazy properties is not associated with a session");
070: } else if (!session.isOpen() || !session.isConnected()) {
071: throw new LazyInitializationException(
072: "session is not connected");
073: }
074:
075: final Object result;
076: initializing = true;
077: try {
078: result = ((LazyPropertyInitializer) session
079: .getFactory().getEntityPersister(entityName))
080: .initializeLazyProperty(fieldName, target,
081: session);
082: } finally {
083: initializing = false;
084: }
085: uninitializedFields = null; //let's assume that there is only one lazy fetch group, for now!
086: return result;
087: } else {
088: return value;
089: }
090: }
091:
092: public final SessionImplementor getSession() {
093: return session;
094: }
095:
096: public final Set getUninitializedFields() {
097: return uninitializedFields;
098: }
099:
100: public final String getEntityName() {
101: return entityName;
102: }
103:
104: public final boolean isInitializing() {
105: return initializing;
106: }
107: }
|