KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > state > LifeCycleState


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * LifeCycleState.java March 10, 2000 Steffi Rauschenbach
26  */

27
28 package com.sun.jdo.spi.persistence.support.sqlstore.state;
29
30 import com.sun.jdo.api.persistence.support.JDOUserException;
31 import com.sun.jdo.spi.persistence.utility.I18NHelper;
32 import com.sun.jdo.spi.persistence.utility.logging.Logger;
33 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperStateManager;
34
35 import java.util.ResourceBundle JavaDoc;
36
37
38 public abstract class LifeCycleState {
39
40     /**
41      * I18N message handler
42      */

43     protected final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
44             "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
45
LifeCycleState.class.getClassLoader());
46
47     protected boolean isPersistent;
48     protected boolean isAutoPersistent;
49     protected boolean isPersistentInDataStore;
50     protected boolean isTransactional;
51     protected boolean isDirty;
52     protected boolean isNew;
53     protected boolean isDeleted;
54     protected boolean isFlushed;
55     protected boolean isNavigable;
56     protected boolean isRefreshable;
57     protected boolean isBeforeImageUpdatable;
58
59     // The following flags need to be checked after state transition.
60
protected boolean needsRegister;
61
62     // The following flags need to be checked before state transition.
63
protected boolean needsReload;
64
65     // The following flag determine whether the original state of the object
66
// needs to be restored on rollback (my depend on retainValues)
67
protected boolean needsRestoreOnRollback;
68
69
70     // The following flag states that merge is needed
71
protected boolean needMerge = true;
72
73     protected int updateAction;
74     protected int stateType;
75
76
77     /**
78      * Constants to specify database operation to be executed
79      */

80     final static protected int
81             NO_OP = 0,
82     INSERT_OP = 1,
83     UPDATE_OP = 2,
84     DELETE_OP = 3;
85
86     /**
87      * Constants to specify the life cycle state type
88      */

89     final static public int
90             HOLLOW = 0,
91     P_NON_TX = 1,
92     P_CLEAN = 2,
93     P_DIRTY = 3,
94     P_NEW = 4,
95     P_NEW_FLUSHED = 5,
96     P_NEW_FLUSHED_DELETED = 6,
97     P_NEW_DELETED = 7,
98     P_DELETED = 8,
99     P_DELETED_FLUSHED = 9,
100     AP_NEW = 10,
101     AP_NEW_PENDING = 11,
102     AP_NEW_FLUSHED = 12,
103     AP_NEW_FLUSHED_PENDING = 13,
104     AP_NEW_FLUSHED_DELETED = 14,
105     AP_NEW_DELETED = 15,
106     TRANSIENT = 16,
107     TOTAL = 17;
108
109     private static LifeCycleState stateTypes[];
110
111     //The logger
112
private static Logger logger = LogHelperStateManager.getLogger();
113
114     // ******************************************************************
115
// Initialisation stuff
116
// ******************************************************************
117

118     /**
119      * Static initialiser.
120      * Initialises the life cycle.
121      */

122     static {
123         initLifeCycleState();
124     }
125
126     /**
127      * Initialises the objects. This class implements the "state pattern".
128      */

129
130     // This method is called (through the static initializer)
131
// when the LifeCycleState class or any of its subclasses is loaded.
132

133     // It is extremely important that this method is called before any of isNew etc is called,
134
// and before stateType() is called !!!
135

136     protected static void initLifeCycleState() {
137         stateTypes = new LifeCycleState[TOTAL];
138         stateTypes[HOLLOW] = new Hollow();
139         stateTypes[P_NON_TX] = new PersistentNonTransactional();
140         stateTypes[P_CLEAN] = new PersistentClean();
141         stateTypes[P_DIRTY] = new PersistentDirty();
142         stateTypes[P_NEW] = new PersistentNew();
143         stateTypes[P_NEW_FLUSHED] = new PersistentNewFlushed();
144         stateTypes[P_NEW_DELETED] = new PersistentNewDeleted();
145         stateTypes[P_NEW_FLUSHED_DELETED] = new PersistentNewFlushedDeleted();
146         stateTypes[P_DELETED] = new PersistentDeleted();
147         stateTypes[P_DELETED_FLUSHED] = new PersistentDeletedFlushed();
148         stateTypes[AP_NEW] = new AutoPersistentNew();
149         stateTypes[AP_NEW_PENDING] = new AutoPersistentNewPending();
150         stateTypes[AP_NEW_FLUSHED] = new AutoPersistentNewFlushed();
151         stateTypes[AP_NEW_FLUSHED_PENDING] = new AutoPersistentNewFlushedPending();
152         stateTypes[AP_NEW_FLUSHED_DELETED] = new AutoPersistentNewFlushedDeleted();
153         stateTypes[AP_NEW_DELETED] = new AutoPersistentNewDeleted();
154         stateTypes[TRANSIENT] = null;
155     }
156
157     /**
158      * | Trans | PNew | PClean | PDirty | Hollow | PNewDel | PDel | PNonTx
159      *-----------------------------------------------------------------------------------
160      * makeP | PNew | unch. | unch. | unch. | unch. | unch. | unch. | unch.
161      *-----------------------------------------------------------------------------------
162      * delP | Error | PNewDel| PDel | PDel | PDel | unch. | unch. | PDel
163      *-----------------------------------------------------------------------------------
164      * readF.| unch. | unch. | unch. | unch. | PClean | Error | Error |!unl.:unch.
165      * opt=f | | | | | | | | unl.:PClean
166      *-----------------------------------------------------------------------------------
167      * readF.| unch. | unch. | unch. | unch. | PNonTx | Error | Error | unch.
168      * opt=t | | | | | | | |
169      *-----------------------------------------------------------------------------------
170      * writeF| unch. | unch. | PDirty | unch. | PDirty | Error | Error | PDirty
171      *-----------------------------------------------------------------------------------
172      * commit| unch. | Hollow | Hollow | Hollow | unch. | Trans | Trans | unch.
173      *-----------------------------------------------------------------------------------
174      * commit| unch. | PNonTx | PNonTx | PNonTx | unch. | Trans | Trans | unch.
175      * rt=t | | | | | | | |
176      *-----------------------------------------------------------------------------------
177      * rollb.| unch. | Trans | Hollow | Hollow | unch. | Trans | Hollow| unch.
178      *-----------------------------------------------------------------------------------
179      * rollb.| unch. | Trans | PNonTx | PNonTx | unch. | Trans | PNonTx| unch.
180      * rt=t | | | | | | | |
181      *-----------------------------------------------------------------------------------
182      * evict | n/a | n/a | Hollow | n/a | unch. | n/a | n/a | Hollow
183      *-----------------------------------------------------------------------------------
184      */

185
186
187     /**
188      * Returns the LifeCycleState for the state constant.
189      *
190      * @param op the type as integer
191      * @return the type as LifeCycleState object
192      */

193     public static LifeCycleState getLifeCycleState(int state) {
194         if (logger.isLoggable(Logger.FINER)) {
195             logger.finer("sqlstore.state.lifecyclestate.initial",stateTypes[state]); // NOI18N
196
}
197
198         return stateTypes[state];
199     }
200
201     /**
202      * Returns the type of the life cycle state
203      *
204      * @return the type of this life cycle state
205      *
206      */

207     public int stateType() {
208         return stateType;
209     }
210
211
212     public LifeCycleState transitionMakePersistent() {
213         return this;
214     }
215
216     public LifeCycleState transitionDeletePersistent() {
217         return this;
218     }
219
220     public LifeCycleState transitionRefreshPersistent() {
221         return this;
222     }
223
224     public LifeCycleState transitionReload(boolean transactionActive) {
225         return this;
226     }
227
228     public LifeCycleState transitionCommit(boolean retainValues) {
229         return this;
230     }
231
232     public LifeCycleState transitionRollback(boolean retainValues) {
233         return this;
234     }
235
236     public LifeCycleState transitionFlushed() {
237         return this;
238     }
239
240     public LifeCycleState transitionMakePending() {
241         return this;
242     }
243
244     public LifeCycleState transitionReadField(boolean optimisitic,
245                                               boolean nontransactionalRead,
246                                               boolean transactionActive) {
247         if (!nontransactionalRead) {
248             assertTransaction(transactionActive);
249         }
250
251         return this;
252     }
253
254     public LifeCycleState transitionWriteField(boolean transactionActive) {
255         assertTransaction(transactionActive);
256         return this;
257     }
258
259     protected void assertTransaction(boolean transactionActive) {
260         if (!transactionActive) {
261             throw new JDOUserException(I18NHelper.getMessage(messages,
262                     "jdo.lifecycle.xactnotactive")); // NOI18N
263
}
264     }
265
266
267     /***************************************************************/
268     /************** State interrogation methods ********************/
269     /***************************************************************/
270
271     /**
272      * Return whether the object is persistent.
273      */

274     public boolean isPersistent() {
275         return isPersistent;
276     }
277
278     /**
279      * Return whether the object is auto-persistent.
280      */

281     public boolean isAutoPersistent() {
282         return isAutoPersistent;
283     }
284
285     /**
286      * Return whether the object is persistent in data store.
287      */

288     public boolean isPersistentInDataStore() {
289         return isPersistentInDataStore;
290     }
291
292
293     /**
294      * Return whether the object is transactional.
295      */

296     public boolean isTransactional() {
297         return isTransactional;
298     }
299
300     /**
301      * Return whether the object is dirty, i.e. has been changed
302      * (created, updated, deleted) in this Tx.
303      */

304     public boolean isDirty() {
305         return isDirty;
306     }
307
308     /**
309      * Return whether the object was newly created.
310      */

311     public boolean isNew() {
312         return isNew;
313     }
314
315     /**
316      * Return whether the object is deleted.
317      */

318     public boolean isDeleted() {
319         return isDeleted;
320     }
321
322     /**
323      * Return whether the object is registered with the persistence manager.
324      */

325     public boolean needsRegister() {
326         return needsRegister;
327     }
328
329     /**
330      * Return whether the object can dynamically navigate to fields that are
331      * not present.
332      */

333     public boolean isNavigable() {
334         return isNavigable;
335     }
336
337     /**
338      * Return whether the object can be refreshed from the database.
339      */

340     public boolean isRefreshable() {
341         return isRefreshable;
342     }
343
344     public boolean isBeforeImageUpdatable() {
345         return isBeforeImageUpdatable;
346     }
347
348
349     public boolean needsReload(boolean optimistic,
350                                boolean nontransactionalRead,
351                                boolean transactionActive) {
352         return needsReload;
353     }
354
355     public boolean needsRestoreOnRollback(boolean retainValues) {
356         //
357
// The default behavior is if retainValues is true, we always
358
// restore the state. Otherwise, the behavior is determined
359
// by needsRetoreOnRollback.
360
//
361
if (retainValues) {
362             return true;
363         }
364
365         return needsRestoreOnRollback;
366     }
367
368     public boolean needMerge() {
369         return needMerge;
370     }
371
372     public int getUpdateAction() {
373         return updateAction;
374     }
375
376     /*************************************************************/
377     /********************* Helper methods ************************/
378     /********* Called only internally by life cycle classes ******/
379     /*************************************************************/
380
381     /**
382      * Life Cycle State change
383      */

384     public LifeCycleState changeState(int newStateType) {
385         if (logger.isLoggable(Logger.FINER)) {
386             Object JavaDoc[] items = new Object JavaDoc[] {this,stateTypes[newStateType]};
387             logger.finer("sqlstore.state.lifecyclestate.changestate",items); // NOI18N
388
}
389
390         return (stateTypes[newStateType]);
391     }
392
393     public String JavaDoc toString() {
394         switch (stateType) {
395             case HOLLOW:
396                 return "HOLLOW"; // NOI18N
397
case P_NON_TX:
398                 return "P_NON_TX"; // NOI18N
399
case P_CLEAN:
400                 return "P_CLEAN"; // NOI18N
401
case P_DIRTY:
402                 return "P_DIRTY"; // NOI18N
403
case P_NEW:
404                 return "P_NEW"; // NOI18N
405
case P_NEW_FLUSHED:
406                 return "P_NEW_FLUSHED"; // NOI18N
407
case P_NEW_FLUSHED_DELETED:
408                 return "P_NEW_FLUSHED_DELETED"; // NOI18N
409
case P_NEW_DELETED:
410                 return "P_NEW_DELETED"; // NOI18N
411
case P_DELETED:
412                 return "P_DELETED"; // NOI18N
413
case P_DELETED_FLUSHED:
414                 return "P_DELETED_FLUSHED"; // NOI18N
415
case AP_NEW:
416                 return "AP_NEW"; // NOI18N
417
case AP_NEW_PENDING:
418                 return "AP_NEW_PENDING"; // NOI18N
419
case AP_NEW_FLUSHED:
420                 return "AP_NEW_FLUSHED"; // NOI18N
421
case AP_NEW_FLUSHED_PENDING:
422                 return "AP_NEW_FLUSHED_PENDING"; // NOI18N
423
case AP_NEW_FLUSHED_DELETED:
424                 return "AP_NEW_FLUSHED_DELETED"; // NOI18N
425
case AP_NEW_DELETED:
426                 return "AP_NEW_DELETED"; //NOI18N
427
}
428
429         return null;
430     }
431
432 }
433
434
435
Popular Tags