Search in sources :

Example 26 with ClassDescriptor

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

Example 27 with ClassDescriptor

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

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

the class RelationshipQuery method updateMetadata.

void updateMetadata(EntityResolver resolver) {
    // caching metadata as it may be accessed multiple times (at a DC and DD level)
    if (metadataResolver != resolver) {
        if (objectId == null) {
            throw new CayenneRuntimeException("Can't resolve query - objectID is null.");
        }
        ClassDescriptor descriptor = resolver.getClassDescriptor(objectId.getEntityName());
        this.arc = (ArcProperty) descriptor.getProperty(relationshipName);
        if (arc == null) {
            throw new CayenneRuntimeException("No relationship named %s found in entity %s; object id: %s", relationshipName, objectId.getEntityName(), objectId);
        }
        this.metadata = new DefaultQueryMetadata() {

            @Override
            public boolean isRefreshingObjects() {
                return refreshing;
            }

            @Override
            public ObjEntity getObjEntity() {
                return arc.getTargetDescriptor().getEntity();
            }

            @Override
            public ClassDescriptor getClassDescriptor() {
                return arc.getTargetDescriptor();
            }

            @Override
            public int getStatementFetchSize() {
                return statementFetchSize;
            }
        };
        this.metadataResolver = resolver;
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 29 with ClassDescriptor

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

the class DataRowUtilsIT method testMerge.

@Test
public void testMerge() throws Exception {
    createOneArtist();
    String n1 = "changed";
    String n2 = "changed again";
    Artist a1 = ObjectSelect.query(Artist.class).select(context).get(0);
    a1.setArtistName(n1);
    DataRow s2 = new DataRow(2);
    s2.put("ARTIST_NAME", n2);
    s2.put("DATE_OF_BIRTH", new java.util.Date());
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Artist");
    DataRowUtils.mergeObjectWithSnapshot(context, d, a1, s2);
    // name was modified, so it should not change during merge
    assertEquals(n1, a1.getArtistName());
    // date of birth came from database, it should be updated during merge
    assertEquals(s2.get("DATE_OF_BIRTH"), a1.getDateOfBirth());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 30 with ClassDescriptor

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

the class CayenneContext method newObject.

/**
 * Creates and registers a new Persistent object instance.
 */
@Override
public <T> T newObject(Class<T> persistentClass) {
    if (persistentClass == null) {
        throw new NullPointerException("Persistent class can't be null.");
    }
    ObjEntity entity = getEntityResolver().getObjEntity(persistentClass);
    if (entity == null) {
        throw new CayenneRuntimeException("No entity mapped for class: %s", persistentClass);
    }
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    @SuppressWarnings("unchecked") T object = (T) descriptor.createObject();
    descriptor.injectValueHolders(object);
    registerNewObject((Persistent) object, entity.getName(), descriptor);
    return object;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

Aggregations

ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)90 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)25 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)23 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)23 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)23 ObjEntity (org.apache.cayenne.map.ObjEntity)22 ArcProperty (org.apache.cayenne.reflect.ArcProperty)21 Persistent (org.apache.cayenne.Persistent)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)15 DbRelationship (org.apache.cayenne.map.DbRelationship)14 ObjRelationship (org.apache.cayenne.map.ObjRelationship)13 DataRow (org.apache.cayenne.DataRow)12 ObjectId (org.apache.cayenne.ObjectId)12 Test (org.junit.Test)12 DbAttribute (org.apache.cayenne.map.DbAttribute)10 DataObject (org.apache.cayenne.DataObject)9 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DbJoin (org.apache.cayenne.map.DbJoin)9 ArrayList (java.util.ArrayList)8