From 8cb8936567f1f74d7f0b664b391802709982624d Mon Sep 17 00:00:00 2001 From: Sebastian Schmelzer Date: Thu, 23 Aug 2012 17:43:54 +0200 Subject: add support for filter, change sorting/pagination api, cleanup --- src/main/java/nu/xss/jpa/dao/Dao.java | 26 ++-- src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 156 ++++++++++++--------- src/main/java/nu/xss/jpa/query/Pagination.java | 33 +++++ src/main/java/nu/xss/jpa/query/Sort.java | 29 ++++ .../nu/xss/jpa/query/filter/AbstractFilter.java | 21 +++ .../java/nu/xss/jpa/query/filter/EntityFilter.java | 55 ++++++++ .../nu/xss/jpa/query/filter/EntityFilterFlags.java | 8 ++ src/main/java/nu/xss/jpa/query/filter/Filter.java | 15 ++ 8 files changed, 266 insertions(+), 77 deletions(-) create mode 100644 src/main/java/nu/xss/jpa/query/Pagination.java create mode 100644 src/main/java/nu/xss/jpa/query/Sort.java create mode 100644 src/main/java/nu/xss/jpa/query/filter/AbstractFilter.java create mode 100644 src/main/java/nu/xss/jpa/query/filter/EntityFilter.java create mode 100644 src/main/java/nu/xss/jpa/query/filter/EntityFilterFlags.java create mode 100644 src/main/java/nu/xss/jpa/query/filter/Filter.java diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java index 317e291..86444f4 100644 --- a/src/main/java/nu/xss/jpa/dao/Dao.java +++ b/src/main/java/nu/xss/jpa/dao/Dao.java @@ -2,20 +2,28 @@ package nu.xss.jpa.dao; import java.util.List; +import nu.xss.jpa.query.Pagination; +import nu.xss.jpa.query.Sort; +import nu.xss.jpa.query.filter.Filter; + public interface Dao { void save(E entity); void merge(E entity); void delete(E entity); + E findById(K id); + + int count(); + int count(Filter... filter); + List findAll(); - List findAll(int count); - List findAll(int count, int offset); + List findAll(Sort sort); + List findAll(Pagination pagination); + List findAll(Sort sort, Pagination pagination); + + List findAll(Filter... filter); + List findAll(Sort sort, Filter... filter); + List findAll(Pagination pagination, Filter... filter); + List findAll(Sort sort, Pagination pagination, Filter... filter); - List findAll(String sortColumn); - List findAll(int count, String sortColumn); - List findAll(int count, int offset, String sortColumn); - - List findAll(String sortColumn, boolean isAscending); - List findAll(int count, String sortColumn, boolean isAscending); - List findAll(int count, int offset, String sortColumn, boolean isAscending); } diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java index 399763a..0c06505 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -3,8 +3,9 @@ package nu.xss.jpa.dao; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.HashSet; import java.util.List; - +import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.NoResultException; @@ -13,9 +14,13 @@ import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import nu.xss.jpa.entity.TypedEntity; +import nu.xss.jpa.query.Pagination; +import nu.xss.jpa.query.Sort; +import nu.xss.jpa.query.filter.Filter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,20 +51,20 @@ public abstract class GenericJpaDao, K> implements } public void save(E entity) { - logger.info("Persist Entity: {}.", entity.toString()); entityManager.persist(entity); logger.info("Saved entity: {}.", entity.toString()); } public void merge(E entity) { - logger.info("Merge Entity: {}.", entity.toString()); entityManager.merge(entity); - logger.info("Saved entity: {}.", entity.toString()); + logger.info("Updated unmanaged entity: {}.", entity.toString()); } public void delete(E entity) { - E persistentEntity = findById(entity.getId()); - entityManager.remove(persistentEntity); + final E persistentEntity = findById(entity.getId()); + if (persistentEntity != null) { + entityManager.remove(persistentEntity); + } logger.info("Deleted entity: {}.", entity.toString()); } @@ -67,86 +72,71 @@ public abstract class GenericJpaDao, K> implements return entityManager.find(entity, id); } - public List findAll() { - return find(null); + @Override + public int count() { + // TODO Auto-generated method stub + return 0; } - public List findAll(int count) { - return find(null, count); + @Override + public int count(Filter... filter) { + // TODO Auto-generated method stub + return 0; } - public List findAll(int count, int offset) { - return find(null, count, offset); + @Override + public List findAll() { + return find(null, null, null); } - public List findAll(String sortColumn) { - return find(null, sortColumn); + @Override + public List findAll(Sort sort) { + return find(null, sort, null); } - public List findAll(int count, String sortColumn) { - return find(null, count, sortColumn); + @Override + public List findAll(Pagination pagination) { + return find(null, null, pagination); } - public List findAll(int count, int offset, String sortColumn) { - return find(null, count, offset, sortColumn); + @Override + public List findAll(Sort sort, Pagination pagination) { + return find(null, sort, pagination); } - public List findAll(String sortColumn, boolean isAscending) { - return find(null, sortColumn, isAscending); + @Override + public List findAll(Filter... filter) { + return find(null, null, null, filter); } - public List findAll(int count, String sortColumn, boolean isAscending) { - return find(null, count, sortColumn, isAscending); + @Override + public List findAll(Sort sort, Filter... filter) { + return find(null, sort, null, filter); } - public List findAll(int count, int offset, String sortColumn, boolean isAscending) { - return find(null, count, offset, sortColumn, isAscending); - } - - protected CriteriaBuilder getCriteriaBuilder() { - return entityManager.getCriteriaBuilder(); + @Override + public List findAll(Pagination pagination, Filter... filter) { + return find(null, null, pagination, filter); } - protected CriteriaQuery createQuery() { - CriteriaQuery c = getCriteriaBuilder().createQuery(this.entity); - return c; + @Override + public List findAll(Sort sort, Pagination pagination, Filter... filter) { + return find(null, sort, pagination, filter); } - protected List find(CriteriaQuery query) { - return find(query, 0, 0, null, true); - } - - protected List find(CriteriaQuery query, int count) { - return find(query, count, 0, null, true); - } - - protected List find(CriteriaQuery query, int count, int offset) { - return find(query, count, offset, null, true); + return find(query, null, null); } - protected List find(CriteriaQuery query, String sortColumn) { - return find(query, sortColumn, true); + protected List find(CriteriaQuery query, Pagination pagination) { + return find(query, null, pagination); } - protected List find(CriteriaQuery query, int count, String sortColumn) { - return find(query, count, sortColumn, true); - } - - protected List find(CriteriaQuery query, int count, int offset, String sortColumn) { - return find(query, count, offset, sortColumn, true); - } - - - protected List find(CriteriaQuery query, String sortColumn, boolean isAscending) { - return find(query, 0, 0, sortColumn, isAscending); + protected List find(CriteriaQuery query, Sort sort) { + return find(query, sort, null); } - protected List find(CriteriaQuery query, int count, String sortColumn, boolean isAscending) { - return find(query, count, 0, sortColumn, isAscending); - } - - protected List find(CriteriaQuery query, int count, int offset, String sortColumn, boolean isAscending) { + protected List find(CriteriaQuery query, Sort sort, Pagination pagination, Filter... filter) { Root root; // check if we have a 'simple' query @@ -157,20 +147,39 @@ public abstract class GenericJpaDao, K> implements root = query.from(this.entity); query.select(root); - if (sortColumn != null) { - if (isAscending) { - query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn))); + if (filter != null) { + Set predicates = new HashSet(); + for (final Filter f: filter) { + f.buildFilters(getCriteriaBuilder(), root); + for (Predicate p : f.getPredicates()) { + predicates.add(p); + } + } + if (predicates.size() > 0) { + query.where((Predicate[]) predicates.toArray()); + } + } + + + // Result Sorting + // TODO: Multcolum sort + if (sort != null) { + if (sort.isAsc()) { + query.orderBy(getCriteriaBuilder().asc(root.get(sort.getColumn()))); } else { - query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn))); + query.orderBy(getCriteriaBuilder().desc(root.get(sort.getColumn()))); } } + // Result pagination TypedQuery q = entityManager.createQuery(query); - if ( count > 0 ) { - q.setFirstResult(offset); - } - if (offset > 0) { - q.setMaxResults(count); + if (pagination != null) { + if ( pagination.getCount() > 0 ) { + q.setFirstResult(pagination.getCount()); + } + if ( pagination.getOffset() > 0) { + q.setMaxResults(pagination.getOffset()); + } } return q.getResultList(); @@ -201,4 +210,15 @@ public abstract class GenericJpaDao, K> implements return find(query).get(0); } } + + + protected CriteriaBuilder getCriteriaBuilder() { + return entityManager.getCriteriaBuilder(); + } + + protected CriteriaQuery createQuery() { + CriteriaQuery c = getCriteriaBuilder().createQuery(this.entity); + return c; + } + } diff --git a/src/main/java/nu/xss/jpa/query/Pagination.java b/src/main/java/nu/xss/jpa/query/Pagination.java new file mode 100644 index 0000000..5ee3798 --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/Pagination.java @@ -0,0 +1,33 @@ +package nu.xss.jpa.query; + +public class Pagination { + + private int offset = 0; + private int count = 0; + + public Pagination(int count) { + this.setCount(count); + } + + public Pagination(int offset, int count) { + this.setOffset(offset); + this.setCount(count); + } + + public int getOffset() { + return offset; + } + + public void setOffset(int offset) { + this.offset = offset; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + +} diff --git a/src/main/java/nu/xss/jpa/query/Sort.java b/src/main/java/nu/xss/jpa/query/Sort.java new file mode 100644 index 0000000..ee69d02 --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/Sort.java @@ -0,0 +1,29 @@ +package nu.xss.jpa.query; + +public class Sort { + private String column; + private boolean asc; + + public Sort(String column) { + this.column = column; + } + + public Sort(String column, boolean asc) { + this.column = column; + this.asc = asc; + } + + public String getColumn() { + return column; + } + public void setColumn(String column) { + this.column = column; + } + public boolean isAsc() { + return asc; + } + public void setAsc(boolean asc) { + this.asc = asc; + } + +} diff --git a/src/main/java/nu/xss/jpa/query/filter/AbstractFilter.java b/src/main/java/nu/xss/jpa/query/filter/AbstractFilter.java new file mode 100644 index 0000000..f2d2bbf --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/filter/AbstractFilter.java @@ -0,0 +1,21 @@ +package nu.xss.jpa.query.filter; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.criteria.Predicate; + +public abstract class AbstractFilter implements Filter { + + private Set predicates = new HashSet(); + + protected void addPredicate(Predicate predicate) { + predicates.add(predicate); + } + + @Override + public Set getPredicates() { + return predicates; + } + +} diff --git a/src/main/java/nu/xss/jpa/query/filter/EntityFilter.java b/src/main/java/nu/xss/jpa/query/filter/EntityFilter.java new file mode 100644 index 0000000..6af0a0b --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/filter/EntityFilter.java @@ -0,0 +1,55 @@ +package nu.xss.jpa.query.filter; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Root; + +public class EntityFilter extends AbstractFilter { + + private Class entity; + + private List flags = new ArrayList(); + + public EntityFilter(Class entity, EntityFilterFlags... flags) { + super(); + this.setEntity(entity); + for (EntityFilterFlags f : flags) { + this.flags.add(f); + } + } + + @Override + public void buildFilters(CriteriaBuilder cb, Root from) { + List fields = getInheritedFields(entity); + List methods = new ArrayList(); + System.out.println(fields.toString()); + for (Method m: entity.getMethods()) { + if (m.getName().startsWith("get") && m.getName() != "getClass") { + methods.add(m); + } + } + System.out.println(methods.toString()); + } + + public Class getEntity() { + return entity; + } + + public void setEntity(Class entity) { + this.entity = entity; + } + + private List getInheritedFields(Class type) { + List fields = new ArrayList(); + for (Class c = type; c != null; c = c.getSuperclass()) { + fields.addAll(Arrays.asList(c.getDeclaredFields())); + } + return fields; + } + +} diff --git a/src/main/java/nu/xss/jpa/query/filter/EntityFilterFlags.java b/src/main/java/nu/xss/jpa/query/filter/EntityFilterFlags.java new file mode 100644 index 0000000..ca05c12 --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/filter/EntityFilterFlags.java @@ -0,0 +1,8 @@ +package nu.xss.jpa.query.filter; + +public enum EntityFilterFlags { + ENABLE_VARCHAR_SUFFIX_WILDCARD, + ENABLE_VARCHAR_PREFIX_WILDCARD, + ENABLE_VARCHAR_CASE_INSESITIVE, + IGNORE_NULL_MEMBERS +} diff --git a/src/main/java/nu/xss/jpa/query/filter/Filter.java b/src/main/java/nu/xss/jpa/query/filter/Filter.java new file mode 100644 index 0000000..c786685 --- /dev/null +++ b/src/main/java/nu/xss/jpa/query/filter/Filter.java @@ -0,0 +1,15 @@ +package nu.xss.jpa.query.filter; + +import java.util.Set; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +public interface Filter { + + void buildFilters(CriteriaBuilder cb, Root from); + + Set getPredicates(); + +} -- cgit v1.2.3-55-g7522