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.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.util.tree.Node;
21 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23 import org.kuali.rice.krad.service.SequenceAccessorService;
24 import org.kuali.rice.krad.uif.UifParameters;
25 import org.kuali.rice.krad.util.GlobalVariables;
26 import org.kuali.rice.krad.util.ObjectUtils;
27 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
28 import org.kuali.rice.krad.web.form.DocumentFormBase;
29 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
30 import org.kuali.rice.krad.web.form.UifFormBase;
31 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
32 import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
33 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
34 import org.kuali.rice.krms.api.repository.term.TermDefinition;
35 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
36 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
37 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
38 import org.kuali.rice.krms.impl.repository.ActionBo;
39 import org.kuali.rice.krms.api.repository.LogicalOperator;
40 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
41 import org.kuali.rice.krms.impl.repository.AgendaBo;
42 import org.kuali.rice.krms.impl.repository.AgendaItemBo;
43 import org.kuali.rice.krms.impl.repository.ContextBoService;
44 import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
45 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
46 import org.kuali.rice.krms.impl.repository.PropositionBo;
47 import org.kuali.rice.krms.impl.repository.RuleBo;
48 import org.kuali.rice.krms.impl.repository.RuleBoService;
49 import org.kuali.rice.krms.impl.repository.TermBo;
50 import org.kuali.rice.krms.impl.rule.AgendaEditorBusRule;
51 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
52 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
53 import org.springframework.stereotype.Controller;
54 import org.springframework.validation.BindingResult;
55 import org.springframework.web.bind.annotation.ModelAttribute;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RequestMethod;
58 import org.springframework.web.bind.annotation.RequestParam;
59 import org.springframework.web.bind.annotation.ResponseBody;
60 import org.springframework.web.servlet.ModelAndView;
61
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
64 import java.util.ArrayList;
65 import java.util.Collection;
66 import java.util.Collections;
67 import java.util.Enumeration;
68 import java.util.HashMap;
69 import java.util.List;
70 import java.util.Map;
71
72
73
74
75
76 @Controller
77 @RequestMapping(value = org.kuali.rice.krms.impl.util.KrmsImplConstants.WebPaths.AGENDA_EDITOR_PATH)
78 public class AgendaEditorController extends MaintenanceDocumentController {
79
80 private SequenceAccessorService sequenceAccessorService;
81
82
83
84
85
86
87
88 @Override
89 @RequestMapping(params = "methodToCall=route")
90 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
91 HttpServletRequest request, HttpServletResponse response) {
92
93 ModelAndView modelAndView;
94 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
95 AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
96 agendaEditor.setSelectedAgendaItemId("");
97 agendaEditor.setDisableButtons(true);
98 modelAndView = super.route(form, result, request, response);
99
100 return modelAndView;
101 }
102
103
104
105
106
107
108 @RequestMapping(params = "methodToCall=" + "refresh")
109 @Override
110 public ModelAndView refresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
111 HttpServletRequest request, HttpServletResponse response) throws Exception {
112 ModelAndView modelAndView = super.refresh(form, result, request, response);
113
114
115 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
116 AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
117 AgendaEditorBusRule rule = new AgendaEditorBusRule();
118 if (rule.validContext(agendaEditor) && rule.validAgendaName(agendaEditor)) {
119
120 if (!StringUtils.equals(agendaEditor.getOldContextId(), agendaEditor.getAgenda().getContextId())) {
121 agendaEditor.setOldContextId(agendaEditor.getAgenda().getContextId());
122
123 String namespace = "";
124 if (!StringUtils.isBlank(agendaEditor.getAgenda().getContextId())) {
125 namespace = getContextBoService().getContextByContextId(agendaEditor.getAgenda().getContextId()).getNamespace();
126 }
127
128 for (AgendaItemBo agendaItem : agendaEditor.getAgenda().getItems()) {
129 agendaItem.getRule().setNamespace(namespace);
130 for (ActionBo action : agendaItem.getRule().getActions()) {
131 action.setNamespace(namespace);
132 }
133 }
134 }
135 }
136 return modelAndView;
137 }
138
139
140 @Override
141 public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
142 HttpServletRequest request, HttpServletResponse response) throws Exception {
143
144
145 form.setPageId(null);
146 return super.maintenanceEdit(form,result,request,response);
147 }
148
149
150
151
152 @RequestMapping(params = "methodToCall=" + "goToAddRule")
153 public ModelAndView goToAddRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
154 HttpServletRequest request, HttpServletResponse response) throws Exception {
155 setAgendaItemLine(form, null);
156 AgendaEditor agendaEditor = getAgendaEditor(form);
157 agendaEditor.setAddRuleInProgress(true);
158 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-AddRule-Page");
159 return super.navigate(form, result, request, response);
160 }
161
162
163
164
165
166
167
168
169
170 private void setAgendaItemLine(UifFormBase form, AgendaItemBo agendaItem) {
171 AgendaEditor agendaEditor = getAgendaEditor(form);
172 if (agendaItem == null) {
173 RuleBo rule = new RuleBo();
174 rule.setId(getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_RULE_S", RuleBo.class)
175 .toString());
176 if (StringUtils.isBlank(agendaEditor.getAgenda().getContextId())) {
177 rule.setNamespace("");
178 } else {
179 rule.setNamespace(getContextBoService().getContextByContextId(agendaEditor.getAgenda().getContextId()).getNamespace());
180 }
181 agendaItem = new AgendaItemBo();
182 agendaItem.setRule(rule);
183 agendaEditor.setAgendaItemLine(agendaItem);
184 } else {
185
186 agendaEditor.setAgendaItemLine((AgendaItemBo) ObjectUtils.deepCopy(agendaItem));
187 }
188
189
190 if (agendaItem.getRule().getActions().isEmpty()) {
191 ActionBo actionBo = new ActionBo();
192 actionBo.setTypeId("");
193 actionBo.setNamespace(agendaItem.getRule().getNamespace());
194 actionBo.setRuleId(agendaItem.getRule().getId());
195 actionBo.setSequenceNumber(1);
196 agendaEditor.setAgendaItemLineRuleAction(actionBo);
197 } else {
198 agendaEditor.setAgendaItemLineRuleAction(agendaItem.getRule().getActions().get(0));
199 }
200
201 agendaEditor.setCustomRuleActionAttributesMap(agendaEditor.getAgendaItemLineRuleAction().getAttributes());
202 agendaEditor.setCustomRuleAttributesMap(agendaEditor.getAgendaItemLine().getRule().getAttributes());
203 }
204
205
206
207
208
209
210
211 private String getSelectedAgendaItemId(UifFormBase form) {
212 AgendaEditor agendaEditor = getAgendaEditor(form);
213 return agendaEditor.getSelectedAgendaItemId();
214 }
215
216
217
218
219
220
221
222 private void setCutAgendaItemId(UifFormBase form, String cutAgendaItemId) {
223 AgendaEditor agendaEditor = getAgendaEditor(form);
224 agendaEditor.setCutAgendaItemId(cutAgendaItemId);
225 }
226
227
228
229
230
231
232
233 private String getCutAgendaItemId(UifFormBase form) {
234 AgendaEditor agendaEditor = getAgendaEditor(form);
235 return agendaEditor.getCutAgendaItemId();
236 }
237
238
239
240
241 @RequestMapping(params = "methodToCall=" + "goToEditRule")
242 public ModelAndView goToEditRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
243 HttpServletRequest request, HttpServletResponse response) throws Exception {
244
245 AgendaEditor agendaEditor = getAgendaEditor(form);
246 agendaEditor.setAddRuleInProgress(false);
247
248 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
249 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
250 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
251
252 setAgendaItemLine(form, node);
253
254 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-EditRule-Page");
255 return super.navigate(form, result, request, response);
256 }
257
258
259
260
261 @RequestMapping(params = "methodToCall=" + "addRule")
262 public ModelAndView addRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
263 HttpServletRequest request, HttpServletResponse response) throws Exception {
264
265 AgendaEditor agendaEditor = getAgendaEditor(form);
266 AgendaBo agenda = agendaEditor.getAgenda();
267 AgendaItemBo newAgendaItem = agendaEditor.getAgendaItemLine();
268
269 if (!validateProposition(newAgendaItem.getRule().getProposition(), newAgendaItem.getRule().getNamespace())) {
270 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-AddRule-Page");
271
272 return super.navigate(form, result, request, response);
273 }
274
275 newAgendaItem.getRule().setAttributes(agendaEditor.getCustomRuleAttributesMap());
276 updateRuleAction(agendaEditor);
277
278 if (agenda.getItems() == null) {
279 agenda.setItems(new ArrayList<AgendaItemBo>());
280 }
281
282 AgendaEditorBusRule rule = new AgendaEditorBusRule();
283 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
284 MaintenanceDocument document = maintenanceForm.getDocument();
285 if (rule.processAgendaItemBusinessRules(document)) {
286 newAgendaItem.setId(getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_AGENDA_ITM_S", AgendaItemBo.class)
287 .toString());
288 newAgendaItem.setAgendaId(getCreateAgendaId(agenda));
289 if (agenda.getFirstItemId() == null) {
290 agenda.setFirstItemId(newAgendaItem.getId());
291 } else {
292
293 String selectedAgendaItemId = getSelectedAgendaItemId(form);
294 if (StringUtils.isBlank(selectedAgendaItemId)) {
295
296 AgendaItemBo node = getFirstAgendaItem(agenda);
297 while (node.getAlways() != null) {
298 node = node.getAlways();
299 }
300 node.setAlwaysId(newAgendaItem.getId());
301 node.setAlways(newAgendaItem);
302 } else {
303
304 AgendaItemBo firstItem = getFirstAgendaItem(agenda);
305 AgendaItemBo node = getAgendaItemById(firstItem, selectedAgendaItemId);
306 newAgendaItem.setAlwaysId(node.getAlwaysId());
307 newAgendaItem.setAlways(node.getAlways());
308 node.setAlwaysId(newAgendaItem.getId());
309 node.setAlways(newAgendaItem);
310 }
311 }
312
313 agenda.getItems().add(newAgendaItem);
314 agendaEditor.setAddRuleInProgress(false);
315 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-Agenda-Page");
316 } else {
317 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-AddRule-Page");
318 }
319 return super.navigate(form, result, request, response);
320 }
321
322
323
324
325
326
327
328
329
330 private boolean validateProposition(PropositionBo proposition, String namespace) {
331 boolean result = true;
332
333 if (proposition != null) {
334
335 if (StringUtils.isBlank(proposition.getDescription())) {
336 GlobalVariables.getMessageMap().putError(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
337 "error.rule.proposition.missingDescription");
338 result &= false;
339 }
340
341 if (StringUtils.isBlank(proposition.getCompoundOpCode())) {
342
343
344 result &= validateSimpleProposition(proposition, namespace);
345
346 } else {
347
348 List<PropositionBo> compoundComponents = proposition.getCompoundComponents();
349
350 if (!CollectionUtils.isEmpty(proposition.getParameters())) {
351 GlobalVariables.getMessageMap().putError(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
352 "error.rule.proposition.compound.invalidParameter", proposition.getDescription());
353 result &= false;
354 }
355
356
357 if (!CollectionUtils.isEmpty(compoundComponents)) for (PropositionBo childProp : compoundComponents) {
358 result &= validateProposition(childProp, namespace);
359 }
360 }
361 }
362
363 return result;
364 }
365
366
367
368
369
370
371
372
373 private boolean validateSimpleProposition(PropositionBo proposition, String namespace) {
374 boolean result = true;
375
376 String propConstant = null;
377 if (proposition.getParameters().get(1) != null) {
378 propConstant = proposition.getParameters().get(1).getValue();
379 }
380 String operator = null;
381 if (proposition.getParameters().get(2) != null) {
382 operator = proposition.getParameters().get(2).getValue();
383 }
384
385 String termId = null;
386 if (proposition.getParameters().get(0) != null) {
387 termId = proposition.getParameters().get(0).getValue();
388 }
389
390 if (StringUtils.isBlank(termId)) {
391 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
392 "error.rule.proposition.simple.blankField", proposition.getDescription(), "Term");
393 result &= false;
394 } else {
395 result = validateTerm(proposition, namespace);
396 }
397 if (StringUtils.isBlank(operator)) {
398 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
399 "error.rule.proposition.simple.blankField", proposition.getDescription(), "Operator");
400 result &= false;
401 }
402 if (StringUtils.isBlank(propConstant) && !operator.endsWith("null")) {
403 GlobalVariables.getMessageMap().putErrorForSectionId(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
404 "error.rule.proposition.simple.blankField", proposition.getDescription(), "Value");
405 result &= false;
406 } else if (operator.endsWith("null")) {
407 if (propConstant != null) {
408 proposition.getParameters().get(1).setValue(null);
409 }
410 } else if (!StringUtils.isBlank(termId)) {
411
412 String termType = lookupTermType(termId);
413 ComparisonOperatorService comparisonOperatorService = KrmsApiServiceLocator.getComparisonOperatorService();
414 if (comparisonOperatorService.canCoerce(termType, propConstant)) {
415 if (comparisonOperatorService.coerce(termType, propConstant) == null) {
416
417 GlobalVariables.getMessageMap().putError(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
418 "error.rule.proposition.simple.invalidValue", proposition.getDescription(), propConstant);
419 result &= false;
420 }
421 }
422 }
423
424 if (!CollectionUtils.isEmpty(proposition.getCompoundComponents())) {
425 GlobalVariables.getMessageMap().putError(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
426 "error.rule.proposition.simple.hasChildren", proposition.getDescription());
427 result &= false;
428 }
429 return result;
430 }
431
432
433
434
435
436
437
438
439 private boolean validateTerm(PropositionBo proposition, String namespace) {
440 boolean result = true;
441
442 String termId = proposition.getParameters().get(0).getValue();
443 if (termId.startsWith(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX)) {
444
445
446
447 if (StringUtils.isBlank(proposition.getNewTermDescription())) {
448 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
449 "error.rule.proposition.simple.emptyTermName", proposition.getDescription());
450 result &= false;
451 } else {
452
453 Map<String, String> criteria = new HashMap<String, String>();
454
455 criteria.put("description", proposition.getNewTermDescription());
456 criteria.put("specification.namespace", namespace);
457
458 Collection<TermBo> matchingTerms =
459 KRADServiceLocator.getBusinessObjectService().findMatching(TermBo.class, criteria);
460
461 if (!CollectionUtils.isEmpty(matchingTerms)) {
462
463 GlobalVariables.getMessageMap().putWarningWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
464 "warning.rule.proposition.simple.duplicateTermName", proposition.getDescription());
465 }
466 }
467
468 String termSpecificationId = termId.substring(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX.length());
469
470 TermResolverDefinition termResolverDefinition =
471 AgendaEditorMaintainable.getSimplestTermResolver(termSpecificationId, namespace);
472
473 if (termResolverDefinition == null) {
474 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
475 "error.rule.proposition.simple.invalidTerm", proposition.getDescription());
476 result &= false;
477 } else {
478 List<String> parameterNames = new ArrayList<String>(termResolverDefinition.getParameterNames());
479 Collections.sort(parameterNames);
480 for (String parameterName : parameterNames) {
481 if (!proposition.getTermParameters().containsKey(parameterName) ||
482 StringUtils.isBlank(proposition.getTermParameters().get(parameterName))) {
483 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
484 "error.rule.proposition.simple.missingTermParameter", proposition.getDescription());
485 result &= false;
486 break;
487 }
488 }
489 }
490
491 } else {
492
493 TermDefinition termDefinition = KrmsRepositoryServiceLocator.getTermBoService().getTerm(termId);
494 if (termDefinition == null) {
495 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
496 "error.rule.proposition.simple.invalidTerm", proposition.getDescription());
497 } else if (!namespace.equals(termDefinition.getSpecification().getNamespace())) {
498 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
499 "error.rule.proposition.simple.invalidTerm", proposition.getDescription());
500 }
501 }
502 return result;
503 }
504
505
506
507
508
509
510 private String lookupTermType(String key) {
511 TermSpecificationDefinition termSpec = null;
512 if (key.startsWith(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX)) {
513 String termSpecificationId = key.substring(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX.length());
514 termSpec = KrmsRepositoryServiceLocator.getTermBoService().getTermSpecificationById(termSpecificationId);
515 } else {
516 TermDefinition term = KrmsRepositoryServiceLocator.getTermBoService().getTerm(key);
517 if (term != null) {
518 termSpec = term.getSpecification();
519 }
520 }
521 if (termSpec != null) {
522 return termSpec.getType();
523 } else {
524 return null;
525 }
526 }
527
528
529
530
531
532 private String getCreateAgendaId(AgendaBo agenda) {
533 if (agenda.getId() == null) {
534 agenda.setId(getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_AGENDA_S", AgendaItemBo.class).toString());
535 }
536 return agenda.getId();
537 }
538
539 private void updateRuleAction(AgendaEditor agendaEditor) {
540 agendaEditor.getAgendaItemLine().getRule().setActions(new ArrayList<ActionBo>());
541 if (StringUtils.isNotBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) {
542 agendaEditor.getAgendaItemLineRuleAction().setAttributes(agendaEditor.getCustomRuleActionAttributesMap());
543 agendaEditor.getAgendaItemLine().getRule().getActions().add(agendaEditor.getAgendaItemLineRuleAction());
544 }
545 }
546
547
548
549
550
551
552
553 private Map<String, KrmsAttributeDefinition> buildAttributeDefinitionMap(String actionTypeId) {
554 KrmsAttributeDefinitionService attributeDefinitionService =
555 KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
556
557
558 Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>();
559
560 List<KrmsAttributeDefinition> attributeDefinitions =
561 attributeDefinitionService.findAttributeDefinitionsByType(actionTypeId);
562
563 for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
564 attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition);
565 }
566 return attributeDefinitionMap;
567 }
568
569
570
571
572 @RequestMapping(params = "methodToCall=" + "editRule")
573 public ModelAndView editRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
574 HttpServletRequest request, HttpServletResponse response) throws Exception {
575 AgendaEditor agendaEditor = getAgendaEditor(form);
576
577 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
578 AgendaItemBo node = getAgendaItemById(firstItem, getSelectedAgendaItemId(form));
579 AgendaItemBo agendaItemLine = agendaEditor.getAgendaItemLine();
580
581 if (!validateProposition(agendaItemLine.getRule().getProposition(), agendaItemLine.getRule().getNamespace())) {
582 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-EditRule-Page");
583
584 return super.navigate(form, result, request, response);
585 }
586
587 agendaItemLine.getRule().setAttributes(agendaEditor.getCustomRuleAttributesMap());
588 updateRuleAction(agendaEditor);
589
590 AgendaEditorBusRule rule = new AgendaEditorBusRule();
591 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
592 MaintenanceDocument document = maintenanceForm.getDocument();
593 if (rule.processAgendaItemBusinessRules(document)) {
594 node.setRule(agendaItemLine.getRule());
595 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-Agenda-Page");
596 } else {
597 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, "AgendaEditorView-EditRule-Page");
598 }
599 return super.navigate(form, result, request, response);
600 }
601
602
603
604
605
606
607 private AgendaItemInstanceChildAccessor getLastChildsAlwaysAccessor(AgendaItemInstanceChildAccessor instanceAccessor) {
608 AgendaItemBo next = instanceAccessor.getChild();
609 if (next == null) return instanceAccessor;
610 while (next.getAlways() != null) { next = next.getAlways(); };
611 return new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.always, next);
612 }
613
614
615
616
617
618
619 private AgendaItemInstanceChildAccessor getInstanceAccessorToChild(AgendaItemBo parent, String agendaItemId) {
620
621
622 for (AgendaItemChildAccessor levelOrderChildAccessor : AgendaItemChildAccessor.children) {
623
624 AgendaItemBo next = levelOrderChildAccessor.getChild(parent);
625
626
627 if (next != null && agendaItemId.equals(next.getId())) return new AgendaItemInstanceChildAccessor(levelOrderChildAccessor, parent);
628
629
630 while (next != null && next.getAlwaysId() != null) {
631 if (next.getAlwaysId().equals(agendaItemId)) return new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.always, next);
632
633 next = next.getAlways();
634 }
635 }
636
637 return null;
638 }
639
640 @RequestMapping(params = "methodToCall=" + "ajaxRefresh")
641 public ModelAndView ajaxRefresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
642 HttpServletRequest request, HttpServletResponse response)
643 throws Exception {
644
645 return getUIFModelAndView(form);
646 }
647
648 @RequestMapping(params = "methodToCall=" + "moveUp")
649 public ModelAndView moveUp(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
650 HttpServletRequest request, HttpServletResponse response)
651 throws Exception {
652 moveSelectedSubtreeUp(form);
653
654 return super.refresh(form, result, request, response);
655 }
656
657 @RequestMapping(params = "methodToCall=" + "ajaxMoveUp")
658 public ModelAndView ajaxMoveUp(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
659 HttpServletRequest request, HttpServletResponse response)
660 throws Exception {
661 moveSelectedSubtreeUp(form);
662
663
664 return getUIFModelAndView(form);
665 }
666
667
668
669
670
671
672
673 @RequestMapping(params = "methodToCall=" + "ajaxValidRuleName", method=RequestMethod.GET)
674 public @ResponseBody boolean ajaxValidRuleName(@RequestParam String name, @RequestParam String namespace) {
675 return (getRuleBoService().getRuleByNameAndNamespace(name, namespace) != null);
676 }
677
678
679
680
681
682
683 private void moveSelectedSubtreeUp(UifFormBase form) {
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704 AgendaEditor agendaEditor = getAgendaEditor(form);
705
706 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
707
708 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
709 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
710 AgendaItemBo parent = getParent(firstItem, selectedItemId);
711 AgendaItemBo parentsOlderCousin = (parent == null) ? null : getNextOldestOfSameGeneration(firstItem, parent);
712
713 StringBuilder ruleEditorMessage = new StringBuilder();
714 AgendaItemChildAccessor childAccessor = getOldestChildAccessor(node, parent);
715 if (childAccessor != null) {
716 if (childAccessor == AgendaItemChildAccessor.whenFalse) {
717
718 AgendaItemInstanceChildAccessor youngestWhenTrueSiblingInsertionPoint =
719 getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenTrue, parent));
720 youngestWhenTrueSiblingInsertionPoint.setChild(node);
721 AgendaItemChildAccessor.whenFalse.setChild(parent, node.getAlways());
722 AgendaItemChildAccessor.always.setChild(node, null);
723
724 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" up ");
725 ruleEditorMessage.append("to last position in When TRUE group of ").append(parent.getRule().getName());
726 } else if (parentsOlderCousin != null) {
727
728 AgendaItemInstanceChildAccessor youngestWhenFalseSiblingInsertionPoint =
729 getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenFalse, parentsOlderCousin));
730 youngestWhenFalseSiblingInsertionPoint.setChild(node);
731 AgendaItemChildAccessor.whenTrue.setChild(parent, node.getAlways());
732 AgendaItemChildAccessor.always.setChild(node, null);
733 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" up ");
734 ruleEditorMessage.append("to When FALSE group of ").append(parentsOlderCousin.getRule().getName());
735 }
736 } else if (!selectedItemId.equals(firstItem.getId())) {
737
738 AgendaItemBo bogusRootNode = null;
739 if (parent == null) {
740
741 bogusRootNode = new AgendaItemBo();
742 AgendaItemChildAccessor.whenTrue.setChild(bogusRootNode, firstItem);
743 parent = bogusRootNode;
744 }
745
746
747 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
748 AgendaItemBo olderSibling = accessorToSelectedNode.getInstance();
749 AgendaItemInstanceChildAccessor accessorToOlderSibling = getInstanceAccessorToChild(parent, olderSibling.getId());
750
751 accessorToOlderSibling.setChild(node);
752 accessorToSelectedNode.setChild(node.getAlways());
753 AgendaItemChildAccessor.always.setChild(node, olderSibling);
754
755 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" up ");
756
757 if (bogusRootNode != null) {
758
759 agendaEditor.getAgenda().setFirstItemId(bogusRootNode.getWhenTrueId());
760 ruleEditorMessage.append(" to ").append(getFirstAgendaItem(agendaEditor.getAgenda()).getRule().getName()).append(" When TRUE group");
761 } else {
762 ruleEditorMessage.append(" within its sibling group, above " + olderSibling.getRule().getName());
763 }
764 }
765 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
766 }
767
768 @RequestMapping(params = "methodToCall=" + "moveDown")
769 public ModelAndView moveDown(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
770 HttpServletRequest request, HttpServletResponse response)
771 throws Exception {
772 moveSelectedSubtreeDown(form);
773
774 return super.refresh(form, result, request, response);
775 }
776
777 @RequestMapping(params = "methodToCall=" + "ajaxMoveDown")
778 public ModelAndView ajaxMoveDown(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
779 HttpServletRequest request, HttpServletResponse response)
780 throws Exception {
781 moveSelectedSubtreeDown(form);
782
783
784 return getUIFModelAndView(form);
785 }
786
787
788
789
790
791
792 private void moveSelectedSubtreeDown(UifFormBase form) {
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813 AgendaEditor agendaEditor = getAgendaEditor(form);
814
815 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
816
817 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
818 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
819 AgendaItemBo parent = getParent(firstItem, selectedItemId);
820 AgendaItemBo parentsYoungerCousin = (parent == null) ? null : getNextYoungestOfSameGeneration(firstItem, parent);
821
822 StringBuilder ruleEditorMessage = new StringBuilder();
823 if (node.getAlways() == null && parent != null) {
824
825 if (parent.getWhenTrue() != null && isSiblings(parent.getWhenTrue(), node)) {
826
827 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
828 accessorToSelectedNode.setChild(null);
829
830 AgendaItemBo parentsFirstChild = parent.getWhenFalse();
831 AgendaItemChildAccessor.whenFalse.setChild(parent, node);
832 AgendaItemChildAccessor.always.setChild(node, parentsFirstChild);
833
834 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" down ");
835 ruleEditorMessage.append("to first child under When FALSE group of ").append(parent.getRule().getName());
836 } else if (parentsYoungerCousin != null) {
837
838 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
839 accessorToSelectedNode.setChild(null);
840
841 AgendaItemBo parentsYoungerCousinsFirstChild = parentsYoungerCousin.getWhenTrue();
842 AgendaItemChildAccessor.whenTrue.setChild(parentsYoungerCousin, node);
843 AgendaItemChildAccessor.always.setChild(node, parentsYoungerCousinsFirstChild);
844
845 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" down ");
846 ruleEditorMessage.append("to first child under When TRUE group of ").append(parentsYoungerCousin.getRule().getName());
847 }
848 } else if (node.getAlways() != null) {
849
850 AgendaItemBo bogusRootNode = null;
851 if (parent == null) {
852
853
854 bogusRootNode = new AgendaItemBo();
855 AgendaItemChildAccessor.whenFalse.setChild(bogusRootNode, firstItem);
856 parent = bogusRootNode;
857 }
858
859
860 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
861 AgendaItemBo youngerSibling = node.getAlways();
862 accessorToSelectedNode.setChild(youngerSibling);
863 AgendaItemChildAccessor.always.setChild(node, youngerSibling.getAlways());
864 AgendaItemChildAccessor.always.setChild(youngerSibling, node);
865
866 if (bogusRootNode != null) {
867
868 agendaEditor.getAgenda().setFirstItemId(bogusRootNode.getWhenFalseId());
869 }
870 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" down ");
871 ruleEditorMessage.append(" within its sibling group, below ").append(youngerSibling.getRule().getName());
872 }
873 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
874 }
875
876 @RequestMapping(params = "methodToCall=" + "moveLeft")
877 public ModelAndView moveLeft(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
878 HttpServletRequest request, HttpServletResponse response)
879 throws Exception {
880 moveSelectedSubtreeLeft(form);
881
882 return super.refresh(form, result, request, response);
883 }
884
885 @RequestMapping(params = "methodToCall=" + "ajaxMoveLeft")
886 public ModelAndView ajaxMoveLeft(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
887 HttpServletRequest request, HttpServletResponse response)
888 throws Exception {
889
890 moveSelectedSubtreeLeft(form);
891
892
893 return getUIFModelAndView(form);
894 }
895
896
897
898
899
900
901 private void moveSelectedSubtreeLeft(UifFormBase form) {
902
903
904
905
906
907 AgendaEditor agendaEditor = getAgendaEditor(form);
908
909 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
910
911 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
912 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
913 AgendaItemBo parent = getParent(firstItem, selectedItemId);
914
915 if (parent != null) {
916 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
917 accessorToSelectedNode.setChild(node.getAlways());
918 AgendaItemChildAccessor.always.setChild(node, parent.getAlways());
919 AgendaItemChildAccessor.always.setChild(parent, node);
920
921 StringBuilder ruleEditorMessage = new StringBuilder();
922 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" left to be a sibling of its parent ").append(parent.getRule().getName());
923 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
924 }
925 }
926
927
928 @RequestMapping(params = "methodToCall=" + "moveRight")
929 public ModelAndView moveRight(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
930 HttpServletRequest request, HttpServletResponse response)
931 throws Exception {
932
933 moveSelectedSubtreeRight(form);
934
935 return super.refresh(form, result, request, response);
936 }
937
938 @RequestMapping(params = "methodToCall=" + "ajaxMoveRight")
939 public ModelAndView ajaxMoveRight(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
940 HttpServletRequest request, HttpServletResponse response)
941 throws Exception {
942
943 moveSelectedSubtreeRight(form);
944
945
946 return getUIFModelAndView(form);
947 }
948
949
950
951
952
953
954 private void moveSelectedSubtreeRight(UifFormBase form) {
955
956
957
958
959
960
961
962 AgendaEditor agendaEditor = getAgendaEditor(form);
963
964 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
965
966 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
967 AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId);
968 AgendaItemBo parent = getParent(firstItem, selectedItemId);
969
970 AgendaItemBo bogusRootNode = null;
971 if (parent == null) {
972
973 bogusRootNode = new AgendaItemBo();
974 AgendaItemChildAccessor.whenFalse.setChild(bogusRootNode, firstItem);
975 parent = bogusRootNode;
976 }
977
978 AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId());
979 AgendaItemBo olderSibling = (accessorToSelectedNode.getInstance() == parent) ? null : accessorToSelectedNode.getInstance();
980
981 StringBuilder ruleEditorMessage = new StringBuilder();
982 if (olderSibling != null) {
983 accessorToSelectedNode.setChild(node.getAlways());
984 AgendaItemInstanceChildAccessor yougestWhenFalseSiblingInsertionPoint =
985 getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenFalse, olderSibling));
986 yougestWhenFalseSiblingInsertionPoint.setChild(node);
987 AgendaItemChildAccessor.always.setChild(node, null);
988
989 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" right to ");
990 ruleEditorMessage.append(olderSibling.getRule().getName()).append(" When FALSE group.");
991 } else if (node.getAlways() != null) {
992 accessorToSelectedNode.setChild(node.getAlways());
993 AgendaItemBo childsWhenTrue = node.getAlways().getWhenTrue();
994 AgendaItemChildAccessor.whenTrue.setChild(node.getAlways(), node);
995 AgendaItemChildAccessor.always.setChild(node, childsWhenTrue);
996
997 ruleEditorMessage.append("Moved ").append(node.getRule().getName()).append(" right to ");
998 if (childsWhenTrue != null) {
999 ruleEditorMessage.append(childsWhenTrue.getRule().getName()).append(" When TRUE group");
1000 }
1001 }
1002
1003 if (bogusRootNode != null) {
1004
1005 agendaEditor.getAgenda().setFirstItemId(bogusRootNode.getWhenFalseId());
1006 ruleEditorMessage.append(getFirstAgendaItem(agendaEditor.getAgenda()).getRule().getName()).append(" When TRUE group");
1007 }
1008 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018 private boolean isSiblings(AgendaItemBo cousin1, AgendaItemBo cousin2) {
1019 if (cousin1.equals(cousin2)) return true;
1020
1021
1022 AgendaItemBo candidate = cousin2;
1023 while (null != (candidate = candidate.getAlways())) {
1024 if (candidate.equals(cousin1)) return true;
1025 }
1026
1027 candidate = cousin1;
1028 while (null != (candidate = candidate.getAlways())) {
1029 if (candidate.equals(cousin2)) return true;
1030 }
1031 return false;
1032 }
1033
1034
1035
1036
1037
1038
1039
1040 private AgendaItemChildAccessor getOldestChildAccessor(
1041 AgendaItemBo child, AgendaItemBo parent) {
1042 AgendaItemChildAccessor levelOrderChildAccessor = null;
1043
1044 if (parent != null) {
1045 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.children) {
1046 if (child.equals(childAccessor.getChild(parent))) {
1047 levelOrderChildAccessor = childAccessor;
1048 break;
1049 }
1050 }
1051 }
1052 return levelOrderChildAccessor;
1053 }
1054
1055
1056
1057
1058
1059
1060
1061 private AgendaItemBo getFirstAgendaItem(AgendaBo agenda) {
1062 AgendaItemBo firstItem = null;
1063 if (agenda != null && agenda.getItems() != null) for (AgendaItemBo agendaItem : agenda.getItems()) {
1064 if (agenda.getFirstItemId().equals(agendaItem.getId())) {
1065 firstItem = agendaItem;
1066 break;
1067 }
1068 }
1069 return firstItem;
1070 }
1071
1072
1073
1074
1075
1076
1077 private AgendaItemBo getNextYoungestOfSameGeneration(AgendaItemBo root, AgendaItemBo agendaItem) {
1078
1079 int genNumber = getAgendaItemGenerationNumber(0, root, agendaItem.getId());
1080 List<AgendaItemBo> genList = new ArrayList<AgendaItemBo>();
1081 buildAgendaItemGenerationList(genList, root, 0, genNumber);
1082
1083 int itemIndex = genList.indexOf(agendaItem);
1084 if (genList.size() > itemIndex + 1) return genList.get(itemIndex + 1);
1085
1086 return null;
1087 }
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 private int getAgendaItemGenerationNumber(int currentLevel, AgendaItemBo node, String agendaItemId) {
1098 int result = -1;
1099 if (agendaItemId.equals(node.getId())) {
1100 result = currentLevel;
1101 } else {
1102 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1103 AgendaItemBo child = childAccessor.getChild(node);
1104 if (child != null) {
1105 int nextLevel = currentLevel;
1106
1107 if (childAccessor != AgendaItemChildAccessor.always) {
1108 nextLevel = currentLevel +1;
1109 }
1110 result = getAgendaItemGenerationNumber(nextLevel, child, agendaItemId);
1111 if (result != -1) break;
1112 }
1113 }
1114 }
1115 return result;
1116 }
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 private void buildAgendaItemGenerationList(List<AgendaItemBo> genList, AgendaItemBo node, int currentLevel, int generation) {
1127 if (currentLevel == generation) {
1128 genList.add(node);
1129 }
1130
1131 if (currentLevel > generation) return;
1132
1133 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1134 AgendaItemBo child = childAccessor.getChild(node);
1135 if (child != null) {
1136 int nextLevel = currentLevel;
1137
1138 if (childAccessor != AgendaItemChildAccessor.always) {
1139 nextLevel = currentLevel +1;
1140 }
1141 buildAgendaItemGenerationList(genList, child, nextLevel, generation);
1142 }
1143 }
1144 }
1145
1146
1147
1148
1149
1150
1151 private AgendaItemBo getNextOldestOfSameGeneration(AgendaItemBo root, AgendaItemBo agendaItem) {
1152
1153 int genNumber = getAgendaItemGenerationNumber(0, root, agendaItem.getId());
1154 List<AgendaItemBo> genList = new ArrayList<AgendaItemBo>();
1155 buildAgendaItemGenerationList(genList, root, 0, genNumber);
1156
1157 int itemIndex = genList.indexOf(agendaItem);
1158 if (itemIndex >= 1) return genList.get(itemIndex - 1);
1159
1160 return null;
1161 }
1162
1163
1164
1165
1166
1167
1168 private AgendaItemBo getParent(AgendaItemBo root, String agendaItemId) {
1169 return getParentHelper(root, null, agendaItemId);
1170 }
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180 private AgendaItemBo getParentHelper(AgendaItemBo node, AgendaItemBo levelOrderParent, String agendaItemId) {
1181 AgendaItemBo result = null;
1182 if (agendaItemId.equals(node.getId())) {
1183 result = levelOrderParent;
1184 } else {
1185 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1186 AgendaItemBo child = childAccessor.getChild(node);
1187 if (child != null) {
1188
1189 AgendaItemBo lop = (childAccessor == AgendaItemChildAccessor.always) ? levelOrderParent : node;
1190 result = getParentHelper(child, lop, agendaItemId);
1191 if (result != null) break;
1192 }
1193 }
1194 }
1195 return result;
1196 }
1197
1198
1199
1200
1201 private AgendaItemBo getAgendaItemById(AgendaItemBo node, String agendaItemId) {
1202 if (node == null) throw new IllegalArgumentException("node must be non-null");
1203
1204 AgendaItemBo result = null;
1205
1206 if (agendaItemId.equals(node.getId())) {
1207 result = node;
1208 } else {
1209 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1210 AgendaItemBo child = childAccessor.getChild(node);
1211 if (child != null) {
1212 result = getAgendaItemById(child, agendaItemId);
1213 if (result != null) break;
1214 }
1215 }
1216 }
1217 return result;
1218 }
1219
1220
1221
1222
1223
1224 private AgendaEditor getAgendaEditor(UifFormBase form) {
1225 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
1226 return ((AgendaEditor)maintenanceForm.getDocument().getDocumentDataObject());
1227 }
1228
1229 private void treeToInOrderList(AgendaItemBo agendaItem, List<AgendaItemBo> listToBuild) {
1230 listToBuild.add(agendaItem);
1231 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1232 AgendaItemBo child = childAccessor.getChild(agendaItem);
1233 if (child != null) treeToInOrderList(child, listToBuild);
1234 }
1235 }
1236
1237
1238 @RequestMapping(params = "methodToCall=" + "delete")
1239 public ModelAndView delete(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1240 HttpServletRequest request, HttpServletResponse response)
1241 throws Exception {
1242
1243 deleteSelectedSubtree(form);
1244
1245 return super.refresh(form, result, request, response);
1246 }
1247
1248 @RequestMapping(params = "methodToCall=" + "ajaxDelete")
1249 public ModelAndView ajaxDelete(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1250 HttpServletRequest request, HttpServletResponse response)
1251 throws Exception {
1252
1253 deleteSelectedSubtree(form);
1254
1255
1256 return getUIFModelAndView(form);
1257 }
1258
1259
1260 private void deleteSelectedSubtree(UifFormBase form) {
1261 AgendaEditor agendaEditor = getAgendaEditor(form);
1262 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
1263
1264 if (firstItem != null) {
1265 String agendaItemSelected = agendaEditor.getSelectedAgendaItemId();
1266 AgendaItemBo selectedItem = getAgendaItemById(firstItem, agendaItemSelected);
1267
1268
1269 if (agendaItemSelected.equals(firstItem.getId())) {
1270 agendaEditor.getAgenda().setFirstItemId(firstItem.getAlwaysId());
1271 } else {
1272 deleteAgendaItem(firstItem, agendaItemSelected);
1273 }
1274
1275 StringBuilder ruleEditorMessage = new StringBuilder();
1276 ruleEditorMessage.append("Deleted ").append(selectedItem.getRule().getName());
1277
1278 if (selectedItem.getWhenTrue() != null) {
1279 removeAgendaItem(agendaEditor.getAgenda().getItems(), selectedItem.getWhenTrue());
1280 ruleEditorMessage.append(" and its When TRUE ").append(selectedItem.getWhenTrue().getRule().getName());
1281 }
1282 if (selectedItem.getWhenFalse() != null) {
1283 removeAgendaItem(agendaEditor.getAgenda().getItems(), selectedItem.getWhenFalse());
1284 ruleEditorMessage.append(" and its When FALSE ").append(selectedItem.getWhenFalse().getRule().getName());
1285 }
1286 agendaEditor.getAgenda().getItems().remove(selectedItem);
1287 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
1288 }
1289 }
1290
1291 private void deleteAgendaItem(AgendaItemBo root, String agendaItemIdToDelete) {
1292 if (deleteAgendaItem(root, AgendaItemChildAccessor.whenTrue, agendaItemIdToDelete) ||
1293 deleteAgendaItem(root, AgendaItemChildAccessor.whenFalse, agendaItemIdToDelete) ||
1294 deleteAgendaItem(root, AgendaItemChildAccessor.always, agendaItemIdToDelete));
1295 }
1296
1297 private boolean deleteAgendaItem(AgendaItemBo agendaItem, AgendaItemChildAccessor childAccessor, String agendaItemIdToDelete) {
1298 if (agendaItem == null || childAccessor.getChild(agendaItem) == null) return false;
1299 if (agendaItemIdToDelete.equals(childAccessor.getChild(agendaItem).getId())) {
1300
1301 AgendaItemBo grandchildToKeep = childAccessor.getChild(agendaItem).getAlways();
1302 childAccessor.setChild(agendaItem, grandchildToKeep);
1303 return true;
1304 } else {
1305 AgendaItemBo child = childAccessor.getChild(agendaItem);
1306
1307 for (AgendaItemChildAccessor nextChildAccessor : AgendaItemChildAccessor.linkedNodes) {
1308 if (deleteAgendaItem(child, nextChildAccessor, agendaItemIdToDelete)) return true;
1309 }
1310 }
1311 return false;
1312 }
1313
1314
1315
1316
1317
1318
1319 private void removeAgendaItem(List<AgendaItemBo> items, AgendaItemBo removeAgendaItem) {
1320 if (removeAgendaItem.getWhenTrue() != null) {
1321 removeAgendaItem(items, removeAgendaItem.getWhenTrue());
1322 }
1323 if (removeAgendaItem.getWhenFalse() != null) {
1324 removeAgendaItem(items, removeAgendaItem.getWhenFalse());
1325 }
1326 if (removeAgendaItem.getAlways() != null) {
1327 removeAgendaItem(items, removeAgendaItem.getAlways());
1328 }
1329 items.remove(removeAgendaItem);
1330 }
1331
1332 @RequestMapping(params = "methodToCall=" + "ajaxCut")
1333 public ModelAndView ajaxCut(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1334 HttpServletRequest request, HttpServletResponse response) throws Exception {
1335
1336 AgendaEditor agendaEditor = getAgendaEditor(form);
1337
1338 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
1339 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
1340
1341 AgendaItemBo selectedAgendaItem = getAgendaItemById(firstItem, selectedItemId);
1342 setCutAgendaItemId(form, selectedItemId);
1343
1344 StringBuilder ruleEditorMessage = new StringBuilder();
1345 ruleEditorMessage.append("Marked ").append(selectedAgendaItem.getRule().getName()).append(" for cutting.");
1346 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
1347
1348 return getUIFModelAndView(form);
1349 }
1350
1351 @RequestMapping(params = "methodToCall=" + "ajaxPaste")
1352 public ModelAndView ajaxPaste(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1353 HttpServletRequest request, HttpServletResponse response) throws Exception {
1354
1355 AgendaEditor agendaEditor = getAgendaEditor(form);
1356
1357 AgendaItemBo firstItem = getFirstAgendaItem(agendaEditor.getAgenda());
1358 String selectedItemId = agendaEditor.getSelectedAgendaItemId();
1359
1360 String agendaItemId = getCutAgendaItemId(form);
1361 if (StringUtils.isNotBlank(selectedItemId) && StringUtils.isNotBlank(agendaItemId)) {
1362 StringBuilder ruleEditorMessage = new StringBuilder();
1363 AgendaItemBo node = getAgendaItemById(firstItem, agendaItemId);
1364 AgendaItemBo orgRefNode = getReferringNode(firstItem, agendaItemId);
1365 AgendaItemBo newRefNode = getAgendaItemById(firstItem, selectedItemId);
1366
1367 if (isSameOrChildNode(node, newRefNode)) {
1368
1369
1370 ruleEditorMessage.append("Cannot paste ").append(node.getRule().getName()).append(" to itself.");
1371 } else {
1372
1373 if (orgRefNode == null) {
1374 agendaEditor.getAgenda().setFirstItemId(node.getAlwaysId());
1375 } else {
1376
1377
1378 if (node.getId().equals(orgRefNode.getWhenTrueId())) {
1379 orgRefNode.setWhenTrueId(node.getAlwaysId());
1380 orgRefNode.setWhenTrue(node.getAlways());
1381 } else if(node.getId().equals(orgRefNode.getWhenFalseId())) {
1382 orgRefNode.setWhenFalseId(node.getAlwaysId());
1383 orgRefNode.setWhenFalse(node.getAlways());
1384 } else {
1385 orgRefNode.setAlwaysId(node.getAlwaysId());
1386 orgRefNode.setAlways(node.getAlways());
1387 }
1388 }
1389
1390
1391 node.setAlwaysId(newRefNode.getAlwaysId());
1392 node.setAlways(newRefNode.getAlways());
1393 newRefNode.setAlwaysId(node.getId());
1394 newRefNode.setAlways(node);
1395
1396 ruleEditorMessage.append(" Pasted ").append(node.getRule().getName());
1397 ruleEditorMessage.append(" to ").append(newRefNode.getRule().getName());
1398 agendaEditor.setRuleEditorMessage(ruleEditorMessage.toString());
1399
1400 }
1401 setCutAgendaItemId(form, null);
1402 }
1403
1404
1405
1406 return getUIFModelAndView(form);
1407 }
1408
1409
1410
1411
1412
1413 @RequestMapping(params = "methodToCall=" + "ajaxCategoryChangeRefresh")
1414 public ModelAndView ajaxCategoryChangeRefresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1415 HttpServletRequest request, HttpServletResponse response)
1416 throws Exception {
1417
1418 String categoryParamName = null;
1419 Enumeration paramNames = request.getParameterNames();
1420 while (paramNames.hasMoreElements()) {
1421 String paramName = paramNames.nextElement().toString();
1422 if (paramName.endsWith("categoryId")) {
1423 categoryParamName = paramName;
1424 break;
1425 }
1426 }
1427
1428 if (categoryParamName != null) {
1429 String categoryId = request.getParameter(categoryParamName);
1430
1431 if (StringUtils.isBlank(categoryId)) { categoryId = null; }
1432
1433 AgendaEditor agendaEditor = getAgendaEditor(form);
1434 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
1435 String selectedPropId = agendaEditor.getSelectedPropositionId();
1436
1437
1438 if (!StringUtils.isBlank(selectedPropId)) {
1439 Node<RuleTreeNode, String> selectedPropositionNode =
1440 findPropositionTreeNode(rule.getPropositionTree().getRootElement(), selectedPropId);
1441 selectedPropositionNode.getData().getProposition().setCategoryId(categoryId);
1442 }
1443 }
1444
1445 return ajaxRefresh(form, result, request, response);
1446 }
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457 private boolean isSameOrChildNode(AgendaItemBo node, AgendaItemBo newParent) {
1458 return isSameOrChildNodeHelper(node, newParent, AgendaItemChildAccessor.children);
1459 }
1460
1461 private boolean isSameOrChildNodeHelper(AgendaItemBo node, AgendaItemBo newParent, AgendaItemChildAccessor[] childAccessors) {
1462 boolean result = false;
1463 if (newParent == null || node == null) {
1464 return false;
1465 }
1466 if (StringUtils.equals(node.getId(), newParent.getId())) {
1467 result = true;
1468 } else {
1469 for (AgendaItemChildAccessor childAccessor : childAccessors) {
1470 AgendaItemBo child = childAccessor.getChild(node);
1471 if (child != null) {
1472 result = isSameOrChildNodeHelper(child, newParent, AgendaItemChildAccessor.linkedNodes);
1473 if (result == true) break;
1474 }
1475 }
1476 }
1477 return result;
1478 }
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489 private AgendaItemBo getReferringNode(AgendaItemBo root, String agendaItemId) {
1490 return getReferringNodeHelper(root, null, agendaItemId);
1491 }
1492
1493 private AgendaItemBo getReferringNodeHelper(AgendaItemBo node, AgendaItemBo referringNode, String agendaItemId) {
1494 AgendaItemBo result = null;
1495 if (agendaItemId.equals(node.getId())) {
1496 result = referringNode;
1497 } else {
1498 for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) {
1499 AgendaItemBo child = childAccessor.getChild(node);
1500 if (child != null) {
1501 result = getReferringNodeHelper(child, node, agendaItemId);
1502 if (result != null) break;
1503 }
1504 }
1505 }
1506 return result;
1507 }
1508
1509
1510
1511
1512 private SequenceAccessorService getSequenceAccessorService() {
1513 if ( sequenceAccessorService == null ) {
1514 sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
1515 }
1516 return sequenceAccessorService;
1517 }
1518
1519
1520
1521
1522 private ContextBoService getContextBoService() {
1523 return KrmsRepositoryServiceLocator.getContextBoService();
1524 }
1525
1526
1527
1528
1529 private RuleBoService getRuleBoService() {
1530 return KrmsRepositoryServiceLocator.getRuleBoService();
1531 }
1532
1533
1534
1535
1536
1537 private static class AgendaItemInstanceChildAccessor {
1538
1539 private final AgendaItemChildAccessor accessor;
1540 private final AgendaItemBo instance;
1541
1542 public AgendaItemInstanceChildAccessor(AgendaItemChildAccessor accessor, AgendaItemBo instance) {
1543 this.accessor = accessor;
1544 this.instance = instance;
1545 }
1546
1547 public void setChild(AgendaItemBo child) {
1548 accessor.setChild(instance, child);
1549 }
1550
1551 public AgendaItemBo getChild() {
1552 return accessor.getChild(instance);
1553 }
1554
1555 public AgendaItemBo getInstance() { return instance; }
1556 }
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586 protected static class AgendaItemChildAccessor {
1587
1588 private enum Child { WHEN_TRUE, WHEN_FALSE, ALWAYS };
1589
1590 private static final AgendaItemChildAccessor whenTrue = new AgendaItemChildAccessor(Child.WHEN_TRUE);
1591 private static final AgendaItemChildAccessor whenFalse = new AgendaItemChildAccessor(Child.WHEN_FALSE);
1592 private static final AgendaItemChildAccessor always = new AgendaItemChildAccessor(Child.ALWAYS);
1593
1594
1595
1596
1597 private static final AgendaItemChildAccessor [] linkedNodes = { whenTrue, whenFalse, always };
1598
1599
1600
1601
1602 private static final AgendaItemChildAccessor [] children = { whenTrue, whenFalse };
1603
1604 private final Child whichChild;
1605
1606 private AgendaItemChildAccessor(Child whichChild) {
1607 if (whichChild == null) throw new IllegalArgumentException("whichChild must be non-null");
1608 this.whichChild = whichChild;
1609 }
1610
1611
1612
1613
1614 public AgendaItemBo getChild(AgendaItemBo parent) {
1615 switch (whichChild) {
1616 case WHEN_TRUE: return parent.getWhenTrue();
1617 case WHEN_FALSE: return parent.getWhenFalse();
1618 case ALWAYS: return parent.getAlways();
1619 default: throw new IllegalStateException();
1620 }
1621 }
1622
1623
1624
1625
1626 public void setChild(AgendaItemBo parent, AgendaItemBo child) {
1627 switch (whichChild) {
1628 case WHEN_TRUE:
1629 parent.setWhenTrue(child);
1630 parent.setWhenTrueId(child == null ? null : child.getId());
1631 break;
1632 case WHEN_FALSE:
1633 parent.setWhenFalse(child);
1634 parent.setWhenFalseId(child == null ? null : child.getId());
1635 break;
1636 case ALWAYS:
1637 parent.setAlways(child);
1638 parent.setAlwaysId(child == null ? null : child.getId());
1639 break;
1640 default: throw new IllegalStateException();
1641 }
1642 }
1643 }
1644
1645
1646
1647 @RequestMapping(params = "methodToCall=" + "copyRule")
1648 public ModelAndView copyRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1649 HttpServletRequest request, HttpServletResponse response) throws Exception {
1650
1651 AgendaEditor agendaEditor = getAgendaEditor(form);
1652 String name = agendaEditor.getCopyRuleName();
1653 String namespace = agendaEditor.getNamespace();
1654
1655
1656 final String copyRuleNameErrorPropertyName = "AgendaEditorView-AddRule-Page";
1657 if (StringUtils.isBlank(name)) {
1658 GlobalVariables.getMessageMap().putError(copyRuleNameErrorPropertyName, "error.rule.missingCopyRuleName");
1659 return super.refresh(form, result, request, response);
1660 }
1661
1662 RuleDefinition oldRuleDefinition = getRuleBoService().getRuleByNameAndNamespace(name, namespace);
1663
1664 if (oldRuleDefinition == null) {
1665 GlobalVariables.getMessageMap().putError(copyRuleNameErrorPropertyName, "error.rule.invalidCopyRuleName", namespace + ":" + name);
1666 return super.refresh(form, result, request, response);
1667 }
1668
1669 RuleBo oldRule = RuleBo.from(oldRuleDefinition);
1670 RuleBo newRule = RuleBo.copyRule(oldRule);
1671 agendaEditor.getAgendaItemLine().setRule( newRule );
1672
1673 if (!newRule.getActions().isEmpty()) {
1674 agendaEditor.setAgendaItemLineRuleAction( newRule.getActions().get(0));
1675 }
1676 return super.refresh(form, result, request, response);
1677 }
1678
1679
1680
1681
1682
1683 @RequestMapping(params = "methodToCall=" + "goToEditProposition")
1684 public ModelAndView goToEditProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1685 HttpServletRequest request, HttpServletResponse response) throws Exception {
1686
1687
1688 AgendaEditor agendaEditor = getAgendaEditor(form);
1689 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
1690 String selectedPropId = agendaEditor.getSelectedPropositionId();
1691
1692 Node<RuleTreeNode,String> root = rule.getPropositionTree().getRootElement();
1693 PropositionBo propositionToToggleEdit = null;
1694 boolean newEditMode = true;
1695
1696
1697 Node<RuleTreeNode,String> parent = findParentPropositionNode( root, selectedPropId);
1698 if (parent != null){
1699 List<Node<RuleTreeNode,String>> children = parent.getChildren();
1700 for( int index=0; index< children.size(); index++){
1701 Node<RuleTreeNode,String> child = children.get(index);
1702 if (propIdMatches(child, selectedPropId)){
1703 PropositionBo prop = child.getData().getProposition();
1704 propositionToToggleEdit = prop;
1705 newEditMode = !prop.getEditMode();
1706 break;
1707 } else {
1708 child.getData().getProposition().setEditMode(false);
1709 }
1710 }
1711 }
1712
1713 resetEditModeOnPropositionTree(root);
1714 if (propositionToToggleEdit != null) {
1715 propositionToToggleEdit.setEditMode(newEditMode);
1716
1717 rule.refreshPropositionTree(null);
1718 }
1719
1720 return getUIFModelAndView(form);
1721 }
1722
1723 @RequestMapping(params = "methodToCall=" + "addProposition")
1724 public ModelAndView addProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1725 HttpServletRequest request, HttpServletResponse response) throws Exception {
1726
1727 AgendaEditor agendaEditor = getAgendaEditor(form);
1728 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
1729 String selectedPropId = agendaEditor.getSelectedPropositionId();
1730
1731
1732
1733 Node<RuleTreeNode,String> root = agendaEditor.getAgendaItemLine().getRule().getPropositionTree().getRootElement();
1734 Node<RuleTreeNode,String> parent = findParentPropositionNode( root, selectedPropId);
1735
1736 resetEditModeOnPropositionTree(root);
1737
1738
1739 if (parent != null){
1740 List<Node<RuleTreeNode,String>> children = parent.getChildren();
1741 for( int index=0; index< children.size(); index++){
1742 Node<RuleTreeNode,String> child = children.get(index);
1743
1744
1745 if (propIdMatches(child, selectedPropId)){
1746
1747
1748
1749
1750 if (parent == root &&
1751 (SimplePropositionNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()) ||
1752 SimplePropositionEditNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()))){
1753
1754
1755 PropositionBo compound = PropositionBo.createCompoundPropositionBoStub(child.getData().getProposition(), true);
1756 compound.setDescription("New Compound Proposition");
1757
1758 rule.setProposition(compound);
1759 rule.refreshPropositionTree(null);
1760 }
1761
1762 else if(SimplePropositionNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()) ||
1763 SimplePropositionEditNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType())){
1764
1765
1766 PropositionBo blank = PropositionBo.createSimplePropositionBoStub(child.getData().getProposition(),PropositionType.SIMPLE.getCode());
1767
1768 PropositionBo parentProp = parent.getData().getProposition();
1769 parentProp.getCompoundComponents().add(((index/2)+1), blank);
1770
1771 rule.refreshPropositionTree(true);
1772 }
1773
1774 break;
1775 }
1776 }
1777 } else {
1778
1779
1780 if (root.getChildren().isEmpty()){
1781 PropositionBo blank = PropositionBo.createSimplePropositionBoStub(null,PropositionType.SIMPLE.getCode());
1782 blank.setRuleId(rule.getId());
1783 rule.setPropId(blank.getId());
1784 rule.setProposition(blank);
1785 rule.refreshPropositionTree(true);
1786 }
1787 }
1788 return getUIFModelAndView(form);
1789 }
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799 private void addOpCodeNode(Node currentNode, PropositionBo prop, int index){
1800 String opCodeLabel = "";
1801
1802 if (LogicalOperator.AND.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){
1803 opCodeLabel = "AND";
1804 } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){
1805 opCodeLabel = "OR";
1806 }
1807 Node<RuleTreeNode, String> aNode = new Node<RuleTreeNode, String>();
1808 aNode.setNodeLabel("");
1809 aNode.setNodeType("ruleTreeNode compoundOpCodeNode");
1810 aNode.setData(new CompoundOpCodeNode(prop));
1811 currentNode.insertChildAt(index, aNode);
1812 }
1813
1814
1815 private boolean propIdMatches(Node<RuleTreeNode, String> node, String propId){
1816 if (propId!=null && node != null && node.getData() != null && propId.equalsIgnoreCase(node.getData().getProposition().getId())) {
1817 return true;
1818 }
1819 return false;
1820 }
1821
1822
1823
1824
1825
1826 private void resetEditModeOnPropositionTree(Node<RuleTreeNode, String> currentNode){
1827 if (currentNode.getData() != null){
1828 RuleTreeNode dataNode = currentNode.getData();
1829 dataNode.getProposition().setEditMode(false);
1830 }
1831 List<Node<RuleTreeNode,String>> children = currentNode.getChildren();
1832 for( Node<RuleTreeNode,String> child : children){
1833 resetEditModeOnPropositionTree(child);
1834 }
1835 }
1836
1837 private Node<RuleTreeNode, String> findPropositionTreeNode(Node<RuleTreeNode, String> currentNode, String selectedPropId){
1838 Node<RuleTreeNode,String> bingo = null;
1839 if (currentNode.getData() != null){
1840 RuleTreeNode dataNode = currentNode.getData();
1841 if (selectedPropId.equalsIgnoreCase(dataNode.getProposition().getId())){
1842 return currentNode;
1843 }
1844 }
1845 List<Node<RuleTreeNode,String>> children = currentNode.getChildren();
1846 for( Node<RuleTreeNode,String> child : children){
1847 bingo = findPropositionTreeNode(child, selectedPropId);
1848 if (bingo != null) break;
1849 }
1850 return bingo;
1851 }
1852
1853 private Node<RuleTreeNode, String> findParentPropositionNode(Node<RuleTreeNode, String> currentNode, String selectedPropId){
1854 Node<RuleTreeNode,String> bingo = null;
1855 if (selectedPropId != null) {
1856
1857 List<Node<RuleTreeNode,String>> children = currentNode.getChildren();
1858 for( Node<RuleTreeNode,String> child : children){
1859 RuleTreeNode dataNode = child.getData();
1860 if (selectedPropId.equalsIgnoreCase(dataNode.getProposition().getId()))
1861 return currentNode;
1862 }
1863
1864
1865 for( Node<RuleTreeNode,String> kid : children){
1866 bingo = findParentPropositionNode(kid, selectedPropId);
1867 if (bingo != null) break;
1868 }
1869 }
1870 return bingo;
1871 }
1872
1873
1874
1875
1876
1877
1878
1879 private int findChildIndex(Node<RuleTreeNode,String> parent, String propId){
1880 int index;
1881 List<Node<RuleTreeNode,String>> children = parent.getChildren();
1882 for(index=0; index< children.size(); index++){
1883 Node<RuleTreeNode,String> child = children.get(index);
1884
1885 if (propIdMatches(child, propId)){
1886 return index;
1887 }
1888 }
1889 return -1;
1890 }
1891
1892 @RequestMapping(params = "methodToCall=" + "movePropositionUp")
1893 public ModelAndView movePropositionUp(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1894 HttpServletRequest request, HttpServletResponse response)
1895 throws Exception {
1896 moveSelectedProposition(form, true);
1897
1898 return getUIFModelAndView(form);
1899 }
1900
1901 @RequestMapping(params = "methodToCall=" + "movePropositionDown")
1902 public ModelAndView movePropositionDown(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1903 HttpServletRequest request, HttpServletResponse response)
1904 throws Exception {
1905 moveSelectedProposition(form, false);
1906
1907 return getUIFModelAndView(form);
1908 }
1909
1910 private void moveSelectedProposition(UifFormBase form, boolean up) {
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920 AgendaEditor agendaEditor = getAgendaEditor(form);
1921 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
1922 String selectedPropId = agendaEditor.getSelectedPropositionId();
1923
1924
1925 Node<RuleTreeNode,String> parent = findParentPropositionNode(rule.getPropositionTree().getRootElement(), selectedPropId);
1926
1927
1928 if (parent != null){
1929 List<Node<RuleTreeNode,String>> children = parent.getChildren();
1930 for( int index=0; index< children.size(); index++){
1931 Node<RuleTreeNode,String> child = children.get(index);
1932
1933 if (propIdMatches(child, selectedPropId)){
1934 if(SimplePropositionNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()) ||
1935 SimplePropositionEditNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()) ||
1936 RuleTreeNode.COMPOUND_NODE_TYPE.equalsIgnoreCase(child.getNodeType()) ){
1937
1938 if (((index > 0) && up) || ((index <(children.size() - 1)&& !up))){
1939
1940 PropositionBo parentProp = parent.getData().getProposition();
1941 PropositionBo workingProp = parentProp.getCompoundComponents().remove(index/2);
1942 if (up){
1943 parentProp.getCompoundComponents().add((index/2)-1, workingProp);
1944 }else{
1945 parentProp.getCompoundComponents().add((index/2)+1, workingProp);
1946 }
1947
1948
1949
1950 boolean editMode = (SimplePropositionEditNode.NODE_TYPE.equalsIgnoreCase(child.getNodeType()));
1951 rule.refreshPropositionTree(editMode);
1952 }
1953 }
1954
1955 break;
1956 }
1957 }
1958 }
1959 }
1960 @RequestMapping(params = "methodToCall=" + "movePropositionLeft")
1961 public ModelAndView movePropositionLeft(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1962 HttpServletRequest request, HttpServletResponse response)
1963 throws Exception {
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973 AgendaEditor agendaEditor = getAgendaEditor(form);
1974 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
1975 String selectedPropId = agendaEditor.getSelectedPropositionId();
1976
1977
1978 Node<RuleTreeNode,String> root = rule.getPropositionTree().getRootElement();
1979 Node<RuleTreeNode,String> parent = findParentPropositionNode(root, selectedPropId);
1980 if ((parent != null) && (RuleTreeNode.COMPOUND_NODE_TYPE.equalsIgnoreCase(parent.getNodeType()))){
1981 Node<RuleTreeNode,String> granny = findParentPropositionNode(root,parent.getData().getProposition().getId());
1982 if (granny != root){
1983 int oldIndex = findChildIndex(parent, selectedPropId);
1984 int newIndex = findChildIndex(granny, parent.getData().getProposition().getId());
1985 if (oldIndex >= 0 && newIndex >= 0){
1986 PropositionBo prop = parent.getData().getProposition().getCompoundComponents().remove(oldIndex/2);
1987 granny.getData().getProposition().getCompoundComponents().add((newIndex/2)+1, prop);
1988 rule.refreshPropositionTree(false);
1989 }
1990 } else {
1991
1992
1993
1994 }
1995 }
1996 return getUIFModelAndView(form);
1997 }
1998
1999 @RequestMapping(params = "methodToCall=" + "movePropositionRight")
2000 public ModelAndView movePropositionRight(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2001 HttpServletRequest request, HttpServletResponse response)
2002 throws Exception {
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 AgendaEditor agendaEditor = getAgendaEditor(form);
2013 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
2014 String selectedPropId = agendaEditor.getSelectedPropositionId();
2015
2016
2017 Node<RuleTreeNode,String> parent = findParentPropositionNode(
2018 rule.getPropositionTree().getRootElement(), selectedPropId);
2019 if (parent != null){
2020 int index = findChildIndex(parent, selectedPropId);
2021
2022 if (index >= 0 && index+1 < parent.getChildren().size()){
2023 Node<RuleTreeNode,String> child = parent.getChildren().get(index);
2024 Node<RuleTreeNode,String> nextSibling = parent.getChildren().get(index+2);
2025
2026 if(RuleTreeNode.COMPOUND_NODE_TYPE.equalsIgnoreCase(nextSibling.getNodeType()) ){
2027
2028 PropositionBo prop = parent.getData().getProposition().getCompoundComponents().remove(index/2);
2029
2030 nextSibling.getData().getProposition().getCompoundComponents().add(0, prop);
2031 rule.refreshPropositionTree(false);
2032 }
2033 }
2034 }
2035 return getUIFModelAndView(form);
2036 }
2037
2038
2039
2040
2041
2042
2043 @RequestMapping(params = "methodToCall=" + "togglePropositionSimpleCompound")
2044 public ModelAndView togglePropositionSimpleCompound(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2045 HttpServletRequest request, HttpServletResponse response)
2046 throws Exception {
2047
2048 AgendaEditor agendaEditor = getAgendaEditor(form);
2049 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
2050 String selectedPropId = agendaEditor.getSelectedPropositionId();
2051
2052 resetEditModeOnPropositionTree(rule.getPropositionTree().getRootElement());
2053
2054 if (!StringUtils.isBlank(selectedPropId)) {
2055
2056 Node<RuleTreeNode,String> parent = findParentPropositionNode(
2057 rule.getPropositionTree().getRootElement(), selectedPropId);
2058 if (parent != null){
2059
2060 int index = findChildIndex(parent, selectedPropId);
2061
2062 PropositionBo propBo = parent.getChildren().get(index).getData().getProposition();
2063
2064
2065 PropositionBo compound = PropositionBo.createCompoundPropositionBoStub(propBo, true);
2066 compound.setDescription("New Compound Proposition");
2067 compound.setEditMode(false);
2068
2069 if (parent.getData() == null) {
2070 rule.setProposition(compound);
2071 } else {
2072 PropositionBo parentBo = parent.getData().getProposition();
2073 List<PropositionBo> siblings = parentBo.getCompoundComponents();
2074
2075 int propIndex = -1;
2076 for (int i=0; i<siblings.size(); i++) {
2077 if (propBo.getId().equals(siblings.get(i).getId())) {
2078 propIndex = i;
2079 break;
2080 }
2081 }
2082
2083 parentBo.getCompoundComponents().set(propIndex, compound);
2084 }
2085 }
2086 }
2087
2088 agendaEditor.getAgendaItemLine().getRule().refreshPropositionTree(true);
2089 return getUIFModelAndView(form);
2090 }
2091
2092
2093 @RequestMapping(params = "methodToCall=" + "cutProposition")
2094 public ModelAndView cutProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2095 HttpServletRequest request, HttpServletResponse response)
2096 throws Exception {
2097
2098 AgendaEditor agendaEditor = getAgendaEditor(form);
2099 String selectedPropId = agendaEditor.getSelectedPropositionId();
2100 agendaEditor.setCutPropositionId(selectedPropId);
2101
2102 return getUIFModelAndView(form);
2103 }
2104
2105 @RequestMapping(params = "methodToCall=" + "pasteProposition")
2106 public ModelAndView pasteProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2107 HttpServletRequest request, HttpServletResponse response)
2108 throws Exception {
2109
2110 AgendaEditor agendaEditor = getAgendaEditor(form);
2111 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
2112
2113
2114 String cutPropId = agendaEditor.getCutPropositionId();
2115 String selectedPropId = agendaEditor.getSelectedPropositionId();
2116
2117 if (StringUtils.isNotBlank(selectedPropId) && selectedPropId.equals(cutPropId)) {
2118
2119 } else {
2120
2121
2122 Node<RuleTreeNode, String> root = rule.getPropositionTree().getRootElement();
2123
2124 if (StringUtils.isNotBlank(selectedPropId) && StringUtils.isNotBlank(cutPropId)) {
2125 Node<RuleTreeNode,String> parentNode = findParentPropositionNode(root, selectedPropId);
2126 PropositionBo newParent;
2127 if (parentNode == root){
2128
2129
2130
2131
2132 newParent = PropositionBo.createCompoundPropositionBoStub2(
2133 root.getChildren().get(0).getData().getProposition());
2134 newParent.setEditMode(true);
2135 rule.setProposition(newParent);
2136 } else {
2137 newParent = parentNode.getData().getProposition();
2138 }
2139 PropositionBo oldParent = findParentPropositionNode(root, cutPropId).getData().getProposition();
2140
2141 PropositionBo workingProp = null;
2142
2143 if (oldParent != null){
2144 List <PropositionBo> children = oldParent.getCompoundComponents();
2145 for( int index=0; index< children.size(); index++){
2146 if (cutPropId.equalsIgnoreCase(children.get(index).getId())){
2147 workingProp = oldParent.getCompoundComponents().remove(index);
2148 break;
2149 }
2150 }
2151 }
2152
2153
2154 if (newParent != null && workingProp != null){
2155 List <PropositionBo> children = newParent.getCompoundComponents();
2156 for( int index=0; index< children.size(); index++){
2157 if (selectedPropId.equalsIgnoreCase(children.get(index).getId())){
2158 children.add(index+1, workingProp);
2159 break;
2160 }
2161 }
2162 }
2163
2164
2165 rule.refreshPropositionTree(false);
2166 }
2167 }
2168 agendaEditor.setCutPropositionId(null);
2169
2170 return getUIFModelAndView(form);
2171 }
2172
2173 @RequestMapping(params = "methodToCall=" + "deleteProposition")
2174 public ModelAndView deleteProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2175 HttpServletRequest request, HttpServletResponse response)
2176 throws Exception {
2177 AgendaEditor agendaEditor = getAgendaEditor(form);
2178 String selectedPropId = agendaEditor.getSelectedPropositionId();
2179 Node<RuleTreeNode, String> root = agendaEditor.getAgendaItemLine().getRule().getPropositionTree().getRootElement();
2180
2181 Node<RuleTreeNode, String> parentNode = findParentPropositionNode(root, selectedPropId);
2182
2183
2184 if (parentNode != null && parentNode.getData() != null) {
2185 PropositionBo parent = parentNode.getData().getProposition();
2186 if (parent != null){
2187 List <PropositionBo> children = parent.getCompoundComponents();
2188 for( int index=0; index< children.size(); index++){
2189 if (selectedPropId.equalsIgnoreCase(children.get(index).getId())){
2190 parent.getCompoundComponents().remove(index);
2191 break;
2192 }
2193 }
2194 }
2195 } else {
2196 if (ObjectUtils.isNotNull(parentNode)) {
2197 parentNode.getChildren().clear();
2198 agendaEditor.getAgendaItemLine().getRule().getPropositionTree().setRootElement(null);
2199 agendaEditor.getAgendaItemLine().getRule().setPropId(null);
2200 agendaEditor.getAgendaItemLine().getRule().setProposition(null);
2201 } else {
2202 GlobalVariables.getMessageMap().putError(KRMSPropertyConstants.Rule.PROPOSITION_TREE_GROUP_ID,
2203 "error.rule.proposition.noneHighlighted");
2204 }
2205 }
2206
2207 agendaEditor.getAgendaItemLine().getRule().refreshPropositionTree(false);
2208 return getUIFModelAndView(form);
2209 }
2210
2211 @RequestMapping(params = "methodToCall=" + "updateCompoundOperator")
2212 public ModelAndView updateCompoundOperator(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
2213 HttpServletRequest request, HttpServletResponse response)
2214 throws Exception {
2215
2216 AgendaEditor agendaEditor = getAgendaEditor(form);
2217 RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
2218 rule.refreshPropositionTree(false);
2219
2220 return getUIFModelAndView(form);
2221 }
2222
2223 }