Updated the query service to enable editing query references.

Change-Id: Icb01ef78299dd377256b531debb70140cbfc64c9
diff --git a/full/Changes b/full/Changes
index 13707b8..b58f77a 100644
--- a/full/Changes
+++ b/full/Changes
@@ -5,6 +5,7 @@
 - Fixed errors due to missing query type and added tests.
 2021-08-13
 - Fixed missing request entity.
+- Updated the query service to enable editing query references.
 
 # version 0.63.2
 2021-06-11
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 9fa399c..c8ae459 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
@@ -76,34 +76,40 @@
         return q.getId();
     }
 
-    public void editQuery (QueryDO query, String name,
-            ResourceType type, CorpusAccess requiredAccess, String koralQuery,
-            String definition, String description, String status,
-            boolean isCached) throws KustvaktException {
+    public void editQuery (QueryDO queryDO, String name, ResourceType type,
+            CorpusAccess requiredAccess, String koralQuery, String definition,
+            String description, String status, boolean isCached,
+            String queryStr, String queryLanguage) throws KustvaktException {
 
         if (name != null && !name.isEmpty()) {
-            query.setName(name);
+            queryDO.setName(name);
         }
         if (type != null) {
-            query.setType(type);
+            queryDO.setType(type);
         }
         if (requiredAccess != null) {
-            query.setRequiredAccess(requiredAccess);
+            queryDO.setRequiredAccess(requiredAccess);
         }
         if (koralQuery != null) {
-            query.setKoralQuery(koralQuery);
+            queryDO.setKoralQuery(koralQuery);
         }
         if (definition != null && !definition.isEmpty()) {
-            query.setDefinition(definition);
+            queryDO.setDefinition(definition);
         }
         if (description != null && !description.isEmpty()) {
-            query.setDescription(description);
+            queryDO.setDescription(description);
         }
         if (status != null && !status.isEmpty()) {
-            query.setStatus(status);
+            queryDO.setStatus(status);
         }
-        query.setCached(isCached);
-        entityManager.merge(query);
+        if(queryStr!=null && !queryStr.isEmpty()) {
+            queryDO.setQuery(queryStr);
+        }
+        if(queryLanguage!=null && !queryLanguage.isEmpty()) {
+            queryDO.setQueryLanguage(queryLanguage);
+        }
+        queryDO.setCached(isCached);
+        entityManager.merge(queryDO);
     }
 
     public void deleteQuery (QueryDO query)
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 c8961934..f54dd92 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
@@ -227,11 +227,17 @@
         String koralQuery = null;
         CorpusAccess requiredAccess = null;
         String corpusQuery = newQuery.getCorpusQuery();
+        String query = newQuery.getQuery();
+        String queryLanguage = newQuery.getQueryLanguage();
         if (corpusQuery != null && !corpusQuery.isEmpty()) {
             koralQuery = serializeCorpusQuery(corpusQuery);
             requiredAccess = determineRequiredAccess(newQuery.isCached(), queryName,
                     koralQuery);
         }
+        else if (query != null && !query.isEmpty() && queryLanguage != null
+                && !queryLanguage.isEmpty()) {
+            koralQuery = serializeQuery(query, queryLanguage);
+        }
 
         ResourceType type = newQuery.getType();
         if (type != null) {
@@ -256,7 +262,7 @@
 
         queryDao.editQuery(existingQuery, queryName, type, requiredAccess,
                 koralQuery, newQuery.getDefinition(), newQuery.getDescription(),
-                newQuery.getStatus(), newQuery.isCached());
+                newQuery.getStatus(), newQuery.isCached(), query, queryLanguage);
     }
 
     private void publishQuery (int queryId) throws KustvaktException {
@@ -471,7 +477,7 @@
             }
 
             queryDao.editQuery(query, null, ResourceType.PROJECT, null, null,
-                    null, null, null, query.isCached());
+                    null, null, null, query.isCached(), null, null);
         }
     }
 
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/QueryReferenceControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/QueryReferenceControllerTest.java
index 0637b0c..260d796 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/QueryReferenceControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/QueryReferenceControllerTest.java
@@ -26,17 +26,16 @@
     private String testUser = "qRefControllerTest";
     private String adminUser = "admin";
 
-    private void checkAndDeleteQuery (JsonNode node, String qName, String query,
+    private void checkQuery (String qName, String query,
             String username, ResourceType resourceType, CorpusAccess access)
             throws KustvaktException {
+        JsonNode node = testRetrieveQueryByName(username, username, qName);
         assertEquals(qName, node.at("/name").asText());
         assertEquals(resourceType.displayName(), node.at("/type").asText());
         assertEquals(username, node.at("/createdBy").asText());
         assertEquals(query, node.at("/query").asText());
         assertEquals("poliqarp", node.at("/queryLanguage").asText());
         assertEquals(access.name(), node.at("/requiredAccess").asText());
-
-        testDeleteQueryByName(qName, username);
     }
     
     @Test
@@ -56,12 +55,36 @@
 
         assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
 
-        JsonNode node = testRetrieveQueryByName(testUser, testUser, qName);
-        checkAndDeleteQuery(node, qName, "der", testUser, ResourceType.PRIVATE,
+        checkQuery(qName, "der", testUser, ResourceType.PRIVATE,
                 CorpusAccess.PUB);
+        testDeleteQueryByName(qName, testUser);
     }
 
     @Test
+    public void testCreatePublishQuery () throws KustvaktException {
+        String json = "{\"type\": \"PUBLISHED\"" 
+                + ",\"queryType\": \"QUERY\""
+                + ",\"queryLanguage\": \"poliqarp\"" 
+                + ",\"query\": \"Regen\"}";
+
+        String qName = "publish_query";
+        ClientResponse response = resource().path(API_VERSION).path("query")
+                .path("~" + testUser).path(qName)
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue(testUser, "pass"))
+                .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
+                .put(ClientResponse.class, json);
+
+        assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
+
+        checkQuery(qName, "Regen", testUser, ResourceType.PUBLISHED,
+                CorpusAccess.PUB);
+        testDeleteQueryByName(qName, testUser);
+        // check if hidden group has been created
+        
+    }
+    
+    @Test
     public void testCreateUserQueryByAdmin () throws KustvaktException {
         String json = "{\"type\": \"PRIVATE\""
                 + ",\"queryType\": \"QUERY\""
@@ -96,9 +119,30 @@
                 .entity(json).put(ClientResponse.class);
 
         assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-        testDeleteQueryByName(qName, "admin");
+        
+        checkQuery(qName, "Sommer", adminUser, ResourceType.SYSTEM, CorpusAccess.PUB);
+        testUpdateQuery(qName);
     }
     
+    private void testUpdateQuery (String qName)
+            throws UniformInterfaceException, ClientHandlerException,
+            KustvaktException {
+        String json = "{\"query\": \"Sonne\""
+                + ",\"queryLanguage\": \"poliqarp\"}";
+        
+        ClientResponse response = resource().path(API_VERSION).path("query")
+                .path("~admin").path(qName)
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue(adminUser, "pass"))
+                .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
+                .entity(json).put(ClientResponse.class);
+        
+        assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
+        
+        checkQuery(qName, "Sonne", adminUser, ResourceType.SYSTEM, CorpusAccess.PUB);
+        testDeleteQueryByName(qName, adminUser);
+    }
+
     @Test
     public void testCreateSystemQueryUnauthorized () throws KustvaktException {
         String json = "{\"type\": \"SYSTEM\""
@@ -139,9 +183,9 @@
 
         assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
 
-        JsonNode node = testRetrieveQueryByName(testUser, testUser, qName);
-        checkAndDeleteQuery(node, qName, "Sohn", testUser, ResourceType.PRIVATE,
+        checkQuery(qName, "Sohn", testUser, ResourceType.PRIVATE,
                 CorpusAccess.PUB);
+        testDeleteQueryByName(qName, testUser);
     }
     
     @Test