[Lada-commits] [PATCH 4 of 5] Added service with filter. Filter uses the sql 'LIKE' operation to request items

Wald Commits scm-commit at wald.intevation.org
Wed Oct 9 14:54:09 CEST 2013


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1381323374 -7200
# Node ID 816291da1ddeb06c878adc5c9b4958139d299c7e
# Parent  ea4d2d685f3263131d32a455b0855be205427db3
Added service with filter. Filter uses the sql 'LIKE' operation to request items.

diff -r ea4d2d685f32 -r 816291da1dde src/main/java/de/intevation/lada/rest/SVerwaltungseinheitService.java
--- 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);
+        }
+    }
 }


More information about the Lada-commits mailing list