1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.controller;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.util.tree.Node;
20 import org.kuali.rice.krad.uif.UifParameters;
21 import org.kuali.rice.krad.util.GlobalVariables;
22 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
23 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
24 import org.kuali.rice.krad.web.form.UifFormBase;
25 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
26 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
27 import org.kuali.rice.krms.dto.AgendaEditor;
28 import org.kuali.rice.krms.dto.PropositionEditor;
29 import org.kuali.rice.krms.dto.RuleEditor;
30 import org.kuali.rice.krms.dto.RuleManagementWrapper;
31 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
32 import org.kuali.rice.krms.service.RuleViewHelperService;
33 import org.kuali.rice.krms.util.AgendaUtilities;
34 import org.kuali.rice.krms.tree.node.SimplePropositionEditNode;
35 import org.kuali.rice.krms.tree.node.SimplePropositionNode;
36 import org.kuali.rice.krms.tree.node.RuleEditorTreeNode;
37 import org.kuali.rice.krms.util.KRMSConstants;
38 import org.kuali.rice.krms.util.PropositionTreeUtil;
39 import org.kuali.rice.krms.util.RuleLogicExpressionParser;
40 import org.kuali.student.common.uif.util.KSControllerHelper;
41 import org.springframework.validation.BindingResult;
42 import org.springframework.web.bind.annotation.ModelAttribute;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Map;
51
52
53
54
55
56
57 public class RuleEditorController extends MaintenanceDocumentController {
58
59
60
61
62
63
64
65
66
67
68
69 @RequestMapping(params = "methodToCall=goToRuleView")
70 public ModelAndView goToRuleView(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
71 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
72
73
74 form.getClientStateForSyncing().clear();
75
76 RuleEditor ruleEditor = AgendaUtilities.retrieveSelectedRuleEditor((MaintenanceDocumentForm) form);
77 this.getViewHelper(form).refreshInitTrees(ruleEditor);
78
79 if (!form.getActionParameters().containsKey(UifParameters.NAVIGATE_TO_PAGE_ID)) {
80 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, KRMSConstants.KRMS_RULE_MAINTENANCE_PAGE_ID);
81 }
82
83
84 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
85
86 return super.navigate(form, result, request, response);
87 }
88
89
90
91
92
93
94
95
96
97
98 @RequestMapping(params = "methodToCall=deleteRule")
99 public ModelAndView deleteRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
100 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
101
102 MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
103 RuleManagementWrapper ruleWrapper = AgendaUtilities.getRuleWrapper(document);
104 String ruleKey = AgendaUtilities.getRuleKey(document);
105
106 AgendaEditor agenda = AgendaUtilities.getSelectedAgendaEditor(ruleWrapper, ruleKey);
107 if (agenda != null) {
108 RuleEditor ruleEditor = agenda.getRuleEditors().get(ruleKey);
109
110
111 if (ruleEditor.getId() != null) {
112 agenda.getDeletedRules().add(ruleEditor);
113 }
114
115 RuleEditor dummyRule = new RuleEditor(ruleEditor.getKey(), true, ruleEditor.getRuleTypeInfo());
116 dummyRule.setParent(ruleEditor.getParent());
117 agenda.getRuleEditors().put(ruleEditor.getKey(), dummyRule);
118 }
119
120 return getUIFModelAndView(document);
121 }
122
123
124
125
126
127
128
129
130
131
132 @RequestMapping(params = "methodToCall=addRule")
133 public ModelAndView addRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
134 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
135
136
137 form.getClientStateForSyncing().clear();
138
139 RuleEditor ruleEditor = AgendaUtilities.retrieveSelectedRuleEditor((MaintenanceDocumentForm) form);
140
141 this.getViewHelper(form).refreshInitTrees(ruleEditor);
142
143 if (!form.getActionParameters().containsKey(UifParameters.NAVIGATE_TO_PAGE_ID)) {
144 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, KRMSConstants.KRMS_RULE_MAINTENANCE_PAGE_ID);
145 }
146 return super.navigate(form, result, request, response);
147 }
148
149
150
151
152
153
154
155
156
157
158
159 @RequestMapping(params = "methodToCall=ajaxRefresh")
160 public ModelAndView ajaxRefresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
161 HttpServletRequest request, HttpServletResponse response) {
162 return getUIFModelAndView(form);
163 }
164
165
166
167
168
169
170
171 protected RuleEditor getRuleEditor(UifFormBase form) {
172 if (form instanceof MaintenanceDocumentForm) {
173 MaintenanceDocumentForm maintenanceDocumentForm = (MaintenanceDocumentForm) form;
174 Object dataObject = maintenanceDocumentForm.getDocument().getNewMaintainableObject().getDataObject();
175
176 if (dataObject instanceof RuleEditor) {
177 return (RuleEditor) dataObject;
178 } else if (dataObject instanceof RuleManagementWrapper) {
179 RuleManagementWrapper wrapper = (RuleManagementWrapper) dataObject;
180 return wrapper.getRuleEditor();
181 }
182 }
183
184 return null;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 @RequestMapping(params = "methodToCall=copyRule")
202 public ModelAndView copyRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
203 HttpServletRequest request, HttpServletResponse response) throws Exception {
204 return super.refresh(form, result, request, response);
205 }
206
207
208
209
210
211
212
213
214
215
216 @RequestMapping(params = "methodToCall=goToEditProposition")
217 public ModelAndView goToEditProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
218 HttpServletRequest request, HttpServletResponse response) {
219
220 RuleViewHelperService viewHelper = this.getViewHelper(form);
221 RuleEditor ruleEditor = getRuleEditor(form);
222
223 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
224 PropositionEditor proposition = PropositionTreeUtil.getProposition(ruleEditor);
225 proposition.setEditMode(true);
226
227 if (!PropositionType.COMPOUND.getCode().equalsIgnoreCase(proposition.getPropositionTypeCode())) {
228
229 String propositionTypeId = proposition.getTypeId();
230 if (propositionTypeId == null) {
231 proposition.setType(null);
232 } else {
233
234 KrmsTypeDefinition type = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(propositionTypeId);
235 if (type != null) {
236 proposition.setType(type.getName());
237 }
238 }
239
240 }
241
242
243 viewHelper.refreshInitTrees(ruleEditor);
244
245 return getUIFModelAndView(form);
246 }
247
248
249
250
251
252
253
254
255
256
257 @RequestMapping(params = "methodToCall=addProposition")
258 public ModelAndView addProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
259 HttpServletRequest request, HttpServletResponse response) {
260
261 RuleEditor ruleEditor = getRuleEditor(form);
262 String selectedPropKey = ruleEditor.getSelectedKey();
263
264
265 Node<RuleEditorTreeNode, String> root = ruleEditor.getEditTree().getRootElement();
266 Node<RuleEditorTreeNode, String> parent = PropositionTreeUtil.findParentPropositionNode(root, selectedPropKey);
267
268
269 RuleViewHelperService viewHelper = this.getViewHelper(form);
270
271
272 if (selectedPropKey.isEmpty() && parent == null && root.getChildren().size() > 0) {
273
274 if (root.getChildren().get(root.getChildren().size() - 1).getNodeType().contains("compoundNode")) {
275 parent = root.getChildren().get(root.getChildren().size() - 1);
276 selectedPropKey = parent.getChildren().get(parent.getChildren().size() - 1).getData().getProposition().getKey();
277 }
278 else {
279 parent = root;
280 selectedPropKey = root.getChildren().get(root.getChildren().size() - 1).getData().getProposition().getKey();
281 }
282 }
283 else if (parent != null) {
284 if (parent.getNodeType().equals("treeRoot") && !parent.getChildren().get(parent.getChildren().size() - 1).getNodeType().contains("simple")) {
285 parent = root.getChildren().get(root.getChildren().size() - 1);
286 selectedPropKey = parent.getChildren().get(parent.getChildren().size() - 1).getData().getProposition().getKey();
287 }
288 }
289
290
291 if (parent != null) {
292 List<Node<RuleEditorTreeNode, String>> children = parent.getChildren();
293 for (int index = 0; index < children.size(); index++) {
294 Node<RuleEditorTreeNode, String> child = children.get(index);
295
296
297 if (propKeyMatches(child, selectedPropKey)) {
298
299
300
301
302 PropositionEditor blank = null;
303 if (parent.equals(root) && (isSimpleNode(child.getNodeType()))) {
304
305
306 blank = viewHelper.createCompoundPropositionBoStub(child.getData().getProposition(), true);
307
308 ruleEditor.setProposition(blank);
309 }
310
311 else if (!parent.equals(root)) {
312
313
314 blank = viewHelper.createSimplePropositionBoStub(child.getData().getProposition());
315
316 PropositionEditor parentProp = parent.getData().getProposition();
317 parentProp.getCompoundEditors().add(((index / 2) + 1), blank);
318 } else {
319 return getUIFModelAndView(form);
320 }
321 this.getViewHelper(form).refreshInitTrees(ruleEditor);
322 if (blank != null) {
323 ruleEditor.setSelectedKey(blank.getKey());
324 } else {
325 ruleEditor.setSelectedKey(null);
326 }
327 break;
328 }
329 }
330 } else {
331
332 if (root.getChildren().isEmpty()) {
333 PropositionEditor blank = viewHelper.createSimplePropositionBoStub(null);
334 blank.setRuleId(ruleEditor.getId());
335 ruleEditor.setPropId(blank.getId());
336 ruleEditor.setProposition(blank);
337 }
338 this.getViewHelper(form).refreshInitTrees(ruleEditor);
339 }
340 return getUIFModelAndView(form);
341 }
342
343
344
345
346
347
348
349
350 private boolean propKeyMatches(Node<RuleEditorTreeNode, String> node, String propKey) {
351 if (propKey != null && node != null && node.getData() != null && propKey.equalsIgnoreCase(node.getData().getProposition().getKey())) {
352 return true;
353 }
354 return false;
355 }
356
357
358
359
360
361
362
363
364 private int findChildIndex(Node<RuleEditorTreeNode, String> parent, String propKey) {
365 int index;
366 List<Node<RuleEditorTreeNode, String>> children = parent.getChildren();
367 for (index = 0; index < children.size(); index++) {
368 Node<RuleEditorTreeNode, String> child = children.get(index);
369
370 if (propKeyMatches(child, propKey)) {
371 return index;
372 }
373 }
374 return -1;
375 }
376
377
378
379
380
381
382
383
384
385
386 @RequestMapping(params = "methodToCall=movePropositionUp")
387 public ModelAndView movePropositionUp(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
388 HttpServletRequest request, HttpServletResponse response) {
389 moveSelectedProposition(form, true);
390
391 return getUIFModelAndView(form);
392 }
393
394
395
396
397
398
399
400
401
402
403 @RequestMapping(params = "methodToCall=movePropositionDown")
404 public ModelAndView movePropositionDown(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
405 HttpServletRequest request, HttpServletResponse response) {
406 moveSelectedProposition(form, false);
407
408 return getUIFModelAndView(form);
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 private void moveSelectedProposition(UifFormBase form, boolean up) {
426
427 RuleEditor ruleEditor = getRuleEditor(form);
428 String selectedPropKey = ruleEditor.getSelectedKey();
429
430
431 Node<RuleEditorTreeNode, String> parent = PropositionTreeUtil.findParentPropositionNode(ruleEditor.getEditTree().getRootElement(), selectedPropKey);
432
433
434 if (parent != null) {
435 List<Node<RuleEditorTreeNode, String>> children = parent.getChildren();
436 for (int index = 0; index < children.size(); index++) {
437 Node<RuleEditorTreeNode, String> child = children.get(index);
438
439 if (propKeyMatches(child, selectedPropKey) &&
440 (isSimpleNode(child.getNodeType()) ||
441 (RuleEditorTreeNode.COMPOUND_NODE_TYPE.equalsIgnoreCase(child.getNodeType())) ||
442 (child.getNodeType().contains(RuleEditorTreeNode.FIRST_IN_GROUP)) ||
443 (child.getNodeType().contains(RuleEditorTreeNode.LAST_IN_GROUP)))) {
444
445
446 PropositionEditor parentProp = parent.getData().getProposition();
447 PropositionEditor workingProp = null;
448 if (index != 0 && up) {
449 workingProp = parentProp.getCompoundEditors().remove(index / 2);
450 } else if (!up && index != (children.size() - 1)) {
451 workingProp = parentProp.getCompoundEditors().remove(index / 2);
452 }
453 if ((index > 0) && up) {
454 parentProp.getCompoundEditors().add((index / 2) - 1, workingProp);
455 } else if ((index < (children.size() - 1) && !up)) {
456 parentProp.getCompoundEditors().add((index / 2) + 1, workingProp);
457 }
458
459 this.getViewHelper(form).refreshInitTrees(ruleEditor);
460 break;
461 }
462 }
463 }
464
465 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
466
467 }
468
469
470
471
472
473
474
475 public boolean isSimpleNode(String nodeType) {
476 if (nodeType.contains(SimplePropositionNode.NODE_TYPE) ||
477 SimplePropositionEditNode.NODE_TYPE.equalsIgnoreCase(nodeType)) {
478 return true;
479 }
480 return false;
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499 @RequestMapping(params = "methodToCall=movePropositionLeft")
500 public ModelAndView movePropositionLeft(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
501 HttpServletRequest request, HttpServletResponse response) {
502
503 RuleEditor ruleEditor = getRuleEditor(form);
504 String selectedpropKey = ruleEditor.getSelectedKey();
505
506
507 Node<RuleEditorTreeNode, String> root = ruleEditor.getEditTree().getRootElement();
508 Node<RuleEditorTreeNode, String> parent = PropositionTreeUtil.findParentPropositionNode(root, selectedpropKey);
509 if ((parent != null) && (!parent.getNodeType().contains(RuleEditorTreeNode.ROOT_TYPE))) {
510 Node<RuleEditorTreeNode, String> granny = PropositionTreeUtil.findParentPropositionNode(root, parent.getData().getProposition().getKey());
511 if (!granny.equals(root)) {
512 int oldIndex = findChildIndex(parent, selectedpropKey);
513 int newIndex = findChildIndex(granny, parent.getData().getProposition().getKey());
514 if (oldIndex >= 0 && newIndex >= 0) {
515 PropositionEditor prop = parent.getData().getProposition().getCompoundEditors().remove(oldIndex / 2);
516 if ((parent.getChildren().size() == 1) || (parent.getChildren().size() == 3)) {
517 PropositionTreeUtil.removeCompoundProp(ruleEditor.getPropositionEditor());
518 }
519 if (granny.getData().getProposition().getCompoundEditors().isEmpty()) {
520 granny.getData().getProposition().getCompoundEditors().add(newIndex, prop);
521 } else {
522 granny.getData().getProposition().getCompoundEditors().add((newIndex / 2) + 1, prop);
523 }
524 this.getViewHelper(form).refreshInitTrees(ruleEditor);
525 }
526 }
527
528 }
529
530 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
531 return getUIFModelAndView(form);
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551 @RequestMapping(params = "methodToCall=movePropositionRight")
552 public ModelAndView movePropositionRight(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
553 HttpServletRequest request, HttpServletResponse response) {
554
555 RuleEditor ruleEditor = getRuleEditor(form);
556 String selectedpropKey = ruleEditor.getSelectedKey();
557
558
559 Node<RuleEditorTreeNode, String> parent = PropositionTreeUtil.findParentPropositionNode(
560 ruleEditor.getEditTree().getRootElement(), selectedpropKey);
561 if (parent != null) {
562 int index = findChildIndex(parent, selectedpropKey);
563
564 if (index >= 0 && index + 1 < parent.getChildren().size()) {
565 Node<RuleEditorTreeNode, String> nextSibling = parent.getChildren().get(index + 2);
566
567 if (nextSibling.getNodeType().contains(RuleEditorTreeNode.COMPOUND_NODE_TYPE)) {
568
569 PropositionEditor prop = parent.getData().getProposition().getCompoundEditors().remove(index / 2);
570
571 nextSibling.getData().getProposition().getCompoundEditors().add(0, prop);
572 }
573
574 PropositionTreeUtil.removeCompoundProp(parent.getData().getProposition());
575 this.getViewHelper(form).refreshInitTrees(ruleEditor);
576 }
577 }
578
579 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
580 return getUIFModelAndView(form);
581 }
582
583
584
585
586
587
588
589
590
591
592
593
594
595 @RequestMapping(params = "methodToCall=togglePropositionSimpleCompound")
596 public ModelAndView togglePropositionSimpleCompound(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
597 HttpServletRequest request, HttpServletResponse response) {
598
599 RuleEditor ruleEditor = getRuleEditor(form);
600 String selectedPropKey = ruleEditor.getSelectedKey();
601
602 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
603 RuleViewHelperService viewHelper = this.getViewHelper(form);
604
605 if (!StringUtils.isBlank(selectedPropKey)) {
606
607 Node<RuleEditorTreeNode, String> parent = PropositionTreeUtil.findParentPropositionNode(
608 ruleEditor.getEditTree().getRootElement(), selectedPropKey);
609 if (parent != null) {
610
611 int index = findChildIndex(parent, selectedPropKey);
612 PropositionEditor propBo = parent.getChildren().get(index).getData().getProposition();
613
614
615 PropositionEditor compound = viewHelper.createCompoundPropositionBoStub(propBo, true);
616
617 if (parent.getData() == null) {
618 ruleEditor.setProposition(compound);
619 } else {
620 PropositionEditor parentBo = parent.getData().getProposition();
621 List<PropositionEditor> siblings = parentBo.getCompoundEditors();
622
623 int propIndex = -1;
624 for (int i = 0; i < siblings.size(); i++) {
625 if (propBo.getKey().equals(siblings.get(i).getKey())) {
626 propIndex = i;
627 break;
628 }
629 }
630
631 parentBo.getCompoundEditors().set(propIndex, compound);
632 compound.getCompoundEditors().get(1).setEditMode(true);
633 }
634
635 viewHelper.refreshInitTrees(ruleEditor);
636 ruleEditor.setSelectedKey(compound.getCompoundEditors().get(1).getKey());
637
638 }
639 }
640
641 return getUIFModelAndView(form);
642 }
643
644
645
646
647
648
649
650
651
652
653
654 @RequestMapping(params = "methodToCall=pasteProposition")
655 public ModelAndView pasteProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
656 HttpServletRequest request, HttpServletResponse response) {
657
658 boolean cutAction = true;
659 RuleEditor ruleEditor = getRuleEditor(form);
660 RuleViewHelperService viewHelper = this.getViewHelper(form);
661
662
663 String selectedPropKey = ruleEditor.getSelectedKey();
664 if (StringUtils.isBlank(selectedPropKey)) {
665 return getUIFModelAndView(form);
666 }
667
668
669 String movePropKey = ruleEditor.getCutKey();
670 if (StringUtils.isBlank(movePropKey)) {
671 movePropKey = ruleEditor.getCopyKey();
672 cutAction = false;
673 }
674
675
676 if (StringUtils.isNotBlank(movePropKey)) {
677
678 PropositionEditor newParent = null;
679 PropositionEditor workingProp = null;
680 PropositionEditor root = ruleEditor.getPropositionEditor();
681
682
683 if (movePropKey.equals(root.getKey())) {
684 newParent = viewHelper.createCompoundPropositionBoStub(root, false);
685 workingProp = viewHelper.copyProposition(root);
686 } else {
687 Node<RuleEditorTreeNode, String> rootNode = ruleEditor.getEditTree().getRootElement();
688 if (selectedPropKey.equals(root.getKey())) {
689 newParent = root;
690 } else {
691 newParent = PropositionTreeUtil.findParentPropositionNode(rootNode, selectedPropKey).getData().getProposition();
692 }
693 PropositionEditor oldParent = PropositionTreeUtil.findParentPropositionNode(rootNode, movePropKey).getData().getProposition();
694
695
696 if (oldParent != null) {
697 List<PropositionEditor> children = oldParent.getCompoundEditors();
698 for (int index = 0; index < children.size(); index++) {
699 if (movePropKey.equalsIgnoreCase(children.get(index).getKey())) {
700 if (cutAction) {
701 workingProp = oldParent.getCompoundEditors().remove(index);
702 } else {
703 workingProp = viewHelper.copyProposition(oldParent.getCompoundEditors().get(index));
704 }
705 break;
706 }
707 }
708 }
709 }
710
711
712 addProposition(selectedPropKey, newParent, workingProp);
713 if (movePropKey.equals(root.getKey())) {
714 ruleEditor.setProposition(newParent);
715 }
716
717
718 PropositionTreeUtil.removeCompoundProp(ruleEditor);
719 ruleEditor.setSelectedKey(StringUtils.EMPTY);
720 viewHelper.refreshInitTrees(ruleEditor);
721 }
722
723
724 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
725
726
727 return getUIFModelAndView(form);
728 }
729
730
731
732
733
734
735
736
737 private void addProposition(String selectedpropKey, PropositionEditor newParent, PropositionEditor workingProp) {
738
739 if (newParent != null && workingProp != null) {
740
741 if(selectedpropKey.equalsIgnoreCase(newParent.getKey())){
742 newParent.getCompoundEditors().add(workingProp);
743 return;
744 }
745
746
747 List<PropositionEditor> children = newParent.getCompoundEditors();
748 for (int index = 0; index < children.size(); index++) {
749 if (selectedpropKey.equalsIgnoreCase(children.get(index).getKey())) {
750 children.add(index + 1, workingProp);
751 return;
752 }
753 }
754 }
755 }
756
757
758
759
760
761
762
763
764
765
766 @RequestMapping(params = "methodToCall=deleteProposition")
767 public ModelAndView deleteProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
768 HttpServletRequest request, HttpServletResponse response) {
769 RuleEditor ruleEditor = getRuleEditor(form);
770 String selectedpropKey = ruleEditor.getSelectedKey();
771 Node<RuleEditorTreeNode, String> root = ruleEditor.getEditTree().getRootElement();
772
773 Node<RuleEditorTreeNode, String> parentNode = PropositionTreeUtil.findParentPropositionNode(root, selectedpropKey);
774
775
776 if (parentNode != null && parentNode.getData() != null) {
777 PropositionEditor parent = parentNode.getData().getProposition();
778 if (parent != null) {
779 List<PropositionEditor> children = parent.getCompoundEditors();
780 for (int index = 0; index < children.size(); index++) {
781 if (selectedpropKey.equalsIgnoreCase(children.get(index).getKey())) {
782 parent.getCompoundComponents().remove(index);
783 break;
784 }
785 }
786 }
787 PropositionTreeUtil.removeCompoundProp(ruleEditor);
788 ruleEditor.setSelectedKey(StringUtils.EMPTY);
789 } else {
790 ruleEditor.reset();
791 }
792
793
794 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
795
796 this.getViewHelper(form).refreshInitTrees(ruleEditor);
797 return getUIFModelAndView(form);
798 }
799
800
801
802
803
804
805
806
807
808
809 @RequestMapping(params = "methodToCall=updateCompoundOperator")
810 public ModelAndView updateCompoundOperator(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
811 HttpServletRequest request, HttpServletResponse response) {
812
813 RuleEditor ruleEditor = getRuleEditor(form);
814 String selectedpropKey = ruleEditor.getSelectedKey();
815 Node<RuleEditorTreeNode, String> parentNode = PropositionTreeUtil.findParentPropositionNode(ruleEditor.getEditTree().getRootElement(), selectedpropKey);
816 PropositionEditor parent = parentNode.getData().getProposition();
817
818 PropositionEditor proposition = PropositionTreeUtil.findProposition(parentNode, selectedpropKey);
819
820 RuleViewHelperService viewHelper = this.getViewHelper(form);
821 viewHelper.setTypeForCompoundOpCode(parent, proposition.getCompoundOpCode());
822 viewHelper.resetDescription(parent);
823 viewHelper.refreshInitTrees(ruleEditor);
824
825
826 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
827
828 return getUIFModelAndView(form);
829 }
830
831
832
833
834
835
836
837
838
839
840
841 @RequestMapping(params = "methodToCall=updateProposition")
842 public ModelAndView updateProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
843 HttpServletRequest request, HttpServletResponse response) {
844
845
846 RuleEditor ruleEditor = getRuleEditor(form);
847 PropositionEditor proposition = PropositionTreeUtil.getProposition(ruleEditor);
848 if (proposition != null) {
849
850
851 this.getViewHelper(form).validateProposition(proposition);
852 if (!GlobalVariables.getMessageMap().getErrorMessages().isEmpty()) {
853 return getUIFModelAndView(form);
854 }
855
856 if (!GlobalVariables.getMessageMap().getWarningMessages().isEmpty()) {
857 if (!hasDialogBeenAnswered(KRMSConstants.KSKRMS_DIALOG_YESNO_WARNING, form)) {
858 return showDialog(KRMSConstants.KSKRMS_DIALOG_YESNO_WARNING, form, request, response);
859 }
860
861 String dialogResponse = getStringDialogResponse(KRMSConstants.KSKRMS_DIALOG_YESNO_WARNING, form, request, response);
862 if ("N".equals(dialogResponse)) {
863 form.getDialogManager().resetDialogStatus(KRMSConstants.KSKRMS_DIALOG_YESNO_WARNING);
864 return getUIFModelAndView(form);
865 }
866 }
867
868
869 this.getViewHelper(form).resetDescription(proposition);
870 if (!GlobalVariables.getMessageMap().getErrorMessages().isEmpty()) {
871 return getUIFModelAndView(form);
872 }
873
874
875 if (ruleEditor.getPropositionEditor().getKey().equals(ruleEditor.getSelectedKey())) {
876 ruleEditor.setProposition(proposition);
877 } else {
878
879 this.setUpdatedProposition(ruleEditor.getPropositionEditor(), proposition);
880 }
881
882 }
883
884 if (ruleEditor.getProposition() != null) {
885 PropositionTreeUtil.resetNewProp(ruleEditor.getPropositionEditor());
886 }
887
888
889 form.getDialogManager().resetDialogStatus(KRMSConstants.KSKRMS_DIALOG_YESNO_WARNING);
890
891
892 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
893
894
895 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
896 this.getViewHelper(form).refreshInitTrees(ruleEditor);
897
898 return getUIFModelAndView(form);
899 }
900
901
902
903
904
905
906
907
908
909
910
911 private void setUpdatedProposition(PropositionEditor proposition, PropositionEditor updatedProposition) {
912
913 if (proposition.getCompoundEditors() != null) {
914 for (int i = 0; i < proposition.getCompoundEditors().size(); i++) {
915 PropositionEditor childProp = proposition.getCompoundEditors().get(i);
916 if (childProp.getKey().equals(updatedProposition.getKey())) {
917 proposition.getCompoundEditors().set(i, updatedProposition);
918 } else {
919 setUpdatedProposition(childProp, updatedProposition);
920 }
921 }
922 }
923 }
924
925 protected void compareRulePropositions(MaintenanceDocumentForm form, RuleEditor ruleEditor) {
926
927 RuleManagementWrapper ruleWrapper = (RuleManagementWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
928
929
930 if (ruleEditor.getProposition() != null) {
931 if (!this.getViewHelper(form).compareRules(ruleWrapper.getRuleEditor())) {
932 GlobalVariables.getMessageMap().putInfoForSectionId(KRMSConstants.KRMS_RULE_TREE_GROUP_ID, "info.krms.tree.rule.changed");
933 } else if (GlobalVariables.getMessageMap().containsMessageKey(KRMSConstants.KRMS_RULE_TREE_GROUP_ID)) {
934 GlobalVariables.getMessageMap().removeAllInfoMessagesForProperty(KRMSConstants.KRMS_RULE_TREE_GROUP_ID);
935 }
936 }
937 }
938
939
940
941
942
943
944
945
946
947
948
949 @RequestMapping(params = "methodToCall=updateRule")
950 public ModelAndView updateRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
951 HttpServletRequest request, HttpServletResponse response) {
952
953 RuleEditor ruleEditor = getRuleEditor(form);
954
955
956 PropositionEditor proposition = PropositionTreeUtil.getProposition(ruleEditor);
957 if ((proposition!=null) && (proposition.isEditMode())) {
958 GlobalVariables.getMessageMap().putErrorForSectionId(KRMSConstants.KRMS_PROPOSITION_DETAILSECTION_ID, KRMSConstants.KRMS_MSG_ERROR_RULE_PREVIEW);
959 return getUIFModelAndView(form);
960 }
961
962 if (!(ruleEditor.getProposition() == null && ruleEditor.getPropId() == null)) {
963 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
964 ruleEditor.setDummy(false);
965 PropositionTreeUtil.resetNewProp(ruleEditor.getPropositionEditor());
966 }
967
968 if(ruleEditor.getPropositionEditor()!=null){
969
970 this.getViewHelper(form).finPropositionEditor(ruleEditor.getPropositionEditor());
971 }
972
973 this.getViewHelper(form).refreshViewTree(ruleEditor);
974
975
976 RuleManagementWrapper ruleWrapper = AgendaUtilities.getRuleWrapper((MaintenanceDocumentForm) form);
977 AgendaEditor agendaEditor = AgendaUtilities.getSelectedAgendaEditor(ruleWrapper, ruleEditor.getKey());
978 agendaEditor.getRuleEditors().put(ruleEditor.getKey(), ruleEditor);
979
980 if (!form.getActionParameters().containsKey(UifParameters.NAVIGATE_TO_PAGE_ID)) {
981 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, KRMSConstants.KRMS_AGENDA_MAINTENANCE_PAGE_ID);
982 }
983 return super.navigate(form, result, request, response);
984 }
985
986
987
988
989
990
991
992
993
994
995
996
997 @RequestMapping(params = "methodToCall=updatePreview")
998 public ModelAndView updatePreview(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
999 HttpServletRequest request, HttpServletResponse response) {
1000 RuleEditor ruleEditor = getRuleEditor(form);
1001 parseRuleExpression(ruleEditor, this.getViewHelper(form));
1002
1003
1004 if (form.getNewCollectionLines().size() != 1) {
1005 List<String> keys = new ArrayList<String>(form.getNewCollectionLines().keySet());
1006 for (String key : keys) {
1007 if (key.contains(PropositionTreeUtil.EDIT_TREE_NEW_COLLECTION_LINE)) {
1008 form.getNewCollectionLines().remove(key);
1009 }
1010 }
1011 }
1012
1013 this.getViewHelper(form).refreshInitTrees(ruleEditor);
1014 return getUIFModelAndView(form);
1015 }
1016
1017
1018
1019
1020
1021
1022 private void parseRuleExpression(RuleEditor ruleEditor, RuleViewHelperService viewHelper) {
1023 RuleLogicExpressionParser ruleLogicExpressionParser = new RuleLogicExpressionParser();
1024 ruleLogicExpressionParser.setExpression(ruleEditor.getLogicArea());
1025
1026 if(ruleEditor.getPropositionEditor()==null){
1027 GlobalVariables.getMessageMap().putInfo("document.newMaintainableObject.dataObject.logicArea", KRMSConstants.KSKRMS_MSG_INFO_LOGIC_NO_STATEMENTS);
1028 return;
1029 }
1030
1031
1032 List<String> errorMessages = new ArrayList<String>();
1033 List<String> keyList = getPropositionKeys(new ArrayList<String>(), ruleEditor.getPropositionEditor());
1034 boolean validExpression = ruleLogicExpressionParser.validateExpression(errorMessages, keyList);
1035
1036
1037 if (!validExpression) {
1038 for (int i = 0; i < errorMessages.size(); i++) {
1039 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.dataObject.logicArea", errorMessages.get(i));
1040 }
1041
1042 return;
1043 }
1044
1045 ruleEditor.setProposition(ruleLogicExpressionParser.parseExpressionIntoRule(ruleEditor, viewHelper));
1046 }
1047
1048
1049
1050
1051
1052
1053
1054
1055 private List<String> getPropositionKeys(List<String> propositionKeys, PropositionEditor propositionEditor) {
1056 propositionKeys.add(propositionEditor.getKey());
1057 if (propositionEditor.getCompoundComponents() != null) {
1058 for (PropositionEditor child : propositionEditor.getCompoundEditors()) {
1059 this.getPropositionKeys(propositionKeys, child);
1060 }
1061 }
1062 return propositionKeys;
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074 @RequestMapping(params = "methodToCall=cancelEditProposition")
1075 public ModelAndView cancelEditProposition(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1076 HttpServletRequest request, HttpServletResponse response) {
1077
1078 RuleEditor ruleEditor = getRuleEditor(form);
1079 PropositionEditor root = ruleEditor.getPropositionEditor();
1080
1081
1082 if (root.isNewProp() && root.isEditMode()) {
1083 ruleEditor.reset();
1084 } else {
1085 PropositionTreeUtil.cancelNewProp(root);
1086 PropositionTreeUtil.removeCompoundProp(ruleEditor);
1087
1088 ruleEditor.setSelectedKey(StringUtils.EMPTY);
1089 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
1090 }
1091
1092
1093 if (form.getNewCollectionLines().size() != 1) {
1094 List<String> keys = new ArrayList<String>(form.getNewCollectionLines().keySet());
1095 for (String key : keys) {
1096 if (key.contains(PropositionTreeUtil.EDIT_TREE_NEW_COLLECTION_LINE)) {
1097 form.getNewCollectionLines().remove(key);
1098 }
1099 }
1100 }
1101
1102 this.getViewHelper(form).refreshInitTrees(ruleEditor);
1103
1104
1105 compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
1106
1107 return getUIFModelAndView(form);
1108 }
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 @RequestMapping(params = "methodToCall=cancelEditRule")
1120 public ModelAndView cancelEditRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1121 HttpServletRequest request, HttpServletResponse response) {
1122
1123 RuleEditor ruleEditor = getRuleEditor(form);
1124 PropositionEditor proposition = ruleEditor.getPropositionEditor();
1125
1126
1127 if (proposition != null) {
1128 PropositionTreeUtil.cancelNewProp(proposition);
1129 }
1130 PropositionTreeUtil.resetEditModeOnPropositionTree(ruleEditor.getPropositionEditor());
1131
1132 if (!form.getActionParameters().containsKey(UifParameters.NAVIGATE_TO_PAGE_ID)) {
1133 form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, KRMSConstants.KRMS_AGENDA_MAINTENANCE_PAGE_ID);
1134 }
1135 return super.navigate(form, result, request, response);
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 @RequestMapping(params = "methodToCall=updatePropositionType")
1148 public ModelAndView updatePropositionType(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1149 HttpServletRequest request, HttpServletResponse response) {
1150
1151 PropositionEditor proposition = PropositionTreeUtil.getProposition(this.getRuleEditor(form));
1152 proposition.clear();
1153 this.getViewHelper(form).configurePropositionForType(proposition);
1154
1155 return getUIFModelAndView(form);
1156 }
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 @RequestMapping(params = "methodToCall=compareRules")
1168 public ModelAndView compareRules(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1169 HttpServletRequest request, HttpServletResponse response) {
1170
1171 doCompareRules(form);
1172
1173
1174 return showDialog(KRMSConstants.KSKRMS_DIALOG_COMPARE, form, request, response);
1175 }
1176
1177 protected void doCompareRules(UifFormBase form) {
1178 MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
1179 Object dataObject = document.getDocument().getNewMaintainableObject().getDataObject();
1180 if (dataObject instanceof RuleManagementWrapper) {
1181 RuleManagementWrapper ruleWrapper = (RuleManagementWrapper) dataObject;
1182 String ruleId = document.getActionParamaterValue(KRMSConstants.KRMS_PARM_RULE_KEY);
1183 RuleEditor ruleEditor = null;
1184 if ((ruleId != null) && (StringUtils.isNotBlank(ruleId))) {
1185
1186 ruleEditor = AgendaUtilities.getSelectedRuleEditor(ruleWrapper, ruleId);
1187 } else {
1188
1189 ruleEditor = ruleWrapper.getRuleEditor();
1190 }
1191
1192
1193 ruleWrapper.setCompareTree(this.getViewHelper(form).buildCompareTree(ruleEditor.getParent(), ruleEditor));
1194 ruleWrapper.setCompareLightBoxHeader(ruleEditor.getRuleTypeInfo().getDescription());
1195 }
1196 }
1197
1198
1199
1200
1201
1202
1203
1204 protected RuleViewHelperService getViewHelper(UifFormBase form) {
1205 return (RuleViewHelperService) KSControllerHelper.getViewHelperService(form);
1206 }
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 @RequestMapping(params = "methodToCall=getSelectedKey")
1218 public ModelAndView getSelectedKey(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1219 HttpServletRequest request, HttpServletResponse response) {
1220
1221
1222 Map<String, String> states = (Map<String, String>) form.getClientStateForSyncing().get(KRMSConstants.KRMS_RULE_TABS_ID);
1223 states.put(KRMSConstants.KRMS_PARM_ACTIVE_TAB, KRMSConstants.KRMS_RULE_EDITWITHOBJECT_ID);
1224
1225
1226 String selectedKey = request.getParameter(KRMSConstants.KRMS_PARM_SELECTED_KEY);
1227 getRuleEditor(form).setSelectedKey(selectedKey);
1228
1229 return this.goToEditProposition(form, result, request, response);
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241 @RequestMapping(params = "methodToCall=refreshLogicArea")
1242 public ModelAndView refreshLogicArea(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1243 HttpServletRequest request, HttpServletResponse response) {
1244
1245 RuleEditor rule = this.getRuleEditor(form);
1246
1247
1248 if (rule.getProposition() != null) {
1249 rule.setLogicArea(PropositionTreeUtil.configureLogicExpression(rule.getPropositionEditor()));
1250 } else {
1251 rule.setLogicArea(StringUtils.EMPTY);
1252 }
1253
1254 return super.getUIFModelAndView(form);
1255 }
1256
1257 }