From 6d49dec89f5dbb15e78e6e1b257d76e84e20daa4 Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Fri, 4 Oct 2019 07:32:00 +0200 Subject: [PATCH] prepared to support Enddatable in findAll --- .../shared/db/CommonAbstractController.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java index 565f9b2..404e3c6 100644 --- a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java @@ -19,6 +19,7 @@ import de.muehlencord.shared.util.DateUtil; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -58,7 +59,7 @@ public abstract class CommonAbstractController { * @return a list of all entities found */ @Lock(LockType.READ) - public List findAll(Class entityClass) { + public List findAll(Class entityClass) { return findAll(entityClass, new ArrayList<>()); } @@ -70,7 +71,7 @@ public abstract class CommonAbstractController { * @return a list of all entities found */ @Lock(LockType.READ) - public List findAll(Class entityClass, String... orderFields) { + public List findAll(Class entityClass, String... orderFields) { return findAll(entityClass, Arrays.asList(orderFields)); } @@ -82,12 +83,20 @@ public abstract class CommonAbstractController { * @return a list of all entities found */ @Lock(LockType.READ) - public List findAll(Class entityClass, List orderFields) { + public List findAll(Class entityClass, List orderFields) { final CriteriaBuilder cb = em.getCriteriaBuilder(); final CriteriaQuery criteria = cb.createQuery(entityClass); final Root r = criteria.from(entityClass); + if (EndDateable.class.isAssignableFrom(entityClass.getClass())) { + Map filters = new HashMap<>(); + // FIXME - add filter to validFrom / validTo + // need to support different conditions, currently only equals supported, need to support e.g. gt, lt, ... + Predicate filterCondition = getFilterCondition(cb, r, filters); + criteria.where(filterCondition); + } + List orderList = new ArrayList<>(); orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field)))); final TypedQuery query = em.createQuery(criteria.orderBy(orderList)); @@ -100,9 +109,8 @@ public abstract class CommonAbstractController { * updates the audit field of the entity class. * * @param audit the audit to apply - * @param onCreate specifies, whether the the update should be applied for a - * new entity or not. If yes, then also the createdBy /createdOn fields are - * updated. Otherwise these fields are skipped. + * @param onCreate specifies, whether the the update should be applied for a new entity or not. If yes, then also the createdBy /createdOn fields + * are updated. Otherwise these fields are skipped. * @param changedBy the username to apply * @return an updated audit object to use for the updated entity. * @throws ControllerException if the audit object cannot be updated @@ -128,8 +136,7 @@ public abstract class CommonAbstractController { * @param entity the entity to create or update * @param createdBy the username to apply write into the audit history * @return the entity after it has been written to the database. - * @throws ControllerException if the the creation or update of the entity - * fails. + * @throws ControllerException if the the creation or update of the entity fails. */ @TransactionAttribute(TransactionAttributeType.REQUIRED) @Transactional @@ -233,8 +240,7 @@ public abstract class CommonAbstractController { } /** - * Deletes an entity from the database. If the entity implements the - * {@link EndDateable} interface, the entity is not deleted but marked as + * Deletes an entity from the database. If the entity implements the {@link EndDateable} interface, the entity is not deleted but marked as * deleted (end date set). * * @param the type of the entity to handle @@ -298,8 +304,7 @@ public abstract class CommonAbstractController { * @param cb the CriteriaBuilder to use * @param root the root to use * @param filters the filters to use - * @param excludeFilters the exclude filters to apply. Entities which match - * these filters, are not taken into the result list. + * @param excludeFilters the exclude filters to apply. Entities which match these filters, are not taken into the result list. * @return the created filter condition */ protected Predicate getFilterCondition(CriteriaBuilder cb, Root root, Map filters, Map excludeFilters) { @@ -329,8 +334,7 @@ public abstract class CommonAbstractController { * @param cb the criteria builder to use * @param root the root of the object to search for * @param filters the filters to apply - * @param include if set to true, the filter is used as include filter - * (equals, in). If set to false, the filter is inverted and used as exclude + * @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is inverted and used as exclude * filter (not equals, not in etc) * @return the created filter condition */ @@ -452,14 +456,13 @@ public abstract class CommonAbstractController { } /** - * returns null, if the list is empty or null itself.Returns the one element - * if there is exactly one element in the list. Otherwise an exception is - * thrown + * returns null, if the list is empty or null itself.Returns the one element if there is exactly one element in the list. Otherwise an exception + * is thrown * * @param the type of the entity to handle * @param entityList the list to validate * @return the one and only element of the provided list - * @throws ControllerException if the list contains more than one element. + * @throws ControllerException if the list contains more than one element. */ public T ensureSingleElement(List entityList) throws ControllerException { if ((entityList == null) || (entityList.isEmpty())) {