extended search by SearchFilter
This commit is contained in:
@ -85,7 +85,7 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
|||||||
* @return a list of all entities.
|
* @return a list of all entities.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public List<T> findAll() {
|
public List<T> findAll() throws ControllerException {
|
||||||
return findAll(new ArrayList<>());
|
return findAll(new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
|||||||
* @return a list of all entities.
|
* @return a list of all entities.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public List<T> findAll(String... orderFields) {
|
public List<T> findAll(String... orderFields) throws ControllerException {
|
||||||
return findAll(Arrays.asList(orderFields));
|
return findAll(Arrays.asList(orderFields));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
|||||||
* @return a list of all entities.
|
* @return a list of all entities.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public List<T> findAll(List<String> orderFields) {
|
public List<T> findAll(List<String> orderFields) throws ControllerException {
|
||||||
return findAll(entityClass, orderFields);
|
return findAll(entityClass, orderFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +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.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -57,9 +57,10 @@ public abstract class CommonAbstractController {
|
|||||||
* @param <T> the type of the entity to search for
|
* @param <T> the type of the entity to search for
|
||||||
* @param entityClass the entity class to return
|
* @param entityClass the entity class to return
|
||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
|
* @throws ControllerException if the search cannot be executed.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> List<T> findAll(Class<T> entityClass) {
|
public <T extends Serializable> List<T> findAll(Class<T> entityClass) throws ControllerException {
|
||||||
return findAll(entityClass, new ArrayList<>());
|
return findAll(entityClass, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,9 +70,10 @@ public abstract class CommonAbstractController {
|
|||||||
* @param entityClass the entity class to return
|
* @param entityClass the entity class to return
|
||||||
* @param orderFields the fields to order the result by.
|
* @param orderFields the fields to order the result by.
|
||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
|
* @throws ControllerException if the search cannot be executed.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> List<T> findAll(Class<T> entityClass, String... orderFields) {
|
public <T extends Serializable> List<T> findAll(Class<T> entityClass, String... orderFields) throws ControllerException {
|
||||||
return findAll(entityClass, Arrays.asList(orderFields));
|
return findAll(entityClass, Arrays.asList(orderFields));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,20 +83,21 @@ public abstract class CommonAbstractController {
|
|||||||
* @param entityClass the entity class to return
|
* @param entityClass the entity class to return
|
||||||
* @param orderFields the fields to order the result by.
|
* @param orderFields the fields to order the result by.
|
||||||
* @return a list of all entities found
|
* @return a list of all entities found
|
||||||
|
* @throws ControllerException if the search cannot be executed.
|
||||||
*/
|
*/
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> List<T> findAll(Class<T> entityClass, List<String> orderFields) {
|
public <T extends Serializable> List<T> findAll(Class<T> entityClass, List<String> orderFields) throws ControllerException {
|
||||||
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())) {
|
if (EndDateable.class.isAssignableFrom(entityClass.getClass())) {
|
||||||
List<DefaultSearchFilter> filterList = new ArrayList<>();
|
Date now = DateUtil.getCurrentTimeInUTC();
|
||||||
Map<String, Object> filters = new HashMap<>();
|
List<SearchFilter> searchFilter = new ArrayList<>();
|
||||||
// FIXME - add filter to validFrom / validTo
|
searchFilter.add(new DefaultSearchFilter("validFrom", Comparator.GREATER_OR_EQUAL_THAN, now));
|
||||||
// need to support different conditions, currently only equals supported, need to support e.g. gt, lt, ...
|
searchFilter.add(new DefaultSearchFilter("validTo", Comparator.LESS_THAN, now));
|
||||||
Predicate filterCondition = getFilterCondition(cb, r, filters);
|
Predicate filterCondition = getFilterCondition(cb, r, searchFilter);
|
||||||
criteria.where(filterCondition);
|
criteria.where(filterCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -464,8 +467,51 @@ public abstract class CommonAbstractController {
|
|||||||
default:
|
default:
|
||||||
throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + "not support for searchValue " + searchValue);
|
throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + "not support for searchValue " + searchValue);
|
||||||
}
|
}
|
||||||
|
returnCondition = addFilterCondition(cb, returnCondition, predicate);
|
||||||
|
} else if (currentFilter.getSearchValue() instanceof Date) {
|
||||||
|
Date searchValue = (Date) currentFilter.getSearchValue();
|
||||||
|
Predicate predicate;
|
||||||
|
Path<Date> datePath = root.<Date>get(currentFilter.getFieldName());
|
||||||
|
switch (currentFilter.getComparator()) {
|
||||||
|
case EQUAL:
|
||||||
|
predicate = cb.equal(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
case NOT_EQUAL:
|
||||||
|
predicate = cb.notEqual(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
case LESS_THAN:
|
||||||
|
predicate = cb.lessThan(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
case LESS_OR_EQUAL_THAN:
|
||||||
|
predicate = cb.lessThanOrEqualTo(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
case GREATER_THAN:
|
||||||
|
predicate = cb.greaterThan(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
case GREATER_OR_EQUAL_THAN:
|
||||||
|
predicate = cb.greaterThanOrEqualTo(datePath, searchValue);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + "not support for searchValue " + searchValue);
|
||||||
|
}
|
||||||
|
returnCondition = addFilterCondition(cb, returnCondition, predicate);
|
||||||
|
} else if (currentFilter.getSearchValue() instanceof Boolean) {
|
||||||
|
Boolean searchValue = (Boolean) currentFilter.getSearchValue();
|
||||||
|
Path<Boolean> booleanPath = root.<Boolean>get(currentFilter.getFieldName());
|
||||||
|
Predicate predicate;
|
||||||
|
switch (currentFilter.getComparator()) {
|
||||||
|
case EQUAL:
|
||||||
|
predicate = cb.equal(booleanPath, searchValue);
|
||||||
|
break;
|
||||||
|
case NOT_EQUAL:
|
||||||
|
predicate = cb.notEqual(booleanPath, searchValue);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + "not support for searchValue " + searchValue);
|
||||||
|
}
|
||||||
|
returnCondition = addFilterCondition(cb, returnCondition, predicate);
|
||||||
} else {
|
} else {
|
||||||
throw new ControllerException(ControllerException.INTERNAL_ERROR, "Not yet implemented");
|
throw new ControllerException(ControllerException.INTERNAL_ERROR, "Filter for " + currentFilter.getSearchValue().getClass().getSimpleName() + " not yet implemented");
|
||||||
}
|
}
|
||||||
} // for all filters
|
} // for all filters
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,11 @@ public enum Comparator {
|
|||||||
|
|
||||||
EQUAL,
|
EQUAL,
|
||||||
NOT_EQUAL,
|
NOT_EQUAL,
|
||||||
CONTAINS;
|
CONTAINS,
|
||||||
|
LESS_THAN,
|
||||||
|
LESS_OR_EQUAL_THAN,
|
||||||
|
GREATER_THAN,
|
||||||
|
GREATER_OR_EQUAL_THAN;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,11 @@ public class StandardController extends CommonAbstractController {
|
|||||||
|
|
||||||
@Lock(LockType.READ)
|
@Lock(LockType.READ)
|
||||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields) {
|
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields) {
|
||||||
|
return find(clazz, filters, orderFields, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Lock(LockType.READ)
|
||||||
|
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, int limit, int offset) {
|
||||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||||
|
|
||||||
@ -69,7 +74,36 @@ public class StandardController extends CommonAbstractController {
|
|||||||
}
|
}
|
||||||
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))
|
||||||
|
.setFirstResult(offset);
|
||||||
|
if (limit > 0) {
|
||||||
|
query.setMaxResults(limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Lock(LockType.READ)
|
||||||
|
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields) throws ControllerException {
|
||||||
|
return find(clazz, filters, orderFields, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Lock(LockType.READ)
|
||||||
|
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields, int limit, int offset) throws ControllerException {
|
||||||
|
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
final CriteriaQuery<T> criteria = cb.createQuery(clazz);
|
||||||
|
|
||||||
|
final Root<T> r = criteria.from(clazz);
|
||||||
|
Predicate filterCondition = getFilterCondition(cb, r, filters);
|
||||||
|
if (filterCondition != null) {
|
||||||
|
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)).setFirstResult(offset);
|
||||||
|
if (limit > 0) {
|
||||||
|
query.setMaxResults(limit);
|
||||||
|
}
|
||||||
|
|
||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user