Search in sources :

Example 11 with Persistent

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

the class ObjectResolver method objectsFromDataRows.

/**
 * Converts rows to objects.
 * <p>
 * Synchronization note. This method requires EXTERNAL synchronization on
 * ObjectStore and DataRowStore.
 * </p>
 */
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) {
        // nulls are possible here since 3.0 for some varieties of EJBQL,
        // simple example of this: "select p.toGallery+ from Painting p" where toGallery is null.
        results.add(objectFromDataRow(row));
    }
    // now deal with snapshots
    cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
    return results;
}
Also used : ArrayList(java.util.ArrayList) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 12 with Persistent

use of org.apache.cayenne.Persistent 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 13 with Persistent

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

the class ObjectStore method objectsUnregistered.

/**
 * Evicts a collection of DataObjects from the ObjectStore, invalidates the underlying
 * cache snapshots. Changes objects state to TRANSIENT. This method can be used for
 * manual cleanup of Cayenne cache.
 */
// this method is exactly the same as "objectsInvalidated", only additionally it
// throws out registered objects
public synchronized void objectsUnregistered(Collection objects) {
    if (objects.isEmpty()) {
        return;
    }
    Collection<ObjectId> ids = new ArrayList<>(objects.size());
    for (Object object1 : objects) {
        Persistent object = (Persistent) object1;
        ObjectId id = object.getObjectId();
        // remove object but not snapshot
        objectMap.remove(id);
        changes.remove(id);
        ids.add(id);
        object.setObjectContext(null);
        object.setPersistenceState(PersistenceState.TRANSIENT);
    }
    // and keep unregister local even for non-nested DC?
    if (getDataRowCache() != null) {
        // send an event for removed snapshots
        getDataRowCache().processSnapshotChanges(this, Collections.<ObjectId, DataRow>emptyMap(), Collections.<ObjectId>emptyList(), ids, Collections.<ObjectId>emptyList());
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent)

Example 14 with Persistent

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

the class ObjectStore method postprocessAfterPhantomCommit.

/**
 * @since 1.2
 */
void postprocessAfterPhantomCommit() {
    for (Object id : changes.keySet()) {
        Persistent object = objectMap.get(id);
        // assume that no new or deleted objects are present (as otherwise commit
        // wouldn't have been phantom).
        object.setPersistenceState(PersistenceState.COMMITTED);
    }
    // clear caches
    this.changes.clear();
}
Also used : DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent)

Example 15 with Persistent

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

the class ObjectStore method processIdChange.

void processIdChange(Object nodeId, Object newId) {
    Persistent object = objectMap.remove(nodeId);
    if (object != null) {
        object.setObjectId((ObjectId) newId);
        objectMap.put(newId, object);
        ObjectDiff change = changes.remove(nodeId);
        if (change != null) {
            changes.put(newId, change);
        }
    }
}
Also used : Persistent(org.apache.cayenne.Persistent)

Aggregations

Persistent (org.apache.cayenne.Persistent)88 ObjectId (org.apache.cayenne.ObjectId)38 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)17 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 DataObject (org.apache.cayenne.DataObject)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)10 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)10 DataRow (org.apache.cayenne.DataRow)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6 List (java.util.List)5