001/**
002 * Copyright 2005-2013 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.krms.util;
017
018import org.kuali.rice.krad.util.ObjectUtils;
019import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
020import org.kuali.rice.krms.dto.AgendaEditor;
021import org.kuali.rice.krms.dto.RuleEditor;
022import org.kuali.rice.krms.dto.RuleManagementWrapper;
023import org.kuali.rice.krms.dto.RuleManager;
024
025/**
026 * @author Kuali Student Team
027 */
028public class AgendaUtilities {
029
030    public static RuleEditor retrieveSelectedRuleEditor(MaintenanceDocumentForm document) {
031
032        RuleManager ruleWrapper = getRuleWrapper(document);
033        RuleEditor ruleEditor = getSelectedRuleEditor(ruleWrapper, getRuleKey(document));
034        ruleWrapper.setRuleEditor((RuleEditor) ObjectUtils.deepCopy(ruleEditor));
035
036        return ruleWrapper.getRuleEditor();
037    }
038
039    public static RuleEditor getSelectedRuleEditor(MaintenanceDocumentForm document) {
040        return AgendaUtilities.getSelectedRuleEditor(getRuleWrapper(document), getRuleKey(document));
041    }
042
043    public static RuleEditor getSelectedRuleEditor(RuleManager wrapper, String ruleKey) {
044
045        AgendaEditor agendaEditor = getSelectedAgendaEditor(wrapper, ruleKey);
046        if (agendaEditor != null) {
047            return agendaEditor.getRuleEditors().get(ruleKey);
048        }
049
050        return null;
051    }
052
053    public static AgendaEditor getSelectedAgendaEditor(MaintenanceDocumentForm document) {
054        return AgendaUtilities.getSelectedAgendaEditor(getRuleWrapper(document), getRuleKey(document));
055    }
056
057    public static AgendaEditor getSelectedAgendaEditor(RuleManager wrapper, String ruleKey) {
058
059        for (AgendaEditor agendaEditor : wrapper.getAgendas()) {
060            if (agendaEditor.getRuleEditors().containsKey(ruleKey)) {
061                return agendaEditor;
062            }
063        }
064
065        return null;
066    }
067
068    public static String getRuleKey(MaintenanceDocumentForm document) {
069        return document.getActionParamaterValue("ruleKey");
070    }
071
072    public static RuleManager getRuleWrapper(MaintenanceDocumentForm document) {
073        return (RuleManager) document.getDocument().getNewMaintainableObject().getDataObject();
074    }
075
076}