Added delete query webservice and tests.

Change-Id: I5693aa9712a366d3b03ffbf67fa49e6c9da28515
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
index faa150c..41faa2a 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
@@ -98,7 +98,8 @@
                         if (DEBUG) {
                             jlog.debug("Delete existing vc: " + filename);
                         }
-                        vcService.deleteVC("system", vc.getId());
+                        vcService.deleteQueryByName("system", vc.getName(),
+                                vc.getCreatedBy(), QueryType.VIRTUAL_CORPUS);
                     }
                 }
                 catch (KustvaktException e) {
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/QueryDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/QueryDao.java
index 27d3b9f..9fa399c 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/QueryDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/QueryDao.java
@@ -40,7 +40,7 @@
 
 /**
  * QueryDao manages database queries and transactions
- * regarding KorAP queries, e.g. retrieving and storing queries.
+ * regarding virtual corpus and KorAP queries.
  * 
  * @author margaretha
  *
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java b/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
index 2289a47..3d60f21 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
@@ -42,14 +42,22 @@
 import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
 import de.ids_mannheim.korap.utils.ParameterChecker;
 import de.ids_mannheim.korap.web.SearchKrill;
+import de.ids_mannheim.korap.web.controller.QueryReferenceController;
 import de.ids_mannheim.korap.web.controller.VirtualCorpusController;
 import de.ids_mannheim.korap.web.input.QueryJson;
 
 /**
- * VirtualCorpusService handles the logic behind
- * {@link VirtualCorpusController}.
- * It communicates with {@link QueryDao} and returns
- * {@link QueryDto} to {@link VirtualCorpusController}.
+ * QueryService handles the logic behind
+ * {@link VirtualCorpusController} and
+ * {@link QueryReferenceController}. Virtual corpora and
+ * stored-queries are both treated as queries of different types.
+ * Thus, they are handled logically similarly.
+ * 
+ * QueryService communicates with {@link QueryDao}, handles
+ * {@link QueryDO} and
+ * returns
+ * {@link QueryDto} to {@link VirtualCorpusController} and
+ * {@link QueryReferenceController}.
  * 
  * @author margaretha
  *
@@ -158,59 +166,15 @@
         return dtos;
     }
 
-    /**
-     * Only admin and the owner of the query are allowed to
-     * delete a query.
-     * 
-     * @param username
-     *            username
-     * @param queryId
-     *            query id
-     * @throws KustvaktException
-     */
-    @Deprecated
-    public void deleteVC (String username, int vcId) throws KustvaktException {
-
-        QueryDO vc = queryDao.retrieveQueryById(vcId);
-
-        if (vc.getCreatedBy().equals(username) || adminDao.isAdmin(username)) {
-
-            if (vc.getType().equals(ResourceType.PUBLISHED)) {
-                QueryAccess access =
-                        accessDao.retrieveHiddenAccess(vcId);
-                accessDao.deleteAccess(access, "system");
-                userGroupService.deleteAutoHiddenGroup(
-                        access.getUserGroup().getId(), "system");
-            }
-            queryDao.deleteQuery(vc);
-        }
-        else {
-            throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
-                    "Unauthorized operation for user: " + username, username);
-        }
-    }
-
-    /**
-     * Only admin and the owner of the virtual corpus are allowed to
-     * delete a virtual corpus.
-     * 
-     * @param username
-     *            username
-     * @param queryName
-     *            virtual corpus name
-     * @param createdBy
-     *            virtual corpus creator
-     * @throws KustvaktException
-     */
     public void deleteQueryByName (String username, String queryName,
-            String createdBy) throws KustvaktException {
+            String createdBy, QueryType type) throws KustvaktException {
 
         QueryDO query = queryDao.retrieveQueryByName(queryName, createdBy);
 
         if (query == null) {
             String code = createdBy + "/" + queryName;
             throw new KustvaktException(StatusCodes.NO_RESOURCE_FOUND,
-                    "Virtual corpus " + code + " is not found.",
+                    "Query " + code + " is not found.",
                     String.valueOf(code));
         }
         else if (query.getCreatedBy().equals(username)
@@ -223,7 +187,8 @@
                 userGroupService.deleteAutoHiddenGroup(
                         access.getUserGroup().getId(), "system");
             }
-            if (KrillCollection.cache.get(query.getName()) != null) {
+            if (type.equals(QueryType.VIRTUAL_CORPUS)
+                    && KrillCollection.cache.get(query.getName()) != null) {
                 KrillCollection.cache.remove(query.getName());
             }
             queryDao.deleteQuery(query);
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/QueryReferenceController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/QueryReferenceController.java
index 2ebd938..4e90699 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/QueryReferenceController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/QueryReferenceController.java
@@ -3,6 +3,7 @@
 import java.util.List;
 
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
@@ -60,7 +61,8 @@
     /**
      * Creates a query reference according to the given Json.
      * The query reference creator must be the same as the
-     * authenticated username.
+     * authenticated username, except for admins. Admins may create
+     * and update system queries and queries for/of any users.
      * 
      * TODO: In the future, this may also update a query.
      *
@@ -69,9 +71,12 @@
      *            the username of the vc creator, must be the same
      *            as the authenticated username
      * @param qName
-     *           the vc name
-     * @param query a json object describing the query and its properties
-     * @return
+     *            the vc name
+     * @param query
+     *            a json object describing the query and its
+     *            properties
+     * @return HTTP Status 201 Created when creating a new query, or 204
+     *         No Content when updating an existing query.
      * @throws KustvaktException
      */
     @PUT
@@ -130,35 +135,35 @@
     }
 
     /**
-     * Only the VC owner and system admins can delete VC. VCA admins
-     * can delete VC-accesses e.g. of project VC, but not the VC
-     * themselves.
+     * Only the query owner and system admins can delete queries.
+     * Query access admins can delete query-accesses e.g. of project
+     * queries, but not the queries themselves.
      * 
      * @param securityContext
      * @param createdBy
-     *            vc creator
-     * @param vcName
-     *            vc name
+     *            query creator
+     * @param qName
+     *            query name
      * @return HTTP status 200, if successful
      */
-    /*
+    
     @DELETE
-    @Path("~{createdBy}/{vcName}")
+    @Path("~{createdBy}/{qName}")
     public Response deleteVCByName (@Context SecurityContext securityContext,
             @PathParam("createdBy") String createdBy,
-            @PathParam("vcName") String vcName) {
+            @PathParam("qName") String qName) {
         TokenContext context =
                 (TokenContext) securityContext.getUserPrincipal();
         try {
             scopeService.verifyScope(context, OAuth2Scope.DELETE_VC);
-            service.deleteVCByName(context.getUsername(), vcName, createdBy);
+            service.deleteQueryByName(context.getUsername(), qName, createdBy,
+                    QueryType.QUERY);
         }
         catch (KustvaktException e) {
         throw kustvaktResponseHandler.throwit(e);
         }
         return Response.ok().build();
     };
-    */
 
     
     /**
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
index 8f74282..eecb1b3 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
@@ -71,21 +71,27 @@
     private OAuth2ScopeService scopeService;
 
     /**
-     * Updates a vc according to the given VirtualCorpusJson, if the
-     * VC exists, otherwise creates a new VC with the given VC creator
-     * and VC name specified as the path parameters. The vc creator
-     * must be the same as the authenticated username.
+     * Creates a new VC with the given VC creator and VC name
+     * specified as the path parameters. If a VC with the same name
+     * and creator exists, the VC will be updated instead.
      * 
      * VC name cannot be updated.
      * 
+     * The VC creator must be the same as the authenticated username,
+     * except for admins. Admins can create or update system VC as
+     * well as VC for any users.
+     * 
+     * 
      * @param securityContext
      * @param vcCreator
      *            the username of the vc creator, must be the same
-     *            as the authenticated username
+     *            as the authenticated username, except admins
      * @param vcName
-     *           the vc name
-     * @param vc a json object describing the VC
-     * @return
+     *            the vc name
+     * @param vc
+     *            a json object describing the VC
+     * @return HTTP Status 201 Created when creating a new VC, or 204
+     *         No Content when updating an existing VC.
      * @throws KustvaktException
      */
     @PUT
@@ -259,7 +265,8 @@
                 (TokenContext) securityContext.getUserPrincipal();
         try {
             scopeService.verifyScope(context, OAuth2Scope.DELETE_VC);
-            service.deleteQueryByName(context.getUsername(), vcName, createdBy);
+            service.deleteQueryByName(context.getUsername(), vcName, createdBy,
+                    QueryType.VIRTUAL_CORPUS);
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);