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;
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<T> {
* @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<T> root, Map<String, Object> filters, boolean include) {
@ -103,7 +103,15 @@ public abstract class AbstractController<T> {
// 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<T> {
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<T> {
@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)
@ -287,9 +306,8 @@ public abstract class AbstractController<T> {
}
/**
* 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