Skip to content

Commit

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

.. deprecating AgObjectId.get()
  • Loading branch information
andrus committed Jan 7, 2022
1 parent 80ec8e1 commit 4d9ef65
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 43 deletions.
2 changes: 2 additions & 0 deletions agrest-engine/src/main/java/io/agrest/AgObjectId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> asMap(AgEntity<?> entity);
Expand Down
82 changes: 42 additions & 40 deletions agrest-engine/src/main/java/io/agrest/SimpleObjectId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> asMap(Collection<AgIdPart> idAttributes) {
AgIdPart idAttribute = idAttributes.iterator().next();
return Collections.singletonMap(idAttribute.getName(), Normalizer.normalize(id, idAttribute.getType()));
}
@Override
protected Map<String, Object> asMap(Collection<AgIdPart> 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();
}
}
3 changes: 2 additions & 1 deletion agrest-engine/src/test/java/io/agrest/GET_PojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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]," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testCompile_CollectionAttributes() {
AgEntity<P8> 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());
Expand Down
11 changes: 11 additions & 0 deletions agrest-engine/src/test/java/io/agrest/pojo/model/P8.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,13 +18,23 @@ public class P8 {
public static final String CHARACTERS = "characters";


private int id;
private Set<String> stringSet;
private List<Number> numberList;
private Collection<? extends Number> wildcardCollection;
private Collection<Boolean> booleans;
private Collection<Double> doubles;
private Collection<Character> characters;

@AgId
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@AgAttribute
public Set<String> getStringSet() {
return stringSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ <T> void findObjects(SelectContext<T> context) {

Map<Object, T> typeBucket = db.bucket(context.getType());
if (context.isById()) {
T object = typeBucket.get(context.getId().get());
Map<String, Object> 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;
Expand Down

0 comments on commit 4d9ef65

Please sign in to comment.