Search in sources :

Example 56 with DataRow

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

the class ObjectStore method processSnapshotEvent.

/**
 * @since 1.2
 */
synchronized void processSnapshotEvent(SnapshotEvent event) {
    Map<ObjectId, DataRow> modifiedDiffs = event.getModifiedDiffs();
    if (modifiedDiffs != null && !modifiedDiffs.isEmpty()) {
        for (Map.Entry<ObjectId, DataRow> entry : modifiedDiffs.entrySet()) {
            processUpdatedSnapshot(entry.getKey(), entry.getValue());
        }
    }
    Collection<ObjectId> deletedIDs = event.getDeletedIds();
    if (deletedIDs != null && !deletedIDs.isEmpty()) {
        for (ObjectId deletedID : deletedIDs) {
            processDeletedID(deletedID);
        }
    }
    processInvalidatedIDs(event.getInvalidatedIds());
    processIndirectlyModifiedIDs(event.getIndirectlyModifiedIds());
    // TODO: andrus, 3/28/2006 - 'SnapshotEventDecorator' serves as a bridge (or
    // rather a noop wrapper) between old snapshot events and new GraphEvents. Once
    // SnapshotEvents are replaced with GraphEvents (in 2.0) we won't need it
    GraphDiff diff = new SnapshotEventDecorator(event);
    ObjectContext originatingContext = (event.getPostedBy() instanceof ObjectContext) ? (ObjectContext) event.getPostedBy() : null;
    context.fireDataChannelChanged(originatingContext, diff);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) DataRow(org.apache.cayenne.DataRow) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 57 with DataRow

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

the class DataDomainQueryAction method interceptRelationshipQuery.

private boolean interceptRelationshipQuery() {
    if (query instanceof RelationshipQuery) {
        RelationshipQuery relationshipQuery = (RelationshipQuery) query;
        if (relationshipQuery.isRefreshing()) {
            return !DONE;
        }
        ObjRelationship relationship = relationshipQuery.getRelationship(domain.getEntityResolver());
        // check if we can derive target PK from FK...
        if (relationship.isSourceIndependentFromTargetChange()) {
            return !DONE;
        }
        // we can assume that there is one and only one DbRelationship as
        // we previously checked that "!isSourceIndependentFromTargetChange"
        DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
        // FK pointing to a unique field that is a 'fake' PK (CAY-1755)...
        // It is not sufficient to generate target ObjectId.
        DbEntity targetEntity = dbRelationship.getTargetEntity();
        if (dbRelationship.getJoins().size() < targetEntity.getPrimaryKeys().size()) {
            return !DONE;
        }
        if (cache == null) {
            return !DONE;
        }
        DataRow sourceRow = cache.getCachedSnapshot(relationshipQuery.getObjectId());
        if (sourceRow == null) {
            return !DONE;
        }
        ObjectId targetId = sourceRow.createTargetObjectId(relationship.getTargetEntityName(), dbRelationship);
        // null id means that FK is null...
        if (targetId == null) {
            this.response = new GenericResponse(Collections.EMPTY_LIST);
            return DONE;
        }
        // target id resolution (unlike source) should be polymorphic
        DataRow targetRow = polymorphicRowFromCache(targetId);
        if (targetRow != null) {
            this.response = new GenericResponse(Collections.singletonList(targetRow));
            return DONE;
        }
        // create a fault
        if (context != null && relationship.isSourceDefiningTargetPrecenseAndType(domain.getEntityResolver())) {
            // prevent passing partial snapshots to ObjectResolver per
            // CAY-724.
            // Create a hollow object right here and skip object conversion
            // downstream
            this.noObjectConversion = true;
            Object object = context.findOrCreateObject(targetId);
            this.response = new GenericResponse(Collections.singletonList(object));
            return DONE;
        }
    }
    return !DONE;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbEntity(org.apache.cayenne.map.DbEntity) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) DbRelationship(org.apache.cayenne.map.DbRelationship) EmbeddableObject(org.apache.cayenne.EmbeddableObject) DataRow(org.apache.cayenne.DataRow)

Example 58 with DataRow

use of org.apache.cayenne.DataRow 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)

Example 59 with DataRow

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

the class PrefetchProcessorJointNode method rowFromFlatRow.

/**
 * Returns a DataRow from the flat row.
 */
DataRow rowFromFlatRow(DataRow flatRow) {
    DataRow row = new DataRow(rowCapacity);
    // extract subset of flat row columns, recasting to the target keys
    for (ColumnDescriptor column : columns) {
        row.put(column.getName(), flatRow.get(column.getDataRowKey()));
    }
    // since JDBC row reader won't inject JOINED entity name, we have to detect it here...
    ClassDescriptor descriptor = resolver.getDescriptor();
    ObjEntity entity = descriptor.getEntityInheritanceTree().entityMatchingRow(row);
    row.setEntityName(entity == null ? null : entity.getName());
    return row;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DataRow(org.apache.cayenne.DataRow)

Example 60 with DataRow

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

the class PrefetchProcessorTreeBuilder method addNode.

boolean addNode(PrefetchProcessorNode node) {
    List<DataRow> rows;
    ArcProperty arc;
    ClassDescriptor descriptor;
    PrefetchProcessorNode currentNode = getParent();
    if (currentNode != null) {
        @SuppressWarnings("unchecked") List<DataRow> dataRows = (List<DataRow>) extraResultsByPath.get(node.getPath());
        rows = dataRows;
        arc = (ArcProperty) currentNode.getResolver().getDescriptor().getProperty(node.getName());
        if (arc == null) {
            throw new CayenneRuntimeException("No relationship with name '%s' found in entity '%s'", node.getName(), currentNode.getResolver().getEntity().getName());
        }
        descriptor = arc.getTargetDescriptor();
    } else {
        arc = null;
        if (this.descriptor != null) {
            descriptor = this.descriptor;
        } else {
            descriptor = queryMetadata.getClassDescriptor();
        }
        rows = mainResultRows;
    }
    node.setDataRows(rows);
    node.setIncoming(arc);
    if (node.getParent() != null && !node.isJointPrefetch()) {
        node.setResolver(new HierarchicalObjectResolverNode(node, context, descriptor, queryMetadata.isRefreshingObjects(), seen));
    } else {
        node.setResolver(new PrefetchObjectResolver(context, descriptor, queryMetadata.isRefreshingObjects(), seen));
    }
    if (node.getParent() == null || node.getParent().isPhantom()) {
        node.setParentAttachmentStrategy(new NoopParentAttachmentStrategy());
    } else if (node.isJointPrefetch()) {
        node.setParentAttachmentStrategy(new StackLookupParentAttachmentStrategy(node));
    } else if (node.getIncoming().getRelationship().isSourceIndependentFromTargetChange()) {
        node.setParentAttachmentStrategy(new JoinedIdParentAttachementStrategy(context.getGraphManager(), node));
    } else {
        node.setParentAttachmentStrategy(new ResultScanParentAttachmentStrategy(node));
    }
    if (currentNode != null) {
        currentNode.addChild(node);
    }
    node.afterInit();
    // push node on stack
    if (nodeStack.isEmpty()) {
        root = node;
    }
    nodeStack.addLast(node);
    return true;
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataRow(org.apache.cayenne.DataRow) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5