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.krms.api.repository.LogicalOperator;
22 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
23 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
24 import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract;
25 import org.kuali.rice.krms.dto.PropositionEditor;
26 import org.kuali.rice.krms.dto.RuleEditor;
27 import org.kuali.rice.krms.tree.node.TreeNode;
28 import org.kuali.rice.krms.util.KRMSConstants;
29 import org.kuali.rice.krms.util.PropositionTreeUtil;
30
31 import java.util.List;
32
33
34
35
36
37
38
39 public class RulePreviewTreeBuilder extends AbstractTreeBuilder{
40
41 public Tree<TreeNode, String> buildTree(RuleEditor rule) {
42
43 Tree myTree = new Tree<TreeNode, String>();
44
45 Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
46 rootNode.setNodeLabel("root");
47 rootNode.setNodeType("rootNode");
48 rootNode.setData(new TreeNode("Rule:"));
49 myTree.setRootElement(rootNode);
50
51 if (rule == null) {
52 return myTree;
53 }
54
55 buildPreviewTree(rule, rootNode, rule.getPropositionEditor());
56
57
58 if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
59 Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
60 if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
61 firstNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEHEADER);
62 addNodeType(firstNode, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
63 TreeNode treeNode = firstNode.getData();
64 treeNode.setData("<u>" + treeNode.getData() + ":</u>");
65 }
66 }
67
68 return myTree;
69 }
70
71 private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
72 if (prop != null) {
73
74 Node<TreeNode, String> newNode = new Node<TreeNode, String>();
75 newNode.setNodeLabel(null);
76 newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
77 addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
78
79 String data = null;
80 boolean compound = false;
81 if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
82 data = this.buildNodeLabel(prop);
83 } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
84 data = this.getDescription(prop);
85 compound = true;
86 boolean first = true;
87 for (PropositionEditor child : prop.getCompoundEditors()) {
88
89 if (!first) {
90
91 Node<TreeNode, String> opNode = new Node<TreeNode, String>();
92 opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
93 opNode.setData(new TreeNode(null));
94 newNode.getChildren().add(opNode);
95 }
96 first = false;
97
98 buildPreviewTree(rule, newNode, child);
99 }
100 }
101
102 newNode.setData(this.createTreeNode(rule, data, prop, compound));
103 currentNode.getChildren().add(newNode);
104
105 }
106 }
107
108 public TreeNode createTreeNode(RuleEditor rule, String data, PropositionEditor prop, boolean compound){
109 TreeNode tNode = new TreeNode(data);
110 tNode.setKey(prop.getKey());
111 return tNode;
112 }
113
114 @Override
115 protected String buildNodeLabel(PropositionEditor prop){
116
117 String prefix = this.getPropositionPrefix(prop);
118 return prefix + this.getDescription(prop);
119 }
120
121 @Override
122 public String getNaturalLanguageUsageKey() {
123 return null;
124 }
125
126 }