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.Group;
20  import org.kuali.rice.krad.uif.container.TreeGroup;
21  import org.kuali.rice.krad.uif.element.Action;
22  import org.kuali.rice.krad.uif.field.DataField;
23  import org.kuali.rice.krad.uif.util.ComponentUtils;
24  import org.kuali.rice.krms.dto.AgendaEditor;
25  import org.kuali.rice.krms.dto.RuleEditor;
26  import org.kuali.rice.krms.service.RuleViewHelperService;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * @author Kuali Student Team
33   */
34  public class AgendaBuilder {
35  
36      public static final String AGENDA = "_agenda";
37      public static final String RULE = "_rule";
38  
39      private RuleViewHelperService viewHelperService;
40  
41      /**
42       * This method dynamically build the components on the screen to render an agenda.
43       *
44       * @param agenda
45       * @return
46       */
47      public Component buildAgenda(AgendaEditor agenda, int index, AgendaSection agendaSection) {
48          String agendaSuffix = AGENDA + Integer.toString(index);
49          Group group = ComponentUtils.copy(agendaSection.getAgendaPrototype(), agendaSuffix);
50          group.setHeaderText(agenda.getAgendaTypeInfo().getDescription());
51  
52          String bindingPrefix = "agendas[" + index + "].";
53          List<Component> components = new ArrayList<Component>();
54          for (RuleEditor rule : agenda.getRuleEditors().values()) {
55              components.add(buildRule(rule, bindingPrefix, agendaSection));
56          }
57  
58          group.setItems(components);
59  
60          return group;
61      }
62  
63      /**
64       * This method dynamically builds a disclosure section for each rule that already exists.
65       *
66       * @param rule
67       * @return
68       */
69      protected Component buildRule(RuleEditor rule, String bindingPrefix, AgendaSection agendaSection) {
70          String ruleSuffix = RULE + rule.getKey();
71          Group group = ComponentUtils.copy(agendaSection.getRulePrototype(), ruleSuffix);
72          group.setHeaderText(rule.getRuleTypeInfo().getDescription());
73  
74          //Set the rule key on the action links.
75          List<Action> actionLinks = ComponentUtils.getComponentsOfTypeDeep(group, Action.class);
76          for (Action actionLink : actionLinks) {
77              actionLink.getActionParameters().put("ruleKey", rule.getKey());
78              actionLink.getActionParameters().put("ruleType", rule.getRuleTypeInfo().getType());
79          }
80  
81          ComponentUtils.updateContextForLine(group, rule, 0, ruleSuffix);
82  
83          String bindingPath = bindingPrefix + "ruleEditors[" + rule.getKey() + "].";
84          this.setPropertyBindingPaths(group, bindingPath);
85  
86          return group;
87      }
88  
89      protected void setPropertyBindingPaths(Group group, String bindingPath) {
90  
91          List<DataField> dataFields = ComponentUtils.getComponentsOfTypeDeep(group.getItems(), DataField.class);
92          for (DataField collectionField : dataFields) {
93              collectionField.getBindingInfo().setBindingName(bindingPath + collectionField.getBindingInfo().getBindingName());
94          }
95  
96          List<TreeGroup> treeFields = ComponentUtils.getComponentsOfTypeDeep(group.getItems(), TreeGroup.class);
97          for (TreeGroup collectionField : treeFields) {
98              collectionField.getBindingInfo().setBindingName(bindingPath + collectionField.getBindingInfo().getBindingName());
99          }
100     }
101 
102     public RuleViewHelperService getViewHelperService() {
103         return viewHelperService;
104     }
105 
106     public void setViewHelperService(RuleViewHelperService viewHelperService) {
107         this.viewHelperService = viewHelperService;
108     }
109 }