001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo;
012:
013: import com.versant.core.common.MapEntries;
014: import com.versant.core.common.OID;
015: import com.versant.core.common.State;
016: import com.versant.core.metadata.FieldMetaData;
017: import com.versant.core.metadata.MDStaticUtils;
018: import com.versant.core.metadata.MDStatics;
019:
020: import javax.jdo.PersistenceManager;
021: import javax.jdo.spi.PersistenceCapable;
022: import javax.jdo.spi.StateManager;
023: import java.lang.reflect.Array;
024: import java.util.*;
025:
026: import com.versant.core.common.BindingSupportImpl;
027:
028: /**
029: *
030: */
031: public class AttachCopyStateManager implements VersantStateManager {
032:
033: private VersantPersistenceManagerImp pm;
034: private State state;
035: private FieldMetaData fmd;
036:
037: public AttachCopyStateManager(VersantPersistenceManagerImp pm) {
038: this .pm = pm;
039: }
040:
041: public void setState(State state) {
042: this .state = state;
043: }
044:
045: public void setFieldMetaData(FieldMetaData fmd) {
046: this .fmd = fmd;
047: }
048:
049: public byte replacingFlags(PersistenceCapable persistenceCapable) {
050: return 0;
051: }
052:
053: public StateManager replacingStateManager(
054: PersistenceCapable persistenceCapable,
055: StateManager stateManager) {
056: return stateManager;
057: }
058:
059: public boolean isDirty(PersistenceCapable persistenceCapable) {
060: return ((VersantDetachable) persistenceCapable)
061: .versantIsDirty();
062: }
063:
064: public boolean isTransactional(PersistenceCapable persistenceCapable) {
065: return false;
066: }
067:
068: public boolean isPersistent(PersistenceCapable persistenceCapable) {
069: return false;
070: }
071:
072: public boolean isNew(PersistenceCapable persistenceCapable) {
073: return getPcOID((VersantDetachable) persistenceCapable).isNew();
074: }
075:
076: public boolean isDeleted(PersistenceCapable persistenceCapable) {
077: return false;
078: }
079:
080: public PersistenceManager getPersistenceManager(
081: PersistenceCapable persistenceCapable) {
082: return null;
083: }
084:
085: public void makeDirty(PersistenceCapable persistenceCapable,
086: String s) {
087: ((VersantDetachable) persistenceCapable).versantMakeDirty(s);
088: }
089:
090: public Object getObjectId(PersistenceCapable persistenceCapable) {
091: return getPcOID((VersantDetachable) persistenceCapable);
092: }
093:
094: public Object getTransactionalObjectId(
095: PersistenceCapable persistenceCapable) {
096: return null;
097: }
098:
099: public boolean isLoaded(PersistenceCapable persistenceCapable, int i) {
100: return ((VersantDetachable) persistenceCapable)
101: .versantIsLoaded(i);
102: }
103:
104: public void preSerialize(PersistenceCapable persistenceCapable) {
105:
106: }
107:
108: public boolean getBooleanField(
109: PersistenceCapable persistenceCapable, int i, boolean b) {
110: return b;
111: }
112:
113: public char getCharField(PersistenceCapable persistenceCapable,
114: int i, char c) {
115: return c;
116: }
117:
118: public byte getByteField(PersistenceCapable persistenceCapable,
119: int i, byte b) {
120: return b;
121: }
122:
123: public short getShortField(PersistenceCapable persistenceCapable,
124: int i, short i1) {
125: return i1;
126: }
127:
128: public int getIntField(PersistenceCapable persistenceCapable,
129: int i, int i1) {
130: return i1;
131: }
132:
133: public long getLongField(PersistenceCapable persistenceCapable,
134: int i, long l) {
135: return l;
136: }
137:
138: public float getFloatField(PersistenceCapable persistenceCapable,
139: int i, float v) {
140: return v;
141: }
142:
143: public double getDoubleField(PersistenceCapable persistenceCapable,
144: int i, double v) {
145: return v;
146: }
147:
148: public String getStringField(PersistenceCapable persistenceCapable,
149: int i, String s) {
150: return s;
151: }
152:
153: public Object getObjectField(PersistenceCapable persistenceCapable,
154: int i, Object o) {
155: return o;
156: }
157:
158: public void setBooleanField(PersistenceCapable persistenceCapable,
159: int i, boolean b, boolean b1) {
160: }
161:
162: public void setCharField(PersistenceCapable persistenceCapable,
163: int i, char c, char c1) {
164: }
165:
166: public void setByteField(PersistenceCapable persistenceCapable,
167: int i, byte b, byte b1) {
168: }
169:
170: public void setShortField(PersistenceCapable persistenceCapable,
171: int i, short i1, short i2) {
172: }
173:
174: public void setIntField(PersistenceCapable persistenceCapable,
175: int i, int i1, int i2) {
176: }
177:
178: public void setLongField(PersistenceCapable persistenceCapable,
179: int i, long l, long l1) {
180: }
181:
182: public void setFloatField(PersistenceCapable persistenceCapable,
183: int i, float v, float v1) {
184: }
185:
186: public void setDoubleField(PersistenceCapable persistenceCapable,
187: int i, double v, double v1) {
188: }
189:
190: public void setStringField(PersistenceCapable persistenceCapable,
191: int i, String s, String s1) {
192: }
193:
194: public void setObjectField(PersistenceCapable persistenceCapable,
195: int i, Object o, Object o1) {
196: }
197:
198: public void providedBooleanField(
199: PersistenceCapable persistenceCapable, int i, boolean b) {
200: state.setBooleanFieldAbs(i, b);
201: }
202:
203: public void providedCharField(
204: PersistenceCapable persistenceCapable, int i, char c) {
205: state.setCharFieldAbs(i, c);
206: }
207:
208: public void providedByteField(
209: PersistenceCapable persistenceCapable, int i, byte b) {
210: state.setByteFieldAbs(i, b);
211: }
212:
213: public void providedShortField(
214: PersistenceCapable persistenceCapable, int i, short i1) {
215: state.setShortFieldAbs(i, i1);
216: }
217:
218: public void providedIntField(PersistenceCapable persistenceCapable,
219: int i, int i1) {
220: state.setIntFieldAbs(i, i1);
221: }
222:
223: public void providedLongField(
224: PersistenceCapable persistenceCapable, int i, long l) {
225: state.setLongFieldAbs(i, l);
226: }
227:
228: public void providedFloatField(
229: PersistenceCapable persistenceCapable, int i, float v) {
230: state.setFloatFieldAbs(i, v);
231: }
232:
233: public void providedDoubleField(
234: PersistenceCapable persistenceCapable, int i, double v) {
235: state.setDoubleFieldAbs(i, v);
236: }
237:
238: public void providedStringField(
239: PersistenceCapable persistenceCapable, int i, String s) {
240: state.setStringFieldAbs(i, s);
241: }
242:
243: public void providedObjectField(
244: PersistenceCapable persistenceCapable, int i, Object o) {
245: if (o == null) {
246: state.setObjectFieldAbs(i, o);
247: } else {
248: state.setObjectFieldAbs(i, getUnresolved(o));
249: }
250: }
251:
252: private Object getUnresolved(Object o) {
253: switch (fmd.category) {
254: case MDStatics.CATEGORY_COLLECTION:
255: if (o instanceof Collection) {
256: Collection col = (Collection) o;
257: ArrayList oids = new ArrayList(col.size());
258: if (fmd.isElementTypePC()) {
259: for (Iterator it = col.iterator(); it.hasNext();) {
260: VersantDetachable detachable = (VersantDetachable) it
261: .next();
262: oids.add(getPC(detachable));
263: }
264: } else {
265: oids.addAll(col);
266: }
267: col = createNewCol();
268: col.addAll(oids);
269: return col;
270: }
271: break;
272: case MDStatics.CATEGORY_ARRAY:
273: if (!o.getClass().isArray())
274: return o;
275: Class type = o.getClass().getComponentType();
276: int length = Array.getLength(o);
277: Object newArray = Array.newInstance(type, length);
278: System.arraycopy(o, 0, newArray, 0, length);
279: if (fmd.isElementTypePC()) {
280: Object[] objects = (Object[]) newArray;
281: for (int i = 0; i < objects.length; i++) {
282: objects[i] = getPC((VersantDetachable) objects[i]);
283: }
284: }
285: return newArray;
286: case MDStatics.CATEGORY_MAP:
287: if (o instanceof Map) {
288: MapEntries entries = new MapEntries();
289: Map map = (Map) o;
290: int size = map.size();
291: entries.keys = new Object[size];
292: entries.values = new Object[size];
293: int x = 0;
294: for (Iterator it = map.keySet().iterator(); it
295: .hasNext();) {
296: Object o1 = (Object) it.next();
297: if (fmd.isKeyTypePC()) {
298: entries.keys[x] = getPC((VersantDetachable) o1);
299: } else {
300: entries.keys[x] = o1;
301: }
302: if (fmd.isElementTypePC()) {
303: entries.values[x] = getPC((VersantDetachable) map
304: .get(o1));
305: } else {
306: entries.values[x] = map.get(o1);
307: }
308: x++;
309: }
310: map = createNewMap();
311: length = entries.keys.length;
312: for (int i = 0; i < length; i++) {
313: Object key = entries.keys[i];
314: Object value = entries.values[i];
315: map.put(key, value);
316: }
317: return map;
318: }
319: break;
320: case MDStatics.CATEGORY_REF:
321: case MDStatics.CATEGORY_POLYREF:
322: VersantDetachable detachable = (VersantDetachable) o;
323: return getPC(detachable);
324: }
325: return o;
326: }
327:
328: private Object getPC(VersantDetachable detachable) {
329: OID pcOID = getPcOID(detachable);
330: return pm.getObjectById(pcOID, false);
331: }
332:
333: private Map createNewMap() {
334: switch (fmd.typeCode) {
335: case MDStatics.MAP:
336: case MDStatics.HASHMAP:
337: return new HashMap();
338: case MDStatics.HASHTABLE:
339: return new Hashtable();
340: case MDStatics.TREEMAP:
341: case MDStatics.SORTEDMAP:
342: return new TreeMap();
343: default:
344: throw BindingSupportImpl.getInstance().notImplemented(
345: "Creating a Map instance for field "
346: + fmd.getName() + " of type "
347: + MDStaticUtils.toSimpleName(fmd.typeCode)
348: + " is not supported");
349: }
350: }
351:
352: private Collection createNewCol() {
353: switch (fmd.typeCode) {
354: case MDStatics.HASHSET:
355: case MDStatics.SET:
356: return new HashSet();
357: case MDStatics.TREESET:
358: case MDStatics.SORTEDSET:
359: return new TreeSet();
360: case MDStatics.COLLECTION:
361: case MDStatics.LIST:
362: case MDStatics.ARRAYLIST:
363: return new ArrayList();
364: case MDStatics.LINKEDLIST:
365: return new LinkedList();
366: case MDStatics.VECTOR:
367: return new Vector();
368: default:
369: throw BindingSupportImpl.getInstance().notImplemented(
370: "Creating a Collection instance for field "
371: + fmd.getName() + " of type "
372: + MDStaticUtils.toSimpleName(fmd.typeCode)
373: + " is not supported");
374: }
375: }
376:
377: public boolean replacingBooleanField(
378: PersistenceCapable persistenceCapable, int i) {
379: return false;
380: }
381:
382: public char replacingCharField(
383: PersistenceCapable persistenceCapable, int i) {
384: return 0;
385: }
386:
387: public byte replacingByteField(
388: PersistenceCapable persistenceCapable, int i) {
389: return 0;
390: }
391:
392: public short replacingShortField(
393: PersistenceCapable persistenceCapable, int i) {
394: return 0;
395: }
396:
397: public int replacingIntField(PersistenceCapable persistenceCapable,
398: int i) {
399: return 0;
400: }
401:
402: public long replacingLongField(
403: PersistenceCapable persistenceCapable, int i) {
404: return 0;
405: }
406:
407: public float replacingFloatField(
408: PersistenceCapable persistenceCapable, int i) {
409: return 0;
410: }
411:
412: public double replacingDoubleField(
413: PersistenceCapable persistenceCapable, int i) {
414: return 0;
415: }
416:
417: public String replacingStringField(
418: PersistenceCapable persistenceCapable, int i) {
419: return null;
420: }
421:
422: public Object replacingObjectField(
423: PersistenceCapable persistenceCapable, int i) {
424: return null;
425: }
426:
427: public void fillNewAppPKField(int fieldNo) {
428: }
429:
430: public void makeDirty(PersistenceCapable pc, int managedFieldNo) {
431: ((VersantDetachable) pc).versantMakeDirty(managedFieldNo);
432: }
433:
434: private OID getPcOID(VersantDetachable persistenceCapable) {
435: return pm.getOID(persistenceCapable);
436: }
437:
438: public OID getOID() {
439: return null;
440: }
441:
442: public PersistenceCapable getPersistenceCapable() {
443: return null;
444: }
445:
446: }
|