use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class ClientServerChannelQueryAction method convertSingleObject.
private Object convertSingleObject(PrefetchTreeNode prefetchTree, ObjectDetachOperation op, Object serverObject) {
Persistent object = (Persistent) serverObject;
ObjectId id = object.getObjectId();
// sanity check
if (id == null) {
throw new CayenneRuntimeException("Server returned an object without an id: %s", object);
}
// have to resolve descriptor here for every object, as
// often a query will not have any info indicating the
// entity type
ClassDescriptor serverDescriptor = serverResolver.getClassDescriptor(id.getEntityName());
return op.detach(object, serverDescriptor, prefetchTree);
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class LegacyDataDomainFlushAction 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);
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class PersistentObject method getMapKey.
/**
* Returns a map key for a given to-many map relationship and a target object.
*
* @since 3.0
*/
protected Object getMapKey(String relationshipName, Object value) {
EntityResolver resolver = objectContext.getEntityResolver();
ClassDescriptor descriptor = resolver.getClassDescriptor(objectId.getEntityName());
if (descriptor == null) {
throw new IllegalStateException("DataObject's entity is unmapped, objectId: " + objectId);
}
PropertyDescriptor property = descriptor.getProperty(relationshipName);
if (property instanceof ToManyMapProperty) {
return ((ToManyMapProperty) property).getMapKey(value);
}
throw new IllegalArgumentException("Relationship '" + relationshipName + "' is not a to-many Map");
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class DataContext method objectFromDataRow.
/**
* Creates a DataObject from DataRow. This variety of the
* 'objectFromDataRow' method is normally used for generic classes.
*
* @see DataRow
* @since 3.1
*/
public DataObject objectFromDataRow(String entityName, DataRow dataRow) {
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entityName);
List<?> list = objectsFromDataRows(descriptor, Collections.singletonList(dataRow));
return (DataObject) list.get(0);
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class ObjectContextDeleteAction method processDeleteRules.
private void processDeleteRules(final Persistent object, int oldState) throws DeleteDenyException {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(object.getObjectId().getEntityName());
for (final ObjRelationship relationship : descriptor.getEntity().getRelationships()) {
boolean processFlattened = relationship.isFlattened() && relationship.isToDependentEntity() && !relationship.isReadOnly();
// first check for no action... bail out if no flattened processing is needed
if (relationship.getDeleteRule() == DeleteRule.NO_ACTION && !processFlattened) {
continue;
}
ArcProperty property = (ArcProperty) descriptor.getProperty(relationship.getName());
final Collection<Persistent> relatedObjects = toCollection(property.readProperty(object));
// no related object, bail out
if (relatedObjects.size() == 0) {
continue;
}
// process DENY rule first...
if (relationship.getDeleteRule() == DeleteRule.DENY) {
object.setPersistenceState(oldState);
String message = relatedObjects.size() == 1 ? "1 related object" : relatedObjects.size() + " related objects";
throw new DeleteDenyException(object, relationship.getName(), message);
}
// object graph
if (processFlattened) {
ArcId arcId = new ArcId(property);
for (Persistent relatedObject : relatedObjects) {
context.getGraphManager().arcDeleted(object.getObjectId(), relatedObject.getObjectId(), arcId);
}
}
// process remaining rules
switch(relationship.getDeleteRule()) {
case DeleteRule.NO_ACTION:
break;
case DeleteRule.NULLIFY:
ArcProperty reverseArc = property.getComplimentaryReverseArc();
if (reverseArc == null) {
// nothing we can do here
break;
}
reverseArc.visit(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
return false;
}
public boolean visitToMany(ToManyProperty property) {
for (Persistent relatedObject : relatedObjects) {
property.removeTarget(relatedObject, object, true);
}
return false;
}
public boolean visitToOne(ToOneProperty property) {
// nullify the reverse relationship
for (Persistent relatedObject : relatedObjects) {
property.setTarget(relatedObject, null, true);
}
return false;
}
});
break;
case DeleteRule.CASCADE:
// Delete all related objects
for (Persistent relatedObject : relatedObjects) {
performDelete(relatedObject);
}
break;
default:
object.setPersistenceState(oldState);
throw new CayenneRuntimeException("Invalid delete rule %s", relationship.getDeleteRule());
}
}
}
Aggregations