changeset 382:816291da1dde

Added service with filter. Filter uses the sql 'LIKE' operation to request items.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 09 Oct 2013 14:56:14 +0200
parents ea4d2d685f32
children 701b52518dcd
files src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java
diffstat 1 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java	Wed Oct 09 14:54:34 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java	Wed Oct 09 14:56:14 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)