1 19 20 package org.apache.cayenne; 21 22 import java.util.Collections ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.cayenne.map.DbAttribute; 27 import org.apache.cayenne.map.DbEntity; 28 import org.apache.cayenne.map.ObjEntity; 29 import org.apache.cayenne.query.ObjectIdQuery; 30 import org.apache.cayenne.query.Query; 31 32 46 public final class DataObjectUtils { 47 48 53 public static int intPKForObject(Persistent dataObject) { 54 Object value = pkForObject(dataObject); 55 56 if (!(value instanceof Number )) { 57 throw new CayenneRuntimeException("PK is not a number: " 58 + dataObject.getObjectId()); 59 } 60 61 return ((Number ) value).intValue(); 62 } 63 64 69 public static Object pkForObject(Persistent dataObject) { 70 Map pk = extractObjectId(dataObject); 71 72 if (pk.size() != 1) { 73 throw new CayenneRuntimeException("Expected single column PK, got " 74 + pk.size() 75 + " columns, ID: " 76 + pk); 77 } 78 79 Map.Entry pkEntry = (Map.Entry ) pk.entrySet().iterator().next(); 80 return pkEntry.getValue(); 81 } 82 83 88 public static Map compoundPKForObject(Persistent dataObject) { 89 return Collections.unmodifiableMap(extractObjectId(dataObject)); 90 } 91 92 static Map extractObjectId(Persistent dataObject) { 93 if (dataObject == null) { 94 throw new IllegalArgumentException ("Null DataObject"); 95 } 96 97 ObjectId id = dataObject.getObjectId(); 98 if (!id.isTemporary()) { 99 return id.getIdSnapshot(); 100 } 101 102 if (id.isReplacementIdAttached()) { 104 ObjEntity objEntity = dataObject 105 .getObjectContext() 106 .getEntityResolver() 107 .lookupObjEntity(dataObject); 108 109 if (objEntity != null) { 110 DbEntity entity = objEntity.getDbEntity(); 111 if (entity != null && entity.isFullReplacementIdAttached(id)) { 112 return id.getReplacementIdMap(); 113 } 114 } 115 } 116 117 throw new CayenneRuntimeException("Can't get primary key from temporary id."); 118 } 119 120 130 public static Object objectForPK(ObjectContext context, Class dataObjectClass, int pk) { 131 return objectForPK(context, buildId(context, dataObjectClass, new Integer (pk))); 132 } 133 134 144 public static Object objectForPK( 145 ObjectContext context, 146 Class dataObjectClass, 147 Object pk) { 148 149 return objectForPK(context, buildId(context, dataObjectClass, pk)); 150 } 151 152 162 public static Object objectForPK(ObjectContext context, Class dataObjectClass, Map pk) { 163 164 ObjEntity entity = context.getEntityResolver().lookupObjEntity(dataObjectClass); 165 if (entity == null) { 166 throw new CayenneRuntimeException("Non-existent ObjEntity for class: " 167 + dataObjectClass); 168 } 169 170 return objectForPK(context, new ObjectId(entity.getName(), pk)); 171 } 172 173 183 public static Object objectForPK(ObjectContext context, String objEntityName, int pk) { 184 return objectForPK(context, buildId(context, objEntityName, new Integer (pk))); 185 } 186 187 197 public static Object objectForPK( 198 ObjectContext context, 199 String objEntityName, 200 Object pk) { 201 return objectForPK(context, buildId(context, objEntityName, pk)); 202 } 203 204 214 public static Object objectForPK(ObjectContext context, String objEntityName, Map pk) { 215 if (objEntityName == null) { 216 throw new IllegalArgumentException ("Null ObjEntity name."); 217 } 218 219 return objectForPK(context, new ObjectId(objEntityName, pk)); 220 } 221 222 231 public static Object objectForPK(ObjectContext context, ObjectId id) { 232 return DataObjectUtils.objectForQuery(context, new ObjectIdQuery( 233 id, 234 false, 235 ObjectIdQuery.CACHE)); 236 } 237 238 245 public static Object objectForQuery(ObjectContext context, Query query) { 246 List objects = context.performQuery(query); 247 248 if (objects.size() == 0) { 249 return null; 250 } 251 else if (objects.size() > 1) { 252 throw new CayenneRuntimeException( 253 "Expected zero or one object, instead query matched: " 254 + objects.size()); 255 } 256 257 return objects.get(0); 258 } 259 260 static ObjectId buildId(ObjectContext context, String objEntityName, Object pk) { 261 if (pk == null) { 262 throw new IllegalArgumentException ("Null PK"); 263 } 264 265 if (objEntityName == null) { 266 throw new IllegalArgumentException ("Null ObjEntity name."); 267 } 268 269 ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName); 270 if (entity == null) { 271 throw new CayenneRuntimeException("Non-existent ObjEntity: " + objEntityName); 272 } 273 274 DbEntity dbEntity = entity.getDbEntity(); 275 if (dbEntity == null) { 276 throw new CayenneRuntimeException("No DbEntity for ObjEntity: " 277 + entity.getName()); 278 } 279 280 List pkAttributes = dbEntity.getPrimaryKey(); 281 if (pkAttributes.size() != 1) { 282 throw new CayenneRuntimeException("PK contains " 283 + pkAttributes.size() 284 + " columns, expected 1."); 285 } 286 287 DbAttribute attr = (DbAttribute) pkAttributes.get(0); 288 return new ObjectId(objEntityName, attr.getName(), pk); 289 } 290 291 static ObjectId buildId(ObjectContext context, Class dataObjectClass, Object pk) { 292 if (pk == null) { 293 throw new IllegalArgumentException ("Null PK"); 294 } 295 296 if (dataObjectClass == null) { 297 throw new IllegalArgumentException ("Null DataObject class."); 298 } 299 300 ObjEntity entity = context.getEntityResolver().lookupObjEntity(dataObjectClass); 301 if (entity == null) { 302 throw new CayenneRuntimeException("Unmapped DataObject Class: " 303 + dataObjectClass.getName()); 304 } 305 306 DbEntity dbEntity = entity.getDbEntity(); 307 if (dbEntity == null) { 308 throw new CayenneRuntimeException("No DbEntity for ObjEntity: " 309 + entity.getName()); 310 } 311 312 List pkAttributes = dbEntity.getPrimaryKey(); 313 if (pkAttributes.size() != 1) { 314 throw new CayenneRuntimeException("PK contains " 315 + pkAttributes.size() 316 + " columns, expected 1."); 317 } 318 319 DbAttribute attr = (DbAttribute) pkAttributes.get(0); 320 return new ObjectId(entity.getName(), attr.getName(), pk); 321 } 322 323 private DataObjectUtils() { 325 } 326 } 327 | Popular Tags |