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.bo;
017
018import org.kuali.rice.core.api.exception.RiceRuntimeException;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.kew.api.rule.RoleName;
022import org.kuali.rice.kew.rule.RuleBaseValues;
023import org.kuali.rice.kew.rule.RuleDelegationBo;
024import org.kuali.rice.kns.document.MaintenanceDocument;
025import org.kuali.rice.kns.util.KNSGlobalVariables;
026import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
027import org.kuali.rice.krad.bo.PersistableBusinessObject;
028import org.kuali.rice.krad.keyvalues.KeyValuesBase;
029
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * A values finder for generating a list of Role names that can be selected for a given RuleTemplate.
035 * 
036 * This is dependant on the template selected on the maintenance document so it needs to use
037 * GlobalVariables to get a reference to the KualiForm so it can examine the business object
038 * and extract the role names from the RuleTemplate.
039 * 
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 */
042public class RoleNameValuesFinder extends KeyValuesBase {
043        
044        @Override
045        public List<KeyValue> getKeyValues() {
046                List<KeyValue> roleNames = new ArrayList<KeyValue>();
047                if (KNSGlobalVariables.getKualiForm() != null && KNSGlobalVariables.getKualiForm() instanceof KualiMaintenanceForm) {
048                        KualiMaintenanceForm form = (KualiMaintenanceForm)KNSGlobalVariables.getKualiForm();
049                        MaintenanceDocument document = (MaintenanceDocument)form.getDocument();
050                        PersistableBusinessObject businessObject = document.getNewMaintainableObject().getBusinessObject();
051                        RuleBaseValues rule = null;
052                        if (businessObject instanceof RuleBaseValues) {
053                                rule = (RuleBaseValues)businessObject;
054                        } else if (businessObject instanceof RuleDelegationBo) {
055                                rule = ((RuleDelegationBo)businessObject).getDelegationRule();
056                        } else {
057                                throw new RiceRuntimeException("Cannot locate RuleBaseValues business object on maintenance document.  Business Object was " + businessObject);
058                        }
059                        RuleTemplateBo ruleTemplate = rule.getRuleTemplate();
060                        List<RoleName> roles = ruleTemplate.getRoles();
061                        for (RoleName role : roles) {
062                                roleNames.add(new ConcreteKeyValue(role.getName(), role.getLabel()));
063                        }
064                }
065                return roleNames;
066        }
067
068}