Resolved #50. Added statistics API with KoralQuery input.

Change-Id: Ib62a15e31ff72063e13fff88ca4e36e65ddeb7a8
diff --git a/core/Changes b/core/Changes
index c750d6d..dcaf3dc 100644
--- a/core/Changes
+++ b/core/Changes
@@ -10,6 +10,8 @@
    - Added backward compatibility support for corpusQuery parameter (margaretha)    
 28/08/2019
    - Resolved #49. Added page param check in the search api (margaretha)
+23/09/2019   
+   - Resolved #50. Added statistics API with KoralQuery input (margaretha) 
 
 # version 0.62
 18/03/2019
diff --git a/core/pom.xml b/core/pom.xml
index 0601242..4b30bac 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -298,7 +298,7 @@
 		<dependency>
 			<groupId>de.ids_mannheim.korap</groupId>
 			<artifactId>Krill</artifactId>
-			<version>[0.58.6,)</version>
+			<version>[0.58.7,)</version>
 			<exclusions>
 				<exclusion>
 					<groupId>org.glassfish.jersey.containers</groupId>
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
index 0e2d966..bac5912 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
@@ -281,8 +281,9 @@
      * 
      * @param json
      *            JSON-LD string with potential meta filters.
+     * @throws KustvaktException 
      */
-    public String getStatistics (String json) {
+    public String getStatistics (String json) throws KustvaktException {
         if (index == null) {
             return "{\"documents\" : -1, error\" : \"No index given\" }";
         };
@@ -320,6 +321,11 @@
         catch (IOException e) {
             e.printStackTrace();
         };
+        
+        if (kc.hasErrors()) {
+            throw new KustvaktException(
+                    "{\"errors\":" + kc.getErrors().toJsonString() + "}");
+        }
         // Build json response
         StringBuilder sb = new StringBuilder("{");
         sb.append("\"documents\":").append(docs).append(",\"tokens\":")
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
index 84c4bb8..7ef29f6 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
@@ -2,7 +2,9 @@
 
 import java.util.Locale;
 
+import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
@@ -16,7 +18,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.sun.jersey.spi.container.ResourceFilters;
 
@@ -114,4 +115,27 @@
         }
         return Response.ok(stats).build();
     }
+    
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
+    public Response getStatisticsFromKoralQuery (@Context SecurityContext context,
+            @Context Locale locale, String koralQuery) {
+        if (koralQuery != null && !koralQuery.isEmpty()) {
+            String stats;
+            try {
+                stats = searchKrill.getStatistics(koralQuery);
+                if (stats.contains("-1")){
+                    throw kustvaktResponseHandler.throwit(StatusCodes.NO_RESULT_FOUND);
+                }
+                return Response.ok(stats).build();
+            }
+            catch (KustvaktException e) {
+                throw kustvaktResponseHandler.throwit(e);
+            }
+        }
+        else {
+            throw kustvaktResponseHandler.throwit(StatusCodes.NO_QUERY, 
+                    "Koral query is missing", "koralQuery");
+        }
+    }
 }