View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This is a helper class to build the preview tree to be displayed on the manage requisites page on the ui to display
35   * a readonly tree of the rule on the edit with logic tab.
36   *
37   * @author Kuali Student Team
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          //Underline the first node in the preview.
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                      // add an opcode node in between each of the children.
89                      if (!first) {
90                          //addOpCodeNode(newNode, propositionEditor);
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                      // call to build the child nodes
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         //Build the node label.
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 }