Search in sources :

Example 31 with DataObject

use of org.apache.cayenne.DataObject in project cayenne by apache.

the class ObjectResolver method objectFromDataRow.

Persistent objectFromDataRow(DataRow row, ObjectId anId, ClassDescriptor classDescriptor) {
    // details
    if (anId == null) {
        return null;
    }
    // this will create a HOLLOW object if it is not registered yet
    Persistent object = context.findOrCreateObject(anId);
    // deal with object state
    int state = object.getPersistenceState();
    switch(state) {
        case PersistenceState.COMMITTED:
        case PersistenceState.MODIFIED:
        case PersistenceState.DELETED:
            // process the above only if refresh is requested...
            if (refreshObjects) {
                DataRowUtils.mergeObjectWithSnapshot(context, classDescriptor, object, row);
                if (object instanceof DataObject) {
                    ((DataObject) object).setSnapshotVersion(row.getVersion());
                }
            }
            break;
        case PersistenceState.HOLLOW:
            if (!refreshObjects) {
                DataRow cachedRow = cache.getCachedSnapshot(anId);
                if (cachedRow != null) {
                    row = cachedRow;
                }
            }
            DataRowUtils.mergeObjectWithSnapshot(context, classDescriptor, object, row);
            if (object instanceof DataObject) {
                ((DataObject) object).setSnapshotVersion(row.getVersion());
            }
            break;
        default:
            break;
    }
    return object;
}
Also used : DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 32 with DataObject

use of org.apache.cayenne.DataObject in project cayenne by apache.

the class ObjectStore method processIndirectlyModifiedIDs.

/**
 * Requires external synchronization.
 *
 * @since 1.1
 */
void processIndirectlyModifiedIDs(Collection<ObjectId> indirectlyModifiedIDs) {
    for (ObjectId oid : indirectlyModifiedIDs) {
        // access object map directly - the method should be called in a synchronized context...
        final DataObject object = (DataObject) objectMap.get(oid);
        if (object == null || object.getPersistenceState() != PersistenceState.COMMITTED) {
            continue;
        }
        // for now break all "independent" object relationships...
        // in the future we may want to be more precise and go after modified
        // relationships only, or even process updated lists without invalidating...
        DataContextDelegate delegate = context.nonNullDelegate();
        if (delegate.shouldMergeChanges(object, null)) {
            ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
            descriptor.visitProperties(new PropertyVisitor() {

                @Override
                public boolean visitToMany(ToManyProperty property) {
                    property.invalidate(object);
                    return true;
                }

                @Override
                public boolean visitToOne(ToOneProperty property) {
                    if (property.getRelationship().isSourceIndependentFromTargetChange()) {
                        property.invalidate(object);
                    }
                    return true;
                }

                @Override
                public boolean visitAttribute(AttributeProperty property) {
                    return true;
                }
            });
            delegate.finishedProcessDelete(object);
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) ObjectId(org.apache.cayenne.ObjectId) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty)

Example 33 with DataObject

use of org.apache.cayenne.DataObject in project cayenne by apache.

the class ObjectStore method processUpdatedSnapshot.

/**
 * Requires external synchronization.
 *
 * @since 1.1
 */
void processUpdatedSnapshot(ObjectId nodeId, DataRow diff) {
    // access object map directly - the method should be called in a synchronized context...
    DataObject object = (DataObject) objectMap.get(nodeId);
    // no object, or HOLLOW object require no processing
    if (object != null) {
        int state = object.getPersistenceState();
        if (state != PersistenceState.HOLLOW) {
            // perform same steps as resolveHollow()
            if (state == PersistenceState.COMMITTED) {
                // consult delegate if it exists
                DataContextDelegate delegate = context.nonNullDelegate();
                if (delegate.shouldMergeChanges(object, diff)) {
                    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(nodeId.getEntityName());
                    // TODO: andrus, 5/26/2006 - call to 'getSnapshot' is expensive,
                    // however my attempts to merge the 'diff' instead of snapshot
                    // via 'refreshObjectWithSnapshot' resulted in even worse performance.
                    // This sounds counterintuitive (Not sure if this is some HotSpot related glitch)...
                    // still keeping the old algorithm here until we switch from snapshot events
                    // to GraphEvents and all this code becomes obsolete.
                    DataRow snapshot = getSnapshot(object.getObjectId());
                    DataRowUtils.refreshObjectWithSnapshot(descriptor, object, snapshot, true);
                    delegate.finishedMergeChanges(object);
                }
            } else // merge modified and deleted
            if (state == PersistenceState.DELETED || state == PersistenceState.MODIFIED) {
                // consult delegate if it exists
                DataContextDelegate delegate = context.nonNullDelegate();
                if (delegate.shouldMergeChanges(object, diff)) {
                    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(nodeId.getEntityName());
                    DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
                    delegate.finishedMergeChanges(object);
                }
            }
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DataRow(org.apache.cayenne.DataRow)

Example 34 with DataObject

use of org.apache.cayenne.DataObject in project cayenne by apache.

the class ObjectStore method registerDiff.

/**
 * Registers object change.
 *
 * @since 1.2
 */
synchronized ObjectDiff registerDiff(Object nodeId, NodeDiff diff) {
    if (diff != null) {
        diff.setDiffId(++currentDiffId);
    }
    ObjectDiff objectDiff = changes.get(nodeId);
    if (objectDiff == null) {
        Persistent object = objectMap.get(nodeId);
        if (object == null) {
            throw new CayenneRuntimeException("No object is registered in context with Id %s", nodeId);
        }
        if (object.getPersistenceState() == PersistenceState.COMMITTED) {
            object.setPersistenceState(PersistenceState.MODIFIED);
            // replacement yet, so we still need to handle them...
            if (object instanceof DataObject) {
                DataObject dataObject = (DataObject) object;
                DataRow snapshot = getCachedSnapshot((ObjectId) nodeId);
                if (snapshot != null && snapshot.getVersion() != dataObject.getSnapshotVersion()) {
                    DataContextDelegate delegate = context.nonNullDelegate();
                    if (delegate.shouldMergeChanges(dataObject, snapshot)) {
                        ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
                        DataRowUtils.forceMergeWithSnapshot(context, descriptor, dataObject, snapshot);
                        dataObject.setSnapshotVersion(snapshot.getVersion());
                        delegate.finishedMergeChanges(dataObject);
                    }
                }
            }
        }
        objectDiff = new ObjectDiff(object);
        objectDiff.setDiffId(++currentDiffId);
        changes.put(nodeId, objectDiff);
    }
    if (diff != null) {
        objectDiff.addDiff(diff, this);
    }
    return objectDiff;
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 35 with DataObject

use of org.apache.cayenne.DataObject in project cayenne by apache.

the class ObjectIdBatchFault method fetchObjects.

private Map<String, Object> fetchObjects() {
    if (sources == null) {
        return Collections.emptyMap();
    }
    EntityResolver resolver = context.getEntityResolver();
    // reasons
    if (sources.size() == 1) {
        String uuid = sources.get(0).getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        ObjEntity entity = resolver.getObjEntity(entityName);
        ObjectId id = new EntityIdCoder(entity).toObjectId(uuid);
        Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
        if (object == null) {
            return Collections.emptyMap();
        } else {
            return Collections.singletonMap(uuid, object);
        }
    }
    Map<String, SelectQuery<DataObject>> queriesByEntity = new HashMap<>();
    Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
    for (ObjectIdBatchSourceItem source : sources) {
        String uuid = source.getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        EntityIdCoder coder = codersByEntity.get(entityName);
        SelectQuery<DataObject> query;
        if (coder == null) {
            coder = new EntityIdCoder(resolver.getObjEntity(entityName));
            codersByEntity.put(entityName, coder);
            query = new SelectQuery<>(entityName);
            queriesByEntity.put(entityName, query);
        } else {
            query = queriesByEntity.get(entityName);
        }
        ObjectId id = coder.toObjectId(uuid);
        Expression idExp = ExpressionFactory.matchAllDbExp(id.getIdSnapshot(), Expression.EQUAL_TO);
        query.orQualifier(idExp);
    }
    int capacity = (int) Math.ceil(sources.size() / 0.75d);
    Map<String, Object> results = new HashMap<>(capacity);
    for (SelectQuery<DataObject> query : queriesByEntity.values()) {
        EntityIdCoder coder = codersByEntity.get(query.getRoot());
        @SuppressWarnings("unchecked") List<DataObject> objects = context.performQuery(query);
        for (DataObject object : objects) {
            String uuid = coder.toStringId(object.getObjectId());
            results.put(uuid, object);
        }
    }
    return results;
}
Also used : EntityIdCoder(org.apache.cayenne.lifecycle.id.EntityIdCoder) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) EntityResolver(org.apache.cayenne.map.EntityResolver) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) Expression(org.apache.cayenne.exp.Expression) DataObject(org.apache.cayenne.DataObject) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2