/*
 * Copyright (c) 2010-2014. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.eventstore.jdbc.criteria;

/**
 * Abstract implementation to use for testing whether an item is present in a collection or not.
 *
 * @author Allard Buijze
 * @author Kristian Rosenvold
 * @since 2.2
 */
public class CollectionOperator extends JdbcCriteria {

    private final JdbcProperty property;
    private final Object expression;
    private final String operator;

    /**
     * Initializes the operator matching given <code>property</code> against the given <code>expression</code> using
     * the given <code>operator</code>.
     *
     * @param property   The property to match
     * @param operator   The JPA operator to match the property against the expression
     * @param expression The expression to match against
     */
    public CollectionOperator(JdbcProperty property, String operator, Object expression) {
        this.property = property;
        this.expression = expression;
        this.operator = operator;
    }

    @Override
    public void parse(String entryKey, StringBuilder whereClause, ParameterRegistry parameters) {
        property.parse(entryKey, whereClause);
        whereClause.append(" ")
                   .append(operator)
                   .append(" ");
        if (expression instanceof JdbcProperty) {
            ((JdbcProperty) expression).parse(entryKey, whereClause);
        } else {
            whereClause.append("(")
                       .append(parameters.register(expression))
                       .append(")");
        }
    }
}

@@ -655,16 +655,86 @@
 teria;%0A%0A
+import java.util.Collection;%0A%0Aimport static java.util.Arrays.asList;%0A%0A
 /**%0A * A
@@ -1846,35 +1846,32 @@
                 
-   
 .append(operator
@@ -1872,19 +1872,16 @@
 erator)%0A
-   
         
@@ -2074,16 +2074,17 @@
 end(%22(%22)
+;
 %0A       
@@ -2084,35 +2084,333 @@
             
-           
+if (expression instanceof Object%5B%5D) %7B%0A                addSequence(whereClause, parameters, asList((Object%5B%5D) expression));%0A            %7D else if (expression instanceof Collection) %7B%0A                addSequence(whereClause, parameters, (Collection)expression);%0A%0A            %7D else %7B%0A                whereClause
 .append(para
@@ -2437,16 +2437,17 @@
 ession))
+;
 %0A       
@@ -2442,32 +2442,35 @@
 n));%0A           
+ %7D%0A
             .app
@@ -2469,19 +2469,416 @@
     
-.append(%22)%22
+whereClause.append(%22)%22);%0A        %7D%0A    %7D%0A%0A    private void addSequence(StringBuilder whereClause, ParameterRegistry parameters, Collection collection) %7B%0A        boolean first = true;%0A        for(Object ob : collection) %7B%0A            if (!first) %7B%0A                whereClause.append(',');%0A            %7D else %7B%0A                first = false;%0A            %7D%0A            whereClause.append(parameters.register(ob)
 );%0A 

Support List or Array for JdbcCriteria IN clause Example: JdbcCriteria criteria = (JdbcCriteria) builder.property("payload_type").in(new String[] {"my.project.InterestingDomainEvent", "my.project.ImportantDomainEvent"}) .and(builder.property("id").greaterThan(1234567)) .and(builder.property("id").lessThan(8901234));