View Javadoc

1   package org.kuali.rice.krms.util;
2   
3   import org.apache.commons.lang.BooleanUtils;
4   import org.kuali.rice.krad.uif.UifConstants;
5   import org.kuali.rice.krad.uif.component.Component;
6   import org.kuali.rice.krad.uif.container.Group;
7   import org.kuali.rice.krad.uif.container.LinkGroup;
8   import org.kuali.rice.krad.uif.container.TreeGroup;
9   import org.kuali.rice.krad.uif.element.Action;
10  import org.kuali.rice.krad.uif.element.Link;
11  import org.kuali.rice.krad.uif.field.MessageField;
12  import org.kuali.rice.krad.uif.util.ComponentFactory;
13  import org.kuali.rice.krad.uif.util.ComponentUtils;
14  import org.kuali.rice.krad.uif.view.View;
15  import org.kuali.rice.krad.util.KRADConstants;
16  import org.kuali.rice.krms.dto.*;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: danie
26   * Date: 3/7/13
27   * Time: 12:08 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class AgendaBuilder {
31  
32      private View view;
33      private Map<String, AgendaTypeInfo> typeRelationsMap;
34  
35      private int agendaCounter;
36      private int ruleCounter;
37  
38      private AlphaIterator alphaIterator = new AlphaIterator();
39  
40      public AgendaBuilder(View view) {
41          this.view = view;
42      }
43  
44      public void setTypeRelationsMap(Map<String, AgendaTypeInfo> typeRelationsMap) {
45          this.typeRelationsMap = typeRelationsMap;
46      }
47  
48      public List<Component> build(RuleManagementWrapper ruleManagementWrapper){
49  
50          // Get the list of existing agendas
51          List<AgendaEditor> agendas = ruleManagementWrapper.getAgendas();
52  
53          // Initialize new array lists.
54          List<AgendaEditor> sortedAgendas = new ArrayList<AgendaEditor>();
55          List<Component> components = new ArrayList<Component>();
56  
57          // Lookup existing agenda by type
58          List<AgendaTypeInfo> agendaTypeInfos = new ArrayList<AgendaTypeInfo>(typeRelationsMap.values());
59          for (AgendaTypeInfo agendaTypeInfo : agendaTypeInfos) {
60              boolean exist = false;
61              for (AgendaEditor agenda : agendas) {
62                  if (agenda.getTypeId().equals(agendaTypeInfo.getId())) {
63                      components.add(this.buildAgenda(agenda));
64                      exist = true;
65                      sortedAgendas.add(agenda);
66                  }
67              }
68              if (!exist) {
69                  AgendaEditor emptyAgenda = new AgendaEditor();
70                  emptyAgenda.setTypeId(agendaTypeInfo.getId());
71                  components.add(this.buildAgenda(emptyAgenda));
72                  sortedAgendas.add(emptyAgenda);
73              }
74          }
75  
76          ruleManagementWrapper.setAgendas(sortedAgendas);
77  
78          return components;
79      }
80  
81      /**
82       * This method dynamically build the components on the screen to render an angenda.
83       *
84       * @param agenda
85       * @return
86       */
87      public Component buildAgenda(AgendaEditor agenda) {
88          // Reset the rule counter.
89          ruleCounter = 0;
90  
91          AgendaTypeInfo agendaType = typeRelationsMap.get(agenda.getTypeId());
92          Group group = (Group) ComponentFactory.getNewComponentInstance("KRMS-AgendaSection-Template");
93          group.setHeaderText(agendaType.getDescription());
94  
95          List<Component> components = new ArrayList<Component>();
96          List<RuleEditor> ruleEditors = new ArrayList<RuleEditor>();
97          for (RuleTypeInfo ruleType : agendaType.getRuleTypes()) {
98  
99              // Add all existing rules of this type.
100             boolean exist = false;
101             if (agenda.getRuleEditors() != null) {
102                 for (RuleEditor rule : agenda.getRuleEditors()) {
103                     if (rule.getTypeId().equals(ruleType.getId()) && (!rule.isDummy())) {
104                         rule.setKey((String)alphaIterator.next());
105                         rule.setRuleTypeInfo(ruleType);
106                         components.add(buildRule(rule, ruleType, this.buildEditRuleSection(rule, ruleType)));
107                         exist = true;
108 
109                         ruleEditors.add(rule);
110                     }
111                 }
112             }
113 
114             // If the ruletype does not exist, add an empty rule section
115             if (!exist) {
116                 RuleEditor ruleEditor = new RuleEditor();
117                 components.add(buildRule(ruleEditor, ruleType, this.buildAddRuleSection(ruleEditor, ruleType)));
118 
119                 ruleEditor.setKey((String)alphaIterator.next());
120                 ruleEditor.setDummy(true);
121                 ruleEditor.setTypeId(ruleType.getId());
122                 ruleEditor.setRuleTypeInfo(ruleType);
123                 ruleEditors.add(ruleEditor);
124             }
125 
126         }
127 
128         group.setItems(components);
129         view.assignComponentIds(group);
130 
131         agendaCounter++;
132         agenda.setRuleEditors(ruleEditors);
133         return group;
134     }
135 
136     /**
137      * This method dynamically builds a disclosure section for each rule that already exists.
138      *
139      * @param rule
140      * @return
141      */
142     protected Component buildRule(RuleEditor rule, RuleTypeInfo ruleTypeInfo, Group ruleSection) {
143         Group group = (Group) ComponentFactory.getNewComponentInstance("KRMS-Rule-Template");
144         group.setHeaderText(ruleTypeInfo.getDescription());
145 
146         //Add edit container to disclosure section
147         List<Component> items = new ArrayList<Component>();
148         items.add(ruleSection);
149         group.setItems(items);
150 
151         ruleCounter++;
152         return group;
153     }
154 
155     protected Group buildEditRuleSection(RuleEditor rule, RuleTypeInfo ruleTypeInfo){
156         Group editSection = (Group) ComponentFactory.getNewComponentInstance("KRMS-RuleEdit-Section");
157         LinkGroup links = (LinkGroup) ComponentUtils.findComponentInList((List<Component>) editSection.getItems(), "KRSM-RuleEdit-ActionLinks");
158         List<Action> actionLinks = (List<Action>) links.getItems();
159         for (Action actionLink : actionLinks) {
160             actionLink.getActionParameters().put("ruleKey", rule.getKey());
161         }
162         MessageField messageField = (MessageField) ComponentUtils.findComponentInList((List<Component>) editSection.getItems(), "KRMS-Instruction-EditMessage");
163         messageField.setMessageText(ruleTypeInfo.getInstruction());
164 
165         Group sectionGroup = (Group) ComponentUtils.findComponentInList((List<Component>) editSection.getItems(), "KRMS-PreviewTree-Group");
166         List<TreeGroup> treeGroups = ComponentUtils.getComponentsOfType((List<Component>) sectionGroup.getItems(), TreeGroup.class);
167         if ((treeGroups != null) && (treeGroups.size() > 0)) {
168             treeGroups.get(0).setPropertyName("agendas[" + agendaCounter + "].ruleEditors[" + ruleCounter + "].viewTree");
169         }
170 
171         return editSection;
172     }
173 
174     /**
175      * This method dynamically builds a disclosure section to allow the user to add a new rule for this rule type.
176      *
177      * @param ruleTypeInfo
178      * @return
179      */
180     protected Group buildAddRuleSection(RuleEditor ruleEditor, RuleTypeInfo ruleTypeInfo) {
181         Group addSection = (Group) ComponentFactory.getNewComponentInstance("KRMS-RuleAdd-Section");
182         LinkGroup links = (LinkGroup) ComponentUtils.findComponentInList((List<Component>) addSection.getItems(), "KRMS-RuleAdd-ActionLink");
183         List<Action> actionLinks = (List<Action>) links.getItems();
184         for (Action actionLink : actionLinks) {
185             actionLink.getActionParameters().put("ruleType", ruleTypeInfo.getId());
186         }
187         MessageField messageField = (MessageField) ComponentUtils.findComponentInList((List<Component>) addSection.getItems(), "KRMS-Instruction-AddMessage");
188         messageField.setMessageText(ruleTypeInfo.getInstruction());
189 
190         return addSection;
191     }
192 
193 }