summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss/jpa/query
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/nu/xss/jpa/query
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/nu/xss/jpa/query')
-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
6 files changed, 161 insertions, 0 deletions
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();
+
+}