enhanced enddateable support

This commit is contained in:
2019-06-16 21:56:28 +02:00
parent e9dede69cb
commit a9e136d3ac

View File

@ -15,6 +15,7 @@
*/ */
package de.muehlencord.shared.db; package de.muehlencord.shared.db;
import de.muehlencord.shared.util.DateUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -89,9 +90,8 @@ public abstract class AbstractController<T> {
* @param cb the criteria builder to use * @param cb the criteria builder to use
* @param root the root of the object to search for * @param root the root of the object to search for
* @param filters the filters to apply * @param filters the filters to apply
* @param include if set to true, the filter is used as include filter * @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is
* (equals, in). If set to false, the filter is inverted and used as exclude * inverted and used as exclude filter (not equals, not in etc)
* filter (not equals, not in etc)
* @return * @return
*/ */
protected Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, boolean include) { protected Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, boolean include) {
@ -103,7 +103,15 @@ public abstract class AbstractController<T> {
// check for differnt types // check for differnt types
// 1st String, either from Enum Status or from other free text string // 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()) { switch (filter.getKey()) {
default: default:
String filterValue = filter.getValue().toString(); String filterValue = filter.getValue().toString();
@ -204,6 +212,10 @@ public abstract class AbstractController<T> {
Updateable updateable = (Updateable) entity; Updateable updateable = (Updateable) entity;
applyUpdateableChanges(updateable, true, createdBy); applyUpdateableChanges(updateable, true, createdBy);
} }
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
EndDateable endDateable = (EndDateable) entity;
endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC());
}
em.persist(entity); em.persist(entity);
return entity; return entity;
} }
@ -223,7 +235,14 @@ public abstract class AbstractController<T> {
@Transactional @Transactional
@Lock(LockType.WRITE) @Lock(LockType.WRITE)
public void delete(T entity, String deletedBy) throws ControllerException { 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) @Lock(LockType.READ)
@ -233,18 +252,18 @@ public abstract class AbstractController<T> {
@Lock(LockType.READ) @Lock(LockType.READ)
public T find(Object id, String... subGraphItems) { public T find(Object id, String... subGraphItems) {
EntityGraph graph = this.em.createEntityGraph(entityClass); EntityGraph graph = this.em.createEntityGraph(entityClass);
for (String subGraphItem : subGraphItems) { for (String subGraphItem : subGraphItems) {
graph.addSubgraph(subGraphItem); graph.addSubgraph(subGraphItem);
} }
Map hints = new HashMap<>(); Map hints = new HashMap<>();
hints.put("javax.persistence.loadgraph", graph); hints.put("javax.persistence.loadgraph", graph);
T entity = (T) em.find(entityClass, id, hints); T entity = (T) em.find(entityClass, id, hints);
return entity; return entity;
} }
@Lock(LockType.READ) @Lock(LockType.READ)
public List<T> findAll() { public List<T> findAll() {
return findAll(new ArrayList<>()); return findAll(new ArrayList<>());
@ -287,9 +306,8 @@ public abstract class AbstractController<T> {
} }
/** /**
* returns null, if the list is empty or null itself. Returns the one * returns null, if the list is empty or null itself. Returns the one element if there is exactly one element in the
* element if there is exactly one element in the list. Otherwise an * list. Otherwise an exception is thrown
* exception is thrown
* *
* @param resultList * @param resultList
* @return * @return