Search in sources :

Example 41 with ClassDescriptor

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

the class ChildDiffLoader method nodePropertyChanged.

@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
    // this change is for simple property, so no need to convert targets to
    // server
    // objects...
    Persistent object = findObject(nodeId);
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
    setExternalChange(Boolean.TRUE);
    try {
        descriptor.getProperty(property).writeProperty(object, oldValue, newValue);
    } catch (Exception e) {
        throw new CayenneRuntimeException("Error setting property: " + property, e);
    } finally {
        setExternalChange(Boolean.FALSE);
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 42 with ClassDescriptor

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

the class SQLResult method getResolvedComponents.

public List<Object> getResolvedComponents(EntityResolver resolver) {
    if (resultDescriptors == null) {
        return Collections.emptyList();
    }
    List<Object> resolvedComponents = new ArrayList<>(resultDescriptors.size());
    int offset = 0;
    for (Object component : getComponents()) {
        if (component instanceof String) {
            resolvedComponents.add(new DefaultScalarResultSegment((String) component, offset));
            offset = offset + 1;
        } else if (component instanceof EntityResult) {
            EntityResult entityResult = (EntityResult) component;
            Map<String, String> fields = entityResult.getDbFields(resolver);
            String entityName = entityResult.getEntityName();
            if (entityName == null) {
                entityName = resolver.getObjEntity(entityResult.getEntityClass()).getName();
            }
            ClassDescriptor classDescriptor = resolver.getClassDescriptor(entityName);
            resolvedComponents.add(new DefaultEntityResultSegment(classDescriptor, fields, offset));
            offset = offset + fields.size();
        } else if (component instanceof EmbeddedResult) {
            EmbeddedResult embeddedResult = (EmbeddedResult) component;
            Map<String, String> fields = embeddedResult.getFields();
            resolvedComponents.add(new DefaultEmbeddableResultSegment(embeddedResult.getEmbeddable(), fields, offset));
            offset = offset + fields.size();
        } else {
            throw new IllegalArgumentException("Unsupported result descriptor component: " + component);
        }
    }
    return resolvedComponents;
}
Also used : DefaultEmbeddableResultSegment(org.apache.cayenne.query.DefaultEmbeddableResultSegment) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 43 with ClassDescriptor

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

the class ShallowMergeOperation method merge.

public <T extends Persistent> T merge(T peerInParentContext) {
    if (peerInParentContext == null) {
        throw new IllegalArgumentException("Null peerInParentContext");
    }
    // handling of HOLLOW peer state is here for completeness... Wonder if we ever
    // have a case where it is applicable.
    int peerState = peerInParentContext.getPersistenceState();
    ObjectId id = peerInParentContext.getObjectId();
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
    GraphManager graphManager = context.getGraphManager();
    // messing up dataobjects per CAY-845.
    synchronized (graphManager) {
        T object = (T) graphManager.getNode(id);
        // merge into an existing object
        if (object == null) {
            object = (T) descriptor.createObject();
            object.setObjectContext(context);
            object.setObjectId(id);
            if (peerState == PersistenceState.HOLLOW) {
                object.setPersistenceState(PersistenceState.HOLLOW);
            } else {
                object.setPersistenceState(PersistenceState.COMMITTED);
            }
            graphManager.registerNode(id, object);
        }
        // TODO: Andrus, 1/24/2006 implement smart merge for modified objects...
        if (peerState != PersistenceState.HOLLOW && object.getPersistenceState() != PersistenceState.MODIFIED && object.getPersistenceState() != PersistenceState.DELETED) {
            descriptor.shallowMerge(peerInParentContext, object);
            if (object.getPersistenceState() == PersistenceState.HOLLOW) {
                object.setPersistenceState(PersistenceState.COMMITTED);
            }
        }
        return object;
    }
}
Also used : GraphManager(org.apache.cayenne.graph.GraphManager) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId)

Example 44 with ClassDescriptor

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

the class ObjectContextQueryAction method interceptRelationshipQuery.

protected boolean interceptRelationshipQuery() {
    if (query instanceof RelationshipQuery) {
        RelationshipQuery relationshipQuery = (RelationshipQuery) query;
        if (!relationshipQuery.isRefreshing()) {
            if (targetContext == null && relationshipQuery.getRelationship(actingContext.getEntityResolver()).isToMany()) {
                return !DONE;
            }
            ObjectId id = relationshipQuery.getObjectId();
            Object object = actingContext.getGraphManager().getNode(id);
            if (object != null) {
                ClassDescriptor descriptor = actingContext.getEntityResolver().getClassDescriptor(id.getEntityName());
                if (!descriptor.isFault(object)) {
                    ArcProperty property = (ArcProperty) descriptor.getProperty(relationshipQuery.getRelationshipName());
                    if (!property.isFault(object)) {
                        Object related = property.readPropertyDirectly(object);
                        List result;
                        // null to-one
                        if (related == null) {
                            result = new ArrayList(1);
                        } else // to-many List
                        if (related instanceof List) {
                            result = (List) related;
                        } else // to-many Set
                        if (related instanceof Set) {
                            result = new ArrayList((Set) related);
                        } else // to-many Map
                        if (related instanceof Map) {
                            result = new ArrayList(((Map) related).values());
                        } else // non-null to-one
                        {
                            result = new ArrayList(1);
                            result.add(related);
                        }
                        this.response = new ListResponse(result);
                        return DONE;
                    }
                    /**
                     * Workaround for CAY-1183. If a Relationship query is being sent
                     * from child context, we assure that local object is not NEW and
                     * relationship - unresolved (this way exception will occur). This
                     * helps when faulting objects that were committed to parent
                     * context (this), but not to database. Checking type of context's
                     * channel is the only way to ensure that we are on the top level
                     * of context hierarchy (there might be more than one-level-deep
                     * nested contexts).
                     */
                    if (((Persistent) object).getPersistenceState() == PersistenceState.NEW && !(actingContext.getChannel() instanceof BaseContext)) {
                        this.response = new ListResponse();
                        return DONE;
                    }
                }
            }
        }
    }
    return !DONE;
}
Also used : BaseContext(org.apache.cayenne.BaseContext) ArcProperty(org.apache.cayenne.reflect.ArcProperty) Set(java.util.Set) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Persistent(org.apache.cayenne.Persistent) Map(java.util.Map)

Example 45 with ClassDescriptor

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

the class PrefetchProcessorTreeBuilderIT method testBuildTreeWithPrefetches.

@Test
public void testBuildTreeWithPrefetches() {
    final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
    ObjEntity e2 = resolver.getObjEntity("Painting");
    ObjEntity e3 = resolver.getObjEntity("Gallery");
    ObjEntity e4 = resolver.getObjEntity("Exhibit");
    ObjEntity e5 = resolver.getObjEntity("ArtistExhibit");
    List<DataRow> mainRows = new ArrayList<>();
    Map<String, List<?>> extraRows = new HashMap<>();
    PrefetchTreeNode tree = new PrefetchTreeNode();
    tree.addPath(Artist.PAINTING_ARRAY.getName()).setPhantom(false);
    tree.addPath(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).dot(Gallery.EXHIBIT_ARRAY).getName()).setPhantom(false);
    tree.addPath(Artist.ARTIST_EXHIBIT_ARRAY.getName()).setPhantom(false);
    QueryMetadata metadata = new MockQueryMetadata() {

        @Override
        public ClassDescriptor getClassDescriptor() {
            return descriptor;
        }

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

        @Override
        public DbEntity getDbEntity() {
            return getObjEntity().getDbEntity();
        }

        @Override
        public DataMap getDataMap() {
            return getObjEntity().getDataMap();
        }

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

        @Override
        public boolean isResolvingInherited() {
            return true;
        }
    };
    HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
    PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, mainRows, extraRows);
    PrefetchProcessorNode n1 = builder.buildTree(tree);
    assertSame(mainRows, n1.getDataRows());
    assertSame(descriptor.getEntity(), n1.getResolver().getEntity());
    PrefetchProcessorNode n2 = (PrefetchProcessorNode) n1.getNode("paintingArray");
    assertNotNull(n2);
    assertSame(e2, n2.getResolver().getEntity());
    assertFalse(n2.isPhantom());
    assertTrue(n2.isPartitionedByParent());
    PrefetchProcessorNode n3 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery");
    assertNotNull(n3);
    assertSame(e3, n3.getResolver().getEntity());
    assertTrue(n3.isPhantom());
    assertTrue(n3.isPartitionedByParent());
    PrefetchProcessorNode n4 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery.exhibitArray");
    assertNotNull(n4);
    assertSame(e4, n4.getResolver().getEntity());
    assertFalse(n4.isPhantom());
    assertTrue(n4.isPartitionedByParent());
    PrefetchProcessorNode n5 = (PrefetchProcessorNode) n1.getNode("artistExhibitArray");
    assertNotNull(n5);
    assertSame(e5, n5.getResolver().getEntity());
    assertFalse(n5.isPhantom());
    assertTrue(n5.isPartitionedByParent());
}
Also used : MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) QueryMetadata(org.apache.cayenne.query.QueryMetadata) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataRow(org.apache.cayenne.DataRow) ObjEntity(org.apache.cayenne.map.ObjEntity) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

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