KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > AttachCopyStateManager


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo;
13
14 import com.versant.core.common.MapEntries;
15 import com.versant.core.common.OID;
16 import com.versant.core.common.State;
17 import com.versant.core.metadata.FieldMetaData;
18 import com.versant.core.metadata.MDStaticUtils;
19 import com.versant.core.metadata.MDStatics;
20
21 import javax.jdo.PersistenceManager;
22 import javax.jdo.spi.PersistenceCapable;
23 import javax.jdo.spi.StateManager;
24 import java.lang.reflect.Array JavaDoc;
25 import java.util.*;
26
27 import com.versant.core.common.BindingSupportImpl;
28
29 /**
30  *
31  */

32 public class AttachCopyStateManager implements VersantStateManager {
33
34     private VersantPersistenceManagerImp pm;
35     private State state;
36     private FieldMetaData fmd;
37
38     public AttachCopyStateManager(VersantPersistenceManagerImp pm) {
39         this.pm = pm;
40     }
41
42     public void setState(State state) {
43         this.state = state;
44     }
45
46     public void setFieldMetaData(FieldMetaData fmd) {
47         this.fmd = fmd;
48     }
49
50     public byte replacingFlags(PersistenceCapable persistenceCapable) {
51         return 0;
52     }
53
54     public StateManager replacingStateManager(
55             PersistenceCapable persistenceCapable, StateManager stateManager) {
56         return stateManager;
57     }
58
59     public boolean isDirty(PersistenceCapable persistenceCapable) {
60         return ((VersantDetachable)persistenceCapable).versantIsDirty();
61     }
62
63     public boolean isTransactional(PersistenceCapable persistenceCapable) {
64         return false;
65     }
66
67     public boolean isPersistent(PersistenceCapable persistenceCapable) {
68         return false;
69     }
70
71     public boolean isNew(PersistenceCapable persistenceCapable) {
72         return getPcOID((VersantDetachable)persistenceCapable).isNew();
73     }
74
75     public boolean isDeleted(PersistenceCapable persistenceCapable) {
76         return false;
77     }
78
79     public PersistenceManager getPersistenceManager(
80             PersistenceCapable persistenceCapable) {
81         return null;
82     }
83
84     public void makeDirty(PersistenceCapable persistenceCapable, String JavaDoc s) {
85         ((VersantDetachable)persistenceCapable).versantMakeDirty(s);
86     }
87
88     public Object JavaDoc getObjectId(PersistenceCapable persistenceCapable) {
89         return getPcOID((VersantDetachable)persistenceCapable);
90     }
91
92     public Object JavaDoc getTransactionalObjectId(
93             PersistenceCapable persistenceCapable) {
94         return null;
95     }
96
97     public boolean isLoaded(PersistenceCapable persistenceCapable, int i) {
98         return ((VersantDetachable)persistenceCapable).versantIsLoaded(i);
99     }
100
101     public void preSerialize(PersistenceCapable persistenceCapable) {
102
103     }
104
105     public boolean getBooleanField(PersistenceCapable persistenceCapable,
106             int i, boolean b) {
107         return b;
108     }
109
110     public char getCharField(PersistenceCapable persistenceCapable, int i,
111             char c) {
112         return c;
113     }
114
115     public byte getByteField(PersistenceCapable persistenceCapable, int i,
116             byte b) {
117         return b;
118     }
119
120     public short getShortField(PersistenceCapable persistenceCapable, int i,
121             short i1) {
122         return i1;
123     }
124
125     public int getIntField(PersistenceCapable persistenceCapable, int i,
126             int i1) {
127         return i1;
128     }
129
130     public long getLongField(PersistenceCapable persistenceCapable, int i,
131             long l) {
132         return l;
133     }
134
135     public float getFloatField(PersistenceCapable persistenceCapable, int i,
136             float v) {
137         return v;
138     }
139
140     public double getDoubleField(PersistenceCapable persistenceCapable, int i,
141             double v) {
142         return v;
143     }
144
145     public String JavaDoc getStringField(PersistenceCapable persistenceCapable, int i,
146             String JavaDoc s) {
147         return s;
148     }
149
150     public Object JavaDoc getObjectField(PersistenceCapable persistenceCapable, int i,
151             Object JavaDoc o) {
152         return o;
153     }
154
155     public void setBooleanField(PersistenceCapable persistenceCapable, int i,
156             boolean b, boolean b1) {
157     }
158
159     public void setCharField(PersistenceCapable persistenceCapable, int i,
160             char c, char c1) {
161     }
162
163     public void setByteField(PersistenceCapable persistenceCapable, int i,
164             byte b, byte b1) {
165     }
166
167     public void setShortField(PersistenceCapable persistenceCapable, int i,
168             short i1, short i2) {
169     }
170
171     public void setIntField(PersistenceCapable persistenceCapable, int i,
172             int i1, int i2) {
173     }
174
175     public void setLongField(PersistenceCapable persistenceCapable, int i,
176             long l, long l1) {
177     }
178
179     public void setFloatField(PersistenceCapable persistenceCapable, int i,
180             float v, float v1) {
181     }
182
183     public void setDoubleField(PersistenceCapable persistenceCapable, int i,
184             double v, double v1) {
185     }
186
187     public void setStringField(PersistenceCapable persistenceCapable, int i,
188             String JavaDoc s, String JavaDoc s1) {
189     }
190
191     public void setObjectField(PersistenceCapable persistenceCapable, int i,
192             Object JavaDoc o, Object JavaDoc o1) {
193     }
194
195     public void providedBooleanField(PersistenceCapable persistenceCapable,
196             int i, boolean b) {
197         state.setBooleanFieldAbs(i, b);
198     }
199
200     public void providedCharField(PersistenceCapable persistenceCapable, int i,
201             char c) {
202         state.setCharFieldAbs(i, c);
203     }
204
205     public void providedByteField(PersistenceCapable persistenceCapable, int i,
206             byte b) {
207         state.setByteFieldAbs(i, b);
208     }
209
210     public void providedShortField(PersistenceCapable persistenceCapable,
211             int i, short i1) {
212         state.setShortFieldAbs(i, i1);
213     }
214
215     public void providedIntField(PersistenceCapable persistenceCapable, int i,
216             int i1) {
217         state.setIntFieldAbs(i, i1);
218     }
219
220     public void providedLongField(PersistenceCapable persistenceCapable, int i,
221             long l) {
222         state.setLongFieldAbs(i, l);
223     }
224
225     public void providedFloatField(PersistenceCapable persistenceCapable,
226             int i, float v) {
227         state.setFloatFieldAbs(i, v);
228     }
229
230     public void providedDoubleField(PersistenceCapable persistenceCapable,
231             int i, double v) {
232         state.setDoubleFieldAbs(i, v);
233     }
234
235     public void providedStringField(PersistenceCapable persistenceCapable,
236             int i, String JavaDoc s) {
237         state.setStringFieldAbs(i, s);
238     }
239
240     public void providedObjectField(PersistenceCapable persistenceCapable,
241             int i, Object JavaDoc o) {
242         if (o == null) {
243             state.setObjectFieldAbs(i, o);
244         } else {
245             state.setObjectFieldAbs(i, getUnresolved(o));
246         }
247     }
248
249     private Object JavaDoc getUnresolved(Object JavaDoc o) {
250         switch (fmd.category) {
251             case MDStatics.CATEGORY_COLLECTION:
252                 if (o instanceof Collection) {
253                     Collection col = (Collection)o;
254                     ArrayList oids = new ArrayList(col.size());
255                     if (fmd.isElementTypePC()) {
256                         for (Iterator it = col.iterator(); it.hasNext();) {
257                             VersantDetachable detachable = (VersantDetachable)it.next();
258                             oids.add(getPC(detachable));
259                         }
260                     } else {
261                         oids.addAll(col);
262                     }
263                     col = createNewCol();
264                     col.addAll(oids);
265                     return col;
266                 }
267                 break;
268             case MDStatics.CATEGORY_ARRAY:
269                 if (!o.getClass().isArray()) return o;
270                 Class JavaDoc type = o.getClass().getComponentType();
271                 int length = Array.getLength(o);
272                 Object JavaDoc newArray = Array.newInstance(type, length);
273                 System.arraycopy(o, 0, newArray, 0, length);
274                 if (fmd.isElementTypePC()) {
275                     Object JavaDoc[] objects = (Object JavaDoc[])newArray;
276                     for (int i = 0; i < objects.length; i++) {
277                         objects[i] = getPC((VersantDetachable)objects[i]);
278                     }
279                 }
280                 return newArray;
281             case MDStatics.CATEGORY_MAP:
282                 if (o instanceof Map) {
283                     MapEntries entries = new MapEntries();
284                     Map map = (Map)o;
285                     int size = map.size();
286                     entries.keys = new Object JavaDoc[size];
287                     entries.values = new Object JavaDoc[size];
288                     int x = 0;
289                     for (Iterator it = map.keySet().iterator(); it.hasNext();) {
290                         Object JavaDoc o1 = (Object JavaDoc)it.next();
291                         if (fmd.isKeyTypePC()) {
292                             entries.keys[x] = getPC((VersantDetachable)o1);
293                         } else {
294                             entries.keys[x] = o1;
295                         }
296                         if (fmd.isElementTypePC()) {
297                             entries.values[x] = getPC(
298                                     (VersantDetachable)map.get(o1));
299                         } else {
300                             entries.values[x] = map.get(o1);
301                         }
302                         x++;
303                     }
304                     map = createNewMap();
305                     length = entries.keys.length;
306                     for (int i = 0; i < length; i++) {
307                         Object JavaDoc key = entries.keys[i];
308                         Object JavaDoc value = entries.values[i];
309                         map.put(key, value);
310                     }
311                     return map;
312                 }
313                 break;
314             case MDStatics.CATEGORY_REF:
315             case MDStatics.CATEGORY_POLYREF:
316                 VersantDetachable detachable = (VersantDetachable)o;
317                 return getPC(detachable);
318         }
319         return o;
320     }
321
322     private Object JavaDoc getPC(VersantDetachable detachable) {
323         OID pcOID = getPcOID(detachable);
324         return pm.getObjectById(pcOID, false);
325     }
326
327     private Map createNewMap() {
328         switch (fmd.typeCode) {
329             case MDStatics.MAP:
330             case MDStatics.HASHMAP:
331                 return new HashMap();
332             case MDStatics.HASHTABLE:
333                 return new Hashtable();
334             case MDStatics.TREEMAP:
335             case MDStatics.SORTEDMAP:
336                 return new TreeMap();
337             default:
338                 throw BindingSupportImpl.getInstance().notImplemented("Creating a Map instance for field " +
339                         fmd.getName() + " of type " + MDStaticUtils.toSimpleName(
340                                 fmd.typeCode) +
341                         " is not supported");
342         }
343     }
344
345     private Collection createNewCol() {
346         switch (fmd.typeCode) {
347             case MDStatics.HASHSET:
348             case MDStatics.SET:
349                 return new HashSet();
350             case MDStatics.TREESET:
351             case MDStatics.SORTEDSET:
352                 return new TreeSet();
353             case MDStatics.COLLECTION:
354             case MDStatics.LIST:
355             case MDStatics.ARRAYLIST:
356                 return new ArrayList();
357             case MDStatics.LINKEDLIST:
358                 return new LinkedList();
359             case MDStatics.VECTOR:
360                 return new Vector();
361             default:
362                 throw BindingSupportImpl.getInstance().notImplemented("Creating a Collection instance for field " +
363                         fmd.getName() + " of type " + MDStaticUtils.toSimpleName(
364                                 fmd.typeCode) +
365                         " is not supported");
366         }
367     }
368
369     public boolean replacingBooleanField(PersistenceCapable persistenceCapable,
370             int i) {
371         return false;
372     }
373
374     public char replacingCharField(PersistenceCapable persistenceCapable,
375             int i) {
376         return 0;
377     }
378
379     public byte replacingByteField(PersistenceCapable persistenceCapable,
380             int i) {
381         return 0;
382     }
383
384     public short replacingShortField(PersistenceCapable persistenceCapable,
385             int i) {
386         return 0;
387     }
388
389     public int replacingIntField(PersistenceCapable persistenceCapable, int i) {
390         return 0;
391     }
392
393     public long replacingLongField(PersistenceCapable persistenceCapable,
394             int i) {
395         return 0;
396     }
397
398     public float replacingFloatField(PersistenceCapable persistenceCapable,
399             int i) {
400         return 0;
401     }
402
403     public double replacingDoubleField(PersistenceCapable persistenceCapable,
404             int i) {
405         return 0;
406     }
407
408     public String JavaDoc replacingStringField(PersistenceCapable persistenceCapable,
409             int i) {
410         return null;
411     }
412
413     public Object JavaDoc replacingObjectField(PersistenceCapable persistenceCapable,
414             int i) {
415         return null;
416     }
417
418     public void fillNewAppPKField(int fieldNo) {}
419
420     public void makeDirty(PersistenceCapable pc,
421             int managedFieldNo) {
422         ((VersantDetachable)pc).versantMakeDirty(managedFieldNo);
423     }
424
425     private OID getPcOID(VersantDetachable persistenceCapable) {
426         return pm.getOID(persistenceCapable);
427     }
428
429     public OID getOID() {
430         return null;
431     }
432
433     public PersistenceCapable getPersistenceCapable() {
434         return null;
435     }
436
437
438
439 }
440
Popular Tags