View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.util;
17  
18  import org.kuali.rice.krad.uif.component.Component;
19  import org.kuali.rice.krad.uif.container.CollectionGroupBase;
20  import org.kuali.rice.krad.uif.container.CollectionGroupBuilder;
21  import org.kuali.rice.krad.uif.container.Group;
22  import org.kuali.rice.krad.uif.container.TreeGroup;
23  import org.kuali.rice.krad.uif.element.Action;
24  import org.kuali.rice.krad.uif.field.DataField;
25  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
26  import org.kuali.rice.krad.uif.util.ComponentUtils;
27  import org.kuali.rice.krms.dto.AgendaEditor;
28  import org.kuali.rice.krms.dto.RuleEditor;
29  import org.kuali.rice.krms.service.RuleViewHelperService;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * @author Kuali Student Team
36   */
37  public class AgendaBuilder {
38  
39      public static final String AGENDA = "_agenda";
40      public static final String RULE = "_rule";
41  
42      private RuleViewHelperService viewHelperService;
43  
44      /**
45       * This method dynamically build the components on the screen to render an agenda.
46       *
47       * @param agenda
48       * @return
49       */
50      public Component buildAgenda(AgendaEditor agenda, int index, AgendaSection agendaSection) {
51          String agendaSuffix = AGENDA + Integer.toString(index);
52          Group group = ComponentUtils.copy(agendaSection.getAgendaPrototype(), agendaSuffix);
53          group.setHeaderText(agenda.getAgendaTypeInfo().getDescription());
54  
55          String bindingPrefix = agendaSection.getPropertyName() + "[" + index + "].";
56          List<Component> components = new ArrayList<Component>();
57          for (RuleEditor rule : agenda.getRuleEditors().values()) {
58              components.add(buildRule(rule, bindingPrefix, agendaSection));
59          }
60  
61          group.setItems(components);
62  
63          return group;
64      }
65  
66      /**
67       * This method dynamically builds a disclosure section for each rule that already exists.
68       *
69       * @param rule
70       * @return
71       */
72      protected Component buildRule(RuleEditor rule, String bindingPrefix, AgendaSection agendaSection) {
73          String ruleSuffix = RULE + rule.getKey();
74          Group group = ComponentUtils.copy(agendaSection.getRulePrototype(), ruleSuffix);
75          group.setHeaderText(rule.getRuleTypeInfo().getDescription());
76  
77          //Set the rule key on the action links.
78          List<Action> actionLinks = ViewLifecycleUtils.getElementsOfTypeDeep(group, Action.class);
79          for (Action actionLink : actionLinks) {
80              actionLink.getActionParameters().put("ruleKey", rule.getKey());
81              actionLink.getActionParameters().put("ruleType", rule.getRuleTypeInfo().getType());
82          }
83  
84          ComponentUtils.updateContextForLine(group, null, rule, 0, ruleSuffix);
85  
86          String bindingPath = bindingPrefix + "ruleEditors[" + rule.getKey() + "].";
87          this.setPropertyBindingPaths(group, bindingPath);
88  
89          return group;
90      }
91  
92      protected void setPropertyBindingPaths(Group group, String bindingPath) {
93  
94          List<DataField> dataFields = ViewLifecycleUtils.getElementsOfTypeDeep(group.getItems(), DataField.class);
95          for (DataField collectionField : dataFields) {
96              collectionField.getBindingInfo().setBindingName(bindingPath + collectionField.getBindingInfo().getBindingName());
97          }
98  
99          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 }