Update scope and add _comment as discussed in #782

Change-Id: I6b40f6208021151c48460797360b11eab6c33e62
diff --git a/Changes b/Changes
index ddf3b85..c710501 100644
--- a/Changes
+++ b/Changes
@@ -15,6 +15,7 @@
 - Improve AvailabilityRewrite and implement operation:override (#781) 
 - Disabled mysql connector Java due to CVE-2023-22102
 - Replace origin with editor in rewrites (#782)
+- Update scope and add _comment as discussed in #782
   
 
 # version 0.75
diff --git a/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java b/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
index 031c3c1..e76d8c9 100644
--- a/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
+++ b/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
@@ -210,7 +210,7 @@
             if (DEBUG) {
                 jlog.debug("setAccessAndLocation: DemoUser: location="
                         + user.locationtoString() + " access="
-                        + user.accesstoString());
+                        + user.getCorpusAccess());
             }
             return;
         }
diff --git a/src/main/java/de/ids_mannheim/korap/core/service/SearchService.java b/src/main/java/de/ids_mannheim/korap/core/service/SearchService.java
index 8d87458..8e8c127 100644
--- a/src/main/java/de/ids_mannheim/korap/core/service/SearchService.java
+++ b/src/main/java/de/ids_mannheim/korap/core/service/SearchService.java
@@ -120,7 +120,7 @@
         if (DEBUG) {
             if (user != null) {
                 jlog.debug("Debug: user location=" + user.locationtoString()
-                        + ", access=" + user.accesstoString());
+                        + ", access=" + user.getCorpusAccess());
             }
         }
         return user;
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/AvailabilityRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/AvailabilityRewrite.java
index e4306eb..934efd0 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/AvailabilityRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/AvailabilityRewrite.java
@@ -3,19 +3,18 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.text.CaseUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.google.common.collect.Lists;
 
-import de.ids_mannheim.korap.config.Attributes;
 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;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.user.User.CorpusAccess;
 import de.ids_mannheim.korap.utils.JsonUtils;
@@ -112,6 +111,7 @@
 
         FullConfiguration fullConfig = (FullConfiguration) config;
         CorpusAccess corpusAccess = user.getCorpusAccess();
+        String corpusAccessName = user.accesstoString();
 		List<String> availabilityRules = getAvailabilityRules(corpusAccess,
 				fullConfig);
 
@@ -126,7 +126,7 @@
                 		availabilityRules, actualAvalability, false);
                 if (!actualAvalability.isEmpty()) {
                 	createOperationAnd(availabilityQuery, jsonNode,
-    						corpusAccess.name(), koralNode);
+    						corpusAccessName, koralNode);
                 	
 //                    builder.with(availabilityQuery);
 //                    builder.setBaseQuery(builder.toJSON());
@@ -136,7 +136,7 @@
 			}
 			else {
 				createOperationAnd(availabilityQuery, jsonNode,
-						corpusAccess.name(), koralNode);
+						corpusAccessName, koralNode);
 			}
         }
 		else {
@@ -145,8 +145,9 @@
 			builder.with(availabilityQuery);
 			JsonNode rewrittenNode = JsonUtils.readTree(builder.toJSON())
 					.at("/collection");
-			RewriteIdentifier identifier = new KoralNode.RewriteIdentifier(
-					Attributes.AVAILABILITY, corpusAccess.name());
+			
+			RewriteIdentifier identifier = new RewriteIdentifier(null, null,
+						corpusAccessName + " corpus access policy has been added.");
 			koralNode.set("collection", rewrittenNode, identifier);
 		}
 
@@ -155,7 +156,7 @@
     }
     
     private void createOperationAnd (String availabilityQuery,
-			JsonNode jsonNode, String corpusAccess, KoralNode node)
+			JsonNode jsonNode, String corpusAccessName, KoralNode node)
 			throws KustvaktException {
 
 		KoralCollectionQueryBuilder availabilityBuilder = 
@@ -171,8 +172,8 @@
 		// Base query must contains collection
 		builder.setBaseQuery(availabilityNode);
 		JsonNode rewrittenNode = builder.mergeWith(jsonNode).at("/collection");
-		RewriteIdentifier identifier = new KoralNode.RewriteIdentifier(null,
-				corpusAccess, sourceNode);
+		RewriteIdentifier identifier = new RewriteIdentifier(null, sourceNode,
+				corpusAccessName + " corpus access policy has been added.");
 		node.replace("collection", rewrittenNode, identifier);
 	}
     
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java b/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
index d2d3610..979602e 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
@@ -24,8 +24,8 @@
                 // EM: MH checks if user has access to corpus
                 //                KustvaktResource corpus = check(id, user);
                 //                if (corpus == null)
-                node.removeNode(new KoralNode.RewriteIdentifier(
-                        Attributes.CORPUS_SIGLE, id));
+                node.removeNode(new RewriteIdentifier(
+                        Attributes.CORPUS_SIGLE, id, "corpus sigle"));
             }
         }
         return node;
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java b/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
index 73615e0..846d2e5 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
@@ -6,7 +6,6 @@
 
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.user.UserSettingProcessor;
 
@@ -20,35 +19,40 @@
     protected LayerMapper mapper;
 
     @Override
-    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+    public KoralNode rewriteQuery (KoralNode koralNode, KustvaktConfiguration config,
             User user) throws KustvaktException {
 
-        if (node.get("@type").equals("koral:span")) {
-            if (!node.isMissingNode("/wrap")) {
-                node = node.at("/wrap");
-                JsonNode term = rewriteQuery(node, config, user).rawNode();
-                node.replaceAt("/wrap", term,
-                        new RewriteIdentifier("koral:term", "replace"));
+    	// EM: I don't know the purpose of the following code and it is not 
+    	// tested
+        if (koralNode.get("@type").equals("koral:span")) {
+            if (!koralNode.isMissingNode("/wrap")) {
+                koralNode = koralNode.at("/wrap");
+                JsonNode term = rewriteQuery(koralNode, config, user).rawNode();
+                koralNode.replaceAt("/wrap", term,
+                        new RewriteIdentifier("koral:term", "replace", ""));
             }
         }
-        else if (node.get("@type").equals("koral:term")
-                && !node.has("foundry")) {
+        else if (koralNode.get("@type").equals("koral:term")
+                && !koralNode.has("foundry")) {
             String layer;
-            if (node.has("layer")) {
-                layer = node.get("layer");
+            if (koralNode.has("layer")) {
+                layer = koralNode.get("layer");
             }
             else {
-                layer = node.get("key");
+                layer = koralNode.get("key");
             }
             UserSettingProcessor settingProcessor = null;
             if (user != null) {
                 settingProcessor = user.getUserSettingProcessor();
             }
             String foundry = mapper.findFoundry(layer, settingProcessor);
-            if (foundry != null)
-                node.put("foundry", foundry);
+			if (foundry != null) {
+				RewriteIdentifier ri = new RewriteIdentifier("foundry", null,
+						"Default foundry has been added.");
+				koralNode.set("foundry", foundry, ri);
+			}
         }
-        return node;
+        return koralNode;
     }
 
     @Override
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 bbc9cd2..fb99954 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/KoralNode.java
@@ -45,22 +45,6 @@
         return this.node.toString();
     }
 
-    public void put (String name, Object value) {
-        if (this.node.isObject() && this.node.path(name).isMissingNode()) {
-            ObjectNode node = (ObjectNode) this.node;
-            if (value instanceof String)
-                node.put(name, (String) value);
-            else if (value instanceof Integer)
-                node.put(name, (Integer) value);
-            else if (value instanceof JsonNode)
-                node.put(name, (JsonNode) value);
-            this.rewrites.add("injection", name);
-        }
-        else
-            throw new UnsupportedOperationException(
-                    "node doesn't support this operation");
-    }
-
     public void remove (Object identifier, RewriteIdentifier ident) {
         boolean set = false;
         if (this.node.isObject() && identifier instanceof String) {
@@ -75,7 +59,7 @@
         }
 
         if (set) {
-            this.rewrites.add("deletion", "", ident);
+            this.rewrites.add("deletion", ident);
         }
     }
 
@@ -89,10 +73,7 @@
             else if (value instanceof JsonNode)
                 n.put(name, (JsonNode) value);
 
-//            if (ident != null)
-//                name = ident.toString();
-
-            this.rewrites.add("override", null, ident);
+            this.rewrites.add("override", ident);
         }
     }
     
@@ -101,7 +82,7 @@
         if (value instanceof ObjectNode) {
             n.removeAll();
             n.setAll((ObjectNode) value);
-            this.rewrites.add("override", null, ident);
+            this.rewrites.add("override", ident);
         }
     }
 
@@ -119,11 +100,33 @@
             this.rewrites.add("override", name);
         }
     }
+    
+    // Please use set instead.
+    @Deprecated
+    public void put (String name, Object value) {
+        if (this.node.isObject() && this.node.path(name).isMissingNode()) {
+            ObjectNode node = (ObjectNode) this.node;
+            if (value instanceof String)
+                node.put(name, (String) value);
+            else if (value instanceof Integer)
+                node.put(name, (Integer) value);
+            else if (value instanceof JsonNode)
+                node.set(name, (JsonNode) value);
+            this.rewrites.add("injection", name);
+        }
+        else
+            throw new UnsupportedOperationException(
+                    "node doesn't support this operation");
+    }
 
     // EM: we agree to use injection instead because it has been introduced to
     // public in several occasions.
     // This method is similar to put
-    @Deprecated
+    /**
+     * @param name 	the key name of the node / JSON property to set
+     * @param value	the value of the JSON property to set
+     * @param ident RewriteIdentifier
+     */
     public void set (String name, Object value, RewriteIdentifier ident) {
         if (this.node.isObject()) {
             ObjectNode n = (ObjectNode) this.node;
@@ -132,12 +135,9 @@
             else if (value instanceof Integer)
                 n.put(name, (Integer) value);
             else if (value instanceof JsonNode)
-                n.put(name, (JsonNode) value);
+                n.set(name, (JsonNode) value);
 
-            if (ident != null)
-                name = ident.toString();
-
-            this.rewrites.add("injection", name);
+            this.rewrites.add("injection", ident);
         }
     }
 
@@ -178,130 +178,14 @@
         this.remove = true;
     }
 
-    public static class RewriteIdentifier {
-
-        private String scope, value;
-        private Object source;
-
-        public RewriteIdentifier (String scope) {
-            this.scope = scope;
-        }
-        
-        @Deprecated
-        public RewriteIdentifier (String scope, String value) {
-            this.scope = scope;
-            this.value = value;
-        }
-        
-        public RewriteIdentifier (String scope, String value, Object source) {
-            this.scope = scope;
-            this.value = value;
-            this.source = source;
-        }
-        
-        public String getScope () {
-            return scope;
-        }
-        
-        public Object getSource () {
-            return source;
-        }
-        
-        @Override
-        public String toString () {
-            return scope + "(" + value + ")";
-        }
-
-    }
-
+    
     public boolean isRemove () {
         return this.remove;
     }
 
-    public static class KoralRewriteBuilder {
+    
 
-        private List<KoralRewrite> rewrites;
-
-        public KoralRewriteBuilder () {
-            this.rewrites = new ArrayList<>();
-        }
-
-        public KoralRewriteBuilder add (String op, Object scope) {
-            KoralRewrite rewrite = new KoralRewrite();
-            rewrite.setOperation(op);
-            if (scope != null) {
-                rewrite.setScope(scope.toString());
-            }
-            this.rewrites.add(rewrite);
-            return this;
-        }
-        
-        public KoralRewriteBuilder add (String op, String scope, RewriteIdentifier ri) {
-            KoralRewrite rewrite = new KoralRewrite();
-            rewrite.setOperation(op);
-            if (ri.getScope() != null) {
-                rewrite.setScope(ri.getScope());
-            }
-            if (ri.getSource() != null) {
-                rewrite.setSource(ri.getSource());
-            }
-            this.rewrites.add(rewrite);
-            return this;
-        }
-
-        public JsonNode build (JsonNode node) {
-            for (KoralRewrite rewrite : this.rewrites) {
-                if (rewrite.map.get("operation") == null)
-                    throw new UnsupportedOperationException(
-                            "operation not set properly");
-
-                if (node.has("rewrites")) {
-                    ArrayNode n = (ArrayNode) node.path("rewrites");
-                    n.add(JsonUtils.valueToTree(rewrite.map));
-                }
-                else if (node.isObject()) {
-                    ObjectNode n = (ObjectNode) node;
-                    List l = new LinkedList<>();
-                    l.add(JsonUtils.valueToTree(rewrite.map));
-                    n.put("rewrites", JsonUtils.valueToTree(l));
-                }
-                else {
-                    //fixme: matches in result will land here. rewrites need to be placed under root node - though then there might be unclear where they belong to
-                }
-
-            }
-            this.rewrites.clear();
-            return node;
-        }
-
-    }
-
-    private static class KoralRewrite {
-
-        private Map<String, Object> map;
-
-        private KoralRewrite () {
-            this.map = new LinkedHashMap<>();
-            this.map.put("@type", "koral:rewrite");
-            this.map.put("src", "Kustvakt");
-            this.map.put("editor", "Kustvakt");
-        }
-
-        public void setOperation (String op) {
-            if (!op.startsWith("operation:"))
-                op = "operation:" + op;
-            this.map.put("operation", op);
-        }
-
-        public void setScope (String scope) {
-            this.map.put("scope", scope);
-        }
-        
-        public void setSource(Object source) {
-            this.map.put("source", source);
-        }
-
-    }
+    
 
     public boolean isMissingNode (String string) {
         return this.node.at(string).isMissingNode();
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewrite.java
new file mode 100644
index 0000000..86e5863
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewrite.java
@@ -0,0 +1,35 @@
+package de.ids_mannheim.korap.rewrite;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class KoralRewrite {
+
+    public Map<String, Object> map;
+
+    public KoralRewrite () {
+        this.map = new LinkedHashMap<>();
+        this.map.put("@type", "koral:rewrite");
+        this.map.put("src", "Kustvakt");
+        this.map.put("editor", "Kustvakt");
+    }
+
+    public void setOperation (String op) {
+        if (!op.startsWith("operation:"))
+            op = "operation:" + op;
+        this.map.put("operation", op);
+    }
+
+    public void setScope (String scope) {
+        this.map.put("scope", scope);
+    }
+    
+    public void setSource(Object source) {
+        this.map.put("source", source);
+    }
+    
+    public void setComment(String comment) {
+        this.map.put("_comment", comment);
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewriteBuilder.java b/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewriteBuilder.java
new file mode 100644
index 0000000..a5824a1
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/KoralRewriteBuilder.java
@@ -0,0 +1,73 @@
+package de.ids_mannheim.korap.rewrite;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+public class KoralRewriteBuilder {
+
+	private List<KoralRewrite> rewrites;
+
+	public KoralRewriteBuilder () {
+		this.rewrites = new ArrayList<>();
+	}
+
+	@Deprecated
+	public KoralRewriteBuilder add (String op, Object scope) {
+		KoralRewrite rewrite = new KoralRewrite();
+		rewrite.setOperation(op);
+		if (scope != null) {
+			rewrite.setScope(scope.toString());
+		}
+		this.rewrites.add(rewrite);
+		return this;
+	}
+
+	public KoralRewriteBuilder add (String op, RewriteIdentifier ri) {
+		KoralRewrite rewrite = new KoralRewrite();
+		rewrite.setOperation(op);
+		if (ri.getScope() != null) {
+			rewrite.setScope(ri.getScope());
+		}
+		if (ri.getSource() != null) {
+			rewrite.setSource(ri.getSource());
+		}
+		if (ri.getComment() != null) {
+			rewrite.setComment(ri.getComment());
+		}
+		this.rewrites.add(rewrite);
+		return this;
+	}
+
+	public JsonNode build (JsonNode node) {
+		for (KoralRewrite rewrite : this.rewrites) {
+			if (rewrite.map.get("operation") == null)
+				throw new UnsupportedOperationException(
+						"operation not set properly");
+
+			if (node.has("rewrites")) {
+				ArrayNode n = (ArrayNode) node.path("rewrites");
+				n.add(JsonUtils.valueToTree(rewrite.map));
+			}
+			else if (node.isObject()) {
+				ObjectNode n = (ObjectNode) node;
+				List l = new LinkedList<>();
+				l.add(JsonUtils.valueToTree(rewrite.map));
+				n.put("rewrites", JsonUtils.valueToTree(l));
+			}
+			else {
+				//fixme: matches in result will land here. rewrites need to be placed under root node - though then there might be unclear where they belong to
+			}
+
+		}
+		this.rewrites.clear();
+		return node;
+	}
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
index 1f75e21..0494fa4 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
@@ -8,7 +8,6 @@
 
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.utils.JsonUtils;
 
@@ -45,8 +44,10 @@
                         .readTree(contextPosition.toString());
                 ArrayNode arrayNode = (ArrayNode) contextPosition.rawNode();
                 arrayNode.set(1, maxContextLength);
-                context.replace(position, arrayNode, new RewriteIdentifier(
-                        position, null, sourceNode));
+				context.replace(position, arrayNode,
+						new RewriteIdentifier(position, sourceNode, position
+								+ " has been replaced. The original value is "
+								+ "described in the source property."));
                 return true;
             }
         }
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 0f6ff8c..b70dbf1 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/QueryReferenceRewrite.java
@@ -4,14 +4,12 @@
 import org.springframework.stereotype.Component;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
 
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.constant.QueryType;
 import de.ids_mannheim.korap.entity.QueryDO;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.service.QueryService;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.utils.JsonUtils;
@@ -93,7 +91,9 @@
             throws KustvaktException {
         String source = koralNode.rawNode().toString();
         JsonNode sourceNode = JsonUtils.readTree(source);
-        koralNode.replace(qref, new RewriteIdentifier(null, "", sourceNode));
+		koralNode.replace(qref, new RewriteIdentifier(null, sourceNode,
+				"This node has been replaced. The original node is described in "
+				+ "the source property."));
         
 //        koralNode.remove("@type",
 //                new RewriteIdentifier("@type", "", jsonNode.at("/@type").asText()));
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/RewriteIdentifier.java b/src/main/java/de/ids_mannheim/korap/rewrite/RewriteIdentifier.java
new file mode 100644
index 0000000..ad56a3b
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/RewriteIdentifier.java
@@ -0,0 +1,37 @@
+package de.ids_mannheim.korap.rewrite;
+
+public class RewriteIdentifier {
+
+    private String scope,comment = "";
+    private Object source;
+
+    public RewriteIdentifier (String scope, String value, String comment) {
+        this.scope = scope;
+        this.source = value;
+        this.comment = comment;
+    }
+    
+	public RewriteIdentifier (String scope, Object source, String comment) {
+		this.scope = scope;
+		this.source = source;
+		this.comment = comment;
+	}
+    
+    public String getScope () {
+        return scope;
+    }
+    
+    public Object getSource () {
+        return source;
+    }
+    
+    public String getComment () {
+		return comment;
+	}
+    
+//    @Override
+//    public String toString () {
+//        return scope + "(" + value + ")";
+//    }
+}
+
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 41da0e2..7dc4219 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
@@ -9,7 +9,6 @@
 import de.ids_mannheim.korap.constant.QueryType;
 import de.ids_mannheim.korap.entity.QueryDO;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.service.QueryService;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.util.StatusCodes;
@@ -99,7 +98,9 @@
         JsonNode jsonNode = koralNode.rawNode();
         String ref = jsonNode.at("/ref").asText();
         String newRef = ref.substring(vcOwner.length() + 1, ref.length());
-        koralNode.replace("ref", newRef, new RewriteIdentifier("ref", "", ref));
+        koralNode.replace("ref", newRef, new RewriteIdentifier("ref", ref, 
+        		"Ref has been replaced. The original value is described at "
+        		+ "the source property."));
     }
 
     protected void rewriteVC (QueryDO vc, KoralNode koralNode)
@@ -110,8 +111,9 @@
         String source = koralNode.rawNode().toString();
         JsonNode sourceNode = JsonUtils.readTree(source);
         
-        koralNode.replace(newKoralQuery, new RewriteIdentifier(
-                null, "", sourceNode));
+		koralNode.replace(newKoralQuery, new RewriteIdentifier(null, sourceNode,
+				"This node has been replaced. The original node is described at "
+						+ "the source property."));
         
         // rewrite
 //        koralNode.remove("@type", new RewriteIdentifier("@type", "",
diff --git a/src/main/java/de/ids_mannheim/korap/user/User.java b/src/main/java/de/ids_mannheim/korap/user/User.java
index 5f46910..eb6a340 100644
--- a/src/main/java/de/ids_mannheim/korap/user/User.java
+++ b/src/main/java/de/ids_mannheim/korap/user/User.java
@@ -169,11 +169,11 @@
 
     {
         if (this.corpusAccess == CorpusAccess.ALL)
-            return "ALL";
+            return "All";
         else if (this.corpusAccess == CorpusAccess.PUB)
-            return "PUB";
+            return "Public";
         else if (this.corpusAccess == CorpusAccess.FREE)
-            return "FREE";
+            return "Free";
         else
             return "???";
     }
diff --git a/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java b/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
index a1baccb..4334b97 100644
--- a/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
+++ b/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
@@ -25,6 +25,13 @@
 
     public final static String API_VERSION = "v1.0";
 
+    protected final static String allCorpusAccess = "All corpus access policy "
+			+ "has been added.";
+	protected final static String freeCorpusAccess = "Free corpus access policy "
+			+ "has been added.";
+	protected final static String publicCorpusAccess = "Public corpus access "
+			+ "policy has been added.";
+    
     @Autowired
     protected GenericApplicationContext applicationContext;
 
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 dad58bd..08d71f5 100644
--- a/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java
+++ b/src/test/java/de/ids_mannheim/korap/misc/KoralNodeTest.java
@@ -3,9 +3,11 @@
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import org.junit.jupiter.api.Test;
+
 import com.fasterxml.jackson.databind.node.ObjectNode;
+
 import de.ids_mannheim.korap.rewrite.KoralNode;
-import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
+import de.ids_mannheim.korap.rewrite.RewriteIdentifier;
 import de.ids_mannheim.korap.utils.JsonUtils;
 
 /**
@@ -40,7 +42,7 @@
         node.put("value_1", "setting_1");
         KoralNode knode = KoralNode.wrapNode(node);
         knode.replace("value_1", "settings_2",
-                new RewriteIdentifier("value_1", "setting_1"));
+                new RewriteIdentifier("value_1", "setting_1", ""));
         assertEquals(knode.rawNode().toString(),
                 "{\"value_1\":\"settings_2\"}");
     }
diff --git a/src/test/java/de/ids_mannheim/korap/rewrite/AvailabilityRewriteTest.java b/src/test/java/de/ids_mannheim/korap/rewrite/AvailabilityRewriteTest.java
index d893540..2469746 100644
--- a/src/test/java/de/ids_mannheim/korap/rewrite/AvailabilityRewriteTest.java
+++ b/src/test/java/de/ids_mannheim/korap/rewrite/AvailabilityRewriteTest.java
@@ -183,9 +183,8 @@
         assertEquals(node.at("/collection/value").asText(), "CC.*");
         assertEquals(node.at("/collection/rewrites/0/@type").asText(),
                 "koral:rewrite");
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(FREE)");
-        // todo:
+        assertEquals(freeCorpusAccess,
+        		node.at("/collection/rewrites/0/_comment").asText());
     }
 
     @Test
diff --git a/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java b/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
index ae9b93b..2500f17 100644
--- a/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
@@ -33,8 +33,8 @@
 		assertEquals("operation:override",
 				node.at("/rewrites/0/operation").asText());
 		assertEquals(source, node.at("/rewrites/0/source"));
-		//        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-		//                "availability(FREE)");
+		assertEquals(freeCorpusAccess,
+				node.at("/rewrites/0/_comment").asText());
 	}
 
 	private void checkAndPublic (String json, JsonNode source)
@@ -46,6 +46,8 @@
 		assertEquals("Kustvakt", node.at("/rewrites/0/editor").asText());
 		assertEquals("operation:override", node.at("/rewrites/0/operation").asText());
 		assertEquals(source, node.at("/rewrites/0/source"));
+		assertEquals(publicCorpusAccess,
+				node.at("/rewrites/0/_comment").asText());
 		
 		node = node.at("/operands/0");
 		assertEquals("match:eq", node.at("/operands/0/match").asText());
@@ -71,6 +73,8 @@
 		assertEquals("Kustvakt", node.at("/rewrites/0/editor").asText());
 		assertEquals("operation:override", node.at("/rewrites/0/operation").asText());
 		assertEquals(source, node.at("/rewrites/0/source"));
+		assertEquals(allCorpusAccess,
+				node.at("/rewrites/0/_comment").asText());
 
 		assertEquals("match:eq", node.at("/operands/1/match").asText());
 		assertEquals("type:regex", node.at("/operands/1/type").asText());
@@ -122,6 +126,8 @@
 		assertEquals("CC.*", node.at("/value").asText());
 		assertEquals("operation:injection",
 				node.at("/rewrites/0/operation").asText());
+		assertEquals("Free corpus access policy has been added.",
+				node.at("/rewrites/0/_comment").asText());
     }
     
     @Test
@@ -169,7 +175,7 @@
                 "src" : "Kustvakt",
                 "editor" : "Kustvakt",
                 "operation" : "operation:injection",
-                "scope" : "availability(PUB)"
+                "_comment" : "Public corpus access policy has been added."
               } ]
             }
 			""";
@@ -232,7 +238,7 @@
                 "src" : "Kustvakt",
                 "editor" : "Kustvakt",
                 "operation" : "operation:injection",
-                "scope" : "availability(ALL)"
+                "_comment" : "All corpus access policy has been added."
               } ]
             }
    			""";
diff --git a/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java b/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
index a2ce70d..a8db188 100644
--- a/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
@@ -119,8 +119,8 @@
         assertEquals("koral:doc", node.at("/collection/@type").asText());
         assertEquals("availability", node.at("/collection/key").asText());
         assertEquals("CC.*", node.at("/collection/value").asText());
-        assertEquals("availability(FREE)",
-                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals(freeCorpusAccess,
+                node.at("/collection/rewrites/0/_comment").asText());
         assertEquals("operation:injection",
                 node.at("/collection/rewrites/0/operation").asText());
     }
@@ -182,8 +182,8 @@
         assertEquals("koral:doc", node.at("/collection/@type").asText());
         assertEquals("availability", node.at("/collection/key").asText());
         assertEquals("CC.*", node.at("/collection/value").asText());
-        assertEquals("availability(FREE)",
-                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals(freeCorpusAccess,
+                node.at("/collection/rewrites/0/_comment").asText());
         assertEquals("operation:injection",
                 node.at("/collection/rewrites/0/operation").asText());
     }
@@ -202,8 +202,8 @@
         assertEquals("koral:doc", node.at("/collection/@type").asText());
         assertEquals("availability", node.at("/collection/key").asText());
         assertEquals("CC.*", node.at("/collection/value").asText());
-        assertEquals("availability(FREE)",
-                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals(freeCorpusAccess,
+                node.at("/collection/rewrites/0/_comment").asText());
         assertEquals("operation:injection",
                 node.at("/collection/rewrites/0/operation").asText());
     }
@@ -231,8 +231,8 @@
         assertEquals("QAO-NC",
                 node.at("/collection/operands/1/operands/1/value").asText());
         assertEquals("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability(PUB)",
-                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals(publicCorpusAccess,
+                node.at("/collection/rewrites/0/_comment").asText());
         assertEquals("operation:injection",
                 node.at("/collection/rewrites/0/operation").asText());
     }
@@ -264,8 +264,8 @@
                 node.at("/collection/operands/1/operands/1/operands/1/value")
                         .asText());
         assertEquals("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability(ALL)",
-                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals(allCorpusAccess,
+                node.at("/collection/rewrites/0/_comment").asText());
         assertEquals("operation:injection",
                 node.at("/collection/rewrites/0/operation").asText());
     }
@@ -363,7 +363,7 @@
         assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
         assertEquals("ACA.*", node.at("/collection/operands/1/value").asText());
         assertEquals("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability(PUB)",
+        assertEquals(publicCorpusAccess,
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
@@ -420,7 +420,7 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertNotEquals(0, node.path("matches").size());
-        assertEquals("availability(FREE)",
+        assertEquals(freeCorpusAccess,
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
@@ -440,7 +440,7 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertNotEquals(0, node.path("matches").size());
-        assertEquals("availability(ALL)",
+        assertEquals(allCorpusAccess,
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
@@ -460,7 +460,7 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertNotEquals(0, node.path("matches").size());
-        assertEquals("availability(PUB)",
+        assertEquals(publicCorpusAccess,
                 node.at("/collection/rewrites/0/scope").asText());
     }
 }
diff --git a/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java b/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
index 39f1cf9..8ddebae 100644
--- a/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
@@ -118,8 +118,8 @@
         assertEquals(1, node.at("/collection/rewrites").size());
         assertEquals(node.at("/collection/rewrites/0/operation").asText(),
                 "operation:injection");
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(FREE)");
+		assertEquals(freeCorpusAccess,
+				node.at("/collection/rewrites/0/_comment").asText());
         node = node.at("/query/wrap/rewrites");
         assertEquals(2, node.size());
         assertEquals(node.at("/0/src").asText(), "Glemm");
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 2b85901..dfa3553 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
@@ -1,16 +1,14 @@
 package de.ids_mannheim.korap.web.controller;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import jakarta.ws.rs.client.Entity;
-import jakarta.ws.rs.core.Response.Status;
-
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
+
 import com.fasterxml.jackson.databind.JsonNode;
 import com.google.common.net.HttpHeaders;
-import jakarta.ws.rs.core.Response;
 
 import de.ids_mannheim.korap.config.SpringJerseyTest;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
@@ -18,6 +16,9 @@
 import de.ids_mannheim.korap.query.serialize.MetaQueryBuilder;
 import de.ids_mannheim.korap.query.serialize.QuerySerializer;
 import de.ids_mannheim.korap.utils.JsonUtils;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
 
 public class SearchPublicMetadataTest extends SpringJerseyTest {
 
@@ -29,9 +30,11 @@
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
         String entity = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(ALL)");
-        assertTrue(node.at("/matches/0/snippet").isMissingNode());
+        assertTrue(node.at("/meta/snippet").isMissingNode());
+        assertEquals(allCorpusAccess,
+        		node.at("/collection/rewrites/0/_comment").asText());
+        assertFalse(node.at("/matches/0/snippet").asBoolean());
+        assertFalse(node.at("/matches/0/tokens").asBoolean());
     }
 
     @Test
@@ -43,8 +46,8 @@
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
         String entity = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(ALL)");
+        assertEquals("All corpus access policy has been added.",
+        		node.at("/collection/rewrites/0/_comment").asText());
         assertTrue(node.at("/matches/0/snippet").isMissingNode());
     }
 
@@ -58,8 +61,8 @@
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
         String entity = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(ALL)");
+		assertEquals(allCorpusAccess,
+				node.at("/collection/rewrites/0/_comment").asText());
         assertTrue(node.at("/matches/0/snippet").isMissingNode());
         assertEquals(node.at("/matches/0/author").asText(),
                 "Goethe, Johann Wolfgang von");
@@ -101,8 +104,8 @@
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
         String ent = response.readEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
-        assertEquals(node.at("/collection/rewrites/0/scope").asText(),
-                "availability(ALL)");
+        assertEquals(allCorpusAccess,
+        		node.at("/collection/rewrites/0/_comment").asText());
         assertTrue(node.at("/matches/0/snippet").isMissingNode());
     }