use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class IdCoderTest method testGetObjectId_Temp.
@Test
public void testGetObjectId_Temp() {
IdCoder handler = new IdCoder(runtime.getChannel().getEntityResolver());
byte[] key = new byte[] { 1, (byte) 0xD7, 10, 100 };
ObjectId decoded = handler.getObjectId(".E1:01D70A64");
assertEquals(new ObjectId("E1", key), decoded);
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class IdCoderTest method testGetSringId_TempWithReplacement.
@Test
public void testGetSringId_TempWithReplacement() {
IdCoder handler = new IdCoder(runtime.getChannel().getEntityResolver());
byte[] key = new byte[] { 5, 2, 11, 99 };
ObjectId id = new ObjectId("E1", key);
id.getReplacementIdMap().put("ID", 6);
E1 e1 = new E1();
e1.setObjectId(id);
assertEquals("E1:6", handler.getStringId(e1));
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class ClientReturnDiffFilter method isClientArc.
private boolean isClientArc(Object id, Object targetId, Object arcId) {
ObjectId oid = (ObjectId) id;
ObjEntity entity = resolver.getObjEntity(oid.getEntityName());
if (!entity.isClientAllowed()) {
return false;
}
if (entity.getRelationship(arcId.toString()).isRuntime()) {
return false;
}
ObjectId targetOid = (ObjectId) targetId;
ObjEntity targetEntity = resolver.getObjEntity(targetOid.getEntityName());
if (!targetEntity.isClientAllowed()) {
return false;
}
return true;
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class DataContext method registerNewObject.
/**
* Registers a transient object with the context, recursively registering
* all transient persistent objects attached to this object via
* relationships.
* <p>
* <i>Note that since 3.0 this method takes Object as an argument instead of
* a {@link DataObject}.</i>
*
* @param object
* new object that needs to be made persistent.
*/
@Override
public void registerNewObject(Object object) {
if (object == null) {
throw new NullPointerException("Can't register null object.");
}
ObjEntity entity = getEntityResolver().getObjEntity((Persistent) object);
if (entity == null) {
throw new IllegalArgumentException("Can't find ObjEntity for Persistent class: " + object.getClass().getName() + ", class is likely not mapped.");
}
final Persistent persistent = (Persistent) object;
// sanity check - maybe already registered
if (persistent.getObjectId() != null) {
if (persistent.getObjectContext() == this) {
// already registered, just ignore
return;
} else if (persistent.getObjectContext() != null) {
throw new IllegalStateException("Persistent is already registered with another DataContext. " + "Try using 'localObjects()' instead.");
}
} else {
persistent.setObjectId(new ObjectId(entity.getName()));
}
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
if (descriptor == null) {
throw new IllegalArgumentException("Invalid entity name: " + entity.getName());
}
injectInitialValue(object);
// now we need to find all arc changes, inject missing value holders and
// pull in
// all transient connected objects
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitToMany(ToManyProperty property) {
property.injectValueHolder(persistent);
if (!property.isFault(persistent)) {
Object value = property.readProperty(persistent);
Collection<Map.Entry> collection = (value instanceof Map) ? ((Map) value).entrySet() : (Collection) value;
Iterator<Map.Entry> it = collection.iterator();
while (it.hasNext()) {
Object target = it.next();
if (target instanceof Persistent) {
Persistent targetDO = (Persistent) target;
// make sure it is registered
registerNewObject(targetDO);
getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
}
}
}
return true;
}
public boolean visitToOne(ToOneProperty property) {
Object target = property.readPropertyDirectly(persistent);
if (target instanceof Persistent) {
Persistent targetDO = (Persistent) target;
// make sure it is registered
registerNewObject(targetDO);
getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
}
return true;
}
public boolean visitAttribute(AttributeProperty property) {
return true;
}
});
}
use of org.apache.cayenne.ObjectId in project cayenne by apache.
the class DataDomainFlushAction method preprocess.
private void preprocess(DataContext context, GraphDiff changes) {
// categorize dirty objects by state
ObjectStore objectStore = context.getObjectStore();
for (Object o : changesByObjectId.keySet()) {
ObjectId id = (ObjectId) o;
Persistent object = (Persistent) objectStore.getNode(id);
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
switch(object.getPersistenceState()) {
case PersistenceState.NEW:
insertBucket.addDirtyObject(object, descriptor);
break;
case PersistenceState.MODIFIED:
updateBucket.addDirtyObject(object, descriptor);
break;
case PersistenceState.DELETED:
deleteBucket.addDirtyObject(object, descriptor);
break;
}
}
new DataDomainIndirectDiffBuilder(this).processIndirectChanges(changes);
insertBucket.appendQueries(queries);
// TODO: the following line depends on the "queries" collection filled by insertBucket.. Moreover it may
// potentially remove values from the passed collection. Replace with something with fewer unobvious
// side-effects...
flattenedBucket.appendInserts(queries);
updateBucket.appendQueries(queries);
flattenedBucket.appendDeletes(queries);
deleteBucket.appendQueries(queries);
}
Aggregations