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