1 package org.apache.ojb.jdori.sql; 2 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import javax.jdo.PersistenceManager; 22 import javax.jdo.spi.PersistenceCapable; 23 24 import org.apache.ojb.broker.Identity; 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.metadata.ClassDescriptor; 27 import org.apache.ojb.broker.query.Criteria; 28 import org.apache.ojb.broker.query.Query; 29 import org.apache.ojb.broker.query.QueryFactory; 30 31 import com.sun.jdori.FieldManager; 32 import com.sun.jdori.PersistenceManagerInternal; 33 import com.sun.jdori.StateManagerInternal; 34 import com.sun.jdori.model.jdo.JDOClass; 35 36 40 public class OjbExtent implements javax.jdo.Extent 41 { 42 private Collection extentCollection; 43 private Class clazz; 44 private PersistenceBroker broker; 45 private PersistenceManagerInternal pmi; 46 47 50 public OjbExtent(Class pClazz, PersistenceBroker pBroker, PersistenceManagerInternal pPmi) 51 { 52 clazz = pClazz; 53 broker = pBroker; 54 pmi = pPmi; 55 Criteria selectExtent = null; 56 Query q = QueryFactory.newQuery(clazz, selectExtent); 57 58 Collection pojoInstances = broker.getCollectionByQuery(q); 60 extentCollection = provideStateManagers(pojoInstances); 63 } 64 65 68 public Iterator iterator() 69 { 70 return extentCollection.iterator(); 71 } 72 73 76 public boolean hasSubclasses() 77 { 78 ClassDescriptor cld = broker.getClassDescriptor(clazz); 79 return cld.isExtent(); 80 } 81 82 85 public Class getCandidateClass() 86 { 87 return clazz; 88 } 89 90 93 public PersistenceManager getPersistenceManager() 94 { 95 return pmi.getCurrentWrapper(); 96 } 97 98 101 public void closeAll() 102 { 103 } 105 106 109 public void close(Iterator pIterator) 110 { 111 } 113 114 120 protected Collection provideStateManagers(Collection pojos) 121 { 122 PersistenceCapable pc; 123 int [] fieldNums; 124 Iterator iter = pojos.iterator(); 125 Collection result = new ArrayList (); 126 127 while (iter.hasNext()) 128 { 129 pc = (PersistenceCapable) iter.next(); 131 Identity oid = new Identity(pc, broker); 132 StateManagerInternal smi = pmi.getStateManager(oid, pc.getClass()); 133 134 JDOClass jdoClass = Helper.getJDOClass(pc.getClass()); 136 fieldNums = jdoClass.getManagedFieldNumbers(); 137 138 FieldManager fm = new OjbFieldManager(pc, broker); 139 smi.replaceFields(fieldNums, fm); 140 smi.retrieve(); 141 142 Object instance = smi.getObject(); 144 result.add(instance); 145 } 146 return result; 147 } 148 149 } 150 | Popular Tags |