1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.tree;
17
18 import org.apache.commons.lang.StringEscapeUtils;
19 import org.kuali.rice.core.api.util.tree.Node;
20 import org.kuali.rice.core.api.util.tree.Tree;
21 import org.kuali.rice.krad.util.ObjectUtils;
22 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
23 import org.kuali.rice.krms.dto.PropositionEditor;
24 import org.kuali.rice.krms.dto.RuleEditor;
25 import org.kuali.rice.krms.tree.node.CompoundOpCodeNode;
26 import org.kuali.rice.krms.tree.node.SimplePropositionEditNode;
27 import org.kuali.rice.krms.tree.node.SimplePropositionNode;
28 import org.kuali.rice.krms.tree.node.RuleEditorTreeNode;
29 import org.kuali.rice.krms.util.KRMSConstants;
30
31
32
33
34
35 public class RuleEditTreeBuilder extends AbstractTreeBuilder{
36
37 private static final long serialVersionUID = 1L;
38
39 public Tree buildTree(RuleEditor rule) {
40
41 Tree myTree = new Tree<RuleEditorTreeNode, String>();
42
43 Node<RuleEditorTreeNode, String> rootNode = new Node<RuleEditorTreeNode, String>();
44 rootNode.setNodeType(RuleEditorTreeNode.ROOT_TYPE);
45
46 myTree.setRootElement(rootNode);
47
48 if (rule.getPropositionEditor() != null){
49 Node firstNode = addChildNode(rule, rootNode, rule.getPropositionEditor());
50 firstNode.setNodeType(firstNode.getNodeType() + " " + RuleEditorTreeNode.ROOT_TYPE);
51 }
52
53 return myTree;
54 }
55
56
57
58
59
60
61
62 private Node addChildNode(RuleEditor rule, Node sprout, PropositionEditor prop) {
63
64
65
66
67 if (prop != null) {
68
69 Node<RuleEditorTreeNode, String> leaf = new Node<RuleEditorTreeNode, String>();
70 if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
71
72 if (null == prop.getKey()) {
73 prop.setKey((String) rule.getSimpleKeys().next());
74 }
75
76 if (prop.isEditMode()) {
77 leaf.setNodeType(SimplePropositionEditNode.NODE_TYPE);
78 PropositionEditor copy = (PropositionEditor) ObjectUtils.deepCopy(prop);
79 leaf.setData(new SimplePropositionEditNode(copy));
80 } else {
81 leaf.setNodeLabel(this.buildNodeLabel(rule, prop));
82 leaf.setNodeType(SimplePropositionNode.NODE_TYPE);
83 addNodeType(leaf, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
84 leaf.setData(new SimplePropositionNode(prop));
85 }
86
87 sprout.getChildren().add(leaf);
88 } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
89
90 if (null == prop.getKey()) {
91 prop.setKey((String) rule.getCompoundKeys().next());
92 }
93
94 leaf.setNodeLabel(this.getDescription(prop));
95 leaf.setNodeType(RuleEditorTreeNode.COMPOUND_NODE_TYPE);
96 leaf.setData(new RuleEditorTreeNode(prop));
97
98 sprout.getChildren().add(leaf);
99
100 int counter = 0;
101 for (PropositionEditor child : prop.getCompoundEditors()) {
102
103 if (counter > 0) {
104 addOpCodeNode(leaf, prop, counter);
105 }
106
107 Node childNode = addChildNode(rule, leaf, child);
108 if (counter==0){
109 addNodeType(childNode, RuleEditorTreeNode.FIRST_IN_GROUP);
110 }
111 if (counter==prop.getCompoundEditors().size()-1){
112 addNodeType(childNode, RuleEditorTreeNode.LAST_IN_GROUP);
113 }
114
115 if((leaf.getData().getProposition().getCompoundEditors().size() - 1) != counter) {
116 if(!leaf.getData().getProposition().getCompoundEditors().get(leaf.getData().getProposition().getCompoundEditors().indexOf(child) + 1).getPropositionTypeCode().equals("C")) {
117 addNodeType(childNode, RuleEditorTreeNode.DISABLE_MOVE_IN);
118 }
119 }
120 else {
121 addNodeType(childNode, RuleEditorTreeNode.DISABLE_MOVE_IN);
122 }
123 counter++;
124 }
125 }
126
127 if(sprout.getData() != null) {
128 if(((RuleEditorTreeNode) sprout.getData()).getProposition().equals(rule.getProposition())) {
129 addNodeType(leaf, RuleEditorTreeNode.DISABLE_MOVE_OUT);
130 }
131 }
132 return leaf;
133 }
134 return null;
135 }
136
137 private String buildNodeLabel(RuleEditor rule, PropositionEditor prop) {
138
139 String prefix = this.getPropositionPrefix(prop);
140 return prefix + this.getDescription(prop);
141 }
142
143
144
145
146
147
148
149
150 private void addOpCodeNode(Node currentNode, PropositionEditor prop, int counter) {
151
152 Node<CompoundOpCodeNode, String> aNode = new Node<CompoundOpCodeNode, String>();
153 aNode.setNodeType(RuleEditorTreeNode.COMPOUND_OP_NODE_TYPE);
154
155
156 PropositionEditor editor = new PropositionEditor();
157 editor.setKey(prop.getKey() + counter);
158 editor.setCompoundOpCode(prop.getCompoundOpCode());
159
160 aNode.setData(new CompoundOpCodeNode(editor));
161 currentNode.getChildren().add(aNode);
162 }
163
164 public String getNaturalLanguageUsageKey(){
165 return null;
166 }
167
168 }