generalized StandardController

This commit is contained in:
2019-12-25 23:04:55 +01:00
parent b4a28bb0b0
commit 4dddc1f3dc
2 changed files with 613 additions and 623 deletions

View File

@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
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;
@ -29,6 +30,7 @@ import javax.ejb.LockType;
import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType; import javax.ejb.TransactionAttributeType;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
@ -38,6 +40,7 @@ import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
/** /**
@ -53,6 +56,92 @@ public abstract class CommonAbstractController {
@ApplicationPU @ApplicationPU
protected EntityManager em; protected EntityManager em;
@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();
}
/** /**
* *
* @param <T> the type of the entity to search for * @param <T> the type of the entity to search for
@ -245,8 +334,8 @@ 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
* deleted (end date set). * (end date set).
* *
* @param <T> the type of the entity to handle * @param <T> the type of the entity to handle
* @param entity the entity to delete * @param entity the entity to delete
@ -581,8 +670,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 * 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
* 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

View File

@ -15,22 +15,7 @@
*/ */
package de.muehlencord.shared.db; 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.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 @Stateless
public class StandardController extends CommonAbstractController { 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();
}
} }