/* Copyright 2004-2005 the original author or authors.
 * 
 * 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.codehaus.groovy.grails.orm.hibernate.metaclass;

import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Disjunction;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * The "findBy*" static persistent method. This method allows querying for
 * instances of grails domain classes based on their properties. This method returns the first result of the query
 * 
 * eg.
 * Account.findByHolder("Joe Blogs"); // Where class "Account" has a property called "holder"
 * Account.findByHolderAndBranch("Joe Blogs", "London" ); // Where class "Account" has a properties called "holder" and "branch"
 * 
 * @author Graeme Rocher
 * @since 31-Aug-2005
 *
 */
public class FindByPersistentMethod extends AbstractClausedStaticPersistentMethod {
	
	private static final String OPERATOR_OR = "Or";
	private static final String OPERATOR_AND = "And";
	
	private static final String METHOD_PATTERN = "(findBy)(\\w+)";
	private static final String[] OPERATORS = new String[]{ OPERATOR_AND, OPERATOR_OR };

	public FindByPersistentMethod(GrailsApplication application,SessionFactory sessionFactory, ClassLoader classLoader) {
 		super(application,sessionFactory, classLoader, Pattern.compile( METHOD_PATTERN ),OPERATORS);
	}

	protected Object doInvokeInternalWithExpressions(final Class clazz, String methodName, final Object[] arguments, final List expressions, String operatorInUse) {

        final String operator = OPERATOR_OR.equals(operatorInUse) ? OPERATOR_OR : OPERATOR_AND;
        return super.getHibernateTemplate().execute( new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				
				Criteria crit = session.createCriteria(clazz);
				if(arguments.length > 0) {
					if(arguments[0] instanceof Map) {
						Map argMap = (Map)arguments[0];
						GrailsHibernateUtil.populateArgumentsForCriteria(clazz, crit,argMap);
					}
				}
                if(operator.equals(OPERATOR_OR)) {
                    if(firstExpressionIsRequiredBoolean()) {
                        GrailsMethodExpression expression = (GrailsMethodExpression) expressions.remove(0);
                        crit.add(expression.getCriterion());
                    }
                    Disjunction dis = Restrictions.disjunction();
                    for (Iterator i = expressions.iterator(); i.hasNext();) {
                        GrailsMethodExpression current = (GrailsMethodExpression) i.next();
                        dis.add( current.getCriterion() );
                    }
                    crit.add(dis);
                }
                else {
                    for (Iterator i = expressions.iterator(); i.hasNext();) {
                        GrailsMethodExpression current = (GrailsMethodExpression) i.next();
                        crit.add( current.getCriterion() );

                    }
                }
                crit.setMaxResults(1);
                return crit.uniqueResult();
            }
		});
	}

    /**
     * Indicates if the first expression in the query is a required boolean property and as such should
     * be ANDed to the other expressions, not ORed.
     *
     * @return true if the first expression is a required boolean property, false otherwise
     * @see org.codehaus.groovy.grails.orm.hibernate.metaclass.FindByBooleanPropertyPersistentMethod
     */
    protected boolean firstExpressionIsRequiredBoolean() {
        return false;
    }

}

@@ -980,28 +980,27 @@
 iterion.
-Restri
+Disjun
 ction
-s
 ;%0Aimport
@@ -1020,35 +1020,36 @@
 e.criterion.
-Disjun
+Restri
 ction
+s
 ;%0Aimport org
@@ -1134,35 +1134,8 @@
 on;%0A
-import java.util.Iterator;%0A
 impo
@@ -2922,16 +2922,179 @@
 rgMap);%0A
+                        if(!argMap.containsKey(GrailsHibernateUtil.ARGUMENT_FETCH)) %7B%0A                            crit.setMaxResults(1);%0A                        %7D%0A
 %09%09%09%09%09%7D%0A%09
@@ -3496,57 +3496,39 @@
 or (
-Iterator i = expressions.iterator(); i.hasNext();
+Object expression : expressions
 ) %7B%0A
@@ -3605,32 +3605,34 @@
 Expression) 
-i.next()
+expression
 ;%0A          
@@ -3649,25 +3649,24 @@
     dis.add(
- 
 current.getC
@@ -3675,17 +3675,16 @@
 terion()
- 
 );%0A     
@@ -3805,57 +3805,39 @@
 or (
-Iterator i = expressions.iterator(); i.hasNext();
+Object expression : expressions
 ) %7B%0A
@@ -3922,16 +3922,18 @@
 on) 
-i.next()
+expression
 ;%0A  
@@ -3963,17 +3963,16 @@
 rit.add(
- 
 current.
@@ -3985,17 +3985,16 @@
 terion()
- 
 );%0A%0A    
@@ -4021,32 +4021,34 @@
               %7D%0A
+%0A%0A
                 
@@ -4047,39 +4047,88 @@
         
-crit.setMaxResults(1);%0A
+final List list = crit.list();%0A                if(!list.isEmpty()) %7B%0A   
         
@@ -4139,34 +4139,74 @@
     
+ 
 return 
-crit.uniqueResult()
+list.get(0);%0A                %7D%0A                return null
 ;%0A  

fix for failing test