package de.ids_mannheim.korap.resource.rewrite;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.ids_mannheim.korap.utils.JsonUtils;

import java.util.Iterator;

/**
 * @author hanl
 * @date 28/07/2015
 */
//todo: test
public class CollectionCleanupFilter extends RewriteQuery {

    // track path to operand
    private StringBuilder builder;

    public CollectionCleanupFilter() {
        this.builder = new StringBuilder();
    }

    @Override
    public JsonNode rewrite(KoralNode node) {
        if (node.rawNode().has("collection")) {
            JsonNode coll = node.rawNode().path("collection");
            process(coll);
        }
        return null;
    }

    private JsonNode process(JsonNode root) {
        if (root.isObject()) {
            if (root.has("operands")) {
                JsonNode node = root.at("/operands");
                Iterator<JsonNode> it = node.elements();
                while (it.hasNext()) {
                    JsonNode n = it.next();
                    JsonNode s = process(n);
                    if (s == null)
                        it.remove();
                }
            }

            JsonNode sub = processNodes(root);
            if (!root.equals(sub)) {
                if (sub == null) {
                    //can't do anything here -- fixme: edge case?!
                    return null;
                }else if (sub.isObject()) {
                    ObjectNode ob = (ObjectNode) root;
                    ob.removeAll();
                    ob.putAll((ObjectNode) sub);
                }

            }
        }
        return root;
    }

    // return null deletes node, if node return replace at level -1
    private JsonNode processNodes(JsonNode jsonNode) {
        if (jsonNode.isObject()) {
            if (jsonNode.has("operands")) {
                JsonNode node = jsonNode.at("/operands");
                int count = node.size();
                if (count == 1) {
                    // move to super node if any
                    return node.path(0);
                }else if (count == 0) {
                    // remove container
                    return null;
                }
                return jsonNode;
            }
        }
        return JsonUtils.createArrayNode();
    }
}

@@ -268,21 +268,8 @@
  */%0A
-//todo: test%0A
 publ
@@ -354,16 +354,32 @@
 operand%0A
+    @Deprecated%0A
     priv
@@ -774,24 +774,53 @@
 ode root) %7B%0A
+        JsonNode sub = root;%0A
         if (
@@ -1211,70 +1211,53 @@
   %7D%0A
-            %7D%0A%0A            JsonNode sub = processNodes(root);%0A
+%0A                int count = node.size();%0A   
     
@@ -1268,32 +1268,24 @@
     
+ 
 if (
-!root.equals(sub)) %7B
+count == 1)
 %0A   
@@ -1301,27 +1301,64 @@
     
-if (
+    
 sub =
-=
  n
-ull) %7B%0A
+ode.path(0);%0A                else if (count%0A 
     
@@ -1372,19 +1372,29 @@
         
+    == 0)
  //
+ 
 can't do
@@ -1470,34 +1470,80 @@
 %0A           
+ %7D%0A%0A
      
-%7Delse
+       if (!root.equals(sub)) %7B%0A               
  if (sub.isO
@@ -1703,33 +1703,32 @@
               %7D%0A
-%0A
             %7D%0A  
@@ -1831,16 +1831,32 @@
 evel -1%0A
+    @Deprecated%0A
     priv

rewrite classes