001/**
002 * Copyright 2005-2017 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.actionrequest.bo;
017
018import org.kuali.rice.core.api.util.ConcreteKeyValue;
019import org.kuali.rice.core.api.util.KeyValue;
020import org.kuali.rice.kew.rule.RuleBaseValues;
021import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
022import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.kns.document.MaintenanceDocument;
025import org.kuali.rice.kns.util.KNSGlobalVariables;
026import org.kuali.rice.kns.web.struts.form.KualiForm;
027import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
028
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * A values finder for returning KEW Action Request codes related to Kuali maintenance forms.
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class RuleMaintenanceActionRequestCodeValuesFinder extends ActionRequestCodeValuesFinder {
039
040        /**
041         * @see org.kuali.rice.krad.keyvalues.KeyValuesFinder#getKeyValues()
042         */
043        @Override
044        public List<KeyValue> getKeyValues() {
045                final List<KeyValue> actionRequestCodes = new ArrayList<KeyValue>();
046                // Acquire the Kuali form, and return the super class' result if the form is not a Kuali maintenance form.
047                final KualiForm kForm = KNSGlobalVariables.getKualiForm();
048                if (!(kForm instanceof KualiMaintenanceForm)) {
049                        return super.getKeyValues();
050                }
051                // Acquire the Kuali maintenance form's document and its rule template.
052                final MaintenanceDocument maintDoc = (MaintenanceDocument) ((KualiMaintenanceForm) kForm).getDocument();
053                final RuleTemplateBo ruleTemplate = ((RuleBaseValues) maintDoc.getNewMaintainableObject().getBusinessObject()).getRuleTemplate();
054                // Ensure that the rule template is defined.
055                if (ruleTemplate == null) {
056                        throw new RuntimeException("Rule template cannot be null for document ID " + maintDoc.getDocumentNumber());
057                }
058                // get the options to check for, as well as their related KEW constants.
059                final RuleTemplateOptionBo[] ruleOpts = {ruleTemplate.getAcknowledge(), ruleTemplate.getComplete(),
060                                ruleTemplate.getApprove(), ruleTemplate.getFyi()};
061                final String[] ruleConsts = {KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ,
062                                KewApiConstants.ACTION_REQUEST_APPROVE_REQ, KewApiConstants.ACTION_REQUEST_FYI_REQ};
063                // Add the rule options to the list if they are not defined (true by default) or if they are explicitly set to true.
064                for (int i = 0; i < ruleOpts.length; i++) {
065                        if (ruleOpts[i] == null || ruleOpts[i].getValue() == null || "true".equals(ruleOpts[i].getValue())) {
066                                actionRequestCodes.add(new ConcreteKeyValue(ruleConsts[i], KewApiConstants.ACTION_REQUEST_CODES.get(ruleConsts[i])));
067                        }
068                }
069                return actionRequestCodes;
070        }
071        
072}