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: Contributors:
016: 2002 Mike Martin - unknown changes
017: 2003 Andy Jefferson - commented
018: 2004 Andy Jefferson - added refresh() handling
019: ...
020: **********************************************************************/package org.jpox.jdo.state;
021:
022: import org.jpox.FetchPlan;
023: import org.jpox.StateManager;
024: import org.jpox.Transaction;
025: import org.jpox.jdo.exceptions.TransactionNotActiveException;
026: import org.jpox.state.IllegalStateTransitionException;
027: import org.jpox.state.LifeCycleState;
028:
029: /**
030: * Class representing the life cycle state of Hollow.
031: *
032: * @version $Revision: 1.8 $
033: **/
034: class Hollow extends LifeCycleState {
035: /** Protected Constructor to prevent external instantiation. */
036: protected Hollow() {
037: isPersistent = true;
038: isDirty = false;
039: isNew = false;
040: isDeleted = false;
041: isTransactional = false;
042:
043: stateType = HOLLOW;
044: }
045:
046: /**
047: * Method to transition to delete persistent.
048: * @param sm StateManager.
049: * @return new LifeCycle state.
050: */
051: public LifeCycleState transitionDeletePersistent(StateManager sm) {
052: sm.clearLoadedFlags();
053: return changeState(sm, P_DELETED);
054: }
055:
056: /**
057: * Method to transition to transactional.
058: * @param sm StateManager.
059: * @return new LifeCycle state.
060: */
061: public LifeCycleState transitionMakeTransactional(StateManager sm) {
062: sm.refreshLoadedFields();
063: return changeState(sm, P_CLEAN);
064: }
065:
066: /**
067: * Method to transition to transient.
068: * @param sm StateManager.
069: * @param useFetchPlan to make transient the fields in the fetch plan
070: * @return new LifeCycle state.
071: **/
072: public LifeCycleState transitionMakeTransient(StateManager sm,
073: boolean useFetchPlan, boolean detachAllOnCommit) {
074: if (useFetchPlan) {
075: sm.loadUnloadedFieldsInFetchPlan();
076: }
077: return changeState(sm, TRANSIENT);
078: }
079:
080: /**
081: * Method to transition to commit state.
082: * @param sm StateManager.
083: * @return new LifeCycle state.
084: */
085: public LifeCycleState transitionCommit(StateManager sm) {
086: throw new IllegalStateTransitionException(this , "commit", sm);
087: }
088:
089: /**
090: * Method to transition to rollback state.
091: * @param sm StateManager.
092: * @return new LifeCycle state.
093: */
094: public LifeCycleState transitionRollback(StateManager sm) {
095: throw new IllegalStateTransitionException(this , "rollback", sm);
096: }
097:
098: /**
099: * Method to transition to read-field state.
100: * @param sm StateManager.
101: * @param isLoaded if the field was previously loaded
102: * @return new LifeCycle state.
103: */
104: public LifeCycleState transitionReadField(StateManager sm,
105: boolean isLoaded) {
106: Transaction tx = sm.getObjectManager().getTransaction();
107: if (!tx.getOptimistic() && tx.isActive()) {
108: return changeState(sm, P_CLEAN);
109: } else if (!tx.getOptimistic() && !tx.isActive()) {
110: if (!tx.getNontransactionalRead()) {
111: throw new TransactionNotActiveException(LOCALISER
112: .msg("027000"), sm.getInternalObjectId());
113: }
114: }
115: return changeState(sm, P_NONTRANS);
116: }
117:
118: /**
119: * Method to transition to write-field state.
120: * @param sm StateManager.
121: * @return new LifeCycle state.
122: */
123: public LifeCycleState transitionWriteField(StateManager sm) {
124: Transaction tx = sm.getObjectManager().getTransaction();
125: if (!tx.getOptimistic() && !tx.isActive()) {
126: if (!tx.getNontransactionalWrite()) {
127: throw new TransactionNotActiveException(LOCALISER
128: .msg("027001"), sm.getInternalObjectId());
129: }
130: }
131: return changeState(sm, tx.isActive() ? P_DIRTY : P_NONTRANS);
132: }
133:
134: /**
135: * Method to transition to retrieve state.
136: * @param sm StateManager.
137: * @param fgOnly only the current fetch group fields
138: * @return new LifeCycle state.
139: */
140: public LifeCycleState transitionRetrieve(StateManager sm,
141: boolean fgOnly) {
142: if (fgOnly) {
143: sm.loadUnloadedFieldsInFetchPlan();
144: } else {
145: sm.loadUnloadedFields();
146: }
147: Transaction tx = sm.getObjectManager().getTransaction();
148: if (!tx.getOptimistic() && tx.isActive()) {
149: return changeState(sm, P_CLEAN);
150: } else if (tx.getOptimistic()) {
151: return changeState(sm, P_NONTRANS);
152: }
153: return super .transitionRetrieve(sm, fgOnly);
154: }
155:
156: /**
157: * Method to transition to retrieve state.
158: * @param sm StateManager.
159: * @param fetchPlan the fetch plan to load fields
160: * @return new LifeCycle state.
161: **/
162: public LifeCycleState transitionRetrieve(StateManager sm,
163: FetchPlan fetchPlan) {
164: sm.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
165: Transaction tx = sm.getObjectManager().getTransaction();
166: if (!tx.getOptimistic() && tx.isActive()) {
167: return changeState(sm, P_CLEAN);
168: } else if (tx.getOptimistic()) {
169: return changeState(sm, P_NONTRANS);
170: }
171: return super .transitionRetrieve(sm, fetchPlan);
172: }
173:
174: /**
175: * Method to transition to refresh state.
176: * @param sm StateManager.
177: * @return new LifeCycle state.
178: */
179: public LifeCycleState transitionRefresh(StateManager sm) {
180: sm.clearSavedFields();
181:
182: // Refresh the FetchPlan fields and unload all others
183: sm.refreshFieldsInFetchPlan();
184: sm.unloadNonFetchPlanFields();
185:
186: // We leave in the same state to be consistent with JDO section 5.9.1
187: return this ;
188: }
189:
190: /**
191: * Method to transition to detached-clean.
192: * @param sm StateManager.
193: * @return new LifeCycle state.
194: **/
195: public LifeCycleState transitionDetach(StateManager sm) {
196: return changeState(sm, DETACHED_CLEAN);
197: }
198:
199: /**
200: * Method to transition when serialised.
201: * @param sm State Manager
202: * @return The new LifeCycle state
203: */
204: public LifeCycleState transitionSerialize(StateManager sm) {
205: Transaction tx = sm.getObjectManager().getTransaction();
206: if (tx.isActive() && !tx.getOptimistic()) {
207: return changeState(sm, P_CLEAN);
208: }
209: return this ;
210: }
211:
212: /**
213: * Method to return a string version of this object.
214: * @return The string "HOLLOW".
215: */
216: public String toString() {
217: return "HOLLOW";
218: }
219: }
|