Search in sources :

Example 6 with Persistent

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

the class DataDomainQueryAction method interceptRefreshQuery.

/**
 * @since 3.0
 */
private boolean interceptRefreshQuery() {
    if (query instanceof RefreshQuery) {
        RefreshQuery refreshQuery = (RefreshQuery) query;
        if (refreshQuery.isRefreshAll()) {
            // not sending any events - peer contexts will not get refreshed
            if (domain.getSharedSnapshotCache() != null) {
                domain.getSharedSnapshotCache().clear();
            } else {
                // remove snapshots from local ObjectStore only
                context.getObjectStore().getDataRowCache().clear();
            }
            context.getQueryCache().clear();
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
        Collection<Persistent> objects = (Collection<Persistent>) refreshQuery.getObjects();
        if (objects != null && !objects.isEmpty()) {
            Collection<ObjectId> ids = new ArrayList<>(objects.size());
            for (final Persistent object : objects) {
                ids.add(object.getObjectId());
            }
            if (domain.getSharedSnapshotCache() != null) {
                // send an event for removed snapshots
                domain.getSharedSnapshotCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
            } else {
                // remove snapshots from local ObjectStore only
                context.getObjectStore().getDataRowCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
            }
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
        // usually does a cascading refresh
        if (refreshQuery.getQuery() != null) {
            Query cachedQuery = refreshQuery.getQuery();
            String cacheKey = cachedQuery.getMetaData(context.getEntityResolver()).getCacheKey();
            context.getQueryCache().remove(cacheKey);
            this.response = domain.onQuery(context, cachedQuery);
            return DONE;
        }
        // 4. refresh groups...
        if (refreshQuery.getGroupKeys() != null && refreshQuery.getGroupKeys().length > 0) {
            String[] groups = refreshQuery.getGroupKeys();
            for (String group : groups) {
                domain.getQueryCache().removeGroup(group);
            }
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
    }
    return !DONE;
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) PrefetchSelectQuery(org.apache.cayenne.query.PrefetchSelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) GenericResponse(org.apache.cayenne.util.GenericResponse) ObjectId(org.apache.cayenne.ObjectId) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Persistent(org.apache.cayenne.Persistent)

Example 7 with Persistent

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

the class DataDomainUpdateBucket method appendQueriesInternal.

@Override
void appendQueriesInternal(Collection<Query> queries) {
    DataDomainDBDiffBuilder diffBuilder = new DataDomainDBDiffBuilder();
    DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
    for (DbEntity dbEntity : dbEntities) {
        Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
        Map<Object, Query> batches = new LinkedHashMap<>();
        for (DbEntityClassDescriptor descriptor : descriptors) {
            ObjEntity entity = descriptor.getEntity();
            diffBuilder.reset(descriptor);
            qualifierBuilder.reset(descriptor);
            boolean isRootDbEntity = entity.getDbEntity() == dbEntity;
            for (Persistent o : objectsByDescriptor.get(descriptor.getClassDescriptor())) {
                ObjectDiff diff = parent.objectDiff(o.getObjectId());
                Map<String, Object> snapshot = diffBuilder.buildDBDiff(diff);
                // check whether MODIFIED object has real db-level modifications
                if (snapshot == null) {
                    continue;
                }
                // after we filtered out "fake" modifications, check if an
                // attempt is made to modify a read only entity
                checkReadOnly(entity);
                Map<String, Object> qualifierSnapshot = qualifierBuilder.createQualifierSnapshot(diff);
                // organize batches by the updated columns + nulls in qualifier
                Set<String> snapshotSet = snapshot.keySet();
                Set<String> nullQualifierNames = new HashSet<>();
                for (Map.Entry<String, Object> entry : qualifierSnapshot.entrySet()) {
                    if (entry.getValue() == null) {
                        nullQualifierNames.add(entry.getKey());
                    }
                }
                List<Set<String>> batchKey = Arrays.asList(snapshotSet, nullQualifierNames);
                UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey);
                if (batch == null) {
                    batch = new UpdateBatchQuery(dbEntity, qualifierBuilder.getAttributes(), updatedAttributes(dbEntity, snapshot), nullQualifierNames, 10);
                    batch.setUsingOptimisticLocking(qualifierBuilder.isUsingOptimisticLocking());
                    batches.put(batchKey, batch);
                }
                batch.add(qualifierSnapshot, snapshot, o.getObjectId());
                // update replacement id with meaningful PK changes
                if (isRootDbEntity) {
                    Map<String, Object> replacementId = o.getObjectId().getReplacementIdMap();
                    for (DbAttribute pk : dbEntity.getPrimaryKeys()) {
                        String name = pk.getName();
                        if (snapshot.containsKey(name) && !replacementId.containsKey(name)) {
                            replacementId.put(name, snapshot.get(name));
                        }
                    }
                }
            }
        }
        queries.addAll(batches.values());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Query(org.apache.cayenne.query.Query) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) Persistent(org.apache.cayenne.Persistent) LinkedHashMap(java.util.LinkedHashMap) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with Persistent

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

the class HierarchicalObjectResolverNode method objectsFromDataRows.

@Override
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) {
        // determine entity to use
        ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
        // not using DataRow.createObjectId for performance reasons -
        // ObjectResolver
        // has all needed metadata already cached.
        ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
        Persistent object = objectFromDataRow(row, anId, classDescriptor);
        if (object == null) {
            throw new CayenneRuntimeException("Can't build Object from row: %s", row);
        }
        // keep the dupe objects (and data rows) around, as there maybe an
        // attached
        // joint prefetch...
        results.add(object);
        node.getParentAttachmentStrategy().linkToParent(row, object);
    }
    // now deal with snapshots
    // TODO: refactoring: dupes will clutter the lists and cause extra
    // processing...
    // removal of dupes happens only downstream, as we need the objects
    // matching
    // fetched rows for joint prefetch resolving... maybe pushback unique
    // and
    // non-unique lists to the "node", instead of returning a single list
    // from this
    // method
    cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
    return results;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) DataRow(org.apache.cayenne.DataRow)

Example 9 with Persistent

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

the class JoinedIdParentAttachementStrategy method linkToParent.

public void linkToParent(DataRow row, Persistent object) {
    Persistent parentObject = null;
    for (ObjEntity entity : sourceEntities) {
        if (entity.isAbstract()) {
            continue;
        }
        ObjectId id = node.getResolver().createObjectId(row, entity, relatedIdPrefix);
        if (id == null) {
            throw new CayenneRuntimeException("Can't build ObjectId from row: %s, entity: %s, prefix: %s", row, entity.getName(), relatedIdPrefix);
        }
        parentObject = (Persistent) graphManager.getNode(id);
        if (parentObject != null) {
            break;
        }
    }
    node.linkToParent(object, parentObject);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent)

Example 10 with Persistent

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

the class ObjectDiff method getArcSnapshotValue.

ObjectId getArcSnapshotValue(String propertyName) {
    Object value = arcSnapshot != null ? arcSnapshot.get(propertyName) : null;
    if (value instanceof Fault) {
        Persistent target = (Persistent) ((Fault) value).resolveFault(object, propertyName);
        value = target != null ? target.getObjectId() : null;
        arcSnapshot.put(propertyName, value);
    }
    return (ObjectId) value;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Fault(org.apache.cayenne.Fault) 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