Search in sources :

Example 1 with MutableObjectChange

use of org.apache.cayenne.commitlog.model.MutableObjectChange 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 2 with MutableObjectChange

use of org.apache.cayenne.commitlog.model.MutableObjectChange in project cayenne by apache.

the class DiffProcessor method arcDeleted.

@Override
public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
    ObjectId id = (ObjectId) nodeId;
    String relationshipName = arcId.toString();
    ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
    ObjRelationship relationship = entity.getRelationship(relationshipName);
    MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
    ObjectId tid = (ObjectId) targetNodeId;
    if (relationship.isToMany()) {
        c.toManyRelationshipDisconnected(relationshipName, tid);
    } else {
        c.toOneRelationshipDisconnected(relationshipName, tid);
    }
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId)

Example 3 with MutableObjectChange

use of org.apache.cayenne.commitlog.model.MutableObjectChange in project cayenne by apache.

the class DiffProcessor method arcCreated.

@Override
public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
    ObjectId id = (ObjectId) nodeId;
    String relationshipName = arcId.toString();
    ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
    ObjRelationship relationship = entity.getRelationship(relationshipName);
    MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
    ObjectId tid = (ObjectId) targetNodeId;
    if (relationship.isToMany()) {
        c.toManyRelationshipConnected(relationshipName, tid);
    } else {
        c.toOneRelationshipConnected(relationshipName, tid);
    }
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)3 MutableObjectChange (org.apache.cayenne.commitlog.model.MutableObjectChange)3 ObjEntity (org.apache.cayenne.map.ObjEntity)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 DataRow (org.apache.cayenne.DataRow)1 QueryResponse (org.apache.cayenne.QueryResponse)1 CommitLogEntity (org.apache.cayenne.commitlog.meta.CommitLogEntity)1 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)1 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)1 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)1 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)1