Fixed collection rewrite

Change-Id: Ib9d0eed501b8d27df638f5ec62c7879b255fad3d
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index f6e85af..f2742ce 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -81,10 +81,6 @@
     private ArrayList<String> foundries;
     private ArrayList<String> layers;
     
-//    private List<String> publicLicenses;
-//    private List<String> freeLicenses;
-//    private List<String> allLicenses;
-    
     private Pattern publicLicensePattern;
     private Pattern freeLicensePattern;
     private Pattern allLicensePattern;
@@ -167,14 +163,12 @@
         
         ldapConfig = properties.getProperty("ldap.config");
         
-//        freeLicenses = Arrays.asList(license.split("|"));
-//        publicLicenses = Arrays.asList(properties.getProperty("kustvakt.availability.public","").split("|"));
-//        allLicenses = Arrays.asList(properties.getProperty("kustvakt.availability.all","").split("|"));
+        // EM: replace this later with KoralQuery
+        freeLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.free",""));
+        publicLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.public",""));
+        allLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.all",""));
         
-        freeLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.free",""));
-        publicLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.public",""));
-        allLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.all",""));
-        
+        // EM: not use in the future
         policyConfig = properties.getProperty("policies.config");
         setFoundriesAndLayers(policyConfig);
         
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
index 9d70e36..1de7d2f 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
@@ -1,22 +1,16 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
-import java.util.ArrayList;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 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.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.query.object.KoralMatchOperator;
 import de.ids_mannheim.korap.resource.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;
 import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
 
@@ -30,62 +24,12 @@
             .getLogger(CollectionRewrite.class);
     public static String AVAILABILITY = "availability";
 
-    public static Pattern notFreeLicense = Pattern.compile("ACA|QAO");
-    public static Pattern notPublicLicense = Pattern.compile("QAO");
-
 
     public CollectionRewrite () {
         super();
     }
 
 
-    private String verifyAvailability (JsonNode node, CorpusAccess access,
-            KustvaktConfiguration config) {
-
-        if (node.has("operands")) {
-            ArrayList<JsonNode> operands = Lists
-                    .newArrayList(node.at("/operands").elements());
-            for (int i = 0; i < operands.size(); i++) {
-                String path = verifyAvailability(operands.get(i), access,
-                        config);
-                if (!path.isEmpty()) { return "/operands/" + i; }
-            }
-        }
-        else if (node.has("key")
-                && node.at("/key").asText().equals(AVAILABILITY)) {
-            Matcher m;
-            String queryAvailability = node.at("/value").asText();
-            if (node.at("/match").asText()
-                    .equals(KoralMatchOperator.EQUALS.toString())) {
-
-                if (access.equals(CorpusAccess.FREE)) {
-                    m = notFreeLicense.matcher(queryAvailability);
-                    if (m.find()) return "/value";
-                }
-                else if (access.equals(CorpusAccess.PUB)) {
-                    m = notPublicLicense.matcher(queryAvailability);
-                    if (m.find()) return "/value";
-                }
-            }
-            // match:ne
-            else {
-                if (access.equals(CorpusAccess.FREE)) {
-                    m = config.getFreeLicensePattern()
-                            .matcher(queryAvailability);
-                    if (m.find()) return "/value";
-                }
-                else if (access.equals(CorpusAccess.PUB)) {
-                    m = config.getPublicLicensePattern()
-                            .matcher(queryAvailability);
-                    if (m.find()) return "/value";
-                }
-            }
-        }
-
-        return "";
-    }
-
-
     @Override
     public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) throws KustvaktException {
@@ -110,24 +54,9 @@
         JsonNode rewrittesNode;
 
         if (jsonNode.has("collection")) {
-            String path = verifyAvailability(jsonNode.at("/collection"),
-                    user.getCorpusAccess(), config);
-            if (!path.isEmpty()) {
-                rewrittesNode = JsonUtils.readTree(builder.toJSON())
-                        .at("/collection");
-                if (path.equals("/value")) {
-                    node.replace("collection", rewrittesNode, identifier);
-                }
-                else {
-                    node.replaceAt("/collection" + path, rewrittesNode,
-                            identifier);
-                }
-            }
-            else {
-                builder.setBaseQuery(builder.toJSON());
-                rewrittesNode = builder.mergeWith(jsonNode).at("/collection");
-                node.set("collection", rewrittesNode, identifier);
-            }
+            builder.setBaseQuery(builder.toJSON());
+            rewrittesNode = builder.mergeWith(jsonNode).at("/collection");
+            node.set("collection", rewrittesNode, identifier);
         }
         else {
             rewrittesNode = JsonUtils.readTree(builder.toJSON())
@@ -135,7 +64,7 @@
             node.set("collection", rewrittesNode, identifier);
         }
 
-        jlog.debug("REWRITES: " + node.at("/collection").toString());
+        jlog.info("REWRITES: " + node.at("/collection").toString());
         return node.rawNode();
     }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
index 97aace6..03421e1 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
@@ -1171,63 +1171,7 @@
                     e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
-        String results;
-//        // fixme: checks for policy matching
-//        // fixme: currently disabled, due to mishab in foundry/layer spec
-//        // fixme:
-//        if (foundries != null && foundries.size() > 1000) {
-//            Set<String> f_list = new HashSet<>();
-//            Set<String> l_list = new HashSet<>();
-//
-//            for (String spl : new ArrayList<>(foundries)) {
-//                try {
-//                    SecurityManager<?> manager = SecurityManager.init(spl, user,
-//                            Permissions.Permission.READ);
-//                    if (!manager.isAllowed())
-//                        continue;
-//
-//                    String[] sep = StringUtils.splitAnnotations(spl);
-//                    if (spl != null) {
-//                        f_list.add(sep[0]);
-//                        l_list.add(sep[1]);
-//                    };
-//                    results = searchKrill.getMatch(matchid,
-//                            new ArrayList<>(f_list), new ArrayList<>(l_list),
-//                            spans, false, true);
-//                }
-//                catch (NotAuthorizedException e) {
-//                    throw KustvaktResponseHandler.throwit(
-//                            StatusCodes.ACCESS_DENIED, "Permission denied",
-//                            matchid);
-//                }
-//
-//            }
-//            // all foundries shall be returned
-//        }
-//        else if (foundries != null && foundries.contains("*")) {
-//            Set<Layer> resources;
-//            try {
-//                resources = ResourceFinder.search(user, Layer.class);
-//            }
-//            catch (KustvaktException e) {
-//                jlog.error("Exception encountered: {}", e.string());
-//                throw KustvaktResponseHandler.throwit(e);
-//            }
-//            // returns foundries and layers.
-//            // todo: needs testing!
-//            foundries = new HashSet<>();
-//            layers = new HashSet<>();
-//            for (Layer r : resources) {
-//                String[] spl = StringUtils.splitAnnotations(r.getName());
-//                if (spl != null) {
-//                    foundries.add(spl[0]);
-//                    layers.add(spl[1]);
-//                }
-//            }
-//        }
         
-        
-        //EM: I dont need user here just corpusAccess
         CorpusAccess corpusAccess = user.getCorpusAccess();
         Pattern p;
         switch (corpusAccess) {
@@ -1241,6 +1185,8 @@
 			p = config.getFreeLicensePattern();
 			break;
 		}
+        
+        String results;
         try {
             if (!match_only){