package stroom.processor.api;

import stroom.meta.shared.MetaFields;
import stroom.processor.shared.ProcessorFilter;
import stroom.query.api.v2.ExpressionItem;
import stroom.query.api.v2.ExpressionOperator;
import stroom.query.api.v2.ExpressionTerm;

/**
 * Collection of utility methods relating to processor filters
 */
public final class ProcessorFilterUtil {

    private ProcessorFilterUtil(){}

    public static boolean shouldImport (final ProcessorFilter processorFilter){
        if (processorFilter == null || processorFilter.getQueryData() == null ||
                processorFilter.getQueryData().getExpression() == null)
            return false;

        ExpressionOperator expression = processorFilter.getQueryData().getExpression();

        return containsIdField (expression) == false;
    }

    public static boolean shouldExport (final ProcessorFilter processorFilter){
        if (processorFilter == null || processorFilter.getQueryData() == null ||
                processorFilter.getQueryData().getExpression() == null)
            return false;

        ExpressionOperator expression = processorFilter.getQueryData().getExpression();

        return containsIdField (expression) == false;
    }

    private static boolean containsIdField (ExpressionOperator expression){
        if (expression == null)
            return false;
        for (ExpressionItem item : expression.getChildren()){
            if (item instanceof ExpressionTerm){
                ExpressionTerm term = (ExpressionTerm) item;
                if (MetaFields.ID.getName().equals(term.getField()))
                    return true;
                if (MetaFields.PARENT_ID.getName().equals(term.getField()))
                    return true;
                if (MetaFields.PROCESSOR_ID.getName().equals(term.getField()))
                    return true;
            } else if (item instanceof ExpressionOperator){
                if (containsIdField((ExpressionOperator) item))
                    return true;
            }
        }
        return false;
    }

}

@@ -390,17 +390,23 @@
 erUtil()
-%7B
+ %7B%0A    
 %7D%0A%0A    p
@@ -434,25 +434,24 @@
 shouldImport
- 
 (final Proce
@@ -469,32 +469,33 @@
 processorFilter)
+ 
 %7B%0A        if (pr
@@ -771,33 +771,32 @@
  containsIdField
- 
 (expression) == 
@@ -847,17 +847,16 @@
 ldExport
- 
 (final P
@@ -886,16 +886,17 @@
 rFilter)
+ 
 %7B%0A      
@@ -1188,17 +1188,16 @@
 sIdField
- 
 (express
@@ -1260,17 +1260,16 @@
 sIdField
- 
 (Express
@@ -1291,16 +1291,17 @@
 ression)
+ 
 %7B%0A      
@@ -1412,16 +1412,17 @@
 ldren())
+ 
 %7B%0A      
@@ -1462,16 +1462,17 @@
 ionTerm)
+ 
 %7B%0A      
@@ -1907,16 +1907,17 @@
 perator)
+ 
 %7B%0A      

Normalize formatting