001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.rule;
017
018import org.kuali.rice.kew.api.extension.ExtensionDefinition;
019import org.kuali.rice.kew.api.rule.RoleName;
020import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
021import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
022import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeFields;
023import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
024import org.kuali.rice.kew.rule.bo.RuleAttribute;
025import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
026
027import java.util.Collections;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * Reference implementation of {@code WorkflowRuleAttributeMediator}.
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class WorkflowRuleAttributeMediatorImpl implements WorkflowRuleAttributeMediator {
037
038    @Override
039    public WorkflowRuleAttributeRows getRuleRows(Map<String, String> parameters, RuleTemplateAttributeBo ruleTemplateAttribute) {
040        required(ruleTemplateAttribute, "ruleTemplateAttribute");
041        if (parameters == null) {
042            parameters = Collections.emptyMap();
043        }
044        RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
045        ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
046        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
047        WorkflowRuleAttributeFields fields = handler.getRuleFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
048        return new WorkflowRuleAttributeRows(fields);
049    }
050
051    @Override
052    public WorkflowRuleAttributeRows getRuleRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
053        required(ruleTemplateAttribute, "ruleTemplateAttribute");
054        if (parameters == null) {
055            parameters = Collections.emptyMap();
056        }
057        ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
058        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
059        WorkflowRuleAttributeFields fields = handler.getRuleFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
060        return new WorkflowRuleAttributeRows(fields);
061    }
062    
063    @Override
064    public WorkflowRuleAttributeRows getRoutingDataRows(Map<String, String> parameters, RuleTemplateAttributeBo ruleTemplateAttribute) {
065        required(ruleTemplateAttribute, "ruleTemplateAttribute");
066        if (parameters == null) {
067            parameters = Collections.emptyMap();
068        }
069        RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
070        ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
071        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
072        WorkflowRuleAttributeFields fields = handler.getRoutingDataFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
073        return new WorkflowRuleAttributeRows(fields);
074    }
075    
076    @Override
077    public WorkflowRuleAttributeRows getRoutingDataRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
078        required(ruleTemplateAttribute, "ruleTemplateAttribute");
079        if (parameters == null) {
080            parameters = Collections.emptyMap();
081        }
082        ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
083        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
084        WorkflowRuleAttributeFields fields = handler.getRoutingDataFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
085        return new WorkflowRuleAttributeRows(fields);
086    }
087
088    @Override
089    public WorkflowRuleAttributeRows getSearchRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
090        required(ruleTemplateAttribute, "ruleTemplateAttribute");
091        if (parameters == null) {
092            parameters = Collections.emptyMap();
093        }
094        ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
095        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
096        WorkflowRuleAttributeFields fields = handler.getSearchFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
097        return new WorkflowRuleAttributeRows(fields);
098
099    }
100
101    @Override
102    public WorkflowRuleAttributeRows getSearchRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute, boolean required) {
103        required(ruleTemplateAttribute, "ruleTemplateAttribute");
104        if (parameters == null) {
105            parameters = Collections.emptyMap();
106        }
107        ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
108        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
109        WorkflowRuleAttributeFields fields = handler.getSearchFields(parameters, extensionDefinition, required);
110        return new WorkflowRuleAttributeRows(fields);
111
112    }
113
114    @Override
115    public List<RoleName> getRoleNames(RuleTemplateAttributeBo ruleTemplateAttribute) {
116        required(ruleTemplateAttribute, "ruleTemplateAttribute");
117        RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
118        ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
119        WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
120        return handler.getRoleNames(extensionDefinition);
121    }
122
123    private void required(Object object, String name) {
124        if (object == null) {
125            throw new IllegalArgumentException(name + " must not be null");
126        }
127    }
128
129    /**
130     * Retrieves the WorkflowRuleAttributeHandlerService which can be used to handle the given ExtensionDefinition. If
131     * such a service cannot be loaded, an IllegalStateException is thrown.
132     *
133     * @param extensionDefinition the definition of the extension which will be accessed through the handler service
134     *
135     * @return a reference to a handler service which can be used to interact with the given extension definition
136     *
137     * @throws IllegalStateException if the handler cannot be found
138     */
139    protected WorkflowRuleAttributeHandlerService getHandler(ExtensionDefinition extensionDefinition) {
140        WorkflowRuleAttributeHandlerService handler =
141                KewFrameworkServiceLocator.getWorkflowRuleAttributeHandlerService(extensionDefinition.getApplicationId());
142        if (handler == null) {
143            throw new IllegalStateException("Failed to locate a WorkflowRuleAttributeHandlerService for the given ExtensionDefinition: " + extensionDefinition);
144        }
145        return handler;
146    }
147
148
149}