generalized StandardController
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -15,22 +15,7 @@
|
||||
*/
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.ejb.Lock;
|
||||
import javax.ejb.LockType;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityGraph;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -39,90 +24,6 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
@Stateless
|
||||
public class StandardController extends CommonAbstractController {
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> T find(Class<T> clazz, Object id) {
|
||||
return em.find(clazz, id);
|
||||
}
|
||||
|
||||
private <T> EntityGraph getEntityGraph(Class<T> clazz, String... subGraphItems) {
|
||||
EntityGraph graph = this.em.createEntityGraph(clazz);
|
||||
if (subGraphItems != null) {
|
||||
for (String subGraphItem : subGraphItems) {
|
||||
graph.addSubgraph(subGraphItem);
|
||||
}
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> T find(Class<T> clazz, Object id, String... subGraphItems) {
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
Map hints = new HashMap<>();
|
||||
hints.put("javax.persistence.loadgraph", graph);
|
||||
|
||||
T entity = (T) em.find(clazz, id, hints);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, String... subGraphItems) {
|
||||
return find(clazz, filters, orderFields, 0, 0, subGraphItems);
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, Map<String, Object> filters, List<String> orderFields, int limit, int offset, String... subGraphItems) {
|
||||
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);
|
||||
}
|
||||
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
query.setHint("javax.persistence.loadgraph", graph);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public <T extends Serializable> List<T> find(Class<T> clazz, List<SearchFilter> filters, List<String> orderFields, String... subGraphItems) 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, String... subGraphItems) 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<>();
|
||||
if (orderFields != null) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (!ArrayUtils.isEmpty(subGraphItems)) {
|
||||
EntityGraph graph = getEntityGraph(clazz, subGraphItems);
|
||||
query.setHint("javax.persistence.loadgraph", graph);
|
||||
}
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user