1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.ui;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.service.KRADServiceLocator;
20 import org.kuali.rice.krad.service.SequenceAccessorService;
21 import org.kuali.rice.krad.uif.UifParameters;
22 import org.kuali.rice.krad.util.ObjectUtils;
23 import org.kuali.rice.krad.web.controller.InquiryController;
24 import org.kuali.rice.krad.web.form.InquiryForm;
25 import org.kuali.rice.krad.web.form.UifFormBase;
26 import org.kuali.rice.krms.impl.repository.ActionBo;
27 import org.kuali.rice.krms.impl.repository.AgendaBo;
28 import org.kuali.rice.krms.impl.repository.AgendaItemBo;
29 import org.kuali.rice.krms.impl.repository.ContextBoService;
30 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
31 import org.kuali.rice.krms.impl.repository.RuleBo;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.web.bind.annotation.ModelAttribute;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.servlet.ModelAndView;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 @Controller
42 @RequestMapping(value = org.kuali.rice.krms.impl.util.KrmsImplConstants.WebPaths.AGENDA_INQUIRY_PATH)
43 public class AgendaInquiryController extends InquiryController {
44
45
46
47
48 @RequestMapping(params = "methodToCall=" + "viewRule")
49 public ModelAndView viewRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
50 HttpServletRequest request, HttpServletResponse response) throws Exception {
51
52 AgendaEditor agendaEditor = getAgendaEditor(form);
53 agendaEditor.setAddRuleInProgress(false);
54
55 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
56
57 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
58 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
59
60 setAgendaItemLine(form, node);
61
62 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-ViewRule-Page");
63 return super.navigate(form, result, request, response);
64 }
65
66
67
68
69
70 private AgendaEditor getAgendaEditor(UifFormBase form) {
71 InquiryForm inquiryForm = (InquiryForm) form;
72 return ((AgendaEditor)inquiryForm.getDataObject());
73 }
74
75
76
77
78
79
80
81 private AgendaItemBo getFirstAgendaItem(AgendaBo agenda) {
82 AgendaItemBo firstItem = null;
83 if (agenda != null && agenda.getItems() != null) for (AgendaItemBo agendaItem : agenda.getItems()) {
84 if (agenda.getFirstItemId().equals(agendaItem.getId())) {
85 firstItem = agendaItem;
86 break;
87 }
88 }
89 return firstItem;
90 }
91
92
93
94
95 private AgendaItemBo getAgendaItemById(AgendaItemBo node, String agendaItemId) {
96 if (node == null) throw new IllegalArgumentException("node must be non-null");
97
98 AgendaItemBo result = null;
99
100 if (agendaItemId.equals(node.getId())) {
101 result = node;
102 } else {
103 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
104 AgendaItemBo child = childAccessor.getChild(node);
105 if (child != null) {
106 result = getAgendaItemById(child, agendaItemId);
107 if (result != null) break;
108 }
109 }
110 }
111 return result;
112 }
113
114
115
116
117
118
119
120
121
122 private void setAgendaItemLine(UifFormBase form, AgendaItemBo agendaItem) {
123 AgendaEditor agendaEditor = getAgendaEditor(form);
124 if (agendaItem == null) {
125 RuleBo rule = new RuleBo();
126 rule.setId(getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_RULE_S", RuleBo.class)
127 .toString());
128 if (StringUtils.isBlank(agendaEditor.getAgenda().getContextId())) {
129 rule.setNamespace("");
130 } else {
131 rule.setNamespace(getContextBoService().getContextByContextId(agendaEditor.getAgenda().getContextId()).getNamespace());
132 }
133 agendaItem = new AgendaItemBo();
134 agendaItem.setRule(rule);
135 agendaEditor.setAgendaItemLine(agendaItem);
136 } else {
137
138 agendaEditor.setAgendaItemLine((AgendaItemBo) ObjectUtils.deepCopy(agendaItem));
139 }
140
141
142 if (agendaItem.getRule().getActions().isEmpty()) {
143 ActionBo actionBo = new ActionBo();
144 actionBo.setTypeId("");
145 actionBo.setNamespace(agendaItem.getRule().getNamespace());
146 actionBo.setRuleId(agendaItem.getRule().getId());
147 actionBo.setSequenceNumber(1);
148 agendaEditor.setAgendaItemLineRuleAction(actionBo);
149 } else {
150 agendaEditor.setAgendaItemLineRuleAction(agendaItem.getRule().getActions().get(0));
151 }
152
153 agendaEditor.setCustomRuleActionAttributesMap(agendaEditor.getAgendaItemLineRuleAction().getAttributes());
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 protected static class AgendaItemChildAccessor {
185
186 private enum Child { WHEN_TRUE, WHEN_FALSE, ALWAYS };
187
188 private static final AgendaItemChildAccessor whenTrue = new AgendaItemChildAccessor(Child.WHEN_TRUE);
189 private static final AgendaItemChildAccessor whenFalse = new AgendaItemChildAccessor(Child.WHEN_FALSE);
190 private static final AgendaItemChildAccessor always = new AgendaItemChildAccessor(Child.ALWAYS);
191
192
193
194
195 private static final AgendaItemChildAccessor [] linkedNodes = { whenTrue, whenFalse, always };
196
197
198
199
200 private static final AgendaItemChildAccessor [] children = { whenTrue, whenFalse };
201
202 private final Child whichChild;
203
204 private AgendaItemChildAccessor(Child whichChild) {
205 if (whichChild == null) throw new IllegalArgumentException("whichChild must be non-null");
206 this.whichChild = whichChild;
207 }
208
209
210
211
212 public AgendaItemBo getChild(AgendaItemBo parent) {
213 switch (whichChild) {
214 case WHEN_TRUE: return parent.getWhenTrue();
215 case WHEN_FALSE: return parent.getWhenFalse();
216 case ALWAYS: return parent.getAlways();
217 default: throw new IllegalStateException();
218 }
219 }
220
221
222
223
224 public void setChild(AgendaItemBo parent, AgendaItemBo child) {
225 switch (whichChild) {
226 case WHEN_TRUE:
227 parent.setWhenTrue(child);
228 parent.setWhenTrueId(child == null ? null : child.getId());
229 break;
230 case WHEN_FALSE:
231 parent.setWhenFalse(child);
232 parent.setWhenFalseId(child == null ? null : child.getId());
233 break;
234 case ALWAYS:
235 parent.setAlways(child);
236 parent.setAlwaysId(child == null ? null : child.getId());
237 break;
238 default: throw new IllegalStateException();
239 }
240 }
241 }
242
243
244
245
246 private SequenceAccessorService getSequenceAccessorService() {
247 return KRADServiceLocator.getSequenceAccessorService();
248 }
249
250
251
252
253 private ContextBoService getContextBoService() {
254 return KrmsRepositoryServiceLocator.getContextBoService();
255 }
256
257 }