Search in sources :

Example 21 with ObjectId

use of org.apache.cayenne.ObjectId 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 22 with ObjectId

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

the class ObjectStore method processSnapshotEvent.

/**
 * @since 1.2
 */
synchronized void processSnapshotEvent(SnapshotEvent event) {
    Map<ObjectId, DataRow> modifiedDiffs = event.getModifiedDiffs();
    if (modifiedDiffs != null && !modifiedDiffs.isEmpty()) {
        for (Map.Entry<ObjectId, DataRow> entry : modifiedDiffs.entrySet()) {
            processUpdatedSnapshot(entry.getKey(), entry.getValue());
        }
    }
    Collection<ObjectId> deletedIDs = event.getDeletedIds();
    if (deletedIDs != null && !deletedIDs.isEmpty()) {
        for (ObjectId deletedID : deletedIDs) {
            processDeletedID(deletedID);
        }
    }
    processInvalidatedIDs(event.getInvalidatedIds());
    processIndirectlyModifiedIDs(event.getIndirectlyModifiedIds());
    // TODO: andrus, 3/28/2006 - 'SnapshotEventDecorator' serves as a bridge (or
    // rather a noop wrapper) between old snapshot events and new GraphEvents. Once
    // SnapshotEvents are replaced with GraphEvents (in 2.0) we won't need it
    GraphDiff diff = new SnapshotEventDecorator(event);
    ObjectContext originatingContext = (event.getPostedBy() instanceof ObjectContext) ? (ObjectContext) event.getPostedBy() : null;
    context.fireDataChannelChanged(originatingContext, diff);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) DataRow(org.apache.cayenne.DataRow) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with ObjectId

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

the class ObjectStoreGraphDiff method preprocess.

private void preprocess(ObjectStore objectStore) {
    Map<Object, ObjectDiff> changes = getChangesByObjectId();
    if (!changes.isEmpty()) {
        for (Entry<Object, ObjectDiff> entry : changes.entrySet()) {
            ObjectId id = (ObjectId) entry.getKey();
            Persistent object = (Persistent) objectStore.getNode(id);
            // address manual id override.
            ObjectId objectId = object.getObjectId();
            if (!id.equals(objectId)) {
                if (objectId != null) {
                    Map<String, Object> replacement = id.getReplacementIdMap();
                    replacement.clear();
                    replacement.putAll(objectId.getIdSnapshot());
                }
                object.setObjectId(id);
            }
        }
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent)

Example 24 with ObjectId

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

the class EJBQLPathAnaliserTranslator method processParameter.

private void processParameter(String boundName, EJBQLExpression expression) {
    Object object = context.getBoundParameter(boundName);
    Map<?, ?> map = null;
    if (object instanceof Persistent) {
        map = ((Persistent) object).getObjectId().getIdSnapshot();
    } else if (object instanceof ObjectId) {
        map = ((ObjectId) object).getIdSnapshot();
    } else if (object instanceof Map) {
        map = (Map<?, ?>) object;
    }
    if (map != null) {
        if (map.size() == 1) {
            context.rebindParameter(boundName, map.values().iterator().next());
        } else {
            addMultiColumnOperand(EJBQLMultiColumnOperand.getObjectOperand(context, map));
            return;
        }
    }
    if (object != null) {
        context.append(" #bind($").append(boundName).append(")");
    } else {
        String type = null;
        Node parent = ((SimpleNode) expression).jjtGetParent();
        context.pushMarker("@processParameter", true);
        EJBQLPathAnaliserTranslator translator = new EJBQLPathAnaliserTranslator(context);
        parent.visit(translator);
        translator.visitPath(parent, parent.getChildrenCount());
        String id = translator.idPath;
        if (id != null) {
            ClassDescriptor descriptor = context.getEntityDescriptor(id);
            if (descriptor == null) {
                throw new EJBQLException("Unmapped id variable: " + id);
            }
            String pathChunk = translator.lastPathComponent;
            PropertyDescriptor property = descriptor.getProperty(pathChunk);
            if (property instanceof AttributeProperty) {
                String atrType = ((AttributeProperty) property).getAttribute().getType();
                type = TypesMapping.getSqlNameByType(TypesMapping.getSqlTypeByJava(atrType));
            }
        }
        context.popMarker();
        if (type == null) {
            type = "VARCHAR";
        }
        // this is a hack to prevent execptions on DB's like Derby for
        // expressions
        // "X = NULL". The 'VARCHAR' parameter is totally bogus, but seems
        // to work on
        // all tested DB's... Also note what JPA spec, chapter 4.11 says:
        // "Comparison
        // or arithmetic operations with a NULL value always yield an
        // unknown value."
        // TODO: andrus 6/28/2007 Ideally we should track the type of the
        // current
        // expression to provide a meaningful type.
        context.append(" #bind($").append(boundName).append(" '" + type + "')");
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ObjectId(org.apache.cayenne.ObjectId) SimpleNode(org.apache.cayenne.ejbql.parser.SimpleNode) Node(org.apache.cayenne.ejbql.parser.Node) AggregateConditionNode(org.apache.cayenne.ejbql.parser.AggregateConditionNode) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) SimpleNode(org.apache.cayenne.ejbql.parser.SimpleNode) Map(java.util.Map)

Example 25 with ObjectId

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

the class DeletedDiffProcessor method nodeRemoved.

@Override
public void nodeRemoved(Object nodeId) {
    ObjectId id = (ObjectId) nodeId;
    final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
    // TODO: rewrite with SelectById query after Cayenne upgrade
    ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
    QueryResponse result = channel.onQuery(null, query);
    @SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
    if (rows.isEmpty()) {
        LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
        return;
    }
    final DataRow row = rows.get(0);
    ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
    final CommitLogEntity entity = entityFactory.getEntity(id);
    descriptor.visitProperties(new PropertyVisitor() {

        @Override
        public boolean visitAttribute(AttributeProperty property) {
            if (!entity.isIncluded(property.getName())) {
                return true;
            }
            Object value;
            if (entity.isConfidential(property.getName())) {
                value = Confidential.getInstance();
            } else {
                String key = property.getAttribute().getDbAttributeName();
                value = row.get(key);
            }
            if (value != null) {
                objectChangeSet.attributeChanged(property.getName(), value, null);
            }
            return true;
        }

        @Override
        public boolean visitToOne(ToOneProperty property) {
            // TODO record FK changes?
            return true;
        }

        @Override
        public boolean visitToMany(ToManyProperty property) {
            return true;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) QueryResponse(org.apache.cayenne.QueryResponse) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

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