package alien4cloud.dao;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * Utility to build simple filters for ES mapping filtering.
 */
public final class FilterUtil {
    private FilterUtil() {
    }

    /**
     * Get a simple filter with a single key.
     *
     * @param key The key for the filter.
     * @param values The value for the filter.
     * @return The filters map.
     */
    public static Map<String, String[]> singleKeyFilter(String key, String... values) {
        Map<String, String[]> filters = Maps.newHashMap();
        filters.put(key, values);
        return filters;
    }

    /**
     * Add a simple filter with a single key to the given filter list only if the key does'nt exists already.
     *
     * @param filters The exiting map of filters in which to add fitlers
     * @param key The key for the filter.
     * @param values The value for the filter.
     * @return The filters map.
     */
    public static Map<String, String[]> singleKeyFilter(Map<String, String[]> filters, String key, String... values) {
        if (filters == null) {
            return singleKeyFilter(key, values);
        }
        if (!filters.containsKey(key)) {
            filters.put(key, values);
        }
        return filters;
    }

    /**
     * Get simple filter map with a single value per key based on key1, value1, key2, value2 etc.
     * 
     * @param keyValues The list of key1, value1, key2, value2 etc.
     * @return The filters map.
     */
    public static Map<String, String[]> fromKeyValueCouples(String... keyValues) {
        Map<String, String[]> filters = Maps.newHashMap();
        for (int i = 0; i < keyValues.length; i += 2) {
            singleKeyFilter(filters, keyValues[i], keyValues[i + 1]);
        }
        return filters;
    }

    /**
     * Add simple filter map with a single value per key based on key1, value1, key2, value2 etc.
     *
     * @param filters The exiting map of filters in which to add fitlers
     * @param keyValues The list of key1, value1, key2, value2 etc.
     * @return The filters map.
     */
    public static Map<String, String[]> fromKeyValueCouples(Map<String, String[]> filters, String... keyValues) {
        if(filters == null) {
            return fromKeyValueCouples(keyValues);
        }
        for (int i = 0; i < keyValues.length; i += 2) {
            singleKeyFilter(filters, keyValues[i], keyValues[i + 1]);
        }
        return filters;
    }
}

@@ -19,16 +19,39 @@
 d.dao;%0A%0A
+import java.util.Map;%0A%0A
 import c
@@ -82,39 +82,16 @@
 .Maps;%0A%0A
-import java.util.Map;%0A%0A
 /**%0A * U
@@ -546,32 +546,136 @@
 s.newHashMap();%0A
+        if (values == null) %7B%0A            filters.put(key, new String%5B%5D %7B null %7D);%0A        %7D else %7B%0A    
         filters.
@@ -684,32 +684,42 @@
 t(key, values);%0A
+        %7D%0A
         return f
@@ -2338,16 +2338,17 @@
       if
+ 
 (filters

Fix filter behaviour for null values.