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);
         }