001: /**********************************************************************
002: Copyright (c) 2002 Kelly Grizzle (TJDO) and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2003 Andy Jefferson - commented
018: 2003 Andy Jefferson - added localiser and Logger
019: 2005 Andy Jefferson - added Detached clean/dirty states
020: ...
021: **********************************************************************/package org.jpox.state;
022:
023: import org.jpox.FetchPlan;
024: import org.jpox.StateManager;
025: import org.jpox.util.JPOXLogger;
026: import org.jpox.util.Localiser;
027: import org.jpox.util.StringUtils;
028:
029: /**
030: * Base Class representing the life cycle state. Implemented for individual
031: * states.
032: *
033: * @version $Revision: 1.26 $
034: **/
035: public abstract class LifeCycleState {
036: protected static final Localiser LOCALISER = Localiser
037: .getInstance("org.jpox.Localisation");
038:
039: /** transient **/
040: public static final int TRANSIENT = 0;
041: /** Persistent-New **/
042: public static final int P_NEW = 1;
043: /** Persistent-Clean **/
044: public static final int P_CLEAN = 2;
045: /** Persistent-Dirty **/
046: public static final int P_DIRTY = 3;
047: /** Hollow **/
048: public static final int HOLLOW = 4;
049: /** Transaction-Clean **/
050: public static final int T_CLEAN = 5;
051: /** Transaction-Dirty **/
052: public static final int T_DIRTY = 6;
053: /** Persistent-New-Deleted **/
054: public static final int P_NEW_DELETED = 7;
055: /** Persistent-Deleted **/
056: public static final int P_DELETED = 8;
057: /** Persistent-NonTransactional **/
058: public static final int P_NONTRANS = 9;
059: /** Persistent-NonTransactionalDirty **/
060: public static final int P_NONTRANS_DIRTY = 10;
061: /** Detached-Clean **/
062: public static final int DETACHED_CLEAN = 11;
063: /** Detached-Dirty **/
064: public static final int DETACHED_DIRTY = 12;
065:
066: /** total number of states **/
067: public static final int TOTAL = 13;
068: /** illegal state **/
069: public static final int ILLEGAL_STATE = 13;
070:
071: // These are state flags that are set to required valuers in specific state
072: protected boolean isDirty;
073: protected boolean isNew;
074: protected boolean isDeleted;
075: protected boolean isTransactional;
076: protected boolean isPersistent;
077:
078: protected int stateType;
079:
080: /**
081: * Returns the type of the life cycle state
082: * @return the type of this life cycle state
083: */
084: public final int stateType() {
085: return stateType;
086: }
087:
088: /**
089: * Utility to change state to a new state.
090: * @param sm The state manager.
091: * @param newStateType The new state
092: * @return new LifeCycle state.
093: **/
094: protected final LifeCycleState changeState(StateManager sm,
095: int newStateType) {
096: LifeCycleState newState = sm.getObjectManager().getOMFContext()
097: .getApiAdapter().getLifeCycleState(newStateType);
098:
099: if (JPOXLogger.LIFECYCLE.isDebugEnabled()) {
100: JPOXLogger.LIFECYCLE.debug(LOCALISER.msg("027016",
101: StringUtils.toJVMIDString(sm.getObject()), sm
102: .getInternalObjectId().toString(), this ,
103: newState));
104: }
105:
106: if (isTransactional) {
107: if (newState == null || !newState.isTransactional) {
108: sm.evictFromTransaction();
109: }
110: } else {
111: if (newState != null && newState.isTransactional) {
112: sm.enlistInTransaction();
113: }
114: }
115:
116: if (newState == null) {
117: sm.disconnect();
118: }
119:
120: return newState;
121: }
122:
123: /**
124: * Utility to change state to a new state.
125: *
126: * @param sm The state manager.
127: * @param newStateType The new state
128: * @return new LifeCycle state.
129: **/
130: protected final LifeCycleState changeTransientState(
131: StateManager sm, int newStateType) {
132: LifeCycleState newState = sm.getObjectManager().getOMFContext()
133: .getApiAdapter().getLifeCycleState(newStateType);
134:
135: try {
136: sm.enlistInTransaction();
137: } catch (Exception e) {
138: //sm is already enlisted
139: }
140:
141: return newState;
142: }
143:
144: /**
145: * Method to transition to persistent state.
146: * @param sm StateManager.
147: * @return new LifeCycle state.
148: **/
149: public LifeCycleState transitionMakePersistent(StateManager sm) {
150: return this ;
151: }
152:
153: /**
154: * Method to transition to delete persistent state.
155: * @param sm StateManager.
156: * @return new LifeCycle state.
157: **/
158: public LifeCycleState transitionDeletePersistent(StateManager sm) {
159: return this ;
160: }
161:
162: /**
163: * Method to transition to transactional state.
164: * @param sm StateManager.
165: * @return new LifeCycle state.
166: **/
167: public LifeCycleState transitionMakeTransactional(StateManager sm) {
168: return this ;
169: }
170:
171: /**
172: * Method to transition to nontransactional state.
173: * @param sm StateManager.
174: * @return new LifeCycle state.
175: **/
176: public LifeCycleState transitionMakeNontransactional(StateManager sm) {
177: return this ;
178: }
179:
180: /**
181: * Method to transition to transient state.
182: * @param sm StateManager.
183: * @param useFetchPlan to make transient the fields in the fetch plan
184: * @return new LifeCycle state.
185: **/
186: public LifeCycleState transitionMakeTransient(StateManager sm,
187: boolean useFetchPlan, boolean detachAllOnCommit) {
188: return this ;
189: }
190:
191: /**
192: * Method to transition to transaction begin state.
193: * @param sm StateManager.
194: * @param tx Transaction.
195: * @return new LifeCycle state.
196: **/
197: public LifeCycleState transitionBegin(StateManager sm,
198: org.jpox.Transaction tx) {
199: return this ;
200: }
201:
202: /**
203: * Method to transition to commit state.
204: * @param sm StateManager.
205: * @param tx the Transaction been committed.
206: * @return new LifeCycle state.
207: **/
208: public LifeCycleState transitionCommit(StateManager sm,
209: org.jpox.Transaction tx) {
210: return this ;
211: }
212:
213: /**
214: * Method to transition to rollback state.
215: * @param sm StateManager.
216: * @param tx Transaction.
217: * @return new LifeCycle state.
218: **/
219: public LifeCycleState transitionRollback(StateManager sm,
220: org.jpox.Transaction tx) {
221: return this ;
222: }
223:
224: /**
225: * Method to transition to refresh state.
226: * @param sm StateManager.
227: * @return new LifeCycle state.
228: **/
229: public LifeCycleState transitionRefresh(StateManager sm) {
230: return this ;
231: }
232:
233: /**
234: * Method to transition to evict state.
235: * @param sm StateManager.
236: * @return new LifeCycle state.
237: **/
238: public LifeCycleState transitionEvict(StateManager sm) {
239: return this ;
240: }
241:
242: /**
243: * Method to transition to read-field state.
244: * @param sm StateManager.
245: * @param isLoaded if the field was previously loaded
246: * @return new LifeCycle state.
247: **/
248: public LifeCycleState transitionReadField(StateManager sm,
249: boolean isLoaded) {
250: return this ;
251: }
252:
253: /**
254: * Method to transition to write-field state.
255: * @param sm StateManager.
256: * @return new LifeCycle state.
257: **/
258: public LifeCycleState transitionWriteField(StateManager sm) {
259: return this ;
260: }
261:
262: /**
263: * Method to transition to retrieve state.
264: * @param sm StateManager.
265: * @param fgOnly only retrieve the current fetch group fields
266: * @return new LifeCycle state.
267: **/
268: public LifeCycleState transitionRetrieve(StateManager sm,
269: boolean fgOnly) {
270: return this ;
271: }
272:
273: /**
274: * Method to transition to retrieve state.
275: * @param sm StateManager.
276: * @param fetchPlan the fetch plan to load fields
277: * @return new LifeCycle state.
278: **/
279: public LifeCycleState transitionRetrieve(StateManager sm,
280: FetchPlan fetchPlan) {
281: return this ;
282: }
283:
284: /**
285: * Method to transition to detached-clean.
286: * @param sm StateManager.
287: * @return new LifeCycle state.
288: **/
289: public LifeCycleState transitionDetach(StateManager sm) {
290: return this ;
291: }
292:
293: /**
294: * Method to transition to persistent-clean.
295: * @param sm StateManager.
296: * @return new LifeCycle state.
297: **/
298: public LifeCycleState transitionAttach(StateManager sm) {
299: return this ;
300: }
301:
302: /**
303: * Method to transition when serialised.
304: * @param sm State Manager
305: * @return The new LifeCycle state
306: */
307: public LifeCycleState transitionSerialize(StateManager sm) {
308: return this ;
309: }
310:
311: /**
312: * Return whether the object is dirty, ie has been changed
313: * (created, updated, deleted) in this Tx.
314: * @return Whether the object is dirty.
315: */
316: public final boolean isDirty() {
317: return isDirty;
318: }
319:
320: /**
321: * Return whether the object was newly created.
322: * @return Whether the object is new.
323: */
324: public final boolean isNew() {
325: return isNew;
326: }
327:
328: /**
329: * Return whether the object is deleted.
330: * @return Whether the object is deleted.
331: */
332: public final boolean isDeleted() {
333: return isDeleted;
334: }
335:
336: /**
337: * Return whether the object is transactional.
338: * @return Whether the object is transactional.
339: */
340: public final boolean isTransactional() {
341: return isTransactional;
342: }
343:
344: /**
345: * Return whether the object is persistent.
346: * @return Whether the object is persistent.
347: */
348: public final boolean isPersistent() {
349: return isPersistent;
350: }
351:
352: /**
353: * Method to return a string version of this object.
354: * @return String version of the object.
355: **/
356: public abstract String toString();
357: }
|