001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: LifeCycleState.java,v 1.7 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.state;
012:
013: import javax.jdo.Transaction;
014: import org.apache.log4j.Category;
015:
016: abstract class LifeCycleState {
017: protected static final Category LOG = Category
018: .getInstance(LifeCycleState.class);
019:
020: public static final int HOLLOW = 0, P_CLEAN = 1, P_DIRTY = 2,
021: P_NEW = 3, P_NEW_DELETED = 4, P_DELETED = 5,
022: P_NONTRANS = 6, T_CLEAN = 7, T_DIRTY = 8, TRANSIENT = 9,
023: TOTAL = 10;
024:
025: protected boolean isPersistent;
026: protected boolean isTransactional;
027: protected boolean isDirty;
028: protected boolean isNew;
029: protected boolean isDeleted;
030:
031: protected int stateType;
032:
033: private static LifeCycleState states[];
034:
035: static {
036: states = new LifeCycleState[TOTAL];
037:
038: states[HOLLOW] = new Hollow();
039: states[P_CLEAN] = new PersistentClean();
040: states[P_DIRTY] = new PersistentDirty();
041: states[P_NEW] = new PersistentNew();
042: states[P_NEW_DELETED] = new PersistentNewDeleted();
043: states[P_DELETED] = new PersistentDeleted();
044: states[P_NONTRANS] = new PersistentNontransactional();
045: states[T_CLEAN] = new TransientClean();
046: states[T_DIRTY] = new TransientDirty();
047: states[TRANSIENT] = null;
048: }
049:
050: /**
051: * Returns the LifeCycleState for the state constant.
052: *
053: * @param stateType the type as integer
054: *
055: * @return the type as LifeCycleState object
056: */
057:
058: public static LifeCycleState getLifeCycleState(int stateType) {
059: return states[stateType];
060: }
061:
062: /**
063: * Returns the type of the life cycle state
064: *
065: * @return the type of this life cycle state
066: */
067:
068: public final int stateType() {
069: return stateType;
070: }
071:
072: protected final LifeCycleState changeState(StateManagerImpl sm,
073: int newStateType) {
074: return changeState(sm, states[newStateType]);
075: }
076:
077: private LifeCycleState changeState(StateManagerImpl sm,
078: LifeCycleState newState) {
079: if (LOG.isDebugEnabled())
080: LOG.debug(sm.toString() + ": " + this + "->" + newState);
081:
082: return newState;
083: }
084:
085: public LifeCycleState transitionRollbackState(StateManagerImpl sm,
086: LifeCycleState oldState) {
087: if (isTransactional) {
088: if (oldState == null || !oldState.isTransactional)
089: sm.evictFromTransaction();
090: } else {
091: if (oldState != null && oldState.isTransactional)
092: sm.enlistInTransaction();
093: }
094:
095: return changeState(sm, oldState);
096: }
097:
098: public LifeCycleState transitionMakePersistent(StateManagerImpl sm,
099: Transaction tx) {
100: return this ;
101: }
102:
103: public LifeCycleState transitionDeletePersistent(
104: StateManagerImpl sm, Transaction tx) {
105: return this ;
106: }
107:
108: public LifeCycleState transitionMakeTransactional(
109: StateManagerImpl sm, Transaction tx) {
110: return this ;
111: }
112:
113: public LifeCycleState transitionMakeNontransactional(
114: StateManagerImpl sm) {
115: return this ;
116: }
117:
118: public LifeCycleState transitionMakeTransient(StateManagerImpl sm) {
119: return this ;
120: }
121:
122: public LifeCycleState transitionCommit(StateManagerImpl sm,
123: Transaction tx) {
124: return this ;
125: }
126:
127: public LifeCycleState transitionRollback(StateManagerImpl sm,
128: Transaction tx) {
129: return this ;
130: }
131:
132: public LifeCycleState transitionRefresh(StateManagerImpl sm,
133: Transaction tx) {
134: return this ;
135: }
136:
137: public LifeCycleState transitionEvict(StateManagerImpl sm) {
138: return this ;
139: }
140:
141: public LifeCycleState transitionReadField(StateManagerImpl sm,
142: Transaction tx) {
143: return this ;
144: }
145:
146: public LifeCycleState transitionWriteField(StateManagerImpl sm,
147: Transaction tx) {
148: return this ;
149: }
150:
151: public LifeCycleState transitionRetrieve(StateManagerImpl sm,
152: Transaction tx, boolean DFGOnly) {
153: return this ;
154: }
155:
156: /**
157: * Returns whether the state is persistent.
158: */
159:
160: public final boolean isPersistent() {
161: return isPersistent;
162: }
163:
164: /**
165: * Returns whether the state is transactional.
166: */
167:
168: public final boolean isTransactional() {
169: return isTransactional;
170: }
171:
172: /**
173: * Returns whether the state is dirty, ie the object has been changed
174: * (created, updated, deleted) in this Tx.
175: */
176:
177: public final boolean isDirty() {
178: return isDirty;
179: }
180:
181: /**
182: * Returns whether the state represents a newly created object.
183: */
184:
185: public final boolean isNew() {
186: return isNew;
187: }
188:
189: /**
190: * Return whether the state represents a deleted object.
191: */
192:
193: public final boolean isDeleted() {
194: return isDeleted;
195: }
196: }
|