001: /**********************************************************************
002: Copyright (c) 2002 Kelly Grizzle 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: 2002 Mike Martin - unknown changes
018: 2003 Andy Jefferson - commented
019: ...
020: **********************************************************************/package org.jpox.jdo.state;
021:
022: import org.jpox.FetchPlan;
023: import org.jpox.Transaction;
024: import org.jpox.state.LifeCycleState;
025: import org.jpox.StateManager;
026:
027: /**
028: * Class representing the life cycle state of PersistentClean.
029: *
030: * @version $Revision: 1.7 $
031: **/
032: class PersistentClean extends LifeCycleState {
033: /** Protected Constructor to prevent external instantiation. */
034: protected PersistentClean() {
035: isPersistent = true;
036: isDirty = false;
037: isNew = false;
038: isDeleted = false;
039: isTransactional = true;
040:
041: stateType = P_CLEAN;
042: }
043:
044: /**
045: * Method to transition to delete persistent.
046: * @param sm StateManager.
047: * @return new LifeCycle state.
048: **/
049: public LifeCycleState transitionDeletePersistent(StateManager sm) {
050: sm.clearLoadedFlags();
051: return changeState(sm, P_DELETED);
052: }
053:
054: /**
055: * Method to transition to nontransactional.
056: * @param sm StateManager.
057: * @return new LifeCycle state.
058: **/
059: public LifeCycleState transitionMakeNontransactional(StateManager sm) {
060: sm.clearSavedFields();
061: return changeState(sm, P_NONTRANS);
062: }
063:
064: /**
065: * Method to transition to transient.
066: * @param sm StateManager.
067: * @param useFetchPlan to make transient the fields in the fetch plan
068: * @return new LifeCycle state.
069: **/
070: public LifeCycleState transitionMakeTransient(StateManager sm,
071: boolean useFetchPlan, boolean detachAllOnCommit) {
072: if (useFetchPlan) {
073: sm.loadUnloadedFieldsInFetchPlan();
074: }
075: return changeState(sm, TRANSIENT);
076: }
077:
078: /**
079: * Method to transition to commit state.
080: * @param sm StateManager.
081: * @param tx the Transaction been committed.
082: * @return new LifeCycle state.
083: **/
084: public LifeCycleState transitionCommit(StateManager sm,
085: Transaction tx) {
086: sm.clearSavedFields();
087:
088: if (tx.getRetainValues()) {
089: return changeState(sm, P_NONTRANS);
090: } else {
091: sm.clearNonPrimaryKeyFields();
092: return changeState(sm, HOLLOW);
093: }
094: }
095:
096: /**
097: * Method to transition to rollback state.
098: * @param sm StateManager.
099: * @param tx The Transaction
100: * @return new LifeCycle state.
101: **/
102: public LifeCycleState transitionRollback(StateManager sm,
103: Transaction tx) {
104: if (tx.getRestoreValues()) {
105: sm.restoreFields();
106: return changeState(sm, P_NONTRANS);
107: } else {
108: sm.clearNonPrimaryKeyFields();
109: sm.clearSavedFields();
110: return changeState(sm, HOLLOW);
111: }
112: }
113:
114: /**
115: * Method to transition to evict state.
116: * @param sm StateManager.
117: * @return new LifeCycle state.
118: **/
119: public LifeCycleState transitionEvict(StateManager sm) {
120: sm.clearNonPrimaryKeyFields();
121: sm.clearSavedFields();
122: return changeState(sm, HOLLOW);
123: }
124:
125: /**
126: * Method to transition to write-field state.
127: * @param sm StateManager.
128: * @return new LifeCycle state.
129: **/
130: public LifeCycleState transitionWriteField(StateManager sm) {
131: Transaction tx = sm.getObjectManager().getTransaction();
132: if (tx.getRestoreValues()) {
133: sm.saveFields();
134: }
135:
136: return changeState(sm, P_DIRTY);
137: }
138:
139: /**
140: * Method to transition to refresh state.
141: * @param sm StateManager.
142: * @return new LifeCycle state.
143: **/
144: public LifeCycleState transitionRefresh(StateManager sm) {
145: sm.clearSavedFields();
146:
147: // Refresh the FetchPlan fields and unload all others
148: sm.refreshFieldsInFetchPlan();
149: sm.unloadNonFetchPlanFields();
150:
151: Transaction tx = sm.getObjectManager().getTransaction();
152: if (tx.isActive()) {
153: return changeState(sm, P_CLEAN);
154: }
155: return changeState(sm, P_NONTRANS);
156: }
157:
158: /**
159: * Method to transition to retrieve state.
160: * @param sm StateManager.
161: * @param fgOnly only the current fetch group fields
162: * @return new LifeCycle state.
163: **/
164: public LifeCycleState transitionRetrieve(StateManager sm,
165: boolean fgOnly) {
166: if (fgOnly) {
167: sm.loadUnloadedFieldsInFetchPlan();
168: } else {
169: sm.loadUnloadedFields();
170: }
171: return this ;
172: }
173:
174: /**
175: * Method to transition to retrieve state.
176: * @param sm StateManager.
177: * @param fetchPlan the fetch plan to load fields
178: * @return new LifeCycle state.
179: **/
180: public LifeCycleState transitionRetrieve(StateManager sm,
181: FetchPlan fetchPlan) {
182: sm.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
183: return this ;
184: }
185:
186: /**
187: * Method to transition to detached-clean.
188: * @param sm StateManager.
189: * @return new LifeCycle state.
190: **/
191: public LifeCycleState transitionDetach(StateManager sm) {
192: return changeState(sm, DETACHED_CLEAN);
193: }
194:
195: /**
196: * Method to return a string version of this object.
197: * @return The string "P_CLEAN".
198: **/
199: public String toString() {
200: return "P_CLEAN";
201: }
202: }
|