KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > PlainPrefetcher


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

17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.ojb.broker.Identity;
26 import org.apache.ojb.broker.PersistenceBroker;
27 import org.apache.ojb.broker.cache.ObjectCache;
28 import org.apache.ojb.broker.core.PersistenceBrokerImpl;
29 import org.apache.ojb.broker.core.proxy.IndirectionHandler;
30 import org.apache.ojb.broker.core.proxy.ProxyHelper;
31 import org.apache.ojb.broker.query.Query;
32
33 /**
34  * Prefetcher for plain list of objects (no relations).
35  *
36  * @author <a HREF="mailto:olegnitz@apache.org">Oleg Nitz</a>
37  * @version $Id: PlainPrefetcher.java,v 1.9.2.2 2005/12/21 22:22:58 tomdz Exp $
38  */

39 public class PlainPrefetcher extends BasePrefetcher
40 {
41
42     public PlainPrefetcher(PersistenceBrokerImpl aBroker, Class JavaDoc anItemClass)
43     {
44         super(aBroker, anItemClass);
45     }
46
47     public void prepareRelationshipSettings()
48     {
49         // no op
50
}
51
52     public void restoreRelationshipSettings()
53     {
54         // no op
55
}
56
57     protected void associateBatched(Collection JavaDoc proxies, Collection JavaDoc realSubjects)
58     {
59         PersistenceBroker pb = getBroker();
60         IndirectionHandler handler;
61         Identity id;
62         Object JavaDoc proxy;
63         Object JavaDoc realSubject;
64         HashMap JavaDoc realSubjectsMap = new HashMap JavaDoc(realSubjects.size());
65
66         for (Iterator JavaDoc it = realSubjects.iterator(); it.hasNext(); )
67         {
68             realSubject = it.next();
69             realSubjectsMap.put(pb.serviceIdentity().buildIdentity(realSubject), realSubject);
70         }
71
72         for (Iterator JavaDoc it = proxies.iterator(); it.hasNext(); )
73         {
74             proxy = it.next();
75             handler = ProxyHelper.getIndirectionHandler(proxy);
76
77             if (handler == null)
78             {
79                 continue;
80             }
81
82             id = handler.getIdentity();
83             realSubject = realSubjectsMap.get(id);
84             if (realSubject != null)
85             {
86                 handler.setRealSubject(realSubject);
87             }
88         }
89     }
90
91     /**
92      * Build the multiple queries for one relationship because of limitation of IN(...)
93      * @param proxies Collection containing all proxy objects to load
94      * @param realSubjects Collection where real subjects found in the cache should be added.
95      */

96     protected Query[] buildPrefetchQueries(Collection JavaDoc proxies, Collection JavaDoc realSubjects)
97     {
98         Collection JavaDoc queries = new ArrayList JavaDoc();
99         Collection JavaDoc idsSubset;
100         Object JavaDoc proxy;
101         IndirectionHandler handler;
102         Identity id;
103         Class JavaDoc realClass;
104         HashMap JavaDoc classToIds = new HashMap JavaDoc();
105         Class JavaDoc topLevelClass = getItemClassDescriptor().getClassOfObject();
106         PersistenceBroker pb = getBroker();
107         ObjectCache cache = pb.serviceObjectCache();
108
109         for (Iterator JavaDoc it = proxies.iterator(); it.hasNext(); )
110         {
111             proxy = it.next();
112             handler = ProxyHelper.getIndirectionHandler(proxy);
113
114             if (handler == null)
115             {
116                 continue;
117             }
118             
119             id = handler.getIdentity();
120             if (cache.lookup(id) != null)
121             {
122                 realSubjects.add(pb.getObjectByIdentity(id));
123                 continue;
124             }
125             realClass = id.getObjectsRealClass();
126             if (realClass == null)
127             {
128                 realClass = Object JavaDoc.class; // to remember that the real class is unknown
129
}
130             idsSubset = (HashSet JavaDoc) classToIds.get(realClass);
131             if (idsSubset == null)
132             {
133                 idsSubset = new HashSet JavaDoc();
134                 classToIds.put(realClass, idsSubset);
135             }
136             idsSubset.add(id);
137             if (idsSubset.size() == pkLimit)
138             {
139                 Query query;
140                 if (realClass == Object JavaDoc.class)
141                 {
142                     query = buildPrefetchQuery(topLevelClass, idsSubset, true);
143                 }
144                 else
145                 {
146                     query = buildPrefetchQuery(realClass, idsSubset, false);
147                 }
148                 queries.add(query);
149                 idsSubset.clear();
150             }
151         }
152
153         for (Iterator JavaDoc it = classToIds.entrySet().iterator(); it.hasNext(); )
154         {
155             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
156             realClass = (Class JavaDoc) entry.getKey();
157             idsSubset = (HashSet JavaDoc) entry.getValue();
158             if (idsSubset.size() > 0)
159             {
160                 Query query;
161                 if (realClass == Object JavaDoc.class)
162                 {
163                     query = buildPrefetchQuery(topLevelClass, idsSubset, true);
164                 }
165                 else
166                 {
167                     query = buildPrefetchQuery(realClass, idsSubset, false);
168                 }
169                 queries.add(query);
170             }
171         }
172
173         return (Query[]) queries.toArray(new Query[queries.size()]);
174     }
175
176     protected Query buildPrefetchQuery(Class JavaDoc clazz, Collection JavaDoc ids, boolean withExtents)
177     {
178         Query query = buildPrefetchQuery(clazz, ids,
179                 getDescriptorRepository().getDescriptorFor(clazz).getPkFields());
180         query.setWithExtents(withExtents);
181         return query;
182     }
183 }
184
Popular Tags