Created
          February 22, 2017 15:03 
        
      - 
      
- 
        Save cokeSchlumpf/4ccfbd63bd4588484079fe8090eae317 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package util.model; | |
| import java.util.List; | |
| import javax.ejb.Stateless; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.PersistenceContext; | |
| import javax.persistence.criteria.CriteriaBuilder; | |
| import javax.persistence.criteria.CriteriaQuery; | |
| import javax.persistence.criteria.Predicate; | |
| import javax.persistence.criteria.Root; | |
| import org.apache.commons.lang3.Validate; | |
| import org.apache.commons.lang3.reflect.FieldUtils; | |
| import org.apache.commons.lang3.tuple.Pair; | |
| import com.google.common.base.Function; | |
| import com.google.common.base.Optional; | |
| import com.google.common.collect.Lists; | |
| import exceptions.ServiceError; | |
| import exceptions.WebApplicationException; | |
| import util.BaseObject; | |
| @Stateless | |
| public class CRUDUtil extends BaseObject { | |
| @PersistenceContext(name = "TesysPU") | |
| private EntityManager em; | |
| /** | |
| * Creates a new instance with a newly assigned techical ID. | |
| * | |
| * @param in | |
| * The new entity. | |
| * @return The new entity with a new ID. | |
| */ | |
| public <T extends AbstractPersistedObject> T create(T in) { | |
| Validate.notNull(in); | |
| Validate.isTrue(in.getId() == null); | |
| em.persist(in); | |
| return in; | |
| } | |
| /** | |
| * Creates a list of new instances. | |
| * | |
| * @param in | |
| * The new entities. | |
| * @return The list with entities and assigned IDs. | |
| */ | |
| public <T extends AbstractPersistedObject> List<T> createAll(List<T> in) { | |
| Validate.notNull(in); | |
| Validate.noNullElements(in); | |
| List<T> result = Lists.newArrayList(); | |
| for (T entity : in) { | |
| result.add(create(entity)); | |
| } | |
| return result; | |
| } | |
| public <T extends AbstractPersistedObject> Optional<T> find(Class<T> entityClass, | |
| Optional<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>> condition) { | |
| CriteriaBuilder cb = em.getCriteriaBuilder(); | |
| CriteriaQuery<T> criteria = cb.createQuery(entityClass); | |
| Root<T> root = criteria.from(entityClass); | |
| if (condition.isPresent()) { | |
| criteria.where(condition.get().apply(Pair.of(cb, root))); | |
| } | |
| List<T> results = em.createQuery(criteria).getResultList(); | |
| if (results.size() > 0) { | |
| return Optional.of(results.get(0)); | |
| } else { | |
| return Optional.absent(); | |
| } | |
| } | |
| public <T extends AbstractPersistedObject> Optional<T> find(Class<T> entityClass, final Pair<String, Object> where) { | |
| Function<Pair<CriteriaBuilder, Root<T>>, Predicate> func = new Function<Pair<CriteriaBuilder, Root<T>>, Predicate>() { | |
| @Override | |
| public Predicate apply(Pair<CriteriaBuilder, Root<T>> in) { | |
| CriteriaBuilder cb = in.getLeft(); | |
| Root<T> root = in.getRight(); | |
| return cb.equal(root.get(where.getLeft()), where.getRight()); | |
| } | |
| }; | |
| return find(entityClass, Optional.of(func)); | |
| } | |
| public <T extends AbstractPersistedObject> T get(Class<T> entityClass, | |
| Optional<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>> condition, String verbalCondition) { | |
| Optional<T> entity = find(entityClass, condition); | |
| if (!entity.isPresent()) { | |
| ServiceError error = ServiceError.EntityNotFoundExceptionWithCondition(entityClass.getSimpleName(), | |
| verbalCondition); | |
| throw new WebApplicationException(error); | |
| } | |
| return entity.get(); | |
| } | |
| public <T extends AbstractPersistedObject> T get(Class<T> entityClass, final Pair<String, ?> where, | |
| String verbalCondition) { | |
| Function<Pair<CriteriaBuilder, Root<T>>, Predicate> func = new Function<Pair<CriteriaBuilder, Root<T>>, Predicate>() { | |
| @Override | |
| public Predicate apply(Pair<CriteriaBuilder, Root<T>> in) { | |
| CriteriaBuilder cb = in.getLeft(); | |
| Root<T> root = in.getRight(); | |
| return cb.equal(root.get(where.getLeft()), where.getRight()); | |
| } | |
| }; | |
| return get(entityClass, Optional.of(func), verbalCondition); | |
| } | |
| /** | |
| * Returns a list of all entities with only the fields filled whcih have an | |
| * {@link XmlListAllAttribute} annotation. | |
| * | |
| * @return A list of all entities in the database. | |
| */ | |
| public <T extends AbstractPersistedObject> List<T> getAll(Class<T> entityClass, | |
| Optional<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>> condition) { | |
| CriteriaBuilder cb = em.getCriteriaBuilder(); | |
| CriteriaQuery<T> criteria = cb.createQuery(entityClass); | |
| Root<T> root = criteria.from(entityClass); | |
| if (condition.isPresent()) { | |
| criteria.where(condition.get().apply(Pair.of(cb, root))); | |
| } | |
| return em.createQuery(criteria).getResultList(); | |
| } | |
| public <T extends AbstractPersistedObject> List<T> getAll(Class<T> entityClass) { | |
| return this.getAll(entityClass, Optional.<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>>absent()); | |
| } | |
| public <T extends AbstractPersistedObject> List<T> getAll(Class<T> entityClass, final Pair<String, ?> where) { | |
| Function<Pair<CriteriaBuilder, Root<T>>, Predicate> func = new Function<Pair<CriteriaBuilder, Root<T>>, Predicate>() { | |
| @Override | |
| public Predicate apply(Pair<CriteriaBuilder, Root<T>> in) { | |
| CriteriaBuilder cb = in.getLeft(); | |
| Root<T> root = in.getRight(); | |
| return cb.equal(root.get(where.getLeft()), where.getRight()); | |
| } | |
| }; | |
| return getAll(entityClass, Optional.of(func)); | |
| } | |
| public <T> long count(Class<T> entityClass, | |
| Optional<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>> condition) { | |
| CriteriaBuilder cb = em.getCriteriaBuilder(); | |
| CriteriaQuery<Long> criteria = cb.createQuery(Long.class); | |
| Root<T> root = criteria.from(entityClass); | |
| criteria.select(cb.count(root)); | |
| if (condition.isPresent()) { | |
| criteria.where(condition.get().apply(Pair.of(cb, root))); | |
| } | |
| return em.createQuery(criteria).getSingleResult(); | |
| } | |
| public <T> long count(Class<T> entityClass, final Pair<String, ?> where) { | |
| Function<Pair<CriteriaBuilder, Root<T>>, Predicate> func = new Function<Pair<CriteriaBuilder, Root<T>>, Predicate>() { | |
| @Override | |
| public Predicate apply(Pair<CriteriaBuilder, Root<T>> in) { | |
| CriteriaBuilder cb = in.getLeft(); | |
| Root<T> root = in.getRight(); | |
| return cb.equal(root.get(where.getLeft()), where.getRight()); | |
| } | |
| }; | |
| return count(entityClass, Optional.of(func)); | |
| } | |
| public <T> long count(Class<T> entityClass) { | |
| return count(entityClass, Optional.<Function<Pair<CriteriaBuilder, Root<T>>, Predicate>>absent()); | |
| } | |
| public <T extends AbstractPersistedObject> T get(Long id, Class<T> entityClass) { | |
| return getById(id, entityClass); | |
| } | |
| public <T extends AbstractPersistedObject> T update(Long id, T entity) { | |
| try { | |
| FieldUtils.writeField(entity, "id", id, true); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| em.persist(entity); | |
| return entity; | |
| } | |
| public <T extends AbstractPersistedObject> T delete(Long id, Class<T> entityClass) { | |
| T entity = getById(id, entityClass); | |
| em.remove(entity); | |
| return entity; | |
| } | |
| public <T extends AbstractPersistedObject> List<T> deleteAll(Class<T> entityClass) { | |
| List<T> all = getAll(entityClass); | |
| for (T e : all) { | |
| delete(e.getId(), entityClass); | |
| } | |
| return all; | |
| } | |
| protected <T extends AbstractPersistedObject> Optional<T> findById(Long id, Class<T> entityClass) { | |
| T entity = em.find(entityClass, id); | |
| if (entity == null) { | |
| return Optional.absent(); | |
| } else { | |
| return Optional.of(entity); | |
| } | |
| } | |
| protected <T extends AbstractPersistedObject> T getById(Long id, Class<T> entityClass) { | |
| T entity = em.find(entityClass, id); | |
| if (entity == null) { | |
| ServiceError error = ServiceError.EntityNotFoundException(entityClass.getSimpleName(), id); | |
| throw new WebApplicationException(error); | |
| } | |
| return entity; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment