1 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 ; 17 import java.util.Iterator ; 18 import javax.jdo.Extent; 19 import javax.jdo.JDOFatalInternalException; 20 import javax.jdo.JDOFatalUserException; 21 22 23 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 queryResultsByIterator = new HashMap (); 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 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 94 95 public Iterator iterator() 96 { 97 QueryResult qr = (QueryResult)query.execute(); 98 Iterator i = qr.iterator(); 99 100 queryResultsByIterator.put(i, qr); 101 102 return i; 103 } 104 105 106 112 113 public boolean hasSubclasses() 114 { 115 return subclasses; 116 } 117 118 119 125 126 public Class getCandidateClass() 127 { 128 return view.getType(); 129 } 130 131 132 138 139 public javax.jdo.PersistenceManager getPersistenceManager() 140 { 141 return pm; 142 } 143 144 145 154 155 public void close(Iterator it) 156 { 157 QueryResult qr = (QueryResult)queryResultsByIterator.remove(it); 158 159 qr.close(); 160 } 161 162 163 169 170 public void closeAll() 171 { 172 Iterator 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 candidateClass) 185 { 186 Class 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 toString() 212 { 213 return "Extent of " + getCandidateClass(); 214 } 215 } 216 | Popular Tags |