KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > ejb > EntityManagerImp


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.ejb;
13
14 import com.versant.core.metadata.ModelMetaData;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.metadata.FetchGroup;
17 import com.versant.core.metadata.FieldMetaData;
18 import com.versant.core.jdo.*;
19 import com.versant.core.storagemanager.StorageManager;
20 import com.versant.core.storagemanager.ApplicationContext;
21 import com.versant.core.common.*;
22 import com.versant.core.ejb.common.EntrySet;
23 import com.versant.core.ejb.common.IdentityEntry;
24
25 import javax.jdo.*;
26 import javax.jdo.spi.PersistenceCapable;
27 import javax.persistence.EntityManager;
28 import javax.persistence.Query;
29 import javax.persistence.EntityNotFoundException;
30 import javax.persistence.TransactionRequiredException;
31 import javax.transaction.Synchronization JavaDoc;
32 import java.util.*;
33 import java.lang.reflect.Field JavaDoc;
34 import java.sql.Connection JavaDoc;
35
36 /**
37  * Implementation of ejb3 EntityManager.
38  */

39 public class EntityManagerImp implements EntityManager, Transaction,
40         ApplicationContext, PersistenceContext {
41     public final StorageManager storageMan;
42     ModelMetaData modelMetaData;
43     private EMProxy proxy;
44     /**
45      * The objects that must be commited at the end of the tx.
46      */

47     private Set<StateManagerImp> txObjects = new HashSet<StateManagerImp>();
48     private LocalCache cache = new LocalCache();
49     /**
50      * The list of instances that has been marked for delete in the current tx.
51      */

52     private final DeletePacket toBeDeleted;
53     /**
54      * This is used to transport the dirty stuff to the server.
55      */

56     public final StatesToStore storeOidStateContainer;
57     private boolean txActive;
58     /**
59      * int val used for creating newoids
60      */

61     private int newOidCounter;
62
63     public EntityManagerImp(StorageManager sm, ModelMetaData modelMetaData) {
64         this.storageMan = sm;
65         this.modelMetaData = modelMetaData;
66         toBeDeleted = new DeletePacket(modelMetaData);
67         storeOidStateContainer = new StatesToStore(modelMetaData);
68     }
69
70     public Transaction getTransaction() {
71         return this;
72     }
73
74     /**
75      * The StorageManager calls this method to check if we need prefetched
76      * data or not.
77      */

78     public boolean isStateRequired(OID oid, FetchGroup fetchGroup) {
79 // State state = cache.getStateByOID(oid);
80
// if (state != null) {
81
// if (fetchGroup == null) {
82
// fetchGroup = state.getClassMetaData().fetchGroups[0];
83
// }
84
// return !state.containsFetchGroup(fetchGroup);
85
// }
86
return true;
87     }
88
89     public void close() {
90     }
91
92     public void isOpen() {
93     }
94
95     /**
96      * " If X is a new entity, it becomes managed. The entity X will be entered
97      * into the database at or before transaction commit or as a result of the
98      * flush operation .
99      *
100      * " If X is a preexisting managed entity, it is ignored by the persist
101      * operation. However, the persist operation is cascaded to entities
102      * referenced by X, if the relationships from X to these other entities
103      * is annotated with the cascade=PERSIST or cascade=ALL annotation member
104      * value.
105      *
106      * " If X is a removed entity, it becomes managed.
107      *
108      * " If X is a detached object, an IllegalArgumentException will be thrown
109      * by the container (or the transaction commit will fail).
110      *
111      * " For all entities Y referenced by a relationship from X, if the
112      * relationship to Y has been annotated with the cascade member
113      * value cascade=PERSIST or cascade=ALL, the persist operation is
114      * applied to Y.
115      * @param entity
116      */

117     public void persist(Object JavaDoc entity) {
118         if (entity == null) return;
119         checkForActiveTransaction();
120
121         //create a '==' based set
122
final IdentityEntry tailPointer = new IdentityEntry("HEAD");
123         EntrySet mergeSet = new EntrySet() {
124             public IdentityEntry tail = tailPointer;
125
126             public Entry createEntry(Object JavaDoc key) {
127                 IdentityEntry ne = new IdentityEntry(key);
128                 tail.nextEntry = ne;
129                 tail = ne;
130                 return ne;
131             }
132         };
133
134         mergeSet.add(entity);
135         for (IdentityEntry ie = tailPointer.nextEntry; ie != null; ie = ie.nextEntry) {
136             persistImp(ie.getKey(), mergeSet);
137         }
138     }
139
140     /**
141      * Add all the OIDs and States in the container to the cache.
142      */

143     public void addToCache(StatesReturned container) {
144         for (Iterator i = container.iterator(); i.hasNext(); ) {
145             com.versant.core.common.EntrySet.Entry e = (com.versant.core.common.EntrySet.Entry)i.next();
146             cache.add((OID)e.getKey(), (State)e.getValue());
147         }
148     }
149
150     private void persistImp(Object JavaDoc entity, EntrySet mergeSet) {
151         if (entity == null) return;
152         LifeCycleStatus lfs = getLifeCycleStatus(entity);
153         switch(lfs) {
154             case NEW:
155                 manage(entity).persistReferences(mergeSet);
156                 break;
157             case MANAGED:
158                 getStateManager((PersistenceCapable) entity).persistReferences(mergeSet);
159                 break;
160             case DETACHED:
161                 throw new IllegalArgumentException JavaDoc(
162                         "The supplied instance is a detached instance. " +
163                         "Please make use of the 'merge' operation.");
164         }
165     }
166
167     /**
168      * Check that an transaction is active.
169      * @throws javax.persistence.TransactionRequiredException
170      */

171     private void checkForActiveTransaction() {
172         if (!txActive) throw new TransactionRequiredException();
173     }
174
175     /**
176      * Takes a new instance (ie not managed or detached) and manage it.
177      * This is not a recursive operation.
178      * @param entity
179      */

180     private StateManagerImp manage(Object JavaDoc entity) {
181         StateManagerImp sm = new StateManagerImp(proxy, modelMetaData);
182         sm.manageNew((PersistenceCapable) entity, cache, newOidCounter++);
183         txObjects.add(sm);
184         return sm;
185     }
186
187     /**
188      * Look at the annotations and manage references reachable from this instance.
189      * @param entity
190      */

191     private void manageReferences(Object JavaDoc entity) {
192         //To change body of created methods use File | Settings | File Templates.
193
}
194
195     /**
196      * Determine the status {@link LifeCycleStatus} of the supplied entity.
197      * @param entity
198      */

199     public LifeCycleStatus getLifeCycleStatus(Object JavaDoc entity) {
200         if (!(entity instanceof PersistenceCapable)) {
201             throw new IllegalArgumentException JavaDoc("The supplied instance '"
202                     + entity.getClass().getName() + "' is not a manageble type");
203         }
204         PersistenceCapable pc = (PersistenceCapable) entity;
205         PersistenceManager pm = pc.jdoGetPersistenceManager();
206         if (pm == null) {
207             if ((entity instanceof VersantDetachable)
208                     && ((VersantDetachable)entity).versantGetDetachedStateManager() != null) {
209                 return LifeCycleStatus.DETACHED;
210             }
211             return LifeCycleStatus.NEW;
212         }
213
214         if (pm != this) {
215             throw new IllegalArgumentException JavaDoc("The supplied entity is not " +
216                     "managed does by this EntityManager");
217         }
218
219         StateManagerImp em = getStateManager(pc);
220         if (em.isRemoved()) {
221             return LifeCycleStatus.REMOVED;
222         }
223         return LifeCycleStatus.MANAGED;
224     }
225
226     private void detachOnCommit() {
227         VersantDetachedStateManager vdsm = new VersantDetachedStateManager();
228         Iterator iter = cache.getCacheIterator();
229         while (iter.hasNext()) {
230             LocalCache.CacheEntry entry = (LocalCache.CacheEntry) iter.next();
231             if (entry.getValue() instanceof StateManagerImp) {
232                 ((StateManagerImp) entry.getValue()).detachOnCommit(vdsm);
233             }
234         }
235     }
236
237     /**
238      * Try and obtain the entitymanager for the pc instance.
239      */

240     private StateManagerImp getStateManager(PersistenceCapable pc) {
241         StateManagerImp em = null;
242         try {
243             Field JavaDoc f = pc.getClass().getDeclaredField("jdoStateManager");
244             f.setAccessible(true);
245             em = (StateManagerImp) f.get(pc);
246         } catch (Exception JavaDoc e) {
247             e.printStackTrace(System.out);
248         }
249         return em;
250     }
251
252     /**
253      * " If X is a detached entity, it is copied onto a pre-existing managed
254      * entity instance X' of the same identity or a new managed copy of X is created.
255      *
256      * " If X is a new entity instance, a new managed entity instance X' is created
257      * and the state of X is copied into the new managed entity instance X'.
258      *
259      * " If X is a removed entity instance, an IllegalArgumentException will be
260      * thrown by the container (or the transaction commit will fail).
261      *
262      * " If X is a managed entity, it is ignored by the merge operation, however,
263      * the merge operation is cascaded to entities referenced by relationships
264      * from X if these relationships have been annotated with the cascade member
265      * value cascade=MERGE or cascade=ALL annotation.
266      *
267      * " For all entities Y referenced by relationships from X having the cascade
268      * member value cascade=MERGE or cascade=ALL, Y is merged recursively as Y'.
269      * For all such Y referenced by X, X' is set to reference Y'.
270      * (Note that if X is managed then X is the same object as X'.)
271      *
272      * " If X is an entity merged to X', with a reference to another entity Y,
273      * where cascade=MERGE or cascade=ALL is not specified, then navigation of
274      * the same association from X' yields a reference to a managed object Y'
275      * with the same persistent identity as Y.
276      *
277      * " Fields or properties of type java.sql.Blob and java.sql.Clob are
278      * ignored by the merge operation.
279      *
280      * @param entity
281      * @return the instance that the state was merged to
282      */

283     public <T> T merge(T entity) {
284         //create a '==' based set
285
final IdentityEntry tailPointer = new IdentityEntry("HEAD");
286         EntrySet mergeSet = new EntrySet() {
287             public IdentityEntry tail = tailPointer;
288
289             public Entry createEntry(Object JavaDoc key) {
290                 IdentityEntry ne = new IdentityEntry(key);
291                 tail.nextEntry = ne;
292                 tail = ne;
293                 return ne;
294             }
295         };
296
297         mergeSet.add(entity);
298         T result = (T) mergeInternal(entity, mergeSet).pc;
299         getStateManager((PersistenceCapable) result).mergeReferences(mergeSet);
300
301         for (IdentityEntry ie = tailPointer.nextEntry.nextEntry; ie != null; ie = ie.nextEntry) {
302             mergeInternal(ie.getKey(), mergeSet).mergeReferences(mergeSet);
303         }
304
305         return result;
306     }
307
308     /**
309      * Merge in the provided entity.
310      * @param entity
311      * @param mergeSet
312      * @return
313      */

314     public StateManagerImp mergeInternal(Object JavaDoc entity, EntrySet mergeSet) {
315         IdentityEntry e = (IdentityEntry) mergeSet.addAndReturnEntry(entity);
316         if (e.value != null) {
317             return (StateManagerImp) e.value;
318         }
319         switch(getLifeCycleStatus(entity)) {
320             case NEW:
321                 return (StateManagerImp) (e.value = manage(entity));
322             case MANAGED:
323                 checkManagedBy(entity);
324                 return (StateManagerImp) (e.value = getStateManager((PersistenceCapable)entity));
325             case DETACHED:
326                 VersantDetachable de = (VersantDetachable) entity;
327                 OID oid = extractOID(de.versantGetOID());
328                 StateManagerImp sm = getObjectById(oid);
329
330                 //check for version
331
if (sm.cmd.optimisticLockingField != null &&
332                         !de.versantGetVersion().equals(sm.getOptimisticLockingValue())) {
333                     throw new RuntimeException JavaDoc("Conncurrent update");
334                 }
335
336                 mergeFromDetached(de, sm);
337                 return (StateManagerImp) (e.value = sm);
338             case REMOVED:
339                 throw new IllegalStateException JavaDoc(
340                         "'merge' operation not allowed on '"
341                         + getLifeCycleStatus(entity) + "' instances");
342             default:
343                 throw BindingSupportImpl.getInstance().internal("Unhandled lifecycle state '" +getLifeCycleStatus(entity)+"'");
344
345         }
346     }
347
348     public <T> T mergeImp(T entity, EntrySet mergeSet) {
349         switch(getLifeCycleStatus(entity)) {
350             case NEW:
351                 return (T)manage(entity).mergeReferences(mergeSet).pc;
352             case MANAGED:
353                 checkManagedBy(entity);
354                 return (T)getStateManager((PersistenceCapable)entity).mergeReferences(mergeSet).pc;
355             case DETACHED:
356                 VersantDetachable de = (VersantDetachable) entity;
357                 OID oid = extractOID(de.versantGetOID());
358                 StateManagerImp sm = getObjectById(oid);
359
360                 //check for version
361
if (sm.cmd.optimisticLockingField != null &&
362                         !de.versantGetVersion().equals(sm.getOptimisticLockingValue())) {
363                     throw new RuntimeException JavaDoc("Conncurrent update");
364                 }
365                 mergeFromDetached(de, sm);
366                 sm.mergeReferences(mergeSet);
367                 return (T) sm.pc;
368             case REMOVED:
369                 throw new IllegalStateException JavaDoc(
370                         "'merge' operation not allowed on '"
371                         + getLifeCycleStatus(entity) + "' instances");
372             default:
373                 throw BindingSupportImpl.getInstance().internal("Unhandled lifecycle state '" +getLifeCycleStatus(entity)+"'");
374
375         }
376     }
377
378     /**
379      * Merge from the detached instance to the managed instance.
380      */

381     private void mergeFromDetached(VersantDetachable de, StateManagerImp sm) {
382         //only copy its dirty fields
383
if (de.versantIsDirty()) {
384             VersantDetachedStateManager dsm = de.versantGetDetachedStateManager();
385             AttachStateManager asm = new AttachStateManager();
386             asm.setSm(sm);
387             try {
388                 de.jdoReplaceStateManager(asm);
389                 FieldMetaData[] mFields = sm.cmd.managedFields;
390                 for (int i = 0; i < mFields.length; i++) {
391                     FieldMetaData mField = mFields[i];
392                     if (de.versantIsDirty(mField.managedFieldNo)) {
393                         de.jdoProvideField(mField.managedFieldNo);
394                     }
395                 }
396             } finally {
397                 de.jdoReplaceStateManager(dsm);
398             }
399         }
400     }
401
402     /**
403      * Create a managed instance that is of the same type as t and fill it with
404      * the date from t
405      * @param t
406      * @return T managed instance created from t
407      */

408     private<T> T createManagedInstanceFrom(T t) {
409         return null;
410     }
411
412     /**
413      * Check that the managedVal is managedBy this EntityManager.
414      * @param managedVal
415      */

416     private void checkManagedBy(Object JavaDoc managedVal) throws UserException {
417         //To change body of created methods use File | Settings | File Templates.
418
}
419
420     /**
421      * Return managed instance for supplied oid. If instance not found in
422      * localCache then manage it.
423      *
424      * @param oid
425      * @return
426      */

427     public StateManagerImp getObjectById(OID oid) {
428         StateManagerImp sm = getSmFromCache(oid);
429         if (sm == null) {
430             ClassMetaData cmd = oid.getAvailableClassMetaData();
431             StatesReturned sr = null;
432             try {
433                 sr = storageMan.fetch(this, oid, null, cmd.fetchGroups[0], null);
434             } catch (JDOObjectNotFoundException e) {
435                 throw throwEntityNotFound(oid);
436             }
437             sm = new StateManagerImp(proxy, modelMetaData);
438             oid = sr.getDirectOID();
439
440             sm.manage(oid, sr.get(oid), cache);
441         }
442         return sm;
443     }
444
445     public StateManagerImp manage(OID oid, State state) {
446         StateManagerImp sm = new StateManagerImp(proxy, modelMetaData);
447         sm.manage(oid, state, cache);
448         return sm;
449     }
450
451     public void fetchState(StateManagerImp sm, FetchGroup fg) {
452         StatesReturned sr = null;
453         try {
454             sr = storageMan.fetch(this, sm.oid, null, fg, null);
455         } catch (JDOObjectNotFoundException e) {
456             throw throwEntityNotFound(sm.oid);
457         }
458         sm.updateState(sr.get(sm.oid));
459     }
460
461     public Object JavaDoc getObjectByIdForState(OID oid, int stateFieldNo, int navClassIndex, OID fromOID) {
462         try {
463             PersistenceCapable pc = null;
464             FieldMetaData fmd = fromOID.getAvailableClassMetaData().stateFields[stateFieldNo];
465             if (fmd.embedded) {
466                 if (true) throw new RuntimeException JavaDoc();
467 // //create a managed instance of the embedded reference
468
// StateManagerImp sm = cache.get(fromOID);
469
// if (fmd.nullIndicatorFmd != null) {
470
// if (sm.state.isFieldNullorZero(fmd.nullIndicatorFmd.stateFieldNo)) {
471
// return null;
472
// }
473
// }
474
// EmbeddedStateManager embeddedSm = sm.createEmbeddedSM(fmd);
475
// pc = embeddedSm.pc;
476
} else {
477                 StateManagerImp sm = getObjectById(oid);
478                 pc = sm.pc;
479             }
480             return pc;
481         } catch (Exception JavaDoc e) {
482             handleException(e);
483             return null;
484         }
485     }
486
487     public OID extractOID(Object JavaDoc oid) {
488         if (oid instanceof VersantOid) {
489             return modelMetaData.convertJDOGenieOIDtoOID((VersantOid)oid);
490         } else {
491             return modelMetaData.convertFromAppIdToOID(oid);
492         }
493     }
494
495     private OID convertNewToActual(OID oid) {
496         if (!oid.isNew()) {
497             oid.getAvailableClassMetaData();
498             return oid;
499         }
500         return oid.getAvailableOID();
501     }
502
503     /**
504      * Find a managed intance that represents the same id as the supplied
505      * detached intance.
506      * @param detached
507      * @return
508      */

509     private <T> T findManagedInstanceforDetached(T detached) {
510         return null;
511     }
512
513     public void remove(Object JavaDoc entity) {
514         checkForActiveTransaction();
515         LifeCycleStatus lfs = getLifeCycleStatus(entity);
516         switch (lfs) {
517             case DETACHED:
518             case REMOVED:
519                 throw new IllegalArgumentException JavaDoc("May not remove 'DETACHED' or 'REMOVED' instances");
520             case MANAGED:
521                 StateManagerImp sm = getStateManager((PersistenceCapable) entity);
522                 sm.remove();
523                 break;
524             case NEW:
525
526                 break;
527             default:
528                 throw new RuntimeException JavaDoc("Unhandled lifecycle state: '"
529                         + lfs + "'");
530         }
531     }
532
533     public Object JavaDoc find(String JavaDoc entityName, Object JavaDoc primaryKey) {
534         return null;
535     }
536
537     public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
538         ClassMetaData cmd = modelMetaData.getClassMetaData(entityClass);
539         if (cmd == null) {
540             throw new IllegalArgumentException JavaDoc("The class '"
541                     + entityClass.getName()
542                     + "' is not a persistable class");
543         }
544         OID oid = cmd.createOID(false).fillFromIDObject(primaryKey);
545         StateManagerImp sm = getSmFromCache(oid);
546         if (sm == null) {
547             StatesReturned sr = null;
548             try {
549                 sr = storageMan.fetch(this, oid, null, cmd.fetchGroups[0], null);
550             } catch (JDOObjectNotFoundException e) {
551                 throw throwEntityNotFound(entityClass, primaryKey);
552             }
553             sm = new StateManagerImp(proxy, modelMetaData);
554             oid = sr.getDirectOID();
555             sm.manage(oid, sr.get(oid), cache);
556         }
557         return (T) sm.getPersistenceCapable();
558     }
559
560     private StateManagerImp getSmFromCache(OID oid) {
561         Object JavaDoc o = cache.get(oid);
562         if (o == null) return null;
563         if (o instanceof StateManagerImp) return (StateManagerImp) o;
564         if (o instanceof State) {
565             return manage(oid, (State) o);
566         }
567         throw BindingSupportImpl.getInstance().internal(
568                 "Unhandled instance type '" + o.getClass().getName() + "' in local cache");
569
570     }
571
572     private static RuntimeException JavaDoc throwEntityNotFound(Class JavaDoc entityClass, Object JavaDoc primaryKey) {
573         return new EntityNotFoundException("Entity '"
574                 + entityClass.getName() + "' with id '" + primaryKey + "' can not be found.");
575     }
576
577     private static RuntimeException JavaDoc throwEntityNotFound(OID oid) {
578         return new EntityNotFoundException("Entity '"
579                 + oid.getAvailableClassMetaData().qname + "' with id '" + oid
580                 + "' can not be found.");
581     }
582
583     public void flush() {
584         checkForActiveTransaction();
585         StatesReturned sc = null;
586         try {
587             //prepare the sm's for to store the states
588
prepareForStore(true);
589             //send the state to the server
590
sc = storageMan.store(storeOidStateContainer, toBeDeleted, false,
591                     StorageManager.STORE_OPTION_FLUSH, false);
592             postStore(sc, true);
593         } catch (Exception JavaDoc e) {
594             handleException(e);
595         } finally {
596             if (sc != null) {
597                 sc.clear();
598             }
599             storeOidStateContainer.clear();
600             toBeDeleted.clear();
601             txObjects.clear();
602         }
603     }
604
605     public void refresh(Object JavaDoc entity) {
606     }
607
608     public boolean contains(Object JavaDoc entity) {
609         return false; //To change body of implemented methods use File | Settings | File Templates.
610
}
611
612     public Query createQuery(String JavaDoc ejbqlString) {
613         return new VersantEjbQueryImp(proxy, ejbqlString);
614     }
615
616     public Query createNamedQuery(String JavaDoc name) {
617         return null;
618     }
619
620     public Query createNativeQuery(String JavaDoc sqlString) {
621         return null;
622     }
623
624     public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass) {
625         return null;
626     }
627
628     public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping) {
629         return null;
630     }
631
632     public void begin() {
633         if (txActive) {
634             throw new RuntimeException JavaDoc("The transaction is alread active");
635         }
636         if (proxy != null) {
637             throw new RuntimeException JavaDoc("The em was not reset correctly at end of previous transaction.");
638         }
639         storageMan.begin(true);
640         proxy = new EMProxy(this);
641         txActive = true;
642     }
643
644     /**
645      * Commit the active transaction. Everything must be detached at the end
646      * of the operation. This would mean that the pc instance must contain the needed
647      * data for it to be detached.
648      */

649     public void commit() {
650         checkForActiveTransaction();
651         StatesReturned sc = null;
652         try {
653             //prepare the sm's for to store the states
654
prepareForStore(true);
655             //send the state to the server
656
sc = storageMan.store(storeOidStateContainer, toBeDeleted, false,
657                     StorageManager.STORE_OPTION_COMMIT,
658                     false);
659             postStore(sc, false);
660             txActive = false;
661         } catch (Exception JavaDoc e) {
662             handleException(e);
663         } finally {
664             if (proxy != null) {
665                 proxy.detach();
666                 proxy = null;
667             }
668             if (sc != null) {
669                 sc.clear();
670             }
671             storeOidStateContainer.clear();
672             toBeDeleted.clear();
673             txObjects.clear();
674         }
675         detachOnCommit();
676         cache.clear();
677     }
678
679     private void postStore(StatesReturned sc, boolean flush) {
680         //give each instance a change to
681
for (Iterator<StateManagerImp> iterator = txObjects.iterator(); iterator.hasNext();) {
682             iterator.next().postStore(sc, flush);
683         }
684     }
685
686     /**
687      * All public method must be wrapped with this.
688      * @param e
689      */

690     private void handleException(Exception JavaDoc e) {
691         e.printStackTrace(System.out);
692         throw new RuntimeException JavaDoc(e);
693     }
694
695     /**
696      * Prepare to store all dirty instances for a commit or flush. This finds
697      * all reachable instances and invokes preStore lifecycle listeners and
698      * jdoPreStore instance callbacks.
699      */

700     private void prepareForStore(boolean commit) {
701         storeOidStateContainer.clear();
702         toBeDeleted.clear();
703         for (Iterator<StateManagerImp> iterator = txObjects.iterator(); iterator.hasNext();) {
704             iterator.next().prepareCommitOrFlush(commit, toBeDeleted, storeOidStateContainer);
705         }
706     }
707
708     public void rollback() {
709         if (!txActive) {
710             throw new RuntimeException JavaDoc("There is no active transaction");
711         }
712         if (proxy != null) {
713             proxy.detach();
714         }
715     }
716
717     public boolean isActive() {
718         return txActive;
719     }
720
721     public void setNontransactionalRead(boolean b) {
722         //To change body of implemented methods use File | Settings | File Templates.
723
}
724
725     public boolean getNontransactionalRead() {
726         return false; //To change body of implemented methods use File | Settings | File Templates.
727
}
728
729     public void setNontransactionalWrite(boolean b) {
730         //To change body of implemented methods use File | Settings | File Templates.
731
}
732
733     public boolean getNontransactionalWrite() {
734         return false; //To change body of implemented methods use File | Settings | File Templates.
735
}
736
737     public void setRetainValues(boolean b) {
738         //To change body of implemented methods use File | Settings | File Templates.
739
}
740
741     public boolean getRetainValues() {
742         return false; //To change body of implemented methods use File | Settings | File Templates.
743
}
744
745     public void setRestoreValues(boolean b) {
746         //To change body of implemented methods use File | Settings | File Templates.
747
}
748
749     public boolean getRestoreValues() {
750         return false; //To change body of implemented methods use File | Settings | File Templates.
751
}
752
753     public void setOptimistic(boolean b) {
754         //To change body of implemented methods use File | Settings | File Templates.
755
}
756
757     public boolean getOptimistic() {
758         return false; //To change body of implemented methods use File | Settings | File Templates.
759
}
760
761     public void setSynchronization(Synchronization JavaDoc synchronization) {
762         //To change body of implemented methods use File | Settings | File Templates.
763
}
764
765     public Synchronization JavaDoc getSynchronization() {
766         return null; //To change body of implemented methods use File | Settings | File Templates.
767
}
768
769     public PersistenceManager getPersistenceManager() {
770         return this;
771     }
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799     
800
801     public boolean isClosed() {
802         return false; //To change body of implemented methods use File | Settings | File Templates.
803
}
804
805     public Transaction currentTransaction() {
806         return null; //To change body of implemented methods use File | Settings | File Templates.
807
}
808
809     public void evict(Object JavaDoc o) {
810         //To change body of implemented methods use File | Settings | File Templates.
811
}
812
813     public void evictAll(Object JavaDoc[] objects) {
814         //To change body of implemented methods use File | Settings | File Templates.
815
}
816
817     public void evictAll(Collection collection) {
818         //To change body of implemented methods use File | Settings | File Templates.
819
}
820
821     public void evictAll() {
822         //To change body of implemented methods use File | Settings | File Templates.
823
}
824
825     public void refreshAll(Object JavaDoc[] objects) {
826         //To change body of implemented methods use File | Settings | File Templates.
827
}
828
829     public void refreshAll(Collection collection) {
830         //To change body of implemented methods use File | Settings | File Templates.
831
}
832
833     public void refreshAll() {
834         //To change body of implemented methods use File | Settings | File Templates.
835
}
836
837     public javax.jdo.Query newQuery() {
838         return null; //To change body of implemented methods use File | Settings | File Templates.
839
}
840
841     public javax.jdo.Query newQuery(Object JavaDoc o) {
842         return null; //To change body of implemented methods use File | Settings | File Templates.
843
}
844
845     public javax.jdo.Query newQuery(String JavaDoc s, Object JavaDoc o) {
846         return null; //To change body of implemented methods use File | Settings | File Templates.
847
}
848
849     public javax.jdo.Query newQuery(Class JavaDoc aClass) {
850         return null; //To change body of implemented methods use File | Settings | File Templates.
851
}
852
853     public javax.jdo.Query newQuery(Extent extent) {
854         return null; //To change body of implemented methods use File | Settings | File Templates.
855
}
856
857     public javax.jdo.Query newQuery(Class JavaDoc aClass, Collection collection) {
858         return null; //To change body of implemented methods use File | Settings | File Templates.
859
}
860
861     public javax.jdo.Query newQuery(Class JavaDoc aClass, String JavaDoc s) {
862         return null; //To change body of implemented methods use File | Settings | File Templates.
863
}
864
865     public javax.jdo.Query newQuery(Class JavaDoc aClass, Collection collection, String JavaDoc s) {
866         return null; //To change body of implemented methods use File | Settings | File Templates.
867
}
868
869     public javax.jdo.Query newQuery(Extent extent, String JavaDoc s) {
870         return null; //To change body of implemented methods use File | Settings | File Templates.
871
}
872
873     public Extent getExtent(Class JavaDoc aClass, boolean b) {
874         return null; //To change body of implemented methods use File | Settings | File Templates.
875
}
876
877     public Object JavaDoc getObjectById(Object JavaDoc o, boolean b) {
878         if (o instanceof OID) {
879             return getObjectById((OID) o);
880         }
881         else {
882             throw BindingSupportImpl.getInstance().unsupported("unsupported operation");
883         }
884     }
885
886     public Object JavaDoc getObjectId(Object JavaDoc o) {
887         return null; //To change body of implemented methods use File | Settings | File Templates.
888
}
889
890     public Object JavaDoc getTransactionalObjectId(Object JavaDoc o) {
891         return null; //To change body of implemented methods use File | Settings | File Templates.
892
}
893
894     public Object JavaDoc newObjectIdInstance(Class JavaDoc aClass, String JavaDoc s) {
895         return null; //To change body of implemented methods use File | Settings | File Templates.
896
}
897
898     public void makePersistent(Object JavaDoc o) {
899         //To change body of implemented methods use File | Settings | File Templates.
900
}
901
902     public void makePersistentAll(Object JavaDoc[] objects) {
903         //To change body of implemented methods use File | Settings | File Templates.
904
}
905
906     public void makePersistentAll(Collection collection) {
907         //To change body of implemented methods use File | Settings | File Templates.
908
}
909
910     public void deletePersistent(Object JavaDoc o) {
911         //To change body of implemented methods use File | Settings | File Templates.
912
}
913
914     public void deletePersistentAll(Object JavaDoc[] objects) {
915         //To change body of implemented methods use File | Settings | File Templates.
916
}
917
918     public void deletePersistentAll(Collection collection) {
919         //To change body of implemented methods use File | Settings | File Templates.
920
}
921
922     public void makeTransient(Object JavaDoc o) {
923         //To change body of implemented methods use File | Settings | File Templates.
924
}
925
926     public void makeTransientAll(Object JavaDoc[] objects) {
927         //To change body of implemented methods use File | Settings | File Templates.
928
}
929
930     public void makeTransientAll(Collection collection) {
931         //To change body of implemented methods use File | Settings | File Templates.
932
}
933
934     public void makeTransactional(Object JavaDoc o) {
935         //To change body of implemented methods use File | Settings | File Templates.
936
}
937
938     public void makeTransactionalAll(Object JavaDoc[] objects) {
939         //To change body of implemented methods use File | Settings | File Templates.
940
}
941
942     public void makeTransactionalAll(Collection collection) {
943         //To change body of implemented methods use File | Settings | File Templates.
944
}
945
946     public void makeNontransactional(Object JavaDoc o) {
947         //To change body of implemented methods use File | Settings | File Templates.
948
}
949
950     public void makeNontransactionalAll(Object JavaDoc[] objects) {
951         //To change body of implemented methods use File | Settings | File Templates.
952
}
953
954     public void makeNontransactionalAll(Collection collection) {
955         //To change body of implemented methods use File | Settings | File Templates.
956
}
957
958     public void retrieve(Object JavaDoc o) {
959         //To change body of implemented methods use File | Settings | File Templates.
960
}
961
962     public void retrieveAll(Collection collection) {
963         //To change body of implemented methods use File | Settings | File Templates.
964
}
965
966     public void retrieveAll(Collection collection, boolean b) {
967         //To change body of implemented methods use File | Settings | File Templates.
968
}
969
970     public void retrieveAll(Object JavaDoc[] objects) {
971         //To change body of implemented methods use File | Settings | File Templates.
972
}
973
974     public void retrieveAll(Object JavaDoc[] objects, boolean b) {
975         //To change body of implemented methods use File | Settings | File Templates.
976
}
977
978     public void setUserObject(Object JavaDoc o) {
979         //To change body of implemented methods use File | Settings | File Templates.
980
}
981
982     public Object JavaDoc getUserObject() {
983         return null; //To change body of implemented methods use File | Settings | File Templates.
984
}
985
986     public PersistenceManagerFactory getPersistenceManagerFactory() {
987         return null; //To change body of implemented methods use File | Settings | File Templates.
988
}
989
990     public Class JavaDoc getObjectIdClass(Class JavaDoc aClass) {
991         return null; //To change body of implemented methods use File | Settings | File Templates.
992
}
993
994     public void setMultithreaded(boolean b) {
995         //To change body of implemented methods use File | Settings | File Templates.
996
}
997
998     public boolean getMultithreaded() {
999         return false; //To change body of implemented methods use File | Settings | File Templates.
1000
}
1001
1002    public void setIgnoreCache(boolean b) {
1003        //To change body of implemented methods use File | Settings | File Templates.
1004
}
1005
1006    public boolean getIgnoreCache() {
1007        return false; //To change body of implemented methods use File | Settings | File Templates.
1008
}
1009
1010    public void addToTxList(StateManagerImp sm) {
1011        if (txActive) {
1012            txObjects.add(sm);
1013        }
1014    }
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034    public boolean isInterceptDfgFieldAccess() {
1035        return false; //To change body of implemented methods use File | Settings | File Templates.
1036
}
1037
1038    public void setInterceptDfgFieldAccess(boolean interceptDfgFieldAccess) {
1039        //To change body of implemented methods use File | Settings | File Templates.
1040
}
1041
1042    public void cancelQueryExecution() {
1043        //To change body of implemented methods use File | Settings | File Templates.
1044
}
1045
1046    public boolean isDirty() {
1047        return !txObjects.isEmpty();
1048    }
1049
1050    public Object JavaDoc getObjectByIDString(String JavaDoc value, boolean toValidate) {
1051        return null; //To change body of implemented methods use File | Settings | File Templates.
1052
}
1053
1054    public Object JavaDoc newObjectIdInstance(Class JavaDoc pcClass, String JavaDoc str, boolean resolved) {
1055        return null; //To change body of implemented methods use File | Settings | File Templates.
1056
}
1057
1058    public Object JavaDoc getObjectByIDString(String JavaDoc value, boolean toValidate, boolean resolved) {
1059        return null; //To change body of implemented methods use File | Settings | File Templates.
1060
}
1061
1062    public void loadFetchGroup(Object JavaDoc pc, String JavaDoc name) {
1063        //To change body of implemented methods use File | Settings | File Templates.
1064
}
1065
1066    public void flush(boolean retainValues) {
1067        //To change body of implemented methods use File | Settings | File Templates.
1068
}
1069
1070    public Connection JavaDoc getJdbcConnection(String JavaDoc datastore) {
1071        return null; //To change body of implemented methods use File | Settings | File Templates.
1072
}
1073
1074    public String JavaDoc getConnectionURL(String JavaDoc dataStore) {
1075        return null; //To change body of implemented methods use File | Settings | File Templates.
1076
}
1077
1078    public String JavaDoc getConnectionDriverName(String JavaDoc dataStore) {
1079        return null; //To change body of implemented methods use File | Settings | File Templates.
1080
}
1081
1082    public void makeTransientRecursive(Object JavaDoc pc) {
1083        //To change body of implemented methods use File | Settings | File Templates.
1084
}
1085
1086    public List versantAllDirtyInstances() {
1087        if (txObjects.isEmpty()) return Collections.EMPTY_LIST;
1088        return new ArrayList(txObjects);
1089    }
1090
1091    public void flushIfDepOn(int[] cmdBits) {
1092        flush();
1093    }
1094
1095    public void setDatastoreTxLocking(int mode) {
1096        int policy;
1097        switch (mode) {
1098            case VersantPersistenceManager.LOCKING_NONE:
1099                policy = StorageManager.LOCK_POLICY_NONE;
1100                break;
1101            case VersantPersistenceManager.LOCKING_FIRST:
1102                policy = StorageManager.LOCK_POLICY_FIRST;
1103                break;
1104            case VersantPersistenceManager.LOCKING_ALL:
1105                policy = StorageManager.LOCK_POLICY_ALL;
1106                break;
1107            default:
1108                throw BindingSupportImpl.getInstance().invalidOperation(
1109                        "Invalid datastoreTxLocking mode: " + mode);
1110        }
1111        storageMan.setLockingPolicy(policy);
1112    }
1113
1114    public int getDatastoreTxLocking() {
1115        switch (storageMan.getLockingPolicy()) {
1116            case StorageManager.LOCK_POLICY_NONE:
1117                return VersantPersistenceManager.LOCKING_NONE;
1118            case StorageManager.LOCK_POLICY_FIRST:
1119                return VersantPersistenceManager.LOCKING_FIRST;
1120            case StorageManager.LOCK_POLICY_ALL:
1121                return VersantPersistenceManager.LOCKING_ALL;
1122        }
1123        return VersantPersistenceManager.LOCKING_NONE;
1124    }
1125
1126    public Object JavaDoc getObjectByIdFromCache(Object JavaDoc oid) {
1127        return null;
1128    }
1129
1130    public boolean isHollow(Object JavaDoc pc) {
1131        return false;
1132    }
1133
1134    public boolean hasIdentity(Object JavaDoc pc) {
1135        return false;
1136    }
1137
1138    public void logEvent(int level, String JavaDoc description, int ms) {
1139        //To change body of implemented methods use File | Settings | File Templates.
1140
}
1141
1142    public int getObjectsById(Object JavaDoc[] oids, int length, Object JavaDoc[] data, int stateFieldNo, int classMetaDataIndex) {
1143        return 0; //To change body of implemented methods use File | Settings | File Templates.
1144
}
1145
1146    public javax.jdo.Query versantNewNamedQuery(Class JavaDoc cls, String JavaDoc queryName) {
1147        return null; //To change body of implemented methods use File | Settings | File Templates.
1148
}
1149
1150    public Collection versantDetachCopy(Collection pcs, String JavaDoc fetchGroup) {
1151        return null; //To change body of implemented methods use File | Settings | File Templates.
1152
}
1153
1154    public boolean isCheckModelConsistencyOnCommit() {
1155        return false; //To change body of implemented methods use File | Settings | File Templates.
1156
}
1157
1158    public void setCheckModelConsistencyOnCommit(boolean on) {
1159        //To change body of implemented methods use File | Settings | File Templates.
1160
}
1161
1162    public void checkModelConsistency() {
1163        //To change body of implemented methods use File | Settings | File Templates.
1164
}
1165
1166    public Collection versantAttachCopy(Collection detached, boolean makeTransactional) {
1167        return null; //To change body of implemented methods use File | Settings | File Templates.
1168
}
1169
1170    public Collection versantAttachCopy(Collection detached, boolean makeTransactional, boolean shallow) {
1171        return null; //To change body of implemented methods use File | Settings | File Templates.
1172
}
1173
1174    public void setPmCacheRefType(Object JavaDoc pc, int type) {
1175        //To change body of implemented methods use File | Settings | File Templates.
1176
}
1177
1178    public void setPmCacheRefType(Object JavaDoc[] pcs, int type) {
1179        //To change body of implemented methods use File | Settings | File Templates.
1180
}
1181
1182    public void setPmCacheRefType(Collection col, int type) {
1183        //To change body of implemented methods use File | Settings | File Templates.
1184
}
1185
1186    public void setPmCacheRefType(int type) {
1187        //To change body of implemented methods use File | Settings | File Templates.
1188
}
1189
1190    public int getPmCacheRefType() {
1191        return 0; //To change body of implemented methods use File | Settings | File Templates.
1192
}
1193
1194    public void setRetainConnectionInOptTx(boolean on) {
1195        //To change body of implemented methods use File | Settings | File Templates.
1196
}
1197
1198    public void evictFromL2CacheAfterCommit(Object JavaDoc o) {
1199        //To change body of implemented methods use File | Settings | File Templates.
1200
}
1201
1202    public void evictAllFromL2CacheAfterCommit(Object JavaDoc[] a) {
1203        //To change body of implemented methods use File | Settings | File Templates.
1204
}
1205
1206    public void evictAllFromL2CacheAfterCommit(Collection c) {
1207        //To change body of implemented methods use File | Settings | File Templates.
1208
}
1209
1210    public void evictAllFromL2CacheAfterCommit(Class JavaDoc cls, boolean includeSubclasses) {
1211        //To change body of implemented methods use File | Settings | File Templates.
1212
}
1213
1214    public void evictAllFromL2CacheAfterCommit() {
1215        //To change body of implemented methods use File | Settings | File Templates.
1216
}
1217
1218    public Object JavaDoc getOptimisticLockingValue(Object JavaDoc o) {
1219        return null; //To change body of implemented methods use File | Settings | File Templates.
1220
}
1221
1222    public void addLifecycleListener(LifecycleListener listener, Class JavaDoc[] classes) {
1223        //To change body of implemented methods use File | Settings | File Templates.
1224
}
1225
1226    public void removeLifecycleListener(LifecycleListener listener) {
1227        //To change body of implemented methods use File | Settings | File Templates.
1228
}
1229
1230    public OID getInternalOID(PersistenceCapable pc) {
1231        return getStateManager(pc).getOID();
1232    }
1233
1234    public PCStateMan getInternalSM(PersistenceCapable pc) {
1235        throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
1236    }
1237
1238
1239    public StorageManager getStorageManager() {
1240        return storageMan;
1241    }
1242
1243
1244    /**
1245     * TODO: move to base class
1246     * Convert all PC, VersantOid and objectid-class params to OIDs. This
1247     * makes it possible to pass an OID instead of a PC instance for PC
1248     * parameters.
1249     */

1250    public void convertPcParamsToOID(Object JavaDoc[] params) {
1251        if (params == null) return;
1252        int n = params.length;
1253        for (int i = 0; i < n; i++) {
1254            Object JavaDoc param = params[i];
1255            if (param == null) continue;
1256            if (param instanceof Collection) {
1257                List l = new ArrayList((Collection)param);
1258                for (int j = 0; j < l.size(); j++) {
1259                    Object JavaDoc o = (Object JavaDoc)l.get(j);
1260                    o = convertPcParamsToOIDImp(o, i);
1261                    if (o instanceof OID) {
1262                        l.set(j, o);
1263                    }
1264                }
1265                params[i] = l;
1266            } else {
1267                params[i] = convertPcParamsToOIDImp(param, i);
1268            }
1269        }
1270    }
1271
1272    private Object JavaDoc convertPcParamsToOIDImp(Object JavaDoc param, int paramIndex) {
1273        if (param == null) return param;
1274        if (param instanceof PersistenceCapable) {
1275            PersistenceCapable pc = (PersistenceCapable)param;
1276            if (pc.jdoGetPersistenceManager() != proxy) {
1277                if (pc.jdoGetPersistenceManager() != null) {
1278                    throw BindingSupportImpl.getInstance().invalidOperation("PC parameter " + paramIndex + " is managed by " + pc.jdoGetPersistenceManager() +
1279                            " (this is " + proxy + "): " +
1280                            pc.getClass() + ": " + Utils.toString(pc));
1281                } else {
1282                    throw BindingSupportImpl.getInstance().invalidOperation("PC parameter " + paramIndex + " is transient: " +
1283                            pc.getClass() + ": " + Utils.toString(pc));
1284                }
1285            }
1286            param = getInternalOID((PersistenceCapable)param);
1287        } else if (param instanceof VersantOid) {
1288            // datastore identity OID parameter
1289
OID oid = modelMetaData.convertJDOGenieOIDtoOID((VersantOid)param);
1290            param = convertNewToActual(oid);
1291        } else {
1292            ClassMetaData cmd =
1293                    modelMetaData.getClassMetaDataForObjectIdClass(
1294                            param.getClass());
1295            if (cmd != null) { // app identity objectid-class parameter
1296
OID oid = cmd.createOID(false);
1297                oid.fillFromPK(param);
1298                param = oid;
1299            }
1300        }
1301        return param;
1302    }
1303}
1304
Popular Tags