1 56 57 package org.objectstyle.cayenne.service; 58 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Collections ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.DataObject; 68 import org.objectstyle.cayenne.ObjectId; 69 import org.objectstyle.cayenne.PersistenceState; 70 import org.objectstyle.cayenne.Persistent; 71 import org.objectstyle.cayenne.QueryResponse; 72 import org.objectstyle.cayenne.access.DataContext; 73 import org.objectstyle.cayenne.access.DataDomain; 74 import org.objectstyle.cayenne.access.DataRowStore; 75 import org.objectstyle.cayenne.access.HierarchicalObjectContext; 76 import org.objectstyle.cayenne.access.ObjectStore; 77 import org.objectstyle.cayenne.access.OperationObserver; 78 import org.objectstyle.cayenne.access.PersistenceContext; 79 import org.objectstyle.cayenne.access.Transaction; 80 import org.objectstyle.cayenne.graph.GraphDiff; 81 import org.objectstyle.cayenne.map.EntityResolver; 82 import org.objectstyle.cayenne.map.ObjEntity; 83 import org.objectstyle.cayenne.query.GenericSelectQuery; 84 import org.objectstyle.cayenne.query.NamedQuery; 85 import org.objectstyle.cayenne.query.Query; 86 import org.objectstyle.cayenne.query.QueryChain; 87 import org.objectstyle.cayenne.query.QueryExecutionPlan; 88 89 96 class ObjectDataContext extends DataContext implements HierarchicalObjectContext { 98 99 PersistenceContext parentContext; 100 EntityResolver entityResolver; 101 102 105 ObjectDataContext(DataDomain parentDomain) { 106 this.parentContext = parentDomain; 107 super.parent = parentDomain; 108 this.entityResolver = parentDomain.getEntityResolver(); 109 110 DataRowStore cache = parentDomain.isSharedCacheEnabled() ? parentDomain 111 .getSharedSnapshotCache() : new DataRowStore( 112 parentDomain.getName(), 113 parentDomain.getProperties()); 114 115 super.objectStore = new ObjectStore(cache); 116 } 117 118 ObjectDataContext(PersistenceContext parentContext, EntityResolver entityResolver, 119 DataRowStore cache) { 120 this.parentContext = parentContext; 121 this.entityResolver = entityResolver; 122 123 if (parentContext instanceof DataDomain) { 126 super.parent = (DataDomain) parentContext; 127 } 128 129 super.objectStore = new ObjectStore(cache); 130 } 131 132 135 public EntityResolver getEntityResolver() { 136 return entityResolver; 138 } 139 140 public int[] performNonSelectingQuery(Query query) { 141 return performUpdateQuery((QueryExecutionPlan) query); 143 } 144 145 public List performQuery(GenericSelectQuery query) { 146 return performSelectQuery((QueryExecutionPlan) query); 148 } 149 150 153 public void performQueries(Collection queries, OperationObserver observer) { 154 QueryChain query = new QueryChain(queries); 155 getParentContext().performQuery(query.resolve(getEntityResolver()), observer); 156 } 157 158 161 public void performQueries( 162 Collection queries, 163 OperationObserver observer, 164 Transaction transaction) { 165 166 QueryChain query = new QueryChain(queries); 167 getParentContext().performQuery( 168 query.resolve(getEntityResolver()), 169 observer, 170 transaction); 171 } 172 173 public int[] performNonSelectingQuery(String queryName, Map parameters) { 174 return performUpdateQuery(new NamedQuery(queryName, parameters)); 175 } 176 177 public int[] performNonSelectingQuery(String queryName) { 178 return performUpdateQuery(new NamedQuery(queryName)); 179 } 180 181 public List performQuery(String queryName, boolean refresh) { 182 return performSelectQuery(new NamedQuery(queryName)); 184 } 185 186 public void commitChanges() throws CayenneRuntimeException { 187 commit(); 188 } 189 190 193 DataObject createAndRegisterNewObject(ObjectId id) { 194 if (id.getObjectClass() == null) { 195 throw new NullPointerException ("DataObject class can't be null."); 196 } 197 198 ObjEntity entity = getEntityResolver().lookupObjEntity(id.getObjectClass()); 199 if (entity == null) { 200 throw new IllegalArgumentException ("Class is not mapped with Cayenne: " 201 + id.getObjectClass().getName()); 202 } 203 204 DataObject dataObject = null; 205 try { 206 dataObject = (DataObject) id.getObjectClass().newInstance(); 207 } 208 catch (Exception ex) { 209 throw new CayenneRuntimeException("Error instantiating object.", ex); 210 } 211 212 dataObject.setObjectId(id); 213 registerNewObject(dataObject); 214 return dataObject; 215 } 216 217 public GraphDiff commit() throws CayenneRuntimeException { 218 return new ObjectDataContextCommitAction().commit(this); 219 } 220 221 public PersistenceContext getParentContext() { 222 return parentContext; 223 } 224 225 public void deleteObject(Persistent object) { 226 227 if (object != null && !(object instanceof DataObject)) { 229 throw new IllegalArgumentException ( 230 this 231 + ": this implementation of ObjectContext only supports full DataObjects. Object " 232 + object 233 + " is not supported."); 234 } 235 236 super.deleteObject((DataObject) object); 237 } 238 239 242 public Persistent newObject(Class persistentClass) { 243 if (persistentClass == null) { 244 throw new NullPointerException ("Null 'persistentClass'"); 245 } 246 247 if (!DataObject.class.isAssignableFrom(persistentClass)) { 249 throw new IllegalArgumentException ( 250 this 251 + ": this implementation of ObjectContext only supports full DataObjects. Class " 252 + persistentClass 253 + " is invalid."); 254 } 255 256 return super.createAndRegisterNewObject(persistentClass); 257 } 258 259 262 public Collection uncommittedObjects() { 263 264 int len = getObjectStore().registeredObjectsCount(); 265 if (len == 0) { 266 return Collections.EMPTY_LIST; 267 } 268 269 Collection objects = new ArrayList (len > 100 ? len / 2 : len); 271 272 Iterator it = getObjectStore().getObjectIterator(); 273 while (it.hasNext()) { 274 Persistent object = (Persistent) it.next(); 275 int state = object.getPersistenceState(); 276 if (state == PersistenceState.MODIFIED 277 || state == PersistenceState.NEW 278 || state == PersistenceState.DELETED) { 279 280 objects.add(object); 281 } 282 } 283 284 return objects; 285 } 286 287 public QueryResponse performGenericQuery(QueryExecutionPlan query) { 288 if (this.getParentContext() == null) { 289 throw new CayenneRuntimeException( 290 "Can't run query - parent PersistenceContext is not set."); 291 } 292 293 return new PersistenceContextQueryAction(getEntityResolver()).performMixed( 294 getParentContext(), 295 query); 296 } 297 298 public int[] performUpdateQuery(QueryExecutionPlan query) { 299 if (this.getParentContext() == null) { 300 throw new CayenneRuntimeException( 301 "Can't run query - parent PersistenceContext is not set."); 302 } 303 304 return new PersistenceContextQueryAction(getEntityResolver()) 305 .performNonSelectingQuery(getParentContext(), query); 306 } 307 308 public List performSelectQuery(QueryExecutionPlan query) { 309 if (this.getParentContext() == null) { 310 throw new CayenneRuntimeException( 311 "Can't run query - parent PersistenceContext is not set."); 312 } 313 314 return new ObjectDataContextSelectAction(this).performQuery(query); 315 } 316 317 319 public void objectWillRead(Persistent object, String property) { 320 throw new CayenneRuntimeException( 322 "Persistent interface methods are not yet handled."); 323 } 324 325 public void objectWillWrite( 326 Persistent object, 327 String property, 328 Object oldValue, 329 Object newValue) { 330 throw new CayenneRuntimeException( 332 "Persistent interface methods are not yet handled."); 333 } 334 } 335 | Popular Tags |