Added cq parameter to VC statistics API and deprecate corpusQuery.

Change-Id: Ic3c7e0827ba1a1070d86bceacc92bab20708ee18
diff --git a/core/Changes b/core/Changes
index 5051644..038b9ce 100644
--- a/core/Changes
+++ b/core/Changes
@@ -4,6 +4,8 @@
      issue #43)
    - Disabled some unused web-services: search post, query serialization, 
      collocation base (margaretha)
+11/07/2019
+   - Added cq parameter to VC statistics API and deprecate corpusQuery (margaretha) 
 
 # version 0.62
 18/03/2019
diff --git a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
index e7a5195..a8cba15 100644
--- a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
@@ -28,6 +28,7 @@
     public static final int UNSUPPORTED_API_VERSION = 112;
     public static final int NON_UNIQUE_RESULT_FOUND = 113;
     public static final int NO_RESOURCE_FOUND = 114;
+    public static final int DEPRECATED_PARAMETER = 115;
     
     /**
      * 200 status codes general JSON serialization error
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 7082fb8..66eb27a 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
@@ -16,10 +16,14 @@
 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;
 
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.response.Notifications;
+import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
 import de.ids_mannheim.korap.web.CoreResponseHandler;
 import de.ids_mannheim.korap.web.SearchKrill;
@@ -57,34 +61,54 @@
      *            SecurityContext
      * @param locale
      *            Locale
-     * @param corpusQuery
+     * @param cq
      *            a collection query specifying a virtual corpus
+     * @param corpusQuery
+     *            (DEPRECATED) a collection query specifying a virtual corpus 
      * @return statistics of the virtual corpus defined by the given
      *         corpusQuery parameter.
      */
     @GET
     public Response getStatistics (@Context SecurityContext context,
             @Context Locale locale,
+            @QueryParam("cq") String cq,
             @QueryParam("corpusQuery") String corpusQuery) {
 
         KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
 
         String stats;
-        if (corpusQuery != null && !corpusQuery.isEmpty()) {
-            builder.with(corpusQuery);
-            String json = null;
-            try {
+        String json = null;
+        boolean isDeprecated = false;
+        boolean hasCq = false;
+        try {
+            if (cq != null && !cq.isEmpty()) {
+                builder.with(cq);
                 json = builder.toJSON();
+                hasCq = true;
             }
-            catch (KustvaktException e) {
-                throw kustvaktResponseHandler.throwit(e);
+            if (corpusQuery != null && !corpusQuery.isEmpty()) {
+                isDeprecated = true;
+                if (!hasCq) {
+                    builder.with(corpusQuery);
+                    json = builder.toJSON();
+                }
             }
             stats = searchKrill.getStatistics(json);
+            
+            if (isDeprecated){
+                Notifications n = new Notifications();
+                n.addWarning(StatusCodes.DEPRECATED_PARAMETER,
+                        "Parameter corpusQuery is deprecated in favor of cq.");
+                ObjectNode warning = (ObjectNode) n.toJsonNode();
+                ObjectNode node = (ObjectNode) JsonUtils.readTree(stats);
+                node.setAll(warning);
+                stats = node.toString();
+            }
         }
-        else {
-            stats = searchKrill.getStatistics(null);
-        };
-
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
+        
         if (stats.contains("-1")) {
             throw kustvaktResponseHandler.throwit(StatusCodes.NO_RESULT_FOUND);
         }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
index be6c1d6..f0d2bd1 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
@@ -12,10 +12,12 @@
 import com.sun.jersey.api.client.ClientResponse;
 
 import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.utils.JsonUtils;
 
 /** 
  * @author margaretha, diewald
- * @date 27/09/2017
  *
  */
 public class StatisticsControllerTest extends SpringJerseyTest {
@@ -40,6 +42,38 @@
         assertEquals(node.get("tokens").asInt(),0);
     }
 
+    @Test
+    public void testStatisticsWithCq () throws KustvaktException{
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
+                .method("GET", ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(query);
+        assertEquals(2, node.at("/documents").asInt());
+        assertEquals(138180, node.at("/tokens").asInt());
+        assertEquals(5687, node.at("/sentences").asInt());
+        assertEquals(258, node.at("/paragraphs").asInt());
+    }
+    
+    @Test
+    public void testStatisticsWithCqAndCorpusQuery () throws KustvaktException{
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
+                .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE")
+                .method("GET", ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(query);
+        assertEquals(2, node.at("/documents").asInt());
+        assertEquals(138180, node.at("/tokens").asInt());
+        assertEquals(5687, node.at("/sentences").asInt());
+        assertEquals(258, node.at("/paragraphs").asInt());
+    }
 
     @Test
     public void testGetStatisticsWithcorpusQuery1 ()
@@ -56,6 +90,11 @@
         JsonNode node = mapper.readTree(ent);
         assertEquals(node.get("documents").asInt(),11);
         assertEquals(node.get("tokens").asInt(),665842);
+        
+        assertEquals(StatusCodes.DEPRECATED_PARAMETER,
+                node.at("/warnings/0/0").asInt());
+        assertEquals("Parameter corpusQuery is deprecated in favor of cq.",
+                node.at("/warnings/0/1").asText());
     }
 
 
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
index 3956acd..b03e28d 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
@@ -9,11 +9,50 @@
 
 import de.ids_mannheim.korap.config.LiteJerseyTest;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.utils.JsonUtils;
 
 public class LiteStatisticControllerTest extends LiteJerseyTest{
 
     @Test
+    public void testStatisticsWithCq () throws KustvaktException{
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
+                .method("GET", ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(query);
+        assertEquals(2, node.at("/documents").asInt());
+        assertEquals(138180, node.at("/tokens").asInt());
+        assertEquals(5687, node.at("/sentences").asInt());
+        assertEquals(258, node.at("/paragraphs").asInt());
+    }
+    
+    @Test
+    public void testStatisticsWithCqAndCorpusQuery () throws KustvaktException{
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
+                .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE")
+                .method("GET", ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(query);
+        assertEquals(2, node.at("/documents").asInt());
+        assertEquals(138180, node.at("/tokens").asInt());
+        assertEquals(5687, node.at("/sentences").asInt());
+        assertEquals(258, node.at("/paragraphs").asInt());
+        
+        assertEquals(StatusCodes.DEPRECATED_PARAMETER,
+                node.at("/warnings/0/0").asInt());
+        assertEquals("Parameter corpusQuery is deprecated in favor of cq.",
+                node.at("/warnings/0/1").asText());
+    }
+    
+    @Test
     public void testStatistics () throws KustvaktException{
         ClientResponse response = resource().path(API_VERSION)
                 .path("statistics")