use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class DataObjectDescriptorFactory_InheritanceMapsIT method testVisitProperties_IterationOrder.
@Test
public void testVisitProperties_IterationOrder() {
DataObjectDescriptorFactory factory = new DataObjectDescriptorFactory(resolver.getClassDescriptorMap(), new SingletonFaultFactory());
for (ObjEntity e : resolver.getObjEntities()) {
ClassDescriptor descriptor = factory.getDescriptor(e.getName());
final PropertyDescriptor[] lastProcessed = new PropertyDescriptor[1];
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitToOne(ToOneProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
public boolean visitToMany(ToManyProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
public boolean visitAttribute(AttributeProperty property) {
DataObjectDescriptorFactoryIT.assertPropertiesAreInOrder(lastProcessed[0], property);
lastProcessed[0] = property;
return true;
}
};
descriptor.visitProperties(visitor);
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class EntityResolverClassDescriptorIT method testServerDescriptorCaching.
@Test
public void testServerDescriptorCaching() {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
resolver.getClassDescriptorMap().clearDescriptors();
ClassDescriptor descriptor = resolver.getClassDescriptor("MtTable1");
assertNotNull(descriptor);
assertSame(descriptor, resolver.getClassDescriptor("MtTable1"));
resolver.getClassDescriptorMap().clearDescriptors();
ClassDescriptor descriptor1 = resolver.getClassDescriptor("MtTable1");
assertNotNull(descriptor1);
assertNotSame(descriptor, descriptor1);
}
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) {
for (Persistent relatedObject : relatedObjects) {
context.getGraphManager().arcDeleted(object.getObjectId(), relatedObject.getObjectId(), relationship.getName());
}
}
// 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());
}
}
}
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 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);
}
Aggregations