Search in sources :

Example 16 with ObjectId

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

the class HierarchicalObjectResolverNode method objectsFromDataRows.

@Override
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
    if (rows == null || rows.size() == 0) {
        return new ArrayList<>(1);
    }
    List<Persistent> results = new ArrayList<>(rows.size());
    for (DataRow row : rows) {
        // 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);
        Persistent object = objectFromDataRow(row, anId, classDescriptor);
        if (object == null) {
            throw new CayenneRuntimeException("Can't build Object from row: %s", row);
        }
        // keep the dupe objects (and data rows) around, as there maybe an
        // attached
        // joint prefetch...
        results.add(object);
        node.getParentAttachmentStrategy().linkToParent(row, object);
    }
    // now deal with snapshots
    // TODO: refactoring: dupes will clutter the lists and cause extra
    // processing...
    // removal of dupes happens only downstream, as we need the objects
    // matching
    // fetched rows for joint prefetch resolving... maybe pushback unique
    // and
    // non-unique lists to the "node", instead of returning a single list
    // from this
    // method
    cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
    return results;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 17 with ObjectId

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

the class JoinedIdParentAttachementStrategy method linkToParent.

public void linkToParent(DataRow row, Persistent object) {
    Persistent parentObject = null;
    for (ObjEntity entity : sourceEntities) {
        if (entity.isAbstract()) {
            continue;
        }
        ObjectId id = node.getResolver().createObjectId(row, entity, relatedIdPrefix);
        if (id == null) {
            throw new CayenneRuntimeException("Can't build ObjectId from row: %s, entity: %s, prefix: %s", row, entity.getName(), relatedIdPrefix);
        }
        parentObject = (Persistent) graphManager.getNode(id);
        if (parentObject != null) {
            break;
        }
    }
    node.linkToParent(object, parentObject);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent)

Example 18 with ObjectId

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

the class ObjectDiff method getArcSnapshotValue.

ObjectId getArcSnapshotValue(String propertyName) {
    Object value = arcSnapshot != null ? arcSnapshot.get(propertyName) : null;
    if (value instanceof Fault) {
        Persistent target = (Persistent) ((Fault) value).resolveFault(object, propertyName);
        value = target != null ? target.getObjectId() : null;
        arcSnapshot.put(propertyName, value);
    }
    return (ObjectId) value;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Fault(org.apache.cayenne.Fault) Persistent(org.apache.cayenne.Persistent)

Example 19 with ObjectId

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

the class ObjectResolver method createObjectId.

ObjectId createObjectId(DataRow dataRow, ObjEntity objEntity, String namePrefix) {
    Collection<DbAttribute> pk = objEntity == this.descriptor.getEntity() ? this.primaryKey : objEntity.getDbEntity().getPrimaryKeys();
    boolean prefix = namePrefix != null && namePrefix.length() > 0;
    if (pk.size() == 1) {
        DbAttribute attribute = pk.iterator().next();
        String key = (prefix) ? namePrefix + attribute.getName() : attribute.getName();
        Object val = dataRow.get(key);
        // this is possible when processing left outer joint prefetches
        if (val == null) {
            if (!dataRow.containsKey(key)) {
                throw new CayenneRuntimeException("No PK column '%s' found in data row.", key);
            }
            return null;
        }
        // PUT without a prefix
        return new ObjectId(objEntity.getName(), attribute.getName(), val);
    }
    // ... handle generic case - PK.size > 1
    Map<String, Object> idMap = new HashMap<>(pk.size() * 2);
    for (final DbAttribute attribute : pk) {
        String key = (prefix) ? namePrefix + attribute.getName() : attribute.getName();
        Object val = dataRow.get(key);
        // this is possible when processing left outer joint prefetches
        if (val == null) {
            if (!dataRow.containsKey(key)) {
                throw new CayenneRuntimeException("No PK column '%s' found in data row.", key);
            }
            return null;
        }
        // PUT without a prefix
        idMap.put(attribute.getName(), val);
    }
    return new ObjectId(objEntity.getName(), idMap);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataObject(org.apache.cayenne.DataObject)

Example 20 with ObjectId

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

the class ObjectStore method processInvalidatedIDs.

/**
 * @since 1.1
 */
void processInvalidatedIDs(Collection<ObjectId> invalidatedIDs) {
    if (invalidatedIDs != null && !invalidatedIDs.isEmpty()) {
        for (ObjectId oid : invalidatedIDs) {
            DataObject object = (DataObject) getNode(oid);
            if (object == null) {
                continue;
            }
            switch(object.getPersistenceState()) {
                case PersistenceState.COMMITTED:
                    object.setPersistenceState(PersistenceState.HOLLOW);
                    break;
                case PersistenceState.MODIFIED:
                    DataContext context = (DataContext) object.getObjectContext();
                    DataRow diff = getSnapshot(oid);
                    // consult delegate if it exists
                    DataContextDelegate delegate = context.nonNullDelegate();
                    if (delegate.shouldMergeChanges(object, diff)) {
                        ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
                        DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
                        delegate.finishedMergeChanges(object);
                    }
                case PersistenceState.HOLLOW:
                    // do nothing
                    break;
                case PersistenceState.DELETED:
                    // TODO: Do nothing? Or treat as merged?
                    break;
            }
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9