From 4d9ef65656e72f812c3397e25bdba9d9f828942c Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Fri, 7 Jan 2022 09:46:40 +0200 Subject: [PATCH] Cayenne entity ID part represented by a DB column to be prefixed with "db:" #521 .. deprecating AgObjectId.get() --- .../src/main/java/io/agrest/AgObjectId.java | 2 + .../main/java/io/agrest/SimpleObjectId.java | 82 ++++++++++--------- .../src/test/java/io/agrest/GET_PojoIT.java | 3 +- .../AnnotationsAgEntityCompilerTest.java | 2 +- .../test/java/io/agrest/pojo/model/P8.java | 11 +++ .../agrest/pojo/runtime/PojoFetchStage.java | 4 +- 6 files changed, 61 insertions(+), 43 deletions(-) diff --git a/agrest-engine/src/main/java/io/agrest/AgObjectId.java b/agrest-engine/src/main/java/io/agrest/AgObjectId.java index 591b0b55b..59cffe95d 100644 --- a/agrest-engine/src/main/java/io/agrest/AgObjectId.java +++ b/agrest-engine/src/main/java/io/agrest/AgObjectId.java @@ -18,7 +18,9 @@ public interface AgObjectId { /** * @return Original ID value, that was used to create this wrapper ID + * @deprecated since 5.0, as there should be no reason to unwrap the ID implementation */ + @Deprecated Object get(); Map asMap(AgEntity entity); diff --git a/agrest-engine/src/main/java/io/agrest/SimpleObjectId.java b/agrest-engine/src/main/java/io/agrest/SimpleObjectId.java index acd6168bc..b10d20ba3 100644 --- a/agrest-engine/src/main/java/io/agrest/SimpleObjectId.java +++ b/agrest-engine/src/main/java/io/agrest/SimpleObjectId.java @@ -10,58 +10,60 @@ /** * A single value id. - * + * * @since 1.24 */ public class SimpleObjectId extends BaseObjectId { - private final Object id; + private final Object id; - public SimpleObjectId(Object id) { - this.id = Objects.requireNonNull(id); - } + public SimpleObjectId(Object id) { + this.id = Objects.requireNonNull(id); + } - @Override - public Object get(String attributeName) { - return get(); - } + @Override + public Object get(String attributeName) { + // TODO: check for valid attribute name? + return id; + } - @Override - public Object get() { - return id; - } + @Deprecated + @Override + public Object get() { + return id; + } - @Override - public int size() { - return 1; - } + @Override + public int size() { + return 1; + } - @Override - protected Map asMap(Collection idAttributes) { - AgIdPart idAttribute = idAttributes.iterator().next(); - return Collections.singletonMap(idAttribute.getName(), Normalizer.normalize(id, idAttribute.getType())); - } + @Override + protected Map asMap(Collection idAttributes) { + AgIdPart idAttribute = idAttributes.iterator().next(); + return Collections.singletonMap(idAttribute.getName(), Normalizer.normalize(id, idAttribute.getType())); + } - @Override - public String toString() { - return id.toString(); - } + @Override + public String toString() { + return id.toString(); + } - @Override - public boolean equals(Object object) { - if (this == object) { - return true; - } + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } - if (!(object instanceof SimpleObjectId)) { - return false; - } + if (!(object instanceof SimpleObjectId)) { + return false; + } - return id.equals(((SimpleObjectId)object).get()); - } + return Objects.equals(id, ((SimpleObjectId) object).id); + } - @Override - public int hashCode() { - return id.hashCode(); - } + @Override + public int hashCode() { + return id.hashCode(); + } } diff --git a/agrest-engine/src/test/java/io/agrest/GET_PojoIT.java b/agrest-engine/src/test/java/io/agrest/GET_PojoIT.java index 9fd3c1c04..197f678d5 100644 --- a/agrest-engine/src/test/java/io/agrest/GET_PojoIT.java +++ b/agrest-engine/src/test/java/io/agrest/GET_PojoIT.java @@ -144,6 +144,7 @@ public void testMapBy() { public void testCollectionAttributes() { P8 o1 = new P8(); + o1.setId(1); o1.setBooleans(Arrays.asList(true, false)); o1.setCharacters(Arrays.asList('a', 'b', 'c')); o1.setDoubles(Arrays.asList(1., 2.5, 3.5)); @@ -157,7 +158,7 @@ public void testCollectionAttributes() { tester.target("/pojo/p8/1").get() .wasOk() - .bodyEquals(1, "{" + + .bodyEquals(1, "{\"id\":1," + "\"booleans\":[true,false]," + "\"characters\":[\"a\",\"b\",\"c\"]," + "\"doubles\":[1.0,2.5,3.5]," + diff --git a/agrest-engine/src/test/java/io/agrest/compiler/AnnotationsAgEntityCompilerTest.java b/agrest-engine/src/test/java/io/agrest/compiler/AnnotationsAgEntityCompilerTest.java index 19a9bf372..5c1cf85a6 100644 --- a/agrest-engine/src/test/java/io/agrest/compiler/AnnotationsAgEntityCompilerTest.java +++ b/agrest-engine/src/test/java/io/agrest/compiler/AnnotationsAgEntityCompilerTest.java @@ -37,7 +37,7 @@ public void testCompile_CollectionAttributes() { AgEntity entity = new AnnotationsAgEntityCompiler(Collections.emptyMap()) .compile(P8.class, new LazyAgDataMap(compilers)); assertNotNull(entity); - assertEquals(0, entity.getIdParts().size()); + assertEquals(1, entity.getIdParts().size()); assertEquals(7, entity.getAttributes().size()); assertEquals(Collection.class, entity.getAttribute(P8.BOOLEANS).getType()); assertEquals(Collection.class, entity.getAttribute(P8.DOUBLES).getType()); diff --git a/agrest-engine/src/test/java/io/agrest/pojo/model/P8.java b/agrest-engine/src/test/java/io/agrest/pojo/model/P8.java index 7afeb9454..b34c3d4db 100644 --- a/agrest-engine/src/test/java/io/agrest/pojo/model/P8.java +++ b/agrest-engine/src/test/java/io/agrest/pojo/model/P8.java @@ -1,6 +1,7 @@ package io.agrest.pojo.model; import io.agrest.annotation.AgAttribute; +import io.agrest.annotation.AgId; import java.util.Collection; import java.util.Collections; @@ -17,6 +18,7 @@ public class P8 { public static final String CHARACTERS = "characters"; + private int id; private Set stringSet; private List numberList; private Collection wildcardCollection; @@ -24,6 +26,15 @@ public class P8 { private Collection doubles; private Collection characters; + @AgId + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + @AgAttribute public Set getStringSet() { return stringSet; diff --git a/agrest-engine/src/test/java/io/agrest/pojo/runtime/PojoFetchStage.java b/agrest-engine/src/test/java/io/agrest/pojo/runtime/PojoFetchStage.java index e1ef1bc24..75aa0375d 100644 --- a/agrest-engine/src/test/java/io/agrest/pojo/runtime/PojoFetchStage.java +++ b/agrest-engine/src/test/java/io/agrest/pojo/runtime/PojoFetchStage.java @@ -33,7 +33,9 @@ void findObjects(SelectContext context) { Map typeBucket = db.bucket(context.getType()); if (context.isById()) { - T object = typeBucket.get(context.getId().get()); + Map idMap = context.getId().asMap(context.getEntity().getAgEntity()); + Object id = idMap.size() > 1 ? idMap : idMap.values().iterator().next(); + T object = typeBucket.get(id); // stores as a result into ResourceEntity context.getEntity().setResult(object != null ? Collections.singletonList(object) : Collections.emptyList()); return;