summaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorSebastian Schmelzer2012-08-23 17:43:54 +0200
committerSebastian Schmelzer2012-08-23 17:43:54 +0200
commit8cb8936567f1f74d7f0b664b391802709982624d (patch)
tree6299c5fb36fde4a04577936fde1a0e8c57acd984 /src/main/java
parentfix sorting bug (diff)
downloadxss-jpa-8cb8936567f1f74d7f0b664b391802709982624d.tar.gz
xss-jpa-8cb8936567f1f74d7f0b664b391802709982624d.tar.xz
xss-jpa-8cb8936567f1f74d7f0b664b391802709982624d.zip
add support for filter, change sorting/pagination api, cleanup
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/nu/xss/jpa/dao/Dao.java26
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java156
-rw-r--r--src/main/java/nu/xss/jpa/query/Pagination.java33
-rw-r--r--src/main/java/nu/xss/jpa/query/Sort.java29
-rw-r--r--src/main/java/nu/xss/jpa/query/filter/AbstractFilter.java21
-rw-r--r--src/main/java/nu/xss/jpa/query/filter/EntityFilter.java55
-rw-r--r--src/main/java/nu/xss/jpa/query/filter/EntityFilterFlags.java8
-rw-r--r--src/main/java/nu/xss/jpa/query/filter/Filter.java15
8 files changed, 266 insertions, 77 deletions
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<E, K> {
void save(E entity);
void merge(E entity);
void delete(E entity);
+
E findById(K id);
+
+ int count();
+ int count(Filter... filter);
+
List<E> findAll();
- List<E> findAll(int count);
- List<E> findAll(int count, int offset);
+ List<E> findAll(Sort sort);
+ List<E> findAll(Pagination pagination);
+ List<E> findAll(Sort sort, Pagination pagination);
+
+ List<E> findAll(Filter... filter);
+ List<E> findAll(Sort sort, Filter... filter);
+ List<E> findAll(Pagination pagination, Filter... filter);
+ List<E> findAll(Sort sort, Pagination pagination, Filter... filter);
- List<E> findAll(String sortColumn);
- List<E> findAll(int count, String sortColumn);
- List<E> findAll(int count, int offset, String sortColumn);
-
- List<E> findAll(String sortColumn, boolean isAscending);
- List<E> findAll(int count, String sortColumn, boolean isAscending);
- List<E> 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<E extends TypedEntity<K>, 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<E extends TypedEntity<K>, K> implements
return entityManager.find(entity, id);
}
- public List<E> findAll() {
- return find(null);
+ @Override
+ public int count() {
+ // TODO Auto-generated method stub
+ return 0;
}
- public List<E> findAll(int count) {
- return find(null, count);
+ @Override
+ public int count(Filter... filter) {
+ // TODO Auto-generated method stub
+ return 0;
}
- public List<E> findAll(int count, int offset) {
- return find(null, count, offset);
+ @Override
+ public List<E> findAll() {
+ return find(null, null, null);
}
- public List<E> findAll(String sortColumn) {
- return find(null, sortColumn);
+ @Override
+ public List<E> findAll(Sort sort) {
+ return find(null, sort, null);
}
- public List<E> findAll(int count, String sortColumn) {
- return find(null, count, sortColumn);
+ @Override
+ public List<E> findAll(Pagination pagination) {
+ return find(null, null, pagination);
}
- public List<E> findAll(int count, int offset, String sortColumn) {
- return find(null, count, offset, sortColumn);
+ @Override
+ public List<E> findAll(Sort sort, Pagination pagination) {
+ return find(null, sort, pagination);
}
- public List<E> findAll(String sortColumn, boolean isAscending) {
- return find(null, sortColumn, isAscending);
+ @Override
+ public List<E> findAll(Filter... filter) {
+ return find(null, null, null, filter);
}
- public List<E> findAll(int count, String sortColumn, boolean isAscending) {
- return find(null, count, sortColumn, isAscending);
+ @Override
+ public List<E> findAll(Sort sort, Filter... filter) {
+ return find(null, sort, null, filter);
}
- public List<E> 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<E> findAll(Pagination pagination, Filter... filter) {
+ return find(null, null, pagination, filter);
}
- protected CriteriaQuery<E> createQuery() {
- CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entity);
- return c;
+ @Override
+ public List<E> findAll(Sort sort, Pagination pagination, Filter... filter) {
+ return find(null, sort, pagination, filter);
}
-
protected List<E> find(CriteriaQuery<E> query) {
- return find(query, 0, 0, null, true);
- }
-
- protected List<E> find(CriteriaQuery<E> query, int count) {
- return find(query, count, 0, null, true);
- }
-
- protected List<E> find(CriteriaQuery<E> query, int count, int offset) {
- return find(query, count, offset, null, true);
+ return find(query, null, null);
}
- protected List<E> find(CriteriaQuery<E> query, String sortColumn) {
- return find(query, sortColumn, true);
+ protected List<E> find(CriteriaQuery<E> query, Pagination pagination) {
+ return find(query, null, pagination);
}
- protected List<E> find(CriteriaQuery<E> query, int count, String sortColumn) {
- return find(query, count, sortColumn, true);
- }
-
- protected List<E> find(CriteriaQuery<E> query, int count, int offset, String sortColumn) {
- return find(query, count, offset, sortColumn, true);
- }
-
-
- protected List<E> find(CriteriaQuery<E> query, String sortColumn, boolean isAscending) {
- return find(query, 0, 0, sortColumn, isAscending);
+ protected List<E> find(CriteriaQuery<E> query, Sort sort) {
+ return find(query, sort, null);
}
- protected List<E> find(CriteriaQuery<E> query, int count, String sortColumn, boolean isAscending) {
- return find(query, count, 0, sortColumn, isAscending);
- }
-
- protected List<E> find(CriteriaQuery<E> query, int count, int offset, String sortColumn, boolean isAscending) {
+ protected List<E> find(CriteriaQuery<E> query, Sort sort, Pagination pagination, Filter... filter) {
Root<E> root;
// check if we have a 'simple' query
@@ -157,20 +147,39 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, 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<Predicate> predicates = new HashSet<Predicate>();
+ 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<E> 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<E extends TypedEntity<K>, K> implements
return find(query).get(0);
}
}
+
+
+ protected CriteriaBuilder getCriteriaBuilder() {
+ return entityManager.getCriteriaBuilder();
+ }
+
+ protected CriteriaQuery<E> createQuery() {
+ CriteriaQuery<E> 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<E> implements Filter {
+
+ private Set<Predicate> predicates = new HashSet<Predicate>();
+
+ protected void addPredicate(Predicate predicate) {
+ predicates.add(predicate);
+ }
+
+ @Override
+ public Set<Predicate> 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<E> extends AbstractFilter<E> {
+
+ private Class<E> entity;
+
+ private List<EntityFilterFlags> flags = new ArrayList<EntityFilterFlags>();
+
+ public EntityFilter(Class<E> entity, EntityFilterFlags... flags) {
+ super();
+ this.setEntity(entity);
+ for (EntityFilterFlags f : flags) {
+ this.flags.add(f);
+ }
+ }
+
+ @Override
+ public void buildFilters(CriteriaBuilder cb, Root<?> from) {
+ List<Field> fields = getInheritedFields(entity);
+ List<Method> methods = new ArrayList<Method>();
+ 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<E> getEntity() {
+ return entity;
+ }
+
+ public void setEntity(Class<E> entity) {
+ this.entity = entity;
+ }
+
+ private List<Field> getInheritedFields(Class<?> type) {
+ List<Field> fields = new ArrayList<Field>();
+ 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<Predicate> getPredicates();
+
+}