1 56 package org.objectstyle.cayenne; 57 58 import java.util.Collection ; 59 import java.util.Collections ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Map ; 63 64 import org.objectstyle.cayenne.access.DataContext; 65 import org.objectstyle.cayenne.access.ObjectStore; 66 import org.objectstyle.cayenne.conf.Configuration; 67 import org.objectstyle.cayenne.map.DbAttribute; 68 import org.objectstyle.cayenne.map.DbEntity; 69 import org.objectstyle.cayenne.map.EntityInheritanceTree; 70 import org.objectstyle.cayenne.map.ObjEntity; 71 72 86 public final class DataObjectUtils { 87 88 93 public static int intPKForObject(DataObject dataObject) { 94 Object value = pkForObject(dataObject); 95 96 if (!(value instanceof Number )) { 97 throw new CayenneRuntimeException("PK is not a number: " 98 + dataObject.getObjectId()); 99 } 100 101 return ((Number ) value).intValue(); 102 } 103 104 109 public static Object pkForObject(DataObject dataObject) { 110 Map pk = extractObjectId(dataObject); 111 112 if (pk.size() != 1) { 113 throw new CayenneRuntimeException("Expected single column PK, got " 114 + pk.size() 115 + " columns, ID: " 116 + pk); 117 } 118 119 Map.Entry pkEntry = (Map.Entry ) pk.entrySet().iterator().next(); 120 return pkEntry.getValue(); 121 } 122 123 128 public static Map compoundPKForObject(DataObject dataObject) { 129 return Collections.unmodifiableMap(extractObjectId(dataObject)); 130 } 131 132 static Map extractObjectId(DataObject dataObject) { 133 if (dataObject == null) { 134 throw new IllegalArgumentException ("Null DataObject"); 135 } 136 137 ObjectId id = dataObject.getObjectId(); 138 if (!id.isTemporary()) { 139 return id.getIdSnapshot(); 140 } 141 142 if (id.isReplacementIdAttached()) { 144 DbEntity entity = dataObject 145 .getDataContext() 146 .getEntityResolver() 147 .lookupDbEntity(dataObject); 148 149 if(entity != null && entity.isFullReplacementIdAttached(id)) { 150 return id.getReplacementIdMap(); 151 } 152 } 153 154 throw new CayenneRuntimeException("Can't get primary key from temporary id."); 155 } 156 157 167 public static DataObject objectForPK( 168 DataContext context, 169 Class dataObjectClass, 170 int pk) { 171 return objectForPK(context, buildId(context, dataObjectClass, new Integer (pk))); 172 } 173 174 184 public static DataObject objectForPK( 185 DataContext context, 186 Class dataObjectClass, 187 Object pk) { 188 189 return objectForPK(context, buildId(context, dataObjectClass, pk)); 190 } 191 192 202 public static DataObject objectForPK( 203 DataContext context, 204 Class dataObjectClass, 205 Map pk) { 206 return objectForPK(context, new ObjectId(dataObjectClass, pk)); 207 } 208 209 219 public static DataObject objectForPK(DataContext context, String objEntityName, int pk) { 220 return objectForPK(context, buildId(context, objEntityName, new Integer (pk))); 221 } 222 223 233 public static DataObject objectForPK( 234 DataContext context, 235 String objEntityName, 236 Object pk) { 237 return objectForPK(context, buildId(context, objEntityName, pk)); 238 } 239 240 250 public static DataObject objectForPK(DataContext context, String objEntityName, Map pk) { 251 if (objEntityName == null) { 252 throw new IllegalArgumentException ("Null ObjEntity name."); 253 } 254 255 ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName); 256 if (entity == null) { 257 throw new CayenneRuntimeException("Non-existent ObjEntity: " + objEntityName); 258 } 259 260 Class dataObjectClass = entity.getJavaClass(Configuration.getResourceLoader()); 261 262 return objectForPK(context, new ObjectId(dataObjectClass, pk)); 263 } 264 265 273 public static DataObject objectForPK(DataContext context, ObjectId id) { 274 ObjectStore objectStore = context.getObjectStore(); 275 276 DataObject object = objectStore.getObject(id); 278 if (object != null) { 279 return object; 280 } 281 282 286 ObjEntity entity = context.getEntityResolver().lookupObjEntity( 287 id.getObjectClass()); 288 EntityInheritanceTree inheritanceHandler = context 289 .getEntityResolver() 290 .lookupInheritanceTree(entity); 291 if (inheritanceHandler != null) { 292 Collection children = inheritanceHandler.getChildren(); 293 if (!children.isEmpty()) { 294 Iterator it = children.iterator(); 296 while (it.hasNext()) { 297 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 298 ObjectId childID = new ObjectId(child.getEntity().getJavaClass( 299 Configuration.getResourceLoader()), id.getIdSnapshot()); 300 301 DataObject childObject = objectStore.getObject(childID); 302 if (childObject != null) { 303 return childObject; 304 } 305 } 306 } 307 } 308 309 311 DataRow row = objectStore.getSnapshot(id, context); 313 314 return (row != null) 315 ? context.objectFromDataRow(id.getObjectClass(), row, false) 316 : null; 317 } 318 319 static ObjectId buildId(DataContext context, String objEntityName, Object pk) { 320 if (pk == null) { 321 throw new IllegalArgumentException ("Null PK"); 322 } 323 324 if (objEntityName == null) { 325 throw new IllegalArgumentException ("Null ObjEntity name."); 326 } 327 328 ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName); 329 if (entity == null) { 330 throw new CayenneRuntimeException("Non-existent ObjEntity: " + objEntityName); 331 } 332 333 DbEntity dbEntity = entity.getDbEntity(); 334 if (dbEntity == null) { 335 throw new CayenneRuntimeException("No DbEntity for ObjEntity: " 336 + entity.getName()); 337 } 338 339 List pkAttributes = dbEntity.getPrimaryKey(); 340 if (pkAttributes.size() != 1) { 341 throw new CayenneRuntimeException("PK contains " 342 + pkAttributes.size() 343 + " columns, expected 1."); 344 } 345 346 DbAttribute attr = (DbAttribute) pkAttributes.get(0); 347 return new ObjectId(entity.getJavaClass(Configuration.getResourceLoader()), attr 348 .getName(), pk); 349 } 350 351 static ObjectId buildId(DataContext context, Class dataObjectClass, Object pk) { 352 if (pk == null) { 353 throw new IllegalArgumentException ("Null PK"); 354 } 355 356 if (dataObjectClass == null) { 357 throw new IllegalArgumentException ("Null DataObject class."); 358 } 359 360 ObjEntity entity = context.getEntityResolver().lookupObjEntity(dataObjectClass); 361 if (entity == null) { 362 throw new CayenneRuntimeException("Unmapped DataObject Class: " 363 + dataObjectClass.getName()); 364 } 365 366 DbEntity dbEntity = entity.getDbEntity(); 367 if (dbEntity == null) { 368 throw new CayenneRuntimeException("No DbEntity for ObjEntity: " 369 + entity.getName()); 370 } 371 372 List pkAttributes = dbEntity.getPrimaryKey(); 373 if (pkAttributes.size() != 1) { 374 throw new CayenneRuntimeException("PK contains " 375 + pkAttributes.size() 376 + " columns, expected 1."); 377 } 378 379 DbAttribute attr = (DbAttribute) pkAttributes.get(0); 380 return new ObjectId(dataObjectClass, attr.getName(), pk); 381 } 382 383 private DataObjectUtils() { 385 } 386 } | Popular Tags |