Search in sources :

Example 6 with ObjectId

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

the class IdCoderTest method testGetObjectId_Temp.

@Test
public void testGetObjectId_Temp() {
    IdCoder handler = new IdCoder(runtime.getChannel().getEntityResolver());
    byte[] key = new byte[] { 1, (byte) 0xD7, 10, 100 };
    ObjectId decoded = handler.getObjectId(".E1:01D70A64");
    assertEquals(new ObjectId("E1", key), decoded);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 7 with ObjectId

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

the class IdCoderTest method testGetSringId_TempWithReplacement.

@Test
public void testGetSringId_TempWithReplacement() {
    IdCoder handler = new IdCoder(runtime.getChannel().getEntityResolver());
    byte[] key = new byte[] { 5, 2, 11, 99 };
    ObjectId id = new ObjectId("E1", key);
    id.getReplacementIdMap().put("ID", 6);
    E1 e1 = new E1();
    e1.setObjectId(id);
    assertEquals("E1:6", handler.getStringId(e1));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) E1(org.apache.cayenne.lifecycle.db.E1) Test(org.junit.Test)

Example 8 with ObjectId

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

the class ClientReturnDiffFilter method isClientArc.

private boolean isClientArc(Object id, Object targetId, Object arcId) {
    ObjectId oid = (ObjectId) id;
    ObjEntity entity = resolver.getObjEntity(oid.getEntityName());
    if (!entity.isClientAllowed()) {
        return false;
    }
    if (entity.getRelationship(arcId.toString()).isRuntime()) {
        return false;
    }
    ObjectId targetOid = (ObjectId) targetId;
    ObjEntity targetEntity = resolver.getObjEntity(targetOid.getEntityName());
    if (!targetEntity.isClientAllowed()) {
        return false;
    }
    return true;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId)

Example 9 with ObjectId

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

the class DataContext method registerNewObject.

/**
 * Registers a transient object with the context, recursively registering
 * all transient persistent objects attached to this object via
 * relationships.
 * <p>
 * <i>Note that since 3.0 this method takes Object as an argument instead of
 * a {@link DataObject}.</i>
 *
 * @param object
 *            new object that needs to be made persistent.
 */
@Override
public void registerNewObject(Object object) {
    if (object == null) {
        throw new NullPointerException("Can't register null object.");
    }
    ObjEntity entity = getEntityResolver().getObjEntity((Persistent) object);
    if (entity == null) {
        throw new IllegalArgumentException("Can't find ObjEntity for Persistent class: " + object.getClass().getName() + ", class is likely not mapped.");
    }
    final Persistent persistent = (Persistent) object;
    // sanity check - maybe already registered
    if (persistent.getObjectId() != null) {
        if (persistent.getObjectContext() == this) {
            // already registered, just ignore
            return;
        } else if (persistent.getObjectContext() != null) {
            throw new IllegalStateException("Persistent is already registered with another DataContext. " + "Try using 'localObjects()' instead.");
        }
    } else {
        persistent.setObjectId(new ObjectId(entity.getName()));
    }
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    if (descriptor == null) {
        throw new IllegalArgumentException("Invalid entity name: " + entity.getName());
    }
    injectInitialValue(object);
    // now we need to find all arc changes, inject missing value holders and
    // pull in
    // all transient connected objects
    descriptor.visitProperties(new PropertyVisitor() {

        public boolean visitToMany(ToManyProperty property) {
            property.injectValueHolder(persistent);
            if (!property.isFault(persistent)) {
                Object value = property.readProperty(persistent);
                Collection<Map.Entry> collection = (value instanceof Map) ? ((Map) value).entrySet() : (Collection) value;
                Iterator<Map.Entry> it = collection.iterator();
                while (it.hasNext()) {
                    Object target = it.next();
                    if (target instanceof Persistent) {
                        Persistent targetDO = (Persistent) target;
                        // make sure it is registered
                        registerNewObject(targetDO);
                        getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
                    }
                }
            }
            return true;
        }

        public boolean visitToOne(ToOneProperty property) {
            Object target = property.readPropertyDirectly(persistent);
            if (target instanceof Persistent) {
                Persistent targetDO = (Persistent) target;
                // make sure it is registered
                registerNewObject(targetDO);
                getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
            }
            return true;
        }

        public boolean visitAttribute(AttributeProperty property) {
            return true;
        }
    });
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ObjEntity(org.apache.cayenne.map.ObjEntity) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) ResultIteratorIterator(org.apache.cayenne.util.ResultIteratorIterator) Iterator(java.util.Iterator) ResultIterator(org.apache.cayenne.ResultIterator) Collection(java.util.Collection) DataObject(org.apache.cayenne.DataObject) Map(java.util.Map) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 10 with ObjectId

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

the class DataDomainFlushAction method preprocess.

private void preprocess(DataContext context, GraphDiff changes) {
    // categorize dirty objects by state
    ObjectStore objectStore = context.getObjectStore();
    for (Object o : changesByObjectId.keySet()) {
        ObjectId id = (ObjectId) o;
        Persistent object = (Persistent) objectStore.getNode(id);
        ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
        switch(object.getPersistenceState()) {
            case PersistenceState.NEW:
                insertBucket.addDirtyObject(object, descriptor);
                break;
            case PersistenceState.MODIFIED:
                updateBucket.addDirtyObject(object, descriptor);
                break;
            case PersistenceState.DELETED:
                deleteBucket.addDirtyObject(object, descriptor);
                break;
        }
    }
    new DataDomainIndirectDiffBuilder(this).processIndirectChanges(changes);
    insertBucket.appendQueries(queries);
    // TODO: the following line depends on the "queries" collection filled by insertBucket.. Moreover it may
    // potentially remove values from the passed collection. Replace with something with fewer unobvious
    // side-effects...
    flattenedBucket.appendInserts(queries);
    updateBucket.appendQueries(queries);
    flattenedBucket.appendDeletes(queries);
    deleteBucket.appendQueries(queries);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent)

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