001: package org.apache.ojb.jdori.sql;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import javax.jdo.PersistenceManager;
023: import javax.jdo.spi.PersistenceCapable;
024:
025: import org.apache.ojb.broker.Identity;
026: import org.apache.ojb.broker.PersistenceBroker;
027: import org.apache.ojb.broker.metadata.ClassDescriptor;
028: import org.apache.ojb.broker.query.Criteria;
029: import org.apache.ojb.broker.query.Query;
030: import org.apache.ojb.broker.query.QueryFactory;
031:
032: import com.sun.jdori.FieldManager;
033: import com.sun.jdori.PersistenceManagerInternal;
034: import com.sun.jdori.StateManagerInternal;
035: import com.sun.jdori.model.jdo.JDOClass;
036:
037: /**
038: * @see javax.jdo.Extent
039: * @author Thomas Mahler
040: */
041: public class OjbExtent implements javax.jdo.Extent {
042: private Collection extentCollection;
043: private Class clazz;
044: private PersistenceBroker broker;
045: private PersistenceManagerInternal pmi;
046:
047: /**
048: * Constructor for OjbExtent.
049: */
050: public OjbExtent(Class pClazz, PersistenceBroker pBroker,
051: PersistenceManagerInternal pPmi) {
052: clazz = pClazz;
053: broker = pBroker;
054: pmi = pPmi;
055: Criteria selectExtent = null;
056: Query q = QueryFactory.newQuery(clazz, selectExtent);
057:
058: // the PB loads plain java objects
059: Collection pojoInstances = broker.getCollectionByQuery(q);
060: // To bring these instances under JDO management,
061: // each instance must be provided with its own StateManager
062: extentCollection = provideStateManagers(pojoInstances);
063: }
064:
065: /**
066: * @see javax.jdo.Extent#iterator()
067: */
068: public Iterator iterator() {
069: return extentCollection.iterator();
070: }
071:
072: /**
073: * @see javax.jdo.Extent#hasSubclasses()
074: */
075: public boolean hasSubclasses() {
076: ClassDescriptor cld = broker.getClassDescriptor(clazz);
077: return cld.isExtent();
078: }
079:
080: /**
081: * @see javax.jdo.Extent#getCandidateClass()
082: */
083: public Class getCandidateClass() {
084: return clazz;
085: }
086:
087: /**
088: * @see javax.jdo.Extent#getPersistenceManager()
089: */
090: public PersistenceManager getPersistenceManager() {
091: return pmi.getCurrentWrapper();
092: }
093:
094: /**
095: * @see javax.jdo.Extent#closeAll()
096: */
097: public void closeAll() {
098: // noop
099: }
100:
101: /**
102: * @see javax.jdo.Extent#close(Iterator)
103: */
104: public void close(Iterator pIterator) {
105: // noop
106: }
107:
108: /**
109: * This methods enhances the objects loaded by a broker query
110: * with a JDO StateManager an brings them under JDO control.
111: * @param pojos the OJB pojos as obtained by the broker
112: * @return the collection of JDO PersistenceCapable instances
113: */
114: protected Collection provideStateManagers(Collection pojos) {
115: PersistenceCapable pc;
116: int[] fieldNums;
117: Iterator iter = pojos.iterator();
118: Collection result = new ArrayList();
119:
120: while (iter.hasNext()) {
121: // obtain a StateManager
122: pc = (PersistenceCapable) iter.next();
123: Identity oid = new Identity(pc, broker);
124: StateManagerInternal smi = pmi.getStateManager(oid, pc
125: .getClass());
126:
127: // fetch attributes into StateManager
128: JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
129: fieldNums = jdoClass.getManagedFieldNumbers();
130:
131: FieldManager fm = new OjbFieldManager(pc, broker);
132: smi.replaceFields(fieldNums, fm);
133: smi.retrieve();
134:
135: // get JDO PersistencecCapable instance from SM and add it to result collection
136: Object instance = smi.getObject();
137: result.add(instance);
138: }
139: return result;
140: }
141:
142: }
|