changeset 455:01d6da858394

Updated GET service for probe objects. Service is using filters now.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 11 Feb 2015 15:27:17 +0100
parents 13ed418234f9
children dbeb56e913fd
files src/main/java/de/intevation/lada/query/QueryTools.java src/main/java/de/intevation/lada/rest/ProbeService.java
diffstat 2 files changed, 82 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/query/QueryTools.java	Wed Feb 11 15:26:06 2015 +0100
+++ b/src/main/java/de/intevation/lada/query/QueryTools.java	Wed Feb 11 15:27:17 2015 +0100
@@ -11,13 +11,18 @@
 import java.io.InputStream;
 import java.io.StringReader;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonException;
 import javax.json.JsonObject;
 import javax.json.JsonReader;
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import javax.ws.rs.core.MultivaluedMap;
 
 
 /**
@@ -133,4 +138,37 @@
             return null;
         }
     }
+
+    public static Query prepareQuery(
+        String sql,
+        List<String> filters,
+        MultivaluedMap<String, String> params,
+        EntityManager manager
+    ) {
+        Query query = manager.createNativeQuery(sql);
+        for (String filter: filters) {
+            List<String> param = params.get(filter);
+            List<String> clean = new ArrayList<String>();
+            for(String p : param) {
+                clean.add(p.replace(",", "|"));
+            }
+            query.setParameter(filter, clean);
+        }
+        return query;
+    }
+
+    public static List<Map<String, Object>> prepareResult(
+        List<Object[]> result,
+        List<String> names
+    ) {
+        List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();
+        for (Object[] row: result) {
+            Map<String, Object> set = new HashMap<String, Object>();
+            for (int i = 0; i < row.length; i++) {
+                set.put(names.get(i), row[i]);
+            }
+            ret.add(set);
+        }
+        return ret;
+    }
 }
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 11 15:26:06 2015 +0100
+++ b/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Feb 11 15:27:17 2015 +0100
@@ -7,10 +7,15 @@
  */
 package de.intevation.lada.rest;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.json.JsonArray;
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.persistence.Query;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -21,11 +26,14 @@
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.log4j.Logger;
 
 import de.intevation.lada.model.land.LProbe;
 import de.intevation.lada.model.land.ProbeTranslation;
+import de.intevation.lada.query.QueryTools;
 import de.intevation.lada.util.annotation.AuthenticationConfig;
 import de.intevation.lada.util.annotation.AuthorizationConfig;
 import de.intevation.lada.util.annotation.RepositoryConfig;
@@ -72,15 +80,49 @@
      *
      * @return Response object containing all probe objects.
      */
+    @SuppressWarnings("unchecked")
     @GET
     @Path("/")
     @Produces("application/json")
-    public Response get(@Context HttpHeaders headers) {
+    public Response get(
+        @Context HttpHeaders headers,
+        @Context UriInfo info
+    ) {
         if (!authentication.isAuthenticated(headers)) {
             logger.debug("User is not authenticated!");
             return new Response(false, 699, null);
         }
-        return defaultRepo.getAll(LProbe.class, "land");
+        MultivaluedMap<String, String> params = info.getQueryParameters();
+        if (params.isEmpty() || !params.containsKey("qid")) {
+            return defaultRepo.getAll(LProbe.class, "land");
+        }
+        String qid = params.getFirst("qid");
+        JsonObject jsonQuery = QueryTools.getQueryById(qid);
+        String sql = "";
+        List<String> filters = new ArrayList<String>();
+        List<String> results = new ArrayList<String>();
+        try {
+            sql = jsonQuery.getString("sql");
+            JsonArray jsonFilters = jsonQuery.getJsonArray("filters");
+            JsonArray jsonResults = jsonQuery.getJsonArray("result");
+            for (int i = 0; i < jsonFilters.size(); i++) {
+                filters.add(
+                    jsonFilters.getJsonObject(i).getString("dataIndex"));
+            }
+            for (int i = 0; i < jsonResults.size(); i++) {
+                results.add(
+                    jsonResults.getJsonObject(i).getString("dataIndex"));
+            }
+        }
+        catch (JsonException je) {
+            return new Response(false, 603, new ArrayList<Object>());
+        }
+        Query query = QueryTools.prepareQuery(
+            sql,
+            filters,
+            params,
+            defaultRepo.entityManager("land"));
+        return new Response(true, 200, QueryTools.prepareResult(query.getResultList(), results));
     }
 
     /**
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)