Search in sources :

Example 1 with DataObject

use of org.apache.cayenne.DataObject 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 2 with DataObject

use of org.apache.cayenne.DataObject 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 3 with DataObject

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

the class ObjectStore method processDeletedID.

/**
 * Requires external synchronization.
 *
 * @since 1.2
 */
void processDeletedID(ObjectId nodeId) {
    // access object map directly - the method should be called in a synchronized context...
    Persistent object = objectMap.get(nodeId);
    if (object != null) {
        DataObject dataObject = (object instanceof DataObject) ? (DataObject) object : null;
        DataContextDelegate delegate;
        switch(object.getPersistenceState()) {
            case PersistenceState.COMMITTED:
            case PersistenceState.HOLLOW:
            case PersistenceState.DELETED:
                // consult delegate
                delegate = context.nonNullDelegate();
                if (dataObject == null || delegate.shouldProcessDelete(dataObject)) {
                    objectMap.remove(nodeId);
                    changes.remove(nodeId);
                    // setting DataContext to null will also set
                    // state to transient
                    object.setObjectContext(null);
                    if (dataObject != null) {
                        delegate.finishedProcessDelete(dataObject);
                    }
                }
                break;
            case PersistenceState.MODIFIED:
                // consult delegate
                delegate = context.nonNullDelegate();
                if (dataObject != null && delegate.shouldProcessDelete(dataObject)) {
                    object.setPersistenceState(PersistenceState.NEW);
                    changes.remove(nodeId);
                    registerNode(nodeId, object);
                    nodeCreated(nodeId);
                    delegate.finishedProcessDelete(dataObject);
                }
                break;
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent)

Example 4 with DataObject

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateNotEqualID.

@Test
public void testProcessTemplateNotEqualID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))";
    Map<String, Object> idMap = new HashMap<>();
    idMap.put("ID_COLUMN1", 3);
    idMap.put("ID_COLUMN2", "aaa");
    ObjectId id = new ObjectId("T", idMap);
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(id);
    Map<String, Object> map = Collections.singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql());
    assertEquals(2, compiled.getBindings().length);
    assertBindingValue(3, compiled.getBindings()[0]);
    assertBindingValue("aaa", compiled.getBindings()[1]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 5 with DataObject

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

the class SimpleIdIncrementalFaultListIT method testIterator.

@Test
public void testIterator() throws Exception {
    SimpleIdIncrementalFaultList<?> list = prepareList(6);
    Iterator<?> it = list.iterator();
    int counter = 0;
    while (it.hasNext()) {
        Object obj = it.next();
        assertNotNull(obj);
        assertTrue(obj instanceof DataObject);
        // iterator must be resolved page by page
        int expectedResolved = list.pageIndex(counter) * list.getPageSize() + list.getPageSize();
        if (expectedResolved > list.size()) {
            expectedResolved = list.size();
        }
        assertEquals(list.size() - expectedResolved, list.getUnfetchedObjects());
        counter++;
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2