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.uif.component.Component;
019import org.kuali.rice.krad.uif.container.Group;
020import org.kuali.rice.krad.uif.container.TreeGroup;
021import org.kuali.rice.krad.uif.element.Action;
022import org.kuali.rice.krad.uif.field.DataField;
023import org.kuali.rice.krad.uif.util.ComponentUtils;
024import org.kuali.rice.krms.dto.AgendaEditor;
025import org.kuali.rice.krms.dto.RuleEditor;
026import org.kuali.rice.krms.service.RuleViewHelperService;
027
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * @author Kuali Student Team
033 */
034public class AgendaBuilder {
035
036    public static final String AGENDA = "_agenda";
037    public static final String RULE = "_rule";
038
039    private RuleViewHelperService viewHelperService;
040
041    /**
042     * This method dynamically build the components on the screen to render an agenda.
043     *
044     * @param agenda
045     * @return
046     */
047    public Component buildAgenda(AgendaEditor agenda, int index, AgendaSection agendaSection) {
048        String agendaSuffix = AGENDA + Integer.toString(index);
049        Group group = ComponentUtils.copy(agendaSection.getAgendaPrototype(), agendaSuffix);
050        group.setHeaderText(agenda.getAgendaTypeInfo().getDescription());
051
052        String bindingPrefix = "agendas[" + index + "].";
053        List<Component> components = new ArrayList<Component>();
054        for (RuleEditor rule : agenda.getRuleEditors().values()) {
055            components.add(buildRule(rule, bindingPrefix, agendaSection));
056        }
057
058        group.setItems(components);
059
060        return group;
061    }
062
063    /**
064     * This method dynamically builds a disclosure section for each rule that already exists.
065     *
066     * @param rule
067     * @return
068     */
069    protected Component buildRule(RuleEditor rule, String bindingPrefix, AgendaSection agendaSection) {
070        String ruleSuffix = RULE + rule.getKey();
071        Group group = ComponentUtils.copy(agendaSection.getRulePrototype(), ruleSuffix);
072        group.setHeaderText(rule.getRuleTypeInfo().getDescription());
073
074        //Set the rule key on the action links.
075        List<Action> actionLinks = ComponentUtils.getComponentsOfTypeDeep(group, Action.class);
076        for (Action actionLink : actionLinks) {
077            actionLink.getActionParameters().put("ruleKey", rule.getKey());
078            actionLink.getActionParameters().put("ruleType", rule.getRuleTypeInfo().getType());
079        }
080
081        ComponentUtils.updateContextForLine(group, rule, 0, ruleSuffix);
082
083        String bindingPath = bindingPrefix + "ruleEditors[" + rule.getKey() + "].";
084        this.setPropertyBindingPaths(group, bindingPath);
085
086        return group;
087    }
088
089    protected void setPropertyBindingPaths(Group group, String bindingPath) {
090
091        List<DataField> dataFields = ComponentUtils.getComponentsOfTypeDeep(group.getItems(), DataField.class);
092        for (DataField collectionField : dataFields) {
093            collectionField.getBindingInfo().setBindingName(bindingPath + collectionField.getBindingInfo().getBindingName());
094        }
095
096        List<TreeGroup> treeFields = ComponentUtils.getComponentsOfTypeDeep(group.getItems(), TreeGroup.class);
097        for (TreeGroup collectionField : treeFields) {
098            collectionField.getBindingInfo().setBindingName(bindingPath + collectionField.getBindingInfo().getBindingName());
099        }
100    }
101
102    public RuleViewHelperService getViewHelperService() {
103        return viewHelperService;
104    }
105
106    public void setViewHelperService(RuleViewHelperService viewHelperService) {
107        this.viewHelperService = viewHelperService;
108    }
109}