/*
 * Copyright 2007 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.kuali.rice.kew.rule;

import org.kuali.rice.core.api.criteria.Predicate;
import org.kuali.rice.core.api.criteria.QueryByCriteria;
import org.kuali.rice.kew.api.KewApiServiceLocator;
import org.kuali.rice.kew.api.WorkflowRuntimeException;
import org.kuali.rice.kew.api.rule.RuleTemplate;
import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
import org.kuali.rice.kew.engine.RouteContext;
import org.kuali.rice.kew.engine.node.RouteNodeInstance;
import org.kuali.rice.kew.routeheader.DocumentContent;
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
import org.kuali.rice.kew.service.KEWServiceLocator;
import org.kuali.rice.kew.util.PerformanceLogger;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static org.kuali.rice.core.api.criteria.PredicateFactory.*;

/**
 * Rule selector that selects rules based on configured template name 
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
class TemplateRuleSelector implements RuleSelector {
    /**
     * Records the number of selected rules, prior to MassRuleAttribute filtering
     */
    private int numberOfSelectedRules;

    /**
     * @return the number of selected rules, prior to MassRuleAttribute filtering
     */
    int getNumberOfSelectedRules() {
	return numberOfSelectedRules;
    }

    public List<Rule> selectRules(RouteContext context, DocumentRouteHeaderValue routeHeader, RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) {
        // for TemplateRuleSelector, the criterion is taken as a ruletemplate name
        final String ruleTemplateName = selectionCriterion;

        Set<MassRuleAttribute> massRules = new HashSet<MassRuleAttribute>();
        RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
        //RuleTemplate template = KewApiServiceLocator.getRuleService().getRuleTemplateByName(ruleTemplateName);
        if (template == null) {
            throw new WorkflowRuntimeException("Could not locate the rule template with name " + ruleTemplateName + " on document " + routeHeader.getDocumentId());
        }
        for (RuleTemplateAttributeBo templateAttribute : template.getActiveRuleTemplateAttributes()) {
            if (!templateAttribute.isWorkflowAttribute()) {
            continue;
            }
            WorkflowAttribute attribute = templateAttribute.getWorkflowAttribute();
            if (attribute instanceof MassRuleAttribute) {
            massRules.add((MassRuleAttribute) attribute);
            }

        }

        List<org.kuali.rice.kew.api.rule.Rule> rules = null;
        QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
        List<Predicate> predicates = new ArrayList<Predicate>();
        predicates.add(equal("ruleTemplate.name", ruleTemplateName));
        predicates.add(equal("docTypeName", routeHeader.getDocumentType().getName()));
        predicates.add(equal("active", new Integer(1)));
        if (effectiveDate != null) {
            predicates.add(
                    and(
                        lessThanOrEqual("activationDate", effectiveDate),
                        greaterThan("deactivationDate", effectiveDate)));
            //rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplateName, routeHeader.getDocumentType().getName(), effectiveDate);
        } /*else {
            rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplateName, routeHeader.getDocumentType().getName());
        }*/
        Predicate p = and(predicates.toArray(new Predicate[]{}));
        query.setPredicates(p);
        rules = KewApiServiceLocator.getRuleService().findRules(query.build()).getResults();
        numberOfSelectedRules = rules.size();

        // TODO really the route context just needs to be able to support nested create and clears
        // (i.e. a Stack model similar to transaction intercepting in Spring) and we wouldn't have to do this
        if (context.getDocument() == null) {
            context.setDocument(routeHeader);
        }
        if (context.getNodeInstance() == null) {
            context.setNodeInstance(nodeInstance);
        }
        DocumentContent documentContent = context.getDocumentContent();
        PerformanceLogger performanceLogger = new PerformanceLogger();
        // have all mass rule attributes filter the list of non applicable rules
        for (Iterator iter = massRules.iterator(); iter.hasNext();) {
            MassRuleAttribute massRuleAttribute = (MassRuleAttribute) iter.next();
            rules = massRuleAttribute.filterNonMatchingRules(context, rules);
        }
        performanceLogger.log("Time to filter massRules for template " + template.getName());

        List<Rule> ruleList = new ArrayList<Rule>(rules.size());
        for (org.kuali.rice.kew.api.rule.Rule ruleDefinition: rules) {
            ruleList.add(new RuleImpl(ruleDefinition));
        }
        return ruleList;
    }

}

@@ -962,24 +962,75 @@
 eAttribute;%0A
+import org.kuali.rice.kew.doctype.bo.DocumentType;%0A
 import org.k
@@ -3699,32 +3699,33 @@
 lateName));%0A
+%0A
         
 predicates.a
@@ -3716,86 +3716,440 @@
     
-predicates.add(equal(%22docTypeName%22, routeHeader.getDocumentType().getName()));
+// Check all document types in ancestry%0A        DocumentType dt = routeHeader.getDocumentType();%0A        List%3CPredicate%3E documentTypeAncestry = new ArrayList%3CPredicate%3E();%0A        while (dt != null) %7B%0A            documentTypeAncestry.add(equal(%22docTypeName%22, dt.getName()));%0A            dt = dt.getParentDocType();%0A        %7D%0A        predicates.add(and(or(documentTypeAncestry.toArray(new Predicate%5BdocumentTypeAncestry.size()%5D))));%0A
 %0A   

include document type ancestry in template selection