use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.
the class ClientServerChannelQueryAction method interceptObjectConversion.
private void interceptObjectConversion() {
if (!serverMetadata.isFetchingDataRows()) {
GenericResponse clientResponse = new GenericResponse();
for (response.reset(); response.next(); ) {
if (response.isList()) {
List serverObjects = response.currentList();
clientResponse.addResultList(toClientObjects(serverObjects));
} else {
clientResponse.addBatchUpdateCount(response.currentUpdateCount());
}
}
this.response = clientResponse;
}
}
use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.
the class DataDomainQueryAction method interceptRelationshipQuery.
private boolean interceptRelationshipQuery() {
if (query instanceof RelationshipQuery) {
RelationshipQuery relationshipQuery = (RelationshipQuery) query;
if (relationshipQuery.isRefreshing()) {
return !DONE;
}
ObjRelationship relationship = relationshipQuery.getRelationship(domain.getEntityResolver());
// check if we can derive target PK from FK...
if (relationship.isSourceIndependentFromTargetChange()) {
return !DONE;
}
// we can assume that there is one and only one DbRelationship as
// we previously checked that "!isSourceIndependentFromTargetChange"
DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
// FK pointing to a unique field that is a 'fake' PK (CAY-1755)...
// It is not sufficient to generate target ObjectId.
DbEntity targetEntity = dbRelationship.getTargetEntity();
if (dbRelationship.getJoins().size() < targetEntity.getPrimaryKeys().size()) {
return !DONE;
}
if (cache == null) {
return !DONE;
}
DataRow sourceRow = cache.getCachedSnapshot(relationshipQuery.getObjectId());
if (sourceRow == null) {
return !DONE;
}
ObjectId targetId = sourceRow.createTargetObjectId(relationship.getTargetEntityName(), dbRelationship);
// null id means that FK is null...
if (targetId == null) {
this.response = new GenericResponse(Collections.EMPTY_LIST);
return DONE;
}
// target id resolution (unlike source) should be polymorphic
DataRow targetRow = polymorphicRowFromCache(targetId);
if (targetRow != null) {
this.response = new GenericResponse(Collections.singletonList(targetRow));
return DONE;
}
// create a fault
if (context != null && relationship.isSourceDefiningTargetPrecenseAndType(domain.getEntityResolver())) {
// prevent passing partial snapshots to ObjectResolver per
// CAY-724.
// Create a hollow object right here and skip object conversion
// downstream
this.noObjectConversion = true;
Object object = context.findOrCreateObject(targetId);
this.response = new GenericResponse(Collections.singletonList(object));
return DONE;
}
}
return !DONE;
}
use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.
the class DataDomainQueryAction method runQuery.
private void runQuery() {
// reset
this.fullResponse = new GenericResponse();
this.response = this.fullResponse;
this.queriesByNode = null;
this.queriesByExecutedQueries = null;
// whether this is null or not will driver further decisions on how to process prefetched rows
this.prefetchResultsByPath = metadata.getPrefetchTree() != null && !metadata.isFetchingDataRows() ? new HashMap<String, List>() : null;
// categorize queries by node and by "executable" query...
query.route(this, domain.getEntityResolver(), null);
// run categorized queries
if (queriesByNode != null) {
for (Map.Entry<QueryEngine, Collection<Query>> entry : queriesByNode.entrySet()) {
QueryEngine nextNode = entry.getKey();
Collection<Query> nodeQueries = entry.getValue();
nextNode.performQueries(nodeQueries, this);
}
}
}
use of org.apache.cayenne.util.GenericResponse 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;
}
use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.
the class ClientChannelTest method testOnQuerySelectOverrideModifiedCached.
@Test
public void testOnQuerySelectOverrideModifiedCached() {
ObjEntity entity = new ObjEntity("test_entity");
entity.setClassName(MockPersistentObject.class.getName());
DataMap dataMap = new DataMap("test");
dataMap.addObjEntity(entity);
Collection<DataMap> entities = Collections.singleton(dataMap);
EntityResolver resolver = new EntityResolver(entities);
CayenneContext context = new CayenneContext();
context.setEntityResolver(resolver);
ObjectId oid = new ObjectId("test_entity", "x", "y");
MockPersistentObject o1 = new MockPersistentObject(oid);
o1.setPersistenceState(PersistenceState.MODIFIED);
context.getGraphManager().registerNode(oid, o1);
assertSame(o1, context.getGraphManager().getNode(oid));
// another object with the same GID ... we must merge it with cached and return
// cached object instead of the one fetched
MockPersistentObject o2 = new MockPersistentObject(oid);
MockClientConnection connection = new MockClientConnection(new GenericResponse(Arrays.asList(o2)));
ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
context.setChannel(channel);
QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
assertNotNull(response);
assertEquals(1, response.size());
List<?> list = response.firstList();
assertNotNull(list);
assertEquals(1, list.size());
assertTrue("Expected cached object, got: " + list, list.contains(o1));
assertSame(o1, context.getGraphManager().getNode(oid));
}
Aggregations