/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.scheduling.api.graphql.fetchers.converter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.ow2.proactive.scheduling.api.graphql.common.Arguments;
import org.ow2.proactive.scheduling.api.graphql.schema.type.inputs.JobTaskCommonAbstractInput;

import com.google.common.collect.ImmutableList;

import graphql.schema.DataFetchingEnvironment;


/**
 * @param <T> {@code JobData} or {@code TaskData} scheduler Job, Task entity object
 * @param <I> {@code JobInput} or {@code TaskInput} graphql input type
 * @author ActiveEon team
 */
public abstract class AbstractJobTaskInputConverter<T, I extends JobTaskCommonAbstractInput>
        implements JobTaskInputPredicatesConverter<T, I> {

    protected LinkedHashMap<String, Object> extraInputCheck(DataFetchingEnvironment environment) {
        return new LinkedHashMap<>();
    }

    protected abstract Function<Map<String, Object>, I> mapFunction();

    @Override
    public List<I> mapToInput(DataFetchingEnvironment environment) {
        List<I> input = new ArrayList<>();

        LinkedHashMap<String, Object> extraInput = extraInputCheck(environment);

        Object filterArgument = environment.getArgument(Arguments.FILTER.getName());
        List<LinkedHashMap<String, Object>> filterInputs;

        if (filterArgument == null) {
            if (!extraInput.isEmpty()) {
                filterInputs = ImmutableList.of(extraInput);
                input.addAll(filterInputs.stream().map(mapFunction()).collect(Collectors.toList()));
            }
        } else {
            filterInputs = (List<LinkedHashMap<String, Object>>) filterArgument;
            if (extraInput.isEmpty()) {
                input.addAll(filterInputs.stream().map(mapFunction()).collect(Collectors.toList()));
            } else {
                input.addAll(filterInputs.stream().map(arg -> {
                    arg.putAll(extraInput);
                    return arg;
                }).map(mapFunction()).collect(Collectors.toList()));
            }
        }

        return input;
    }

    public static class WildCardInputPredicateBuilder {
        private static final String START_WITH = ".*\\*$";

        private static final String END_WITH = "^\\*.*";

        private static final String CONTAINS = "^\\*.*\\*$";

        private static final String NOT_CONTAIN = "^\\!\\*.*\\*$";

        private static final String NOT_EQUAL = "^\\!.*";

        public static <T> Predicate build(CriteriaBuilder criteriaBuilder, Root<T> root, String entityFieldName,
                String fieldInputValue) {
            if (Pattern.matches(NOT_CONTAIN, fieldInputValue)) {
                return criteriaBuilder.notLike(root.get(entityFieldName),
                                               fieldInputValue.replaceFirst("!*", "*").replace("*", "%"));
            } else if (Pattern.matches(CONTAINS, fieldInputValue)) {
                return criteriaBuilder.like(root.get(entityFieldName), fieldInputValue.replace("*", "%"));
            } else if (Pattern.matches(START_WITH, fieldInputValue)) {
                return criteriaBuilder.like(root.get(entityFieldName), fieldInputValue.replace("*", "%"));
            } else if (Pattern.matches(END_WITH, fieldInputValue)) {
                return criteriaBuilder.like(root.get(entityFieldName), fieldInputValue.replace("*", "%"));
            } else if (Pattern.matches(NOT_EQUAL, fieldInputValue)) {
                return criteriaBuilder.notEqual(root.get(entityFieldName), fieldInputValue.replaceFirst("!", ""));
            }
            return criteriaBuilder.equal(root.get(entityFieldName), fieldInputValue);

        }
    }

}

@@ -1334,108 +1334,9 @@
 ria.
-CriteriaBuilder;%0Aimport javax.persistence.criteria.Predicate;%0Aimport javax.persistence.criteria.Root
+*
 ;%0A%0Ai
@@ -3797,24 +3797,189 @@
 putValue) %7B%0A
+            Expression%3CString%3E expressionToCheck = criteriaBuilder.lower(root.get(entityFieldName));%0A            String filterValue = fieldInputValue.toLowerCase();%0A
             
@@ -4009,32 +4009,28 @@
 _CONTAIN, fi
-eldInput
+lter
 Value)) %7B%0A  
@@ -4078,33 +4078,25 @@
 ike(
-root.get(entityFieldName)
+expressionToCheck
 ,%0A  
@@ -4142,24 +4142,20 @@
       fi
-eldInput
+lter
 Value.re
@@ -4243,32 +4243,28 @@
 CONTAINS, fi
-eldInput
+lter
 Value)) %7B%0A  
@@ -4309,45 +4309,33 @@
 ike(
-root.get(entityFieldName), fieldInput
+expressionToCheck, filter
 Valu
@@ -4405,32 +4405,28 @@
 ART_WITH, fi
-eldInput
+lter
 Value)) %7B%0A  
@@ -4471,45 +4471,33 @@
 ike(
-root.get(entityFieldName), fieldInput
+expressionToCheck, filter
 Valu
@@ -4565,32 +4565,28 @@
 END_WITH, fi
-eldInput
+lter
 Value)) %7B%0A  
@@ -4631,45 +4631,33 @@
 ike(
-root.get(entityFieldName), fieldInput
+expressionToCheck, filter
 Valu
@@ -4726,32 +4726,28 @@
 OT_EQUAL, fi
-eldInput
+lter
 Value)) %7B%0A  
@@ -4796,45 +4796,33 @@
 ual(
-root.get(entityFieldName), fieldInput
+expressionToCheck, filter
 Valu
@@ -4906,45 +4906,33 @@
 ual(
-root.get(entityFieldName), fieldInput
+expressionToCheck, filter
 Valu

Make Scheduling filter case insensentive (#44)