Search in sources :

Example 6 with ClassDescriptor

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

the class PostprocessVisitor method processObjectChange.

private void processObjectChange(DbRowOp dbRow) {
    if (dbRow.getChangeId().getEntityName().startsWith(ASTDbPath.DB_PREFIX)) {
        return;
    }
    DataRow dataRow = context.currentSnapshot(dbRow.getObject());
    if (dbRow.getObject() instanceof DataObject) {
        DataObject dataObject = (DataObject) dbRow.getObject();
        dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
        dataObject.setSnapshotVersion(dataRow.getVersion());
    }
    if (updatedSnapshots == null) {
        updatedSnapshots = new HashMap<>();
    }
    updatedSnapshots.put(dbRow.getObject().getObjectId(), dataRow);
    // update Map reverse relationships
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(dbRow.getChangeId().getEntityName());
    for (ArcProperty arc : descriptor.getMapArcProperties()) {
        ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
        // must resolve faults... hopefully for to-one this will not cause extra fetches...
        Object source = arc.readProperty(dbRow.getObject());
        if (source != null && !reverseArc.isFault(source)) {
            remapTarget(reverseArc, source, dbRow.getObject());
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow)

Example 7 with ClassDescriptor

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

the class EntityResolverClassDescriptorIT method testArcProperties.

@Test
public void testArcProperties() {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    resolver.getClassDescriptorMap().clearDescriptors();
    ClassDescriptor descriptor = resolver.getClassDescriptor("MtTable1");
    assertNotNull(descriptor);
    PropertyDescriptor p = descriptor.getProperty(MtTable1.TABLE2ARRAY.getName());
    assertTrue(p instanceof ArcProperty);
    ClassDescriptor target = ((ArcProperty) p).getTargetDescriptor();
    assertNotNull(target);
    assertSame(resolver.getClassDescriptor("MtTable2"), target);
    assertNotNull(((ArcProperty) p).getComplimentaryReverseArc());
    assertEquals(MtTable2.TABLE1.getName(), ((ArcProperty) p).getComplimentaryReverseArc().getName());
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) Test(org.junit.Test)

Example 8 with ClassDescriptor

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

the class EntityResolverClassDescriptorIT method testServerDescriptorFactory.

@Test
public void testServerDescriptorFactory() {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    resolver.getClassDescriptorMap().clearDescriptors();
    ClassDescriptor descriptor = mock(ClassDescriptor.class);
    ClassDescriptorFactory factory = mock(ClassDescriptorFactory.class);
    when(factory.getDescriptor("MtTable1")).thenReturn(descriptor);
    resolver.getClassDescriptorMap().addFactory(factory);
    try {
        ClassDescriptor resolved = resolver.getClassDescriptor("MtTable1");
        assertNotNull(resolved);
        resolved = ((LazyClassDescriptorDecorator) resolved).getDescriptor();
        assertSame(descriptor, resolved);
    } finally {
        resolver.getClassDescriptorMap().removeFactory(factory);
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ClassDescriptorFactory(org.apache.cayenne.reflect.ClassDescriptorFactory) Test(org.junit.Test)

Example 9 with ClassDescriptor

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

the class DataContext method objectFromDataRow.

/**
 * Creates a DataObject from DataRow.
 *
 * @see DataRow
 * @since 3.1
 */
public <T extends Persistent> T objectFromDataRow(Class<T> objectClass, DataRow dataRow) {
    ObjEntity entity = this.getEntityResolver().getObjEntity(objectClass);
    if (entity == null) {
        throw new CayenneRuntimeException("Unmapped Java class: %s", objectClass);
    }
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    List<T> list = objectsFromDataRows(descriptor, Collections.singletonList(dataRow));
    return list.get(0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 10 with ClassDescriptor

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

the class DataContext method findOrCreateObject.

/**
 * An internal version of {@link #localObject(Persistent)} that operates on
 * ObjectId instead of Persistent, and wouldn't attempt to look up an object
 * in the parent channel.
 *
 * @since 3.1
 */
Persistent findOrCreateObject(ObjectId id) {
    if (id == null) {
        throw new IllegalArgumentException("Null ObjectId");
    }
    // behavior.
    synchronized (getGraphManager()) {
        Persistent cachedObject = (Persistent) getGraphManager().getNode(id);
        // return an existing object
        if (cachedObject != null) {
            int state = cachedObject.getPersistenceState();
            // objects...
            if (state != PersistenceState.MODIFIED && state != PersistenceState.DELETED) {
                ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName());
                descriptor.injectValueHolders(cachedObject);
            }
            return cachedObject;
        }
        // create and register a hollow object
        ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName());
        Persistent localObject = (Persistent) descriptor.createObject();
        localObject.setObjectContext(this);
        localObject.setObjectId(id);
        getGraphManager().registerNode(id, localObject);
        localObject.setPersistenceState(PersistenceState.HOLLOW);
        return localObject;
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Persistent(org.apache.cayenne.Persistent)

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