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