updated API description
This commit is contained in:
@ -34,7 +34,8 @@ import javax.persistence.criteria.Root;
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord (joern@muehlencord.de)
|
||||
* @param <T>
|
||||
* @param <T> the entity this controller will serve. All objects returned are of
|
||||
* this type.
|
||||
*/
|
||||
public abstract class AbstractController<T extends Serializable> extends CommonAbstractController {
|
||||
|
||||
@ -44,16 +45,26 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
||||
this.entityClass = clazz;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* * general find methods ***
|
||||
* general find method
|
||||
*
|
||||
* @param id the primary key of the entity to search for.
|
||||
* @return the found entity instance or null if the entity does not exist
|
||||
*/
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public T find(Object id) {
|
||||
return em.find(entityClass, id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* general find method with the option to initialize lists/sets of 1:n
|
||||
* relations during the query.
|
||||
*
|
||||
* @param id the primary key of the entity to search for.
|
||||
* @param subGraphItems the name of the subgraph items to initialize.
|
||||
* Typically this is the name of the 1:n set or list.
|
||||
* @return the found entity instance or null if the entity does not exist
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public T find(Object id, String... subGraphItems) {
|
||||
EntityGraph graph = this.em.createEntityGraph(entityClass);
|
||||
@ -68,16 +79,32 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a list of all entities.
|
||||
*
|
||||
* @return a list of all entities.
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public List<T> findAll() {
|
||||
return findAll(new ArrayList<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a list of all entities.
|
||||
*
|
||||
* @param orderFields the list of field names to order the result by.
|
||||
* @return a list of all entities.
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public List<T> findAll(String... orderFields) {
|
||||
return findAll(Arrays.asList(orderFields));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param orderFields the list of field names to order the result by.
|
||||
* @return a list of all entities.
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public List<T> findAll(List<String> orderFields) {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
@ -92,6 +119,14 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/**
|
||||
* searches for entities by specifying a filter map. The map contains a
|
||||
* field name and field value.
|
||||
*
|
||||
* @param filters the filters to apply
|
||||
* @param orderFields the fields to order the result by.
|
||||
* @return a list of found entities.
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public List<T> find(Map<String, Object> filters, List<String> orderFields) {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
@ -108,6 +143,5 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -44,20 +44,43 @@ import org.apache.commons.lang3.StringUtils;
|
||||
*/
|
||||
public abstract class CommonAbstractController {
|
||||
|
||||
/**
|
||||
* the entity manager to use
|
||||
*/
|
||||
@Inject
|
||||
@ApplicationPU
|
||||
protected EntityManager em;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the type of the entity to search for
|
||||
* @param entityClass the entity class to return
|
||||
* @return a list of all entities found
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public <T> List<T> findAll(Class<T> entityClass) {
|
||||
return findAll(entityClass, new ArrayList<>());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the type of the entity to search for
|
||||
* @param entityClass the entity class to return
|
||||
* @param orderFields the fields to order the result by.
|
||||
* @return a list of all entities found
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public <T> List<T> findAll(Class<T> entityClass, String... orderFields) {
|
||||
return findAll(entityClass, Arrays.asList(orderFields));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the type of the entity to search for
|
||||
* @param entityClass the entity class to return
|
||||
* @param orderFields the fields to order the result by.
|
||||
* @return a list of all entities found
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public <T> List<T> findAll(Class<T> entityClass, List<String> orderFields) {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
@ -73,6 +96,17 @@ public abstract class CommonAbstractController {
|
||||
}
|
||||
|
||||
/* **** standard crud options *** */
|
||||
/**
|
||||
* 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 changedBy the username to apply
|
||||
* @return an updated audit object to use for the updated entity.
|
||||
* @throws ControllerException
|
||||
*/
|
||||
public Audit applyAuditChanges(Audit audit, boolean onCreate, String changedBy) throws ControllerException {
|
||||
if (audit == null) {
|
||||
audit = new Audit();
|
||||
@ -87,6 +121,16 @@ public abstract class CommonAbstractController {
|
||||
return audit;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates an new entity or if the entity already exists updates it
|
||||
*
|
||||
* @param <T> the type of the entity to search for
|
||||
* @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.
|
||||
*/
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
||||
@Transactional
|
||||
@Lock(LockType.WRITE)
|
||||
@ -99,6 +143,15 @@ public abstract class CommonAbstractController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates an new entity.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @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 fails
|
||||
*/
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
||||
@Transactional
|
||||
@Lock(LockType.WRITE)
|
||||
@ -121,6 +174,15 @@ public abstract class CommonAbstractController {
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* updates an existing entity.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entity the entity to update
|
||||
* @param updatedBy 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 updates fails
|
||||
*/
|
||||
private <T extends Serializable> T executeUpdate(T entity, String updatedBy) throws ControllerException {
|
||||
T currentEntity = attach(entity);
|
||||
if (Auditable.class.isAssignableFrom(currentEntity.getClass())) {
|
||||
@ -145,6 +207,15 @@ public abstract class CommonAbstractController {
|
||||
return currentEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* updates an existing entity.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entity the entity to update
|
||||
* @param updatedBy 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 updates fails
|
||||
*/
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
||||
@Transactional
|
||||
@Lock(LockType.WRITE)
|
||||
@ -161,6 +232,16 @@ 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
|
||||
* deleted (end date set).
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entity the entity to delete
|
||||
* @param deletedBy the username to apply write into the audit history
|
||||
* @throws ControllerException if the deletion fails.
|
||||
*/
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
||||
@Transactional
|
||||
@Lock(LockType.WRITE)
|
||||
@ -174,20 +255,53 @@ public abstract class CommonAbstractController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* attaches the given entity
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entity the entity to attach
|
||||
* @return the entity after it has been attached
|
||||
*/
|
||||
public <T extends Serializable> T attach(T entity) {
|
||||
return em.merge(entity);
|
||||
}
|
||||
|
||||
public <T extends Serializable> void refresh (T entity) {
|
||||
|
||||
/**
|
||||
* Refreshes an entity.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entity the entity after it has been refreshed.
|
||||
*/
|
||||
public <T extends Serializable> void refresh(T entity) {
|
||||
em.refresh(entity);
|
||||
}
|
||||
|
||||
|
||||
/* *** filter methods *** */
|
||||
/**
|
||||
* Creates a filter condition.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param cb the CriteriaBuilder to use
|
||||
* @param root the root to use
|
||||
* @param filters the filters to use
|
||||
* @return the created filter condition
|
||||
*/
|
||||
protected <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters) {
|
||||
return getFilterCondition(cb, root, filters, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a filter condition.
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @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.
|
||||
* @return the created filter condition
|
||||
*/
|
||||
protected <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, Map<String, Object> excludeFilters) {
|
||||
// Predicate filterCondition = null;
|
||||
// filterCondition = getFilterCondition(filterCondition, cb, root, filters, true);
|
||||
@ -208,14 +322,17 @@ public abstract class CommonAbstractController {
|
||||
}
|
||||
|
||||
/**
|
||||
* extends the given filterCondition by the addtional filters
|
||||
* extends the given filterCondition by the additional filters
|
||||
*
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param filterCondition the current filter condition
|
||||
* @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 filter (not equals, not in etc)
|
||||
* @return
|
||||
* @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
|
||||
*/
|
||||
protected <T extends Serializable> Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, boolean include) {
|
||||
String wildCard = "%";
|
||||
@ -301,6 +418,14 @@ public abstract class CommonAbstractController {
|
||||
return filterCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a filter condition to an existing condition
|
||||
*
|
||||
* @param cb the builder to use
|
||||
* @param filterCondition the existing filter condition
|
||||
* @param addCondition the condition to add to the existing condition.
|
||||
* @return an updated filter condition.
|
||||
*/
|
||||
protected Predicate addFilterCondition(CriteriaBuilder cb, Predicate filterCondition, Predicate addCondition) {
|
||||
if (addCondition == null) {
|
||||
return filterCondition;
|
||||
@ -327,20 +452,23 @@ 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 resultList
|
||||
* @return
|
||||
* @throws ControllerException
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param entityList the list to validate
|
||||
* @return the one and only element of the provided list
|
||||
* @throws ControllerException if the list contains more than one element.
|
||||
*/
|
||||
public <T> T ensureSingleElement(List<T> resultList) throws ControllerException {
|
||||
if ((resultList == null) || (resultList.isEmpty())) {
|
||||
public <T> T ensureSingleElement(List<T> entityList) throws ControllerException {
|
||||
if ((entityList == null) || (entityList.isEmpty())) {
|
||||
return null;
|
||||
}
|
||||
if (resultList.size() > 1) {
|
||||
if (entityList.size() > 1) {
|
||||
throw new ControllerException(ControllerException.CAUSE_TOO_MANY_ROWS, "More than one element found in list - expected exactly one");
|
||||
}
|
||||
return resultList.get(0);
|
||||
return entityList.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user