Fixed rewrite bugs (updated rewriteQuery & KoralNode)

Change-Id: Ic010e0f6210d34a8fe8dfe15a68f1112423f60eb
diff --git a/core/Changes b/core/Changes
index eb77c1b..91f2034 100644
--- a/core/Changes
+++ b/core/Changes
@@ -9,6 +9,7 @@
    - Fixed KoralNode at method (margaretha)
 11/12/2018
    - Removed document controllers and KustvaktResource (margaretha) 
+   - Fixed rewrite bugs (updated rewriteQuery & KoralNode) (margaretha)   
 
 version 0.61.3
 22/10/2018
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanRewrite.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanRewrite.java
index 0d5e691..7a4ab70 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanRewrite.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanRewrite.java
@@ -18,9 +18,10 @@
 public class CollectionCleanRewrite implements RewriteTask.RewriteNodeAt {
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) {
-        return process(node.rawNode());
+        JsonNode jsonNode = process(node.rawNode());
+        return node.wrapNode(jsonNode);
     }
 
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
index 8562a4b..1048f1b 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
@@ -29,7 +29,7 @@
 
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) throws KustvaktException {
         LayerMapper mapper;
         // EM: do not use DB
@@ -44,7 +44,7 @@
         if (node.get("@type").equals("koral:span")) {
             if (!node.isMissingNode("/wrap")){
                 node = node.at("/wrap");
-                JsonNode term = rewriteQuery(node, config, user);
+                JsonNode term = rewriteQuery(node, config, user).rawNode();
                 node.replaceAt("/wrap", term, new RewriteIdentifier("koral:term", "replace"));
             }
         }
@@ -58,7 +58,7 @@
             if (foundry != null)
                 node.put("foundry", foundry);
         }
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
index e35fd17..4dc91b9 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
@@ -21,14 +21,14 @@
 
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) {
         if (node.get("@type").equals("koral:token")) {
             String s = extractToken(node.rawNode());
             if (s != null && !s.isEmpty())
                 node.put("idn", s + "_" + counter++);
         }
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
index d61247a..0bf497f 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
@@ -11,24 +11,29 @@
  * @author hanl
  * @date 04/07/2015
  */
-public abstract class KoralNode {
+public class KoralNode {
     private JsonNode node;
     private KoralRewriteBuilder rewrites;
     private boolean remove;
 
 
-    private KoralNode (JsonNode node) {
+    public KoralNode (JsonNode node) {
         this.node = node;
         this.rewrites = new KoralRewriteBuilder();
         this.remove = false;
     }
+    
+    public KoralNode (JsonNode node, KoralRewriteBuilder rewrites) {
+        this.node = node;
+        this.rewrites = rewrites;
+        this.remove = false;
+    }
 
 
     public static KoralNode wrapNode (JsonNode node) {
-        return new KoralNode(node) {};
+        return new KoralNode(node);
     }
-
-
+    
     public void buildRewrites (JsonNode node) {
         this.rewrites.build(node);
     }
@@ -151,7 +156,7 @@
     public KoralNode at (String name) {
 //        this.node = this.node.at(name);
 //        return this;
-        return wrapNode( this.node.at(name) );
+        return new KoralNode(this.node.at(name), this.rewrites);
     }
 
 
@@ -291,4 +296,6 @@
 //        this.node = this.node.get(i);
         return this.wrapNode(this.node.get(i));
     }
+    
+    
 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
index 522ac45..f0e90e3 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
@@ -13,7 +13,7 @@
 
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) {
         // redundant
         if (node.rawNode().has("meta")) {
@@ -21,7 +21,7 @@
             //todo: check meta parameter
             System.out.println("HAVE TO CHECK THE META ENTRIES");
         }
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
index b9ff05c..59101c1 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
@@ -283,7 +283,7 @@
                 }
 
                 if (!post & task instanceof RewriteTask.RewriteQuery)
-                    ((RewriteTask.RewriteQuery) task).rewriteQuery(next,
+                    next = ((RewriteTask.RewriteQuery) task).rewriteQuery(next,
                             RewriteHandler.this.config, user);
                 else if (task instanceof RewriteTask.RewriteResult)
                     ((RewriteTask.RewriteResult) task).rewriteResult(next);
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
index 9eab6ca..deb4cd7 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
@@ -29,7 +29,7 @@
          *            checked properly
          * @return
          */
-        JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+        KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
                 User user) throws KustvaktException;
 
     }
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
index 5713570..5235359 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
@@ -60,11 +60,11 @@
 
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) {
         System.out.println("FIND PATH " + node.rawNode().findParent(pointer));
 
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/full/Changes b/full/Changes
index 49d53bd..d2d6755 100644
--- a/full/Changes
+++ b/full/Changes
@@ -17,6 +17,7 @@
    - Implemented revoking all tokens of a user client via a super client
    (margaretha)   
    - Removed document controllers and KustvaktResource (margaretha)
+   - Fixed rewrite bugs (updated rewriteQuery & KoralNode) (margaretha)
 
    
 # version 0.61.3
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
index 3228f76..19040e9 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
@@ -15,7 +15,7 @@
 public class CollectionConstraint implements RewriteTask.IterableRewritePath {
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) {
         if (node.get("@type").equals("koral:doc")) {
             if (node.get("key").equals(Attributes.CORPUS_SIGLE)) {
@@ -27,7 +27,7 @@
                             Attributes.CORPUS_SIGLE, id));
             }
         }
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
index 153c3db..806069b 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
@@ -11,8 +11,8 @@
 import com.google.common.collect.Lists;
 
 import de.ids_mannheim.korap.config.Attributes;
-import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.config.FullConfiguration;
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.query.object.KoralMatchOperator;
 import de.ids_mannheim.korap.query.object.KoralOperation;
@@ -127,7 +127,7 @@
     }
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) throws KustvaktException {
         JsonNode jsonNode = node.rawNode();
 
@@ -188,7 +188,7 @@
         node = node.at("/collection");
         jlog.debug("REWRITES: " + node.toString());
         
-        return node.rawNode();
+        return node;
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
index 320dcdb..55110d0 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
@@ -24,13 +24,13 @@
     private VirtualCorpusService vcService;
 
     @Override
-    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) throws KustvaktException {
         if (node.has("collection")) {
             node = node.at("/collection");
             findVCRef(user.getUsername(), node);
         }
-        return node.rawNode();
+        return node;
     }
 
     private void findVCRef (String username, KoralNode koralNode)
diff --git a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewriteTest.java b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewriteTest.java
index f153baa..28bab56 100644
--- a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewriteTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewriteTest.java
@@ -241,6 +241,7 @@
                 node.at("/collection/operands/1/operands/0/key").asText());
         assertEquals("textClass",
                 node.at("/collection/operands/1/operands/1/key").asText());
+        System.out.println(node);
         assertEquals("koral:rewrite", node.at("/collection/rewrites/0/@type")
                 .asText());
         assertEquals("availability(FREE)", node.at("/collection/rewrites/0/scope")
diff --git a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
index e91dde2..7702beb 100644
--- a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
@@ -36,32 +36,31 @@
     private VirtualCorpusDao dao;
 
     @Test
-    public void testNoRewriteWithCachedVCRef ()
+    public void testCachedVCRef ()
             throws KustvaktException, IOException, QueryException {
         KrillCollection.cache = CacheManager.newInstance().getCache("named_vc");
         vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
 
-        // ClientResponse response =
-        // resource().path(API_VERSION).path("search")
-        // .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
-        // .queryParam("cq", "referTo named-vc1")
-        // .get(ClientResponse.class);
-        //
-        // String ent = response.getEntity(String.class);
-        // JsonNode node = JsonUtils.readTree(ent);
-        // node = node.at("/collection");
-        //
-        // assertEquals("koral:docGroup", node.at("/@type").asText());
-        // assertTrue(node.at("/operands/1/rewrites").isMissingNode());
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+                .queryParam("cq", "referTo named-vc1")
+                .get(ClientResponse.class);
 
-        testNoRewriteWithUsername();
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        node = node.at("/collection");
+
+        assertEquals("koral:docGroup", node.at("/@type").asText());
+        assertTrue(node.at("/operands/1/rewrites").isMissingNode());
+
+        testCachedVCRefWithUsername();
 
         KrillCollection.cache.removeAll();
         VirtualCorpus vc = dao.retrieveVCByName("named-vc1", "system");
         dao.deleteVirtualCorpus(vc);
     }
 
-    private void testNoRewriteWithUsername ()
+    private void testCachedVCRefWithUsername ()
             throws KustvaktException, IOException, QueryException {
 
         ClientResponse response = resource().path(API_VERSION).path("search")
@@ -144,7 +143,6 @@
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-        // System.out.println(node);
         assertEquals("koral:docGroup", node.at("/@type").asText());
         node = node.at("/operands/1/rewrites");
         assertEquals(3, node.size());