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