Search in sources :

Example 66 with ClassDescriptor

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

the class ObjectResolver method objectFromDataRow.

Persistent objectFromDataRow(DataRow row) {
    // determine entity to use
    ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
    // not using DataRow.createObjectId for performance reasons -
    // ObjectResolver has all needed metadata already cached.
    ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
    return objectFromDataRow(row, anId, classDescriptor);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId)

Example 67 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor 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 68 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor 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 69 with ClassDescriptor

use of org.apache.cayenne.reflect.ClassDescriptor 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 70 with ClassDescriptor

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

the class CayenneContext method createFault.

Persistent createFault(ObjectId id) {
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName());
    Persistent object = (Persistent) descriptor.createObject();
    object.setPersistenceState(PersistenceState.HOLLOW);
    object.setObjectContext(this);
    object.setObjectId(id);
    graphManager.registerNode(id, object);
    return object;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

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