prepared to support Enddatable in findAll
This commit is contained in:
@ -19,6 +19,7 @@ import de.muehlencord.shared.util.DateUtil;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -58,7 +59,7 @@ public abstract class CommonAbstractController {
|
|||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@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<>());
|
return findAll(entityClass, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ public abstract class CommonAbstractController {
|
|||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@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));
|
return findAll(entityClass, Arrays.asList(orderFields));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,12 +83,20 @@ public abstract class CommonAbstractController {
|
|||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@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 CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
final CriteriaQuery<T> criteria = cb.createQuery(entityClass);
|
final CriteriaQuery<T> criteria = cb.createQuery(entityClass);
|
||||||
|
|
||||||
final Root<T> r = criteria.from(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<>();
|
List<Order> orderList = new ArrayList<>();
|
||||||
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
|
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
|
||||||
final TypedQuery<T> query = em.createQuery(criteria.orderBy(orderList));
|
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.
|
* updates the audit field of the entity class.
|
||||||
*
|
*
|
||||||
* @param audit the audit to apply
|
* @param audit the audit to apply
|
||||||
* @param onCreate specifies, whether the the update should be applied for a
|
* @param onCreate specifies, whether the the update should be applied for a new entity or not. If yes, then also the createdBy /createdOn fields
|
||||||
* new entity or not. If yes, then also the createdBy /createdOn fields are
|
* are updated. Otherwise these fields are skipped.
|
||||||
* updated. Otherwise these fields are skipped.
|
|
||||||
* @param changedBy the username to apply
|
* @param changedBy the username to apply
|
||||||
* @return an updated audit object to use for the updated entity.
|
* @return an updated audit object to use for the updated entity.
|
||||||
* @throws ControllerException if the audit object cannot be updated
|
* @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 entity the entity to create or update
|
||||||
* @param createdBy the username to apply write into the audit history
|
* @param createdBy the username to apply write into the audit history
|
||||||
* @return the entity after it has been written to the database.
|
* @return the entity after it has been written to the database.
|
||||||
* @throws ControllerException if the the creation or update of the entity
|
* @throws ControllerException if the the creation or update of the entity fails.
|
||||||
* fails.
|
|
||||||
*/
|
*/
|
||||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -233,8 +240,7 @@ public abstract class CommonAbstractController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes an entity from the database. If the entity implements the
|
* Deletes an entity from the database. If the entity implements the {@link EndDateable} interface, the entity is not deleted but marked as
|
||||||
* {@link EndDateable} interface, the entity is not deleted but marked as
|
|
||||||
* deleted (end date set).
|
* deleted (end date set).
|
||||||
*
|
*
|
||||||
* @param <T> the type of the entity to handle
|
* @param <T> the type of the entity to handle
|
||||||
@ -298,8 +304,7 @@ public abstract class CommonAbstractController {
|
|||||||
* @param cb the CriteriaBuilder to use
|
* @param cb the CriteriaBuilder to use
|
||||||
* @param root the root to use
|
* @param root the root to use
|
||||||
* @param filters the filters to use
|
* @param filters the filters to use
|
||||||
* @param excludeFilters the exclude filters to apply. Entities which match
|
* @param excludeFilters the exclude filters to apply. Entities which match these filters, are not taken into the result list.
|
||||||
* these filters, are not taken into the result list.
|
|
||||||
* @return the created filter condition
|
* @return the created filter condition
|
||||||
*/
|
*/
|
||||||
protected <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, Map<String, Object> excludeFilters) {
|
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 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 inverted and used as exclude
|
||||||
* (equals, in). If set to false, the filter is inverted and used as exclude
|
|
||||||
* filter (not equals, not in etc)
|
* filter (not equals, not in etc)
|
||||||
* @return the created filter condition
|
* @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
|
* 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
|
||||||
* if there is exactly one element in the list. Otherwise an exception is
|
* is thrown
|
||||||
* thrown
|
|
||||||
*
|
*
|
||||||
* @param <T> the type of the entity to handle
|
* @param <T> the type of the entity to handle
|
||||||
* @param entityList the list to validate
|
* @param entityList the list to validate
|
||||||
* @return the one and only element of the provided list
|
* @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> T ensureSingleElement(List<T> entityList) throws ControllerException {
|
public <T> T ensureSingleElement(List<T> entityList) throws ControllerException {
|
||||||
if ((entityList == null) || (entityList.isEmpty())) {
|
if ((entityList == null) || (entityList.isEmpty())) {
|
||||||
|
|||||||
Reference in New Issue
Block a user