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