Skip to content

Commit

Permalink
Clean up the API of EntityUpdate #636
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Jun 27, 2023
1 parent 546c585 commit 8eed02a
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected <T extends DataObject> void doExecute(UpdateContext<T> context) {

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

T o = (T) u.getMergedTo();
T o = (T) u.getTargetObject();
if (o != null && seen.add(o.getObjectId())) {
objects.add(o);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ protected <T extends DataObject> void delete(T o) {
protected <T extends DataObject> void create(UpdateContext<T> context, ObjectRelator relator, EntityUpdate<T> update) {

ObjectContext objectContext = CayenneUpdateStartStage.cayenneContext(context);
DataObject o = objectContext.newObject(context.getType());
T o = objectContext.newObject(context.getType());

Map<String, Object> idByAgAttribute = update.getId();
Map<String, Object> idParts = update.getIdParts();

// set explicit ID
if (idByAgAttribute != null) {
if (!idParts.isEmpty()) {

ObjEntity objEntity = objectContext.getEntityResolver().getObjEntity(context.getType());
DbEntity dbEntity = objEntity.getDbEntity();
AgEntity agEntity = context.getEntity().getAgEntity();

Map<DbAttribute, Object> idByDbAttribute = mapToDbAttributes(agEntity, idByAgAttribute);
Map<DbAttribute, Object> idByDbAttribute = mapToDbAttributes(agEntity, idParts);

// need to make an additional check that the AgId is unique
checkExisting(objectContext, agEntity, idByDbAttribute, idByAgAttribute);
checkExisting(objectContext, agEntity, idByDbAttribute, idParts);

if (isPrimaryKey(dbEntity, idByDbAttribute.keySet())) {
createSingleFromPk(objEntity, idByDbAttribute, o);
} else {
createSingleFromIdValues(objEntity, idByDbAttribute, idByAgAttribute, o);
createSingleFromIdValues(objEntity, idByDbAttribute, idParts, o);
}
}

Expand Down Expand Up @@ -232,10 +232,10 @@ private boolean isPrimaryKey(DbEntity entity, Collection<DbAttribute> maybePk) {
return countPk >= pkSize;
}

private <T extends DataObject> void mergeChanges(EntityUpdate<T> entityUpdate, DataObject o, ObjectRelator relator) {
private <T extends DataObject> void mergeChanges(EntityUpdate<T> entityUpdate, T o, ObjectRelator relator) {

// attributes
for (Map.Entry<String, Object> e : entityUpdate.getValues().entrySet()) {
for (Map.Entry<String, Object> e : entityUpdate.getAttributes().entrySet()) {
o.writeProperty(e.getKey(), e.getValue());
}

Expand Down Expand Up @@ -302,7 +302,7 @@ public void removeUpdateForRelatedObject(DataObject relatedObject) {
}
}

entityUpdate.setMergedTo(o);
entityUpdate.setTargetObject(o);
}

private boolean allElementsNull(Collection<?> elements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ private <T> void fillIdsFromMeaningfulPk(UpdateContext<T> context) {
private void fillIdsFromMappedPk(UpdateContext<?> context, String propertyName) {

for (EntityUpdate<?> u : context.getUpdates()) {
Object pk = u.getValues().get(propertyName);
Object pk = u.getAttributes().get(propertyName);

// Unlike id parts mapped from the DB layer, this one is tracked by its normal property name
u.getOrCreateId().putIfAbsent(propertyName, pk);
u.addIdPartIfAbsent(propertyName, pk);
}
}

Expand All @@ -183,7 +183,7 @@ private void fillIdsFromRelatedId(UpdateContext<?> context, String agRelationshi

// if size != 1 : throw?
if (pk != null && pk.size() == 1) {
u.getOrCreateId().putIfAbsent(ASTDbPath.DB_PREFIX + outgoingJoin.getSourceName(), pk.iterator().next());
u.addIdPartIfAbsent(ASTDbPath.DB_PREFIX + outgoingJoin.getSourceName(), pk.iterator().next());
}
}
}
Expand All @@ -205,7 +205,7 @@ private void fillIdsFromRelatedIds(UpdateContext<?> context, String agRelationsh
Map<String, Object> pkMap = (Map<String, Object>) pk.iterator().next();
for (DbJoin join : outgoingJoins) {
// 'getSourceName' and 'getTargetName' assumes AgEntity's id attribute name is based on DbAttribute name
u.getOrCreateId().putIfAbsent(ASTDbPath.DB_PREFIX + join.getSourceName(), pkMap.get(join.getTargetName()));
u.addIdPartIfAbsent(ASTDbPath.DB_PREFIX + join.getSourceName(), pkMap.get(join.getTargetName()));
}
}
}
Expand All @@ -226,7 +226,7 @@ private <T> void fillIdsFromExplicitContextId(UpdateContext<T> context) {

AgEntity<T> entity = context.getEntity().getAgEntity();
EntityUpdate<T> u = context.getFirst();
u.getOrCreateId().putAll(context.getId().asMap(entity));
u.setIdParts(context.getId().asMap(entity));
}
}

Expand All @@ -249,8 +249,7 @@ private <T> void fillIdsFromParentId(UpdateContext<T> context) {
}

for (EntityUpdate<T> u : context.getUpdates()) {
Map<String, Object> updateId = u.getOrCreateId();
idTranslated.forEach(updateId::putIfAbsent);
idTranslated.forEach(u::addIdPartIfAbsent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public DataResponse<E3> create(@Context UriInfo uriInfo, EntityUpdate<E3> update

int e2Id = Cayenne.intPKForObject(e3Response.getData().get(0));

EntityUpdate<E2> e2Update = update.getRelatedUpdate(E3.E2.getName());
EntityUpdate<E2> e2Update = update.getToOne(E3.E2.getName());
if (e2Update != null) {
AgJaxrs
.create(E2.class, config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CreateAuthorizerIT extends MainDbTest {
static final MainModelTester tester = tester(Resource.class)
.entities(E2.class, E3.class, E4.class)
.agCustomizer(ab -> ab
.entityOverlay(AgEntity.overlay(E2.class).createAuthorizer(u -> !"blocked".equals(u.getValues().get("name"))))
.entityOverlay(AgEntity.overlay(E2.class).createAuthorizer(u -> !"blocked".equals(u.getAttributes().get("name"))))
).build();

@Test
Expand Down Expand Up @@ -100,7 +100,7 @@ public SimpleResponse putE2RequestAndStackFilter(

return AgJaxrs.createOrUpdate(E2.class, config)
.clientParams(uriInfo.getQueryParameters())
.createAuthorizer(E2.class, u -> !name.equals(u.getValues().get("name")))
.createAuthorizer(E2.class, u -> !name.equals(u.getAttributes().get("name")))
.sync(updates);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class UpdateAuthorizerIT extends MainDbTest {
static final MainModelTester tester = tester(Resource.class)
.entities(E2.class, E3.class, E4.class)
.agCustomizer(ab -> ab
.entityOverlay(AgEntity.overlay(E2.class).updateAuthorizer((o, u) -> o.getName().equals(u.getValues().get("name"))))
.entityOverlay(AgEntity.overlay(E2.class).updateAuthorizer((o, u) -> o.getName().equals(u.getAttributes().get("name"))))
).build();

@Test
Expand Down Expand Up @@ -116,7 +116,7 @@ public SimpleResponse putE2RequestAndStackFilter(

return AgJaxrs
.createOrUpdate(E2.class, config)
.updateAuthorizer(E2.class, (o, u) -> !name.equals(u.getValues().get("name")))
.updateAuthorizer(E2.class, (o, u) -> !name.equals(u.getAttributes().get("name")))
.sync(updates);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;


public class UpdateRequestParserIT extends MainDbTest {
Expand All @@ -35,18 +34,20 @@ public void parse_Nested_ToMany() {
assertEquals(1, updates.size());

EntityUpdate<E2> e2 = updates.get(0);
assertNull(e2.getId());
assertEquals(Map.of("name", "a"), e2.getValues());
assertEquals(0, e2.getIdParts().size());
assertEquals(Map.of("name", "a"), e2.getAttributes());
assertEquals(Map.of(), e2.getRelatedIds());
assertEquals(1, e2.getRelatedUpdates().size());
assertEquals(2, e2.getRelatedUpdates().get("e3s").size());

EntityUpdate<E3> e31 = (EntityUpdate<E3>) e2.getRelatedUpdates().get("e3s").get(0);
assertEquals(Map.of("db:id_", 2), e31.getId());
assertEquals(Map.of("name", "n2"), e31.getValues());

EntityUpdate<E3> e32 = (EntityUpdate<E3>) e2.getRelatedUpdates().get("e3s").get(1);
assertEquals(Map.of("db:id_", 1), e32.getId());
assertEquals(Map.of("name", "n1"), e32.getValues());
assertEquals(0, e2.getToOnes().size());
assertEquals(1, e2.getToManys().size());
assertEquals(2, e2.getToMany("e3s").size());

List<EntityUpdate<E3>> e3s1 = e2.getToMany("e3s");
EntityUpdate<E3> e31 = e3s1.get(0);
assertEquals(Map.of("db:id_", 2), e31.getIdParts());
assertEquals(Map.of("name", "n2"), e31.getAttributes());

EntityUpdate<E3> e32 = e3s1.get(1);
assertEquals(Map.of("db:id_", 1), e32.getIdParts());
assertEquals(Map.of("name", "n1"), e32.getAttributes());
}
}
Loading

0 comments on commit 8eed02a

Please sign in to comment.