KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > pobjects > tck > InstanceCallbackClass


1 /*
2  * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
3  * Use is subject to license terms.
4  *
5  */

6  
7
8 package org.objectweb.speedo.pobjects.tck;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.jdo.Extent;
16 import javax.jdo.InstanceCallbacks;
17 import javax.jdo.JDOHelper;
18 import javax.jdo.PersistenceManager;
19 import javax.jdo.Transaction;
20
21
22 public class InstanceCallbackClass implements InstanceCallbacks{
23     public static final int TRANSIENT = 0;
24     public static final int PERSISTENT_NEW = 1;
25     public static final int PERSISTENT_CLEAN = 2;
26     public static final int PERSISTENT_DIRTY = 3;
27     public static final int HOLLOW = 4;
28     public static final int TRANSIENT_CLEAN = 5;
29     public static final int TRANSIENT_DIRTY = 6;
30     public static final int PERSISTENT_NEW_DELETED = 7;
31     public static final int PERSISTENT_DELETED = 8;
32     public static final int PERSISTENT_NONTRANSACTIONAL = 9;
33     public static final int NUM_STATES = 10;
34     public static final int ILLEGAL_STATE = 10;
35
36     public static final String JavaDoc[] states = {
37             "transient",
38             "persistent-new",
39             "persistent-clean",
40             "persistent-dirty",
41             "hollow",
42             "transient-clean",
43             "transient-dirty",
44             "persistent-new-deleted",
45             "persistent-deleted",
46             "persistent-nontransactional",
47             "illegal"
48     };
49     
50     private static final int IS_PERSISTENT = 0;
51     private static final int IS_TRANSACTIONAL = 1;
52     private static final int IS_DIRTY = 2;
53     private static final int IS_NEW = 3;
54     private static final int IS_DELETED = 4;
55     private static final int NUM_STATUSES = 5;
56
57     private static final boolean state_statuses[][] = {
58 // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
59
// transient
60
{ false, false, false, false, false},
61
62 // persistent-new
63
{ true, true, true, true, false},
64
65 // persistent-clean
66
{ true, true, false, false, false},
67
68 // persistent-dirty
69
{ true, true, true, false, false},
70
71 // hollow
72
{ true, false, false, false, false},
73
74 // transient-clean
75
{ false, true, false, false, false},
76
77 // transient-dirty
78
{ false, true, true, false, false},
79
80 // persistent-new-deleted
81
{ true, true, true, true, true},
82
83 // persistent-deleted
84
{ true, true, true, false, true},
85
86 // persistent-nontransactional
87
{ true, false, false, false, false}
88     };
89     
90     public static int currentState(Object JavaDoc o)
91     {
92         boolean[] status = new boolean[5];
93         status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
94         status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
95         status[IS_DIRTY] = JDOHelper.isDirty(o);
96         status[IS_NEW] = JDOHelper.isNew(o);
97         status[IS_DELETED] = JDOHelper.isDeleted(o);
98         int i, j;
99         outerloop:
100             for( i = 0; i < NUM_STATES; ++i ){
101                 for( j = 0; j < NUM_STATUSES; ++j ){
102                     if( status[j] != state_statuses[i][j] )
103                         continue outerloop;
104                 }
105                 return i;
106             }
107             return NUM_STATES;
108     }
109     
110     
111     
112     
113     
114     
115     static public boolean performPreClearTests;
116     static public boolean performPreDeleteTests;
117     static public boolean performPreStoreTests;
118     
119     // The following two variables used in CallingJdoPreDelete tests
120
static boolean preDeleteCalled;
121     static int objectState;
122     
123     // The rest of these variables used in FieldsInPredelete tests, except to set a variable to make object dirty.
124
static public int arraySize = 5;
125     static public String JavaDoc[] capturedName = new String JavaDoc[arraySize];
126     static public Date JavaDoc[] capturedTimeStamp = new Date JavaDoc[arraySize];
127     static public double[] capturedDoubleValue = new double[arraySize];
128     static public short[] capturedChildToDelete = new short[arraySize];
129     static public char[] capturedCharValue = new char[arraySize];
130     static public String JavaDoc[] capturedNextObjName = new String JavaDoc[arraySize];
131     static public int[] numberOfChildren = new int[arraySize];
132     static public int[] sumOfChildrenIntValue = new int[arraySize];
133     static public boolean[] processedIndex = new boolean[arraySize];
134     
135     static public boolean[] transactionActive = new boolean[arraySize];
136     
137     static private int nextKeyValue = 1;
138     private int keyValue; // persistent--used as key field in application identity
139
public String JavaDoc name;
140     public Date JavaDoc timeStamp;
141     public InstanceCallbackClass nextObj;
142     public HashSet JavaDoc children;
143     public int intValue;
144     public double doubleValue;
145     public short childToDelete;
146     public char charValue;
147     
148     static public void initializeStaticsForTest()
149     {
150         performPreClearTests = false;
151         performPreDeleteTests = false;
152         performPreStoreTests = false;
153         preDeleteCalled = false;
154         for( int i = 0; i < arraySize; ++i){
155             processedIndex[i] = false;
156             transactionActive[i] = false;
157         }
158     }
159
160     static public void removeAllInstances(PersistenceManager pm)
161     {
162         //Extent e = pm.getExtent(org.objectweb.speedo.runtime.tck.InstanceCallbackClass.class, true);
163
Extent e = pm.getExtent(org.objectweb.speedo.pobjects.tck.InstanceCallbackClass.class, false);
164         Iterator JavaDoc i = e.iterator();
165         while( i.hasNext() ){
166             pm.deletePersistent(i.next());
167         }
168     }
169     
170     InstanceCallbackClass() { // not used by application
171
}
172     
173     public InstanceCallbackClass(String JavaDoc label,Date JavaDoc createTime,int intValue,double doubleValue,short childToDelete,char charValue,InstanceCallbackClass obj) {
174         keyValue = nextKeyValue++;
175         name = label;
176         timeStamp = createTime;
177         nextObj = obj;
178         children = new HashSet JavaDoc();
179         this.intValue = intValue;
180         this.doubleValue = doubleValue;
181         this.childToDelete = childToDelete;
182         this.charValue = charValue;
183     }
184     
185     public void addChild(InstanceCallbackClass child) {
186         children.add(child);
187     }
188     
189     public void jdoPreStore() {
190         if(!performPreStoreTests) { return; }
191         captureValues2();
192         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
193         usePM(pm);
194     }
195     
196     public void jdoPreDelete() {
197         if(!performPreDeleteTests) { return; }
198         // The following two variables set for CallingJdoPreDelete tests
199
preDeleteCalled = true;
200         objectState = InstanceCallbackClass.currentState(this);
201         
202         captureValues2();
203         
204         // The rest of this routine used for Accessing FieldsInPredelete tests
205
// check that intValue is a valid index
206
if(intValue >= 0 & intValue < arraySize) {
207             PersistenceManager pm = JDOHelper.getPersistenceManager(this);
208             usePM(pm);
209             if(nextObj != null) {
210                 pm.deletePersistent(nextObj); // delete referenced object
211

212                 // delete designated child
213
for(Iterator JavaDoc i = children.iterator(); i.hasNext();) {
214                      InstanceCallbackClass obj = (InstanceCallbackClass)i.next();
215                      if( obj.intValue == childToDelete) {
216                         pm.deletePersistent(obj);
217                         break;
218                      }
219                 }
220             }
221         }
222     }
223     
224     public void jdoPostLoad() {
225     }
226     
227     public void jdoPreClear() {
228         if(!performPreClearTests) { return; }
229         // the following code is copied from captureValues, because it must
230
// not be enhanced for execution during jdoPreClear.
231
// check that intValue is a valid index
232
if(intValue >= 0 & intValue < arraySize) {
233             processedIndex[intValue] = true;
234             
235             // capture values of the attributes
236
capturedName[intValue] = name;
237             capturedTimeStamp[intValue] = timeStamp;
238             capturedDoubleValue[intValue] = doubleValue;
239             numberOfChildren[intValue] = children.size();
240             sumOfChildrenIntValue[intValue] = 0;
241             for(Iterator JavaDoc i = children.iterator(); i.hasNext();) {
242                 InstanceCallbackClass o = (InstanceCallbackClass)i.next();
243                 sumOfChildrenIntValue[intValue] += o.intValue;
244             }
245             capturedChildToDelete[intValue] = childToDelete;
246             capturedCharValue[intValue] = charValue;
247             if(nextObj != null) {
248                 capturedNextObjName[intValue] = nextObj.name;
249             } else {
250                 capturedNextObjName[intValue] = null;
251             }
252         }
253     }
254     
255     private void captureValues2() {
256         // check that intValue is a valid index
257
if(intValue >= 0 & intValue < arraySize) {
258             processedIndex[intValue] = true;
259             
260             // capture values of the attributes
261
capturedName[intValue] = name;
262             capturedTimeStamp[intValue] = timeStamp;
263             capturedDoubleValue[intValue] = doubleValue;
264             numberOfChildren[intValue] = children.size();
265             sumOfChildrenIntValue[intValue] = 0;
266             for(Iterator JavaDoc i = children.iterator(); i.hasNext();) {
267                 InstanceCallbackClass o = (InstanceCallbackClass)i.next();
268                 sumOfChildrenIntValue[intValue] += o.intValue;
269             }
270             capturedChildToDelete[intValue] = childToDelete;
271             capturedCharValue[intValue] = charValue;
272             if(nextObj != null) {
273                 capturedNextObjName[intValue] = nextObj.name;
274             } else {
275                 capturedNextObjName[intValue] = null;
276             }
277         }
278     }
279     
280     private void usePM(PersistenceManager pm) {
281         if(intValue >= 0 & intValue < arraySize) {
282             Transaction t = pm.currentTransaction();
283             if(t.isActive()) {
284                 transactionActive[intValue] = true;
285             }
286         }
287     }
288     
289 public static class KeyClass implements Serializable JavaDoc {
290     public int keyValue;
291
292     public KeyClass() {
293     }
294
295     public KeyClass(String JavaDoc s) {
296         try{ keyValue = Integer.parseInt(s);}
297         catch(NumberFormatException JavaDoc e){
298             keyValue = 0;}
299     }
300     
301     public boolean equals(Object JavaDoc obj) {
302         if( obj == null || !this.getClass().equals(obj.getClass()) ) return false;
303         else return keyValue == ((KeyClass)obj).keyValue;
304     }
305     
306     public int hashCode() {
307         return keyValue;
308     }
309     
310     public String JavaDoc toString() {
311         return Integer.toString(keyValue);
312     }
313 }
314 }
315
Popular Tags