Skip to content

Commit

Permalink
Cayenne entity ID part represented by a DB column to be prefixed wit…
Browse files Browse the repository at this point in the history
…h "db:" #521

.. using ID reader to fill AgObjectId
  • Loading branch information
andrus committed Jan 2, 2022
1 parent fd9a80d commit 3a165ae
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import io.agrest.CompoundObjectId;
import io.agrest.EntityUpdate;
import io.agrest.NestedResourceEntity;
import io.agrest.ResourceEntity;
import io.agrest.SimpleObjectId;
import io.agrest.ToManyResourceEntity;
import io.agrest.processor.Processor;
import io.agrest.processor.ProcessorOutcome;
import io.agrest.property.PropertyReader;
import io.agrest.runtime.processor.update.UpdateContext;
import org.apache.cayenne.DataObject;
import org.apache.cayenne.Fault;
Expand Down Expand Up @@ -48,8 +50,6 @@ protected <T extends DataObject> void doExecute(UpdateContext<T> context) {
// 'seen' is for a case of multiple updates per object in a request
Set<ObjectId> seen = new HashSet<>();

Map<String, NestedResourceEntity<?>> children = context.getEntity().getChildren();

for (EntityUpdate<T> u : context.getUpdates()) {

T o = (T) u.getMergedTo();
Expand All @@ -58,17 +58,21 @@ protected <T extends DataObject> void doExecute(UpdateContext<T> context) {

// TODO: child entities should be seeded via a special NestedDataResolver to read from parent
// instead of manually traversing objects
assignChildrenToParent(o, children);
assignChildrenToParent(o, context.getEntity());
}
}

context.getEntity().setResult(objects);
}
}

protected void assignChildrenToParent(DataObject root, Map<String, NestedResourceEntity<?>> children) {
protected void assignChildrenToParent(DataObject root, ResourceEntity<?> entity) {

PropertyReader idReader = entity.getAgEntity().getIdReader();
Map<String, NestedResourceEntity<?>> children = entity.getChildren();

if (!children.isEmpty()) {

for (Map.Entry<String, NestedResourceEntity<?>> e : children.entrySet()) {
NestedResourceEntity childEntity = e.getValue();

Expand All @@ -78,21 +82,22 @@ protected void assignChildrenToParent(DataObject root, Map<String, NestedResourc
}

// TODO: getIdSnapshot() will not prefix keys with "db". Must use AgIdParts to resolve the ID
AgObjectId id = root.getObjectId().getIdSnapshot().size() > 1
? new CompoundObjectId(root.getObjectId().getIdSnapshot())
: new SimpleObjectId(root.getObjectId().getIdSnapshot().values().iterator().next());
Map<String, Object> idMap = (Map<String, Object>) idReader.value(root);
AgObjectId id = idMap.size() > 1
? new CompoundObjectId(idMap)
: new SimpleObjectId(idMap.values().iterator().next());

if (childEntity instanceof ToManyResourceEntity) {
List r = (List) result;

((ToManyResourceEntity) childEntity).addResultList(id, r);
for (Object ro : r) {
assignChildrenToParent((DataObject) ro, childEntity.getChildren());
assignChildrenToParent((DataObject) ro, childEntity);
}

} else {
childEntity.addResult(id, result);
assignChildrenToParent((DataObject) result, childEntity.getChildren());
assignChildrenToParent((DataObject) result, childEntity);
}
}
}
Expand Down

0 comments on commit 3a165ae

Please sign in to comment.