[Lada-commits] [PATCH 01 of 15] Use paging parameters to send sublists of the result

Wald Commits scm-commit at wald.intevation.org
Mon Mar 23 17:59:53 CET 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1426753669 -3600
# Node ID 9dd72a2860b50ead88538dbb4ad7d53dd9624153
# Parent  20e87ff455c0f129f25754e45df46b872ffd7a22
Use paging parameters to send sublists of the result.

diff -r 20e87ff455c0 -r 9dd72a2860b5 src/main/java/de/intevation/lada/rest/ProbeService.java
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java	Wed Mar 18 15:44:23 2015 +0100
+++ b/src/main/java/de/intevation/lada/rest/ProbeService.java	Thu Mar 19 09:27:49 2015 +0100
@@ -7,14 +7,18 @@
  */
 package de.intevation.lada.rest;
 
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonException;
 import javax.json.JsonObject;
+import javax.json.JsonReader;
 import javax.persistence.Query;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -110,6 +114,14 @@
         List<String> results = new ArrayList<String>();
         try {
             sql = jsonQuery.getString("sql");
+            if (params.containsKey("sort")) {
+                String sort = params.getFirst("sort");
+                JsonReader reader = Json.createReader(new StringReader(sort));
+                JsonObject sortProperties = reader.readObject();
+                sql += " ORDER BY ";
+                sql += sortProperties.getJsonString("property") + " ";
+                sql += sortProperties.getJsonString("direction");
+            }
             JsonArray jsonFilters = jsonQuery.getJsonArray("filters");
             JsonArray jsonResults = jsonQuery.getJsonArray("result");
             for (int i = 0; i < jsonFilters.size(); i++) {
@@ -130,7 +142,15 @@
             filters,
             params,
             defaultRepo.entityManager("land"));
-        return new Response(true, 200, QueryTools.prepareResult(query.getResultList(), results));
+        List<Map<String, Object>> result =
+            QueryTools.prepareResult(query.getResultList(), results);
+        if (params.containsKey("start") && params.containsKey("limit")) {
+            int start = Integer.valueOf(params.getFirst("start"));
+            int limit = Integer.valueOf(params.getFirst("limit"));
+            List<Map<String, Object>> subList = result.subList(start, limit + start);
+            return new Response(true, 200, subList, result.size());
+        }
+        return new Response(true, 200, result, result.size());
     }
 
     /**


More information about the Lada-commits mailing list