Skip to content

Commit

Permalink
fix complex fields selections, select collection fields
Browse files Browse the repository at this point in the history
  • Loading branch information
osbeorn committed Aug 6, 2018
1 parent 757b029 commit cc4bfad
Showing 1 changed file with 82 additions and 27 deletions.
109 changes: 82 additions & 27 deletions core/src/main/java/com/kumuluz/ee/rest/utils/JPAUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static <T> List<T> queryEntities(EntityManager em, Class<T> entity, Query
return (List<T>) tq.getResultList();
} else {

return createEntityFromTuple((List<Tuple>)tq.getResultList(), entity, getEntityIdField(em, entity));
return createEntitiesFromTuples((List<Tuple>)tq.getResultList(), entity, getEntityIdField(em, entity));
}
}

Expand Down Expand Up @@ -485,7 +485,8 @@ private static CriteriaWhereQuery createWhereQueryInternal(CriteriaBuilder cb, R

///// Private helper methods

private static <T> List<T> createEntityFromTuple(List<Tuple> tuples, Class<T> entity, String idField) {
@SuppressWarnings("unchecked")
private static <T> List<T> createEntitiesFromTuples(List<Tuple> tuples, Class<T> entity, String idField) {

List<T> entities = new ArrayList<>();

Expand Down Expand Up @@ -516,22 +517,35 @@ private static <T> List<T> createEntityFromTuple(List<Tuple> tuples, Class<T> en

try {
String[] fName = te.getAlias().split("\\.");
Field f = getFieldFromEntity(entity, fName);
f.setAccessible(true);

if (isCollectionField(f)) {
Object c = f.get(el);
if (c == null) {
f.set(el, new ArrayList<>());
c = f.get(el);
}

Method add = Collection.class.getDeclaredMethod("add", Object.class);
add.invoke(c, o);
} else if (isObjectField(f)) {
// TODO
if (fName.length == 1) {

Field f = getFieldFromEntity(entity, fName[0]);
setEntityFieldValue(el, f, o);
} else {
f.set(el, o);
T el2 = el;

Field field = null;
Class entity2 = entity;

try {

for (int i = 0; i < fName.length; i++) {

field = getFieldFromEntity(entity2, fName[i]);
entity2 = field.getType();

if (i < fName.length - 1) {

el2 = (T) initializeField(el2, field, entity2);
}
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {

throw new AssertionError();
}

setEntityFieldValue(el2, field, o);
}
} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {

Expand All @@ -547,12 +561,55 @@ private static <T> List<T> createEntityFromTuple(List<Tuple> tuples, Class<T> en
}

@SuppressWarnings("unchecked")
private static String getEntityIdField(EntityManager em, Class entity) {
private static <T> T initializeField(T entity, Field field, Class<T> entity2Class)
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
field.setAccessible(true);

if (isCollectionField(field)) {
Object collection = field.get(entity);

if (collection == null) {
field.set(entity, new ArrayList<>());
}
} else if (isObjectField(field)) {
Object object = field.get(entity);

if (object == null) {
field.set(entity, entity2Class.getConstructor().newInstance());
}
}

return (T) field.get(entity);
}

private static <T> void setEntityFieldValue(T entity, Field field, Object value)
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
{
field.setAccessible(true);

if (isCollectionField(field)) {
Object collection = field.get(entity);
if (collection == null) {
field.set(entity, new ArrayList<>());
collection = field.get(entity);
}

Method add = Collection.class.getDeclaredMethod("add", Object.class);
add.invoke(collection, value);
} else if (isObjectField(field)) {
field.set(entity, value);
} else {
field.set(entity, value);
}
}

@SuppressWarnings("unchecked")
private static String getEntityIdField(EntityManager em, Class entityClass) {

String idProperty = "";

Metamodel metamodel = em.getMetamodel();
EntityType e = metamodel.entity(entity);
EntityType e = metamodel.entity(entityClass);
Set<SingularAttribute> singularAttributes = e.getSingularAttributes();

for (SingularAttribute singularAttribute : singularAttributes) {
Expand All @@ -567,29 +624,27 @@ private static String getEntityIdField(EntityManager em, Class entity) {
return idProperty;
}

private static Field getFieldFromEntity(Class entity, String fieldName) throws
NoSuchFieldException {
private static Field getFieldFromEntity(Class entityClass, String fieldName) throws NoSuchFieldException {

try {
return entity.getDeclaredField(fieldName);
return entityClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {

if (entity.getSuperclass() == null) {
if (entityClass.getSuperclass() == null) {
throw e;
}

return getFieldFromEntity(entity.getSuperclass(), fieldName);
return getFieldFromEntity(entityClass.getSuperclass(), fieldName);
}
}

private static Field getFieldFromEntity(Class entity, String[] fieldNames) throws
NoSuchFieldException {
private static Field getFieldFromEntity(Class entityClass, String[] fieldNames) throws NoSuchFieldException {

if (fieldNames.length == 1) {
return getFieldFromEntity(entity, fieldNames[0]);
return getFieldFromEntity(entityClass, fieldNames[0]);
}

Field field = getFieldFromEntity(entity, fieldNames[0]);
Field field = getFieldFromEntity(entityClass, fieldNames[0]);
for (String fieldName : Arrays.stream(fieldNames).skip(1).collect(Collectors.toList())) {
field.setAccessible(true);
field = getFieldFromEntity(field.getType(), fieldName);
Expand Down

0 comments on commit cc4bfad

Please sign in to comment.