Search in sources :

Example 86 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)

Example 87 with ClassDescriptor

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

Example 88 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 89 with ClassDescriptor

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

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

Aggregations

ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)90 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)25 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)23 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)23 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)23 ObjEntity (org.apache.cayenne.map.ObjEntity)22 ArcProperty (org.apache.cayenne.reflect.ArcProperty)21 Persistent (org.apache.cayenne.Persistent)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)15 DbRelationship (org.apache.cayenne.map.DbRelationship)14 ObjRelationship (org.apache.cayenne.map.ObjRelationship)13 DataRow (org.apache.cayenne.DataRow)12 ObjectId (org.apache.cayenne.ObjectId)12 Test (org.junit.Test)12 DbAttribute (org.apache.cayenne.map.DbAttribute)10 DataObject (org.apache.cayenne.DataObject)9 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DbJoin (org.apache.cayenne.map.DbJoin)9 ArrayList (java.util.ArrayList)8