prepared to support Enddatable in findAll

This commit is contained in:
2019-10-04 07:32:00 +02:00
parent ee1a20836d
commit 6d49dec89f

View File

@ -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 <T> List<T> findAll(Class<T> entityClass) {
public <T extends Serializable> List<T> findAll(Class<T> 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 <T> List<T> findAll(Class<T> entityClass, String... orderFields) {
public <T extends Serializable> List<T> findAll(Class<T> 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 <T> List<T> findAll(Class<T> entityClass, List<String> orderFields) {
public <T extends Serializable> List<T> findAll(Class<T> entityClass, List<String> orderFields) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> criteria = cb.createQuery(entityClass);
final Root<T> r = criteria.from(entityClass);
if (EndDateable.class.isAssignableFrom(entityClass.getClass())) {
Map<String, Object> 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<Order> orderList = new ArrayList<>();
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
final TypedQuery<T> 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 <T> 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 <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, Map<String, Object> 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,9 +456,8 @@ 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 <T> the type of the entity to handle
* @param entityList the list to validate