Address several rewrite issues.

Update VirtualCorpusRewrite Remove Owner (#779)
Add source to QueryReferenceRewrite (#783) and VirtualCorpusRewrite
(#780)

Change-Id: I53fbae553758d8319d9438afe761f3cc7299cdd0
diff --git a/Changes b/Changes
index 43009a8..3b27f54 100644
--- a/Changes
+++ b/Changes
@@ -5,6 +5,8 @@
 - Remove hidden group from test database
 - Implemented QueryContextRewrite (#755)
 - Replace operation:insertion with operation:injection (#778) 
+- Update VirtualCorpusRewrite Remove Owner (#779)
+- Add source to QueryReferenceRewrite (#783) and VirtualCorpusRewrite (#780).
 
 
 # version 0.75
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java b/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java
index 52eb5dc..c7ca653 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java
@@ -74,11 +74,8 @@
             set = true;
         }
 
-        if (ident != null)
-            identifier = ident.toString();
-
         if (set) {
-            this.rewrites.add("deletion", identifier);
+            this.rewrites.add("deletion", "", ident);
         }
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java
index 9521c72..c682e1b 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java
@@ -93,9 +93,9 @@
             throws KustvaktException {
         JsonNode jsonNode = koralNode.rawNode();
         koralNode.remove("@type",
-                new RewriteIdentifier("@type", jsonNode.at("/@type").asText()));
+                new RewriteIdentifier("@type", "", jsonNode.at("/@type").asText()));
         koralNode.remove("ref",
-                new RewriteIdentifier("ref", jsonNode.at("/ref").asText()));
+                new RewriteIdentifier("ref", "", jsonNode.at("/ref").asText()));
         koralNode.setAll((ObjectNode) qref);
     }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
index 2fe7502..1cdbeef 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
@@ -99,11 +99,8 @@
             KoralNode koralNode) throws KustvaktException {
         JsonNode jsonNode = koralNode.rawNode();
         String ref = jsonNode.at("/ref").asText();
-        koralNode.remove("ref", new RewriteIdentifier("ref", ref));
-
-        ref = ref.substring(vcOwner.length() + 1, ref.length());
-        koralNode.put("ref", ref);
-//        koralNode.set("ref", ref, new RewriteIdentifier("ref", ref));
+        String newRef = ref.substring(vcOwner.length() + 1, ref.length());
+        koralNode.replace("ref", newRef, new RewriteIdentifier("ref", "", ref));
     }
 
     private void rewriteVC (QueryDO vc, KoralNode koralNode)
@@ -112,10 +109,10 @@
         JsonNode kq = JsonUtils.readTree(koralQuery).at("/collection");
         JsonNode jsonNode = koralNode.rawNode();
         // rewrite
-        koralNode.remove("@type",
-                new RewriteIdentifier("@type", jsonNode.at("/@type").asText()));
+        koralNode.remove("@type", new RewriteIdentifier("@type", "",
+                jsonNode.at("/@type").asText()));
         koralNode.remove("ref",
-                new RewriteIdentifier("ref", jsonNode.at("/ref").asText()));
+                new RewriteIdentifier("ref", "", jsonNode.at("/ref").asText()));
         koralNode.setAll((ObjectNode) kq);
     }
 }
diff --git a/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java b/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java
index 7f923d6..dad58bd 100644
--- a/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java
+++ b/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java
@@ -29,7 +29,8 @@
         ObjectNode node = JsonUtils.createObjectNode();
         node.put("value_1", "setting_1");
         KoralNode knode = KoralNode.wrapNode(node);
-        knode.remove("value_1", null);
+        knode.remove("value_1",
+                new RewriteIdentifier("value_1", "", "settings_1"));
         assertEquals(knode.rawNode().toString(), "{}");
     }
 
diff --git a/src/test/java/de/ids_mannheim/korap/rewrite/QueryRewriteTest.java b/src/test/java/de/ids_mannheim/korap/rewrite/QueryRewriteTest.java
index 524dad5..5a3ee46 100644
--- a/src/test/java/de/ids_mannheim/korap/rewrite/QueryRewriteTest.java
+++ b/src/test/java/de/ids_mannheim/korap/rewrite/QueryRewriteTest.java
@@ -25,8 +25,8 @@
                 .request().get();
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
-        assertEquals(node.at("/errors/0/1").asText(),
-                "Query system/examplequery is not found.");
+        assertEquals("Query system/examplequery is not found.",
+                node.at("/errors/0/1").asText());
     }
 
     @Test
@@ -36,8 +36,19 @@
                 .resolveTemplate("q", "[orth=der]{#system-q} Baum").request()
                 .get();
         String ent = response.readEntity(String.class);
-        // System.out.println(ent);
         JsonNode node = JsonUtils.readTree(ent);
+        
+        node = node.at("/query/operands/1/rewrites");
+        assertEquals(3,node.size());
+        assertEquals("Kustvakt", node.at("/0/origin").asText());
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("@type", node.at("/0/scope").asText());
+        assertEquals("koral:queryRef", node.at("/0/source").asText());
+
+        assertEquals("ref", node.at("/1/scope").asText());
+        assertEquals("system-q", node.at("/1/source").asText());
+        
+        assertEquals("operation:injection", node.at("/2/operation").asText());
     }
 
     @Test
@@ -53,7 +64,16 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertEquals(node.at("/query/operands/1/@type").asText(),
                 "koral:token");
-        assertEquals(node.at("/query/operands/1/rewrites/0/scope").asText(),
-                "@type(koral:queryRef)");
+        
+        node = node.at("/query/operands/1/rewrites");
+        assertEquals("Kustvakt", node.at("/0/origin").asText());
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("@type", node.at("/0/scope").asText());
+        assertEquals("koral:queryRef", node.at("/0/source").asText());
+
+        assertEquals("ref", node.at("/1/scope").asText());
+        assertEquals("dory/dory-q", node.at("/1/source").asText());
+        
+        assertEquals("operation:injection", node.at("/2/operation").asText());
     }
 }
diff --git a/src/test/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewriteTest.java b/src/test/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewriteTest.java
index b3a308f..1f9c049 100644
--- a/src/test/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewriteTest.java
+++ b/src/test/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewriteTest.java
@@ -46,8 +46,9 @@
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-        assertEquals(node.at("/@type").asText(), "koral:docGroup");
+        assertEquals("koral:docGroup", node.at("/@type").asText());
         assertTrue(node.at("/operands/1/rewrites").isMissingNode());
+        
         testRefCachedVCWithUsername();
         QueryDO vc = dao.retrieveQueryByName("named-vc1", "system");
         dao.deleteQuery(vc);
@@ -68,9 +69,13 @@
         node = node.at("/collection");
         assertEquals(node.at("/@type").asText(), "koral:docGroup");
         node = node.at("/operands/1/rewrites");
-        assertEquals(2, node.size());
-        assertEquals(node.at("/0/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/1/operation").asText(), "operation:injection");
+        
+        assertEquals(1, node.size());
+        assertEquals("koral:rewrite", node.at("/0/@type").asText());
+        assertEquals("Kustvakt", node.at("/0/origin").asText());
+        assertEquals("operation:override", node.at("/0/operation").asText());
+        assertEquals("ref", node.at("/0/scope").asText());
+        assertEquals("system/named-vc1", node.at("/0/source").asText());
     }
 
     @Test
@@ -82,16 +87,23 @@
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-        assertEquals(node.at("/@type").asText(), "koral:docGroup");
-        assertEquals(node.at("/operands/0/@type").asText(), "koral:doc");
-        assertEquals(node.at("/operands/1/@type").asText(), "koral:doc");
-        assertEquals(node.at("/operands/1/value").asText(), "GOE");
-        assertEquals(node.at("/operands/1/key").asText(), "corpusSigle");
+        assertEquals("koral:docGroup", node.at("/@type").asText());
+        assertEquals("koral:doc", node.at("/operands/0/@type").asText());
+        assertEquals("koral:doc", node.at("/operands/1/@type").asText());
+        assertEquals("GOE", node.at("/operands/1/value").asText());
+        assertEquals("corpusSigle", node.at("/operands/1/key").asText());
         node = node.at("/operands/1/rewrites");
+
         assertEquals(3, node.size());
-        assertEquals(node.at("/0/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/1/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/2/operation").asText(), "operation:injection");
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("@type", node.at("/0/scope").asText());
+        assertEquals("koral:docGroupRef", node.at("/0/source").asText());
+
+        assertEquals("operation:deletion", node.at("/1/operation").asText());
+        assertEquals("ref", node.at("/1/scope").asText());
+        assertEquals("system-vc", node.at("/1/source").asText());
+    
+        assertEquals("operation:injection", node.at("/2/operation").asText());
     }
 
     @Test
@@ -105,13 +117,20 @@
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-        assertEquals(node.at("/@type").asText(), "koral:docGroup");
-        assertEquals(node.at("/operands/0/@type").asText(), "koral:docGroup");
+        assertEquals("koral:docGroup", node.at("/@type").asText());
+        assertEquals("koral:docGroup", node.at("/operands/0/@type").asText());
         node = node.at("/operands/1/rewrites");
+
         assertEquals(3, node.size());
-        assertEquals(node.at("/0/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/1/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/2/operation").asText(), "operation:injection");
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("@type", node.at("/0/scope").asText());
+        assertEquals("koral:docGroupRef", node.at("/0/source").asText());
+
+        assertEquals("operation:deletion", node.at("/1/operation").asText());
+        assertEquals("ref", node.at("/1/scope").asText());
+        assertEquals("system/system-vc", node.at("/1/source").asText());
+
+        assertEquals("operation:injection", node.at("/2/operation").asText());
     }
 
     @Test
@@ -126,7 +145,7 @@
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-        assertEquals(node.at("/@type").asText(), "koral:docGroup");
+        assertEquals("koral:docGroup",node.at("/@type").asText());
         node = node.at("/operands/1/rewrites");
         assertEquals(3, node.size());
     }
diff --git a/src/test/java/de/ids_mannheim/korap/web/controller/SearchPublicMetadataTest.java b/src/test/java/de/ids_mannheim/korap/web/controller/SearchPublicMetadataTest.java
index cd5e617..92b0ced 100644
--- a/src/test/java/de/ids_mannheim/korap/web/controller/SearchPublicMetadataTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/controller/SearchPublicMetadataTest.java
@@ -125,11 +125,16 @@
         assertEquals(node.at("/key").asText(), "corpusSigle");
         assertEquals(node.at("/rewrites/0/operation").asText(),
                 "operation:deletion");
-        assertEquals(node.at("/rewrites/0/scope").asText(),
-                "@type(koral:docGroupRef)");
+        assertEquals(node.at("/rewrites/0/scope").asText(), "@type");
+        assertEquals(node.at("/rewrites/0/source").asText(),
+                "koral:docGroupRef");
+        
         assertEquals(node.at("/rewrites/1/operation").asText(),
                 "operation:deletion");
-        assertEquals(node.at("/rewrites/1/scope").asText(), "ref(system-vc)");
+        assertEquals(node.at("/rewrites/1/scope").asText(), "ref");
+        assertEquals(node.at("/rewrites/1/source").asText(), "system-vc");
+        
+        
         assertEquals(node.at("/rewrites/2/operation").asText(),
                 "operation:injection");
     }
diff --git a/src/test/java/de/ids_mannheim/korap/web/controller/vc/VirtualCorpusReferenceTest.java b/src/test/java/de/ids_mannheim/korap/web/controller/vc/VirtualCorpusReferenceTest.java
index 76377a2..1394d30 100644
--- a/src/test/java/de/ids_mannheim/korap/web/controller/vc/VirtualCorpusReferenceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/controller/vc/VirtualCorpusReferenceTest.java
@@ -190,9 +190,13 @@
         node = node.at("/collection/operands/1/rewrites");
         assertEquals(3, node.size());
         assertEquals(node.at("/0/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/0/scope").asText(), "@type(koral:docGroupRef)");
+        assertEquals(node.at("/0/scope").asText(), "@type");
+        assertEquals(node.at("/0/source").asText(), "koral:docGroupRef");
+        
         assertEquals(node.at("/1/operation").asText(), "operation:deletion");
-        assertEquals(node.at("/1/scope").asText(), "ref(marlin/published-vc)");
+        assertEquals(node.at("/1/scope").asText(), "ref");
+        assertEquals(node.at("/1/source").asText(), "marlin/published-vc");
+        
         assertEquals(node.at("/2/operation").asText(), "operation:injection");
     }