KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ClassViewExtent


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ClassViewExtent.java,v 1.9 2004/02/01 18:22:42 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import com.triactive.jdo.model.ClassMetaData;
15 import com.triactive.jdo.util.IntArrayList;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import javax.jdo.Extent;
19 import javax.jdo.JDOFatalInternalException;
20 import javax.jdo.JDOFatalUserException;
21
22
23 /**
24  * An Extent of all persistent objects backed by a view.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.9 $
28  *
29  * @see Extent
30  */

31
32 class ClassViewExtent implements Extent, Queryable
33 {
34     private final PersistenceManager pm;
35     private final ClassView view;
36     private final boolean subclasses;
37     private final StoreManager storeMgr;
38     private final DatabaseAdapter dba;
39     private final Query query;
40     private final int fieldCount;
41     private final int[] prefetchFieldNumbers;
42     private final ColumnMapping[] prefetchFieldMappings;
43
44     private HashMap JavaDoc queryResultsByIterator = new HashMap JavaDoc();
45
46
47     public ClassViewExtent(PersistenceManager pm, ClassView view, boolean subclasses)
48     {
49         this.pm = pm;
50         this.view = view;
51         this.subclasses = subclasses;
52
53         storeMgr = view.getStoreManager();
54         dba = storeMgr.getDatabaseAdapter();
55         query = storeMgr.getQuery(pm, null);
56
57         Class JavaDoc candidateClass = getCandidateClass();
58
59         query.setClass(candidateClass);
60         query.setCandidates(this);
61
62         ClassMetaData cmd = ClassMetaData.forClass(candidateClass);
63         fieldCount = cmd.getFieldCount();
64         IntArrayList colfn = new IntArrayList(fieldCount);
65         ColumnMapping[] colfm = new ColumnMapping[fieldCount];
66
67         for (int i = 0; i < fieldCount; ++i)
68         {
69             if (view.isFieldPersistent(i))
70             {
71                 Mapping m = view.getFieldMapping(i);
72
73                 if (!(m instanceof ColumnMapping))
74                     throw new JDOFatalInternalException("Mapping " + m + " not suitable for a view column?");
75
76                 colfn.add(i);
77                 colfm[i] = (ColumnMapping)m;
78             }
79         }
80
81         if (colfn.isEmpty())
82             throw new JDOFatalUserException("View class has no persistent fields: " + candidateClass.getName());
83
84         prefetchFieldNumbers = colfn.toArray();
85         prefetchFieldMappings = colfm;
86     }
87
88
89     /**
90      * Returns an iterator over all the instances in the Extent.
91      *
92      * @return an iterator over all the instances in the Extent.
93      */

94
95     public Iterator JavaDoc iterator()
96     {
97         QueryResult qr = (QueryResult)query.execute();
98         Iterator JavaDoc i = qr.iterator();
99
100         queryResultsByIterator.put(i, qr);
101
102         return i;
103     }
104
105
106     /**
107      * Returns whether this Extent was defined to contain subclasses.
108      *
109      * @return true if this Extent was defined to contain instances
110      * that are of a subclass type
111      */

112
113     public boolean hasSubclasses()
114     {
115         return subclasses;
116     }
117
118
119     /**
120      * An Extent contains all instances of a particular Class in the data
121      * store; this method returns the Class of the instances
122      *
123      * @return the Class of instances of this Extent
124      */

125
126     public Class JavaDoc getCandidateClass()
127     {
128         return view.getType();
129     }
130
131
132     /**
133      * An Extent is managed by a PersistenceManager; this method gives access
134      * to the owning PersistenceManager.
135      *
136      * @return the owning PersistenceManager
137      */

138
139     public javax.jdo.PersistenceManager getPersistenceManager()
140     {
141         return pm;
142     }
143
144
145     /**
146      * Close an Iterator associated with this Extent instance. Iterators closed
147      * by this method will return false to hasNext() and will throw
148      * NoSuchElementException on next(). The Extent instance can still be used
149      * as a parameter of Query.setCandidates, and to get an Iterator.
150      *
151      * @param it an iterator obtained by the method iterator() on this Extent
152      * instance.
153      */

154
155     public void close(Iterator JavaDoc it)
156     {
157         QueryResult qr = (QueryResult)queryResultsByIterator.remove(it);
158
159         qr.close();
160     }
161
162
163     /**
164      * Close all Iterators associated with this Extent instance. Iterators
165      * closed by this method will return false to hasNext() and will throw
166      * NoSuchElementException on next(). The Extent instance can still be used
167      * as a parameter of Query.setCandidates, and to get an Iterator.
168      */

169
170     public void closeAll()
171     {
172         Iterator JavaDoc i = queryResultsByIterator.values().iterator();
173
174         while (i.hasNext())
175         {
176             QueryResult qr = (QueryResult)i.next();
177
178             qr.close();
179             i.remove();
180         }
181     }
182
183
184     public QueryStatement newQueryStatement(Class JavaDoc candidateClass)
185     {
186         Class JavaDoc extentType = view.getType();
187
188         if (!extentType.equals(candidateClass))
189             throw new IncompatibleQueryElementTypeException(extentType, candidateClass);
190
191         return dba.newQueryStatement(view);
192     }
193
194
195     public Query.ResultObjectFactory newResultObjectFactory(QueryStatement stmt)
196     {
197         int[] columnNumbersByField = new int[prefetchFieldMappings.length];
198
199         for (int i = 0; i < prefetchFieldMappings.length; ++i)
200         {
201             ColumnMapping m = prefetchFieldMappings[i];
202
203             if (m != null)
204                 columnNumbersByField[i] = stmt.select(m.getColumn());
205         }
206
207         return new TransientIDROF(pm, getCandidateClass(), prefetchFieldNumbers, prefetchFieldMappings, columnNumbersByField);
208     }
209
210
211     public String JavaDoc toString()
212     {
213         return "Extent of " + getCandidateClass();
214     }
215 }
216
Popular Tags