Search in sources :

Example 56 with ClassDescriptor

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);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) SingletonFaultFactory(org.apache.cayenne.reflect.SingletonFaultFactory) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) Test(org.junit.Test)

Example 57 with ClassDescriptor

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);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Test(org.junit.Test)

Example 58 with ClassDescriptor

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());
        }
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 59 with ClassDescriptor

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");
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) EntityResolver(org.apache.cayenne.map.EntityResolver)

Example 60 with ClassDescriptor

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);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent)

Aggregations

ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)81 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)23 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)21 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)21 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)21 ObjEntity (org.apache.cayenne.map.ObjEntity)18 ArcProperty (org.apache.cayenne.reflect.ArcProperty)18 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 Persistent (org.apache.cayenne.Persistent)17 ObjectId (org.apache.cayenne.ObjectId)14 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)14 DbRelationship (org.apache.cayenne.map.DbRelationship)13 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 DataRow (org.apache.cayenne.DataRow)10 DbAttribute (org.apache.cayenne.map.DbAttribute)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DbEntity (org.apache.cayenne.map.DbEntity)9 DbJoin (org.apache.cayenne.map.DbJoin)9