KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdori > sql > OjbExtent


1 package org.apache.ojb.jdori.sql;
2 /* Copyright 2002-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
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 /**
37  * @see javax.jdo.Extent
38  * @author Thomas Mahler
39  */

40 public class OjbExtent implements javax.jdo.Extent
41 {
42     private Collection JavaDoc extentCollection;
43     private Class JavaDoc clazz;
44     private PersistenceBroker broker;
45     private PersistenceManagerInternal pmi;
46
47     /**
48      * Constructor for OjbExtent.
49      */

50     public OjbExtent(Class JavaDoc 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         // the PB loads plain java objects
59
Collection JavaDoc pojoInstances = broker.getCollectionByQuery(q);
60         // To bring these instances under JDO management,
61
// each instance must be provided with its own StateManager
62
extentCollection = provideStateManagers(pojoInstances);
63     }
64
65     /**
66      * @see javax.jdo.Extent#iterator()
67      */

68     public Iterator JavaDoc iterator()
69     {
70         return extentCollection.iterator();
71     }
72
73     /**
74      * @see javax.jdo.Extent#hasSubclasses()
75      */

76     public boolean hasSubclasses()
77     {
78         ClassDescriptor cld = broker.getClassDescriptor(clazz);
79         return cld.isExtent();
80     }
81
82     /**
83      * @see javax.jdo.Extent#getCandidateClass()
84      */

85     public Class JavaDoc getCandidateClass()
86     {
87         return clazz;
88     }
89
90     /**
91      * @see javax.jdo.Extent#getPersistenceManager()
92      */

93     public PersistenceManager getPersistenceManager()
94     {
95         return pmi.getCurrentWrapper();
96     }
97
98     /**
99      * @see javax.jdo.Extent#closeAll()
100      */

101     public void closeAll()
102     {
103         // noop
104
}
105
106     /**
107      * @see javax.jdo.Extent#close(Iterator)
108      */

109     public void close(Iterator JavaDoc pIterator)
110     {
111         // noop
112
}
113     
114     /**
115      * This methods enhances the objects loaded by a broker query
116      * with a JDO StateManager an brings them under JDO control.
117      * @param pojos the OJB pojos as obtained by the broker
118      * @return the collection of JDO PersistenceCapable instances
119      */

120     protected Collection JavaDoc provideStateManagers(Collection JavaDoc pojos)
121     {
122         PersistenceCapable pc;
123         int [] fieldNums;
124         Iterator JavaDoc iter = pojos.iterator();
125         Collection JavaDoc result = new ArrayList JavaDoc();
126         
127         while (iter.hasNext())
128         {
129             // obtain a StateManager
130
pc = (PersistenceCapable) iter.next();
131             Identity oid = new Identity(pc, broker);
132             StateManagerInternal smi = pmi.getStateManager(oid, pc.getClass());
133             
134             // fetch attributes into StateManager
135
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             // get JDO PersistencecCapable instance from SM and add it to result collection
143
Object JavaDoc instance = smi.getObject();
144             result.add(instance);
145         }
146         return result;
147     }
148
149 }
150
Popular Tags