changeset 383:701b52518dcd

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 09 Oct 2013 14:57:28 +0200
parents 816291da1dde (diff) a72df178f1b9 (current diff)
children 5c4010659967
files
diffstat 14 files changed, 169 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LKommentarMRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LKommentarMRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -179,4 +179,8 @@
         }
         return response;
     }
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LKommentarPRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -187,4 +187,9 @@
         }
         return response;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LMessungRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LMessungRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -207,4 +207,8 @@
         }
         return response;
     }
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LMesswertRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LMesswertRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -195,4 +195,8 @@
         }
         return response;
     }
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
\ No newline at end of file
--- a/src/main/java/de/intevation/lada/data/LOrtRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LOrtRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -208,4 +208,9 @@
         }
         return response;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -223,4 +223,9 @@
     public Response delete(Object object) {
         return null;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LStatusRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LStatusRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -181,4 +181,9 @@
         }
         return response;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/LZusatzwertRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LZusatzwertRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -192,4 +192,9 @@
         }
         return response;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
--- a/src/main/java/de/intevation/lada/data/OrtRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/OrtRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -171,4 +171,9 @@
         }
         return response;
     }
+
+    @Override
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        return null;
+    }
 }
\ No newline at end of file
--- a/src/main/java/de/intevation/lada/data/QueryBuilder.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/QueryBuilder.java	Wed Oct 09 14:57:28 2013 +0200
@@ -5,6 +5,7 @@
 import javax.persistence.EntityManager;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 
@@ -63,6 +64,25 @@
     }
 
     /**
+     * Logical AND with like operation.
+     *
+     * @param id    The database column name.
+     * @param value The filter value
+     * @return The builder itself.
+     */
+    public QueryBuilder<T> andLike(String id, String value) {
+        Path<String> path = this.root.get(id);
+        Predicate p = this.builder.like(path, value);
+        if (this.filter != null) {
+            this.filter = this.builder.and(this.filter, p);
+        }
+        else {
+            this.filter = this.builder.and(p);
+        }
+        return this;
+    }
+
+    /**
      * Logical OR operation.
      *
      * @param id    The database column name
@@ -81,6 +101,25 @@
     }
 
     /**
+     * Logical OR with like operation.
+     *
+     * @param column    The database column name.
+     * @param value     The filter value
+     * @return The builder itself.
+     */
+    public QueryBuilder<T> orLike(String id, String value) {
+        Path<String> path = this.root.get(id);
+        Predicate p = this.builder.like(path, value);
+        if (this.filter != null) {
+            this.filter = this.builder.or(this.filter, p);
+        }
+        else {
+            this.filter = this.builder.or(p);
+        }
+        return this;
+    }
+
+    /**
      * Logical AND operation.
      * All elements in <i>values</i> will be concatenated with AND operator.
      *
--- a/src/main/java/de/intevation/lada/data/ReadOnlyRepository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/ReadOnlyRepository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -43,6 +43,21 @@
     }
 
     /**
+     * Filter object list by the given criteria.
+     *
+     * @param filter  The filter query.
+     * @return Response object.
+     */
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start) {
+        List<T> result = em.createQuery(filter).getResultList();
+        if (size > 0 && start > -1) {
+            List<T> newList = result.subList(start, size + start);
+            return new Response(true, 200, newList, result.size());
+        }
+        return new Response(true, 200, result);
+    }
+
+    /**
      * Get all objects of type <link>clazz</link>from database.
      *
      * @param clazz     The object type.
--- a/src/main/java/de/intevation/lada/data/Repository.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/Repository.java	Wed Oct 09 14:57:28 2013 +0200
@@ -21,6 +21,8 @@
 
     public <T> Response filter(CriteriaQuery<T> filter);
 
+    public <T> Response filter(CriteriaQuery<T> filter, int size, int start);
+
     public <T> Response findAll(Class<T> clazz);
 
     public <T> Response findById(Class<T> clazz, String id);
--- a/src/main/java/de/intevation/lada/rest/Response.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/Response.java	Wed Oct 09 14:57:28 2013 +0200
@@ -28,6 +28,7 @@
     private Map<String, String> errors;
     private Map<String, String> warnings;
     private Boolean readonly;
+    private int totalCount;
 
     /**
      * Constructor to create a basic Response object.
@@ -43,6 +44,24 @@
         this.errors = new HashMap<String, String>();
         this.warnings = new HashMap<String, String>();
         this.readonly = Boolean.FALSE;
+        this.totalCount = 0;
+    }
+
+    /**
+     * Constructor to create a basic Response object.
+     *
+     * @param success   Information if the operation was successful.
+     * @param code      The return code.
+     * @param data      The data object wrapped by the response.
+     */
+    public Response(boolean success, int code, Object data, int totalCount) {
+        this.success = success;
+        this.message = Integer.toString(code);
+        this.data = data;
+        this.errors = new HashMap<String, String>();
+        this.warnings = new HashMap<String, String>();
+        this.readonly = Boolean.FALSE;
+        this.totalCount = totalCount;
     }
 
     public Boolean getSuccess() {
@@ -93,6 +112,20 @@
         this.readonly = readonly;
     }
 
+    /**
+     * @return the totalCount
+     */
+    public int getTotalCount() {
+        return totalCount;
+    }
+
+    /**
+     * @param totalCount the totalCount to set
+     */
+    public void setTotalCount(int totalCount) {
+        this.totalCount = totalCount;
+    }
+
     private HashMap<String, String> convertCodes(Map<String, Integer> codes) {
         HashMap<String, String> converted = new HashMap<String, String>();
         if (codes == null || codes.isEmpty()) {
--- a/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java	Fri Sep 27 14:54:02 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java	Wed Oct 09 14:57:28 2013 +0200
@@ -11,9 +11,12 @@
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
 
 import de.intevation.lada.auth.Authentication;
 import de.intevation.lada.auth.AuthenticationException;
+import de.intevation.lada.data.QueryBuilder;
 import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.SVerwaltungseinheit;
 
@@ -46,7 +49,7 @@
      * @param headers   The HTTP header containing authorization information.
      * @return Response object.
      */
-    @GET
+/*    @GET
     @Produces("text/json")
     public Response findAll(@Context HttpHeaders headers) {
         try {
@@ -59,7 +62,7 @@
             return new Response(false, 699, new ArrayList<SVerwaltungseinheit>());
         }
     }
-
+*/
     /**
      * Request a SVerwaltungseinheit object via its id.
      *
@@ -84,4 +87,37 @@
             return new Response(false, 699, new ArrayList<SVerwaltungseinheit>());
         }
     }
+
+    /**
+     * Request SVerwaltungseinheit objects filtered by the given criteria.
+     *
+     * @param filter    The filter string.
+     * @param headers   The HTTP header containing authorization information.
+     * @return Response object.
+     */
+    @GET
+    @Produces("text/json")
+    public Response filter(
+        @Context UriInfo info,
+        @Context HttpHeaders headers
+    ){
+        try {
+            if (!authentication.isAuthorizedUser(headers)) {
+                return new Response(false, 699, null);
+            }
+            MultivaluedMap<String, String> params = info.getQueryParameters();
+            if (params.isEmpty() || !params.containsKey("query")) {
+                return repository.findAll(SVerwaltungseinheit.class);
+            }
+            String filter = params.getFirst("query");
+            QueryBuilder<SVerwaltungseinheit> builder =
+                new QueryBuilder<SVerwaltungseinheit>(
+                    repository.getEntityManager(), SVerwaltungseinheit.class);
+            builder.andLike("bezeichnung", filter + "%");
+            return repository.filter(builder.getQuery());
+        }
+        catch(AuthenticationException ae) {
+            return new Response(false, 699, null);
+        }
+    }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)