diff --git a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java index 0821833..1112d53 100644 --- a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java @@ -15,6 +15,7 @@ */ package de.muehlencord.shared.db; +import de.muehlencord.shared.util.DateUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -89,9 +90,8 @@ public abstract class AbstractController { * @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 - * filter (not equals, not in etc) + * @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 */ protected Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root root, Map filters, boolean include) { @@ -103,7 +103,15 @@ public abstract class AbstractController { // check for differnt types // 1st String, either from Enum Status or from other free text string - if (String.class.equals(filter.getValue().getClass())) { + if (filter.getValue() == null) { + Predicate predicate; + if (include) { + predicate = cb.isNull(path); + } else { + predicate = cb.isNotNull(path); + } + filterCondition = addFilterCondition(cb, filterCondition, predicate); + } else if (String.class.equals(filter.getValue().getClass())) { switch (filter.getKey()) { default: String filterValue = filter.getValue().toString(); @@ -204,6 +212,10 @@ public abstract class AbstractController { Updateable updateable = (Updateable) entity; applyUpdateableChanges(updateable, true, createdBy); } + if (EndDateable.class.isAssignableFrom(entity.getClass())) { + EndDateable endDateable = (EndDateable) entity; + endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC()); + } em.persist(entity); return entity; } @@ -223,7 +235,14 @@ public abstract class AbstractController { @Transactional @Lock(LockType.WRITE) public void delete(T entity, String deletedBy) throws ControllerException { - em.remove(attach(entity)); + // if the entity is endDateable just set the validToDate to now intead of executing the deletion + if (EndDateable.class.isAssignableFrom(entity.getClass())) { + EndDateable endDateable = (EndDateable) entity; + endDateable.setValidTo(DateUtil.getCurrentTimeInUTC()); + update(entity, deletedBy); + } else { + em.remove(attach(entity)); + } } @Lock(LockType.READ) @@ -233,18 +252,18 @@ public abstract class AbstractController { @Lock(LockType.READ) public T find(Object id, String... subGraphItems) { - EntityGraph graph = this.em.createEntityGraph(entityClass); + EntityGraph graph = this.em.createEntityGraph(entityClass); for (String subGraphItem : subGraphItems) { - graph.addSubgraph(subGraphItem); + graph.addSubgraph(subGraphItem); } - - Map hints = new HashMap<>(); + + Map hints = new HashMap<>(); hints.put("javax.persistence.loadgraph", graph); T entity = (T) em.find(entityClass, id, hints); return entity; } - + @Lock(LockType.READ) public List findAll() { return findAll(new ArrayList<>()); @@ -287,9 +306,8 @@ public abstract class AbstractController { } /** - * 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 resultList * @return