Search in sources :

Example 6 with ToManyProperty

use of org.apache.cayenne.reflect.ToManyProperty in project cayenne by apache.

the class CayenneContextChildDiffLoader method arcDeleted.

@Override
public void arcDeleted(Object nodeId, final Object targetNodeId, Object arcId) {
    final Persistent source = findObject(nodeId);
    // changing their relationships
    if (source == null) {
        return;
    }
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
    PropertyDescriptor property = descriptor.getProperty(arcId.toString());
    final Persistent[] target = new Persistent[1];
    target[0] = findObject(targetNodeId);
    property.visit(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            return false;
        }

        public boolean visitToMany(ToManyProperty property) {
            if (target[0] == null) {
                // this is usually the case when a NEW object was deleted and then
                // its relationships were manipulated; so try to locate the object
                // in the collection ... the performance of this is rather dubious
                // of course...
                target[0] = findObjectInCollection(targetNodeId, property.readProperty(source));
            }
            if (target[0] != null) {
                property.removeTargetDirectly(source, target[0]);
            }
            return false;
        }

        public boolean visitToOne(ToOneProperty property) {
            property.setTarget(source, null, false);
            return false;
        }
    });
    context.propertyChanged(source, (String) arcId, target[0], null);
}
Also used : 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)

Example 7 with ToManyProperty

use of org.apache.cayenne.reflect.ToManyProperty in project cayenne by apache.

the class CayenneContextMergeHandler method arcCreated.

public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
    // null source or target likely means the object is not faulted yet... Faults
    // shouldn't get disturbed by adding/removing arcs
    Object source = context.internalGraphManager().getNode(nodeId);
    if (source == null) {
        // no need to connect non-existent object
        return;
    }
    // TODO (Andrus, 10/17/2005) - check for local modifications to avoid
    // overwriting...
    ArcProperty p = (ArcProperty) propertyForId(nodeId, arcId.toString());
    if (p.isFault(source)) {
        return;
    }
    Object target = context.internalGraphManager().getNode(targetNodeId);
    if (target == null) {
        target = context.createFault((ObjectId) targetNodeId);
    }
    if (p instanceof ToManyProperty) {
        ((ToManyProperty) p).addTargetDirectly(source, target);
    } else {
        p.writePropertyDirectly(source, null, target);
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty)

Example 8 with ToManyProperty

use of org.apache.cayenne.reflect.ToManyProperty in project cayenne by apache.

the class BaseContext method prepareForAccess.

@Override
public void prepareForAccess(Persistent object, String property, boolean lazyFaulting) {
    if (object.getPersistenceState() == PersistenceState.HOLLOW) {
        ObjectId oid = object.getObjectId();
        List<?> objects = performQuery(new ObjectIdQuery(oid, false, ObjectIdQuery.CACHE));
        if (objects.size() == 0) {
            throw new FaultFailureException("Error resolving fault, no matching row exists in the database for ObjectId: " + oid);
        } else if (objects.size() > 1) {
            throw new FaultFailureException("Error resolving fault, more than one row exists in the database for ObjectId: " + oid);
        }
    // 5/28/2013 - Commented out this block to allow for modifying
    // objects in the postLoad callback
    // sanity check...
    // if (object.getPersistenceState() != PersistenceState.COMMITTED) {
    // 
    // String state =
    // PersistenceState.persistenceStateName(object.getPersistenceState());
    // 
    // // TODO: andrus 4/13/2006, modified and deleted states are
    // // possible due to
    // // a race condition, should we handle them here?
    // throw new
    // FaultFailureException("Error resolving fault for ObjectId: " +
    // oid + " and state (" + state
    // +
    // "). Possible cause - matching row is missing from the database.");
    // }
    }
    // resolve relationship fault
    if (lazyFaulting && property != null) {
        ClassDescriptor classDescriptor = getEntityResolver().getClassDescriptor(object.getObjectId().getEntityName());
        PropertyDescriptor propertyDescriptor = classDescriptor.getProperty(property);
        // be used.
        if (propertyDescriptor == null) {
            final StringBuilder errorMessage = new StringBuilder();
            errorMessage.append(String.format("Property '%s' is not declared for entity '%s'.", property, object.getObjectId().getEntityName()));
            errorMessage.append(" Declared properties are: ");
            // Grab each of the declared properties.
            final List<String> properties = new ArrayList<>();
            classDescriptor.visitProperties(new PropertyVisitor() {

                @Override
                public boolean visitAttribute(final AttributeProperty property) {
                    properties.add(property.getName());
                    return true;
                }

                @Override
                public boolean visitToOne(final ToOneProperty property) {
                    properties.add(property.getName());
                    return true;
                }

                @Override
                public boolean visitToMany(final ToManyProperty property) {
                    properties.add(property.getName());
                    return true;
                }
            });
            // Now add the declared property names to the error message.
            boolean first = true;
            for (String declaredProperty : properties) {
                if (first) {
                    errorMessage.append(String.format("'%s'", declaredProperty));
                    first = false;
                } else {
                    errorMessage.append(String.format(", '%s'", declaredProperty));
                }
            }
            errorMessage.append(".");
            throw new CayenneRuntimeException(errorMessage.toString());
        }
        // this should trigger fault resolving
        propertyDescriptor.readProperty(object);
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ArrayList(java.util.ArrayList) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 9 with ToManyProperty

use of org.apache.cayenne.reflect.ToManyProperty in project cayenne by apache.

the class DeletedDiffProcessor method nodeRemoved.

@Override
public void nodeRemoved(Object nodeId) {
    ObjectId id = (ObjectId) nodeId;
    final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
    // TODO: rewrite with SelectById query after Cayenne upgrade
    ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
    QueryResponse result = channel.onQuery(null, query);
    @SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
    if (rows.isEmpty()) {
        LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
        return;
    }
    final DataRow row = rows.get(0);
    ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
    final CommitLogEntity entity = entityFactory.getEntity(id);
    descriptor.visitProperties(new PropertyVisitor() {

        @Override
        public boolean visitAttribute(AttributeProperty property) {
            if (!entity.isIncluded(property.getName())) {
                return true;
            }
            Object value;
            if (entity.isConfidential(property.getName())) {
                value = Confidential.getInstance();
            } else {
                String key = property.getAttribute().getDbAttributeName();
                value = row.get(key);
            }
            if (value != null) {
                objectChangeSet.attributeChanged(property.getName(), value, null);
            }
            return true;
        }

        @Override
        public boolean visitToOne(ToOneProperty property) {
            // TODO record FK changes?
            return true;
        }

        @Override
        public boolean visitToMany(ToManyProperty property) {
            return true;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) QueryResponse(org.apache.cayenne.QueryResponse) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 10 with ToManyProperty

use of org.apache.cayenne.reflect.ToManyProperty in project cayenne by apache.

the class DataObjectDescriptorFactoryIT method testVisitDeclaredProperties_IterationOrder.

@Test
public void testVisitDeclaredProperties_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) {
                assertPropertiesAreInOrder(lastProcessed[0], property);
                lastProcessed[0] = property;
                return true;
            }

            public boolean visitToMany(ToManyProperty property) {
                assertPropertiesAreInOrder(lastProcessed[0], property);
                lastProcessed[0] = property;
                return true;
            }

            public boolean visitAttribute(AttributeProperty property) {
                assertPropertiesAreInOrder(lastProcessed[0], property);
                lastProcessed[0] = property;
                return true;
            }
        };
        descriptor.visitDeclaredProperties(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)

Aggregations

ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)31 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)29 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)28 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)28 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)21 ArcProperty (org.apache.cayenne.reflect.ArcProperty)13 ObjRelationship (org.apache.cayenne.map.ObjRelationship)9 Persistent (org.apache.cayenne.Persistent)8 DbRelationship (org.apache.cayenne.map.DbRelationship)8 ObjAttribute (org.apache.cayenne.map.ObjAttribute)8 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)8 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)7 DbJoin (org.apache.cayenne.map.DbJoin)7 ObjEntity (org.apache.cayenne.map.ObjEntity)7 ObjectId (org.apache.cayenne.ObjectId)6 DbAttribute (org.apache.cayenne.map.DbAttribute)6 HashSet (java.util.HashSet)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3