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   *
35   * @author Kuali Student Team
36   */
37  public class RulePreviewTreeBuilder extends AbstractTreeBuilder{
38  
39      public Tree<TreeNode, String> buildTree(RuleEditor rule) {
40  
41          Tree myTree = new Tree<TreeNode, String>();
42  
43          Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
44          rootNode.setNodeLabel("root");
45          rootNode.setNodeType("rootNode");
46          rootNode.setData(new TreeNode("Rule:"));
47          myTree.setRootElement(rootNode);
48  
49          if (rule == null) {
50              return myTree;
51          }
52  
53          buildPreviewTree(rule, rootNode, rule.getPropositionEditor());
54  
55          //Underline the first node in the preview.
56          if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
57              Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
58              if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
59                  firstNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEHEADER);
60                  addNodeType(firstNode, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
61                  TreeNode treeNode = firstNode.getData();
62                  treeNode.setData("<u>" + treeNode.getData() + ":</u>");
63              }
64          }
65  
66          return myTree;
67      }
68  
69      private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
70          if (prop != null) {
71  
72              Node<TreeNode, String> newNode = new Node<TreeNode, String>();
73              newNode.setNodeLabel(null);
74              newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
75              addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
76  
77              TreeNode tNode = null;
78              if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
79                  tNode = new TreeNode(this.buildNodeLabel(prop));
80              } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
81                  tNode = new TreeNode(StringEscapeUtils.escapeHtml(this.getDescription(prop)));
82                  boolean first = true;
83                  for (PropositionEditor child : prop.getCompoundEditors()) {
84                      // add an opcode node in between each of the children.
85                      if (!first) {
86                          //addOpCodeNode(newNode, propositionEditor);
87                          Node<TreeNode, String> opNode = new Node<TreeNode, String>();
88                          opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
89                          opNode.setData(new TreeNode(null));
90                          newNode.getChildren().add(opNode);
91  
92                      }
93                      first = false;
94                      // call to build the childs node
95                      buildPreviewTree(rule, newNode, child);
96                  }
97              }
98  
99              tNode.setListItems(this.getListItems(prop));
100             newNode.setData(tNode);
101             tNode.setKey(prop.getKey());
102             currentNode.getChildren().add(newNode);
103 
104         }
105     }
106 
107     @Override
108     protected String buildNodeLabel(PropositionEditor prop){
109         //Build the node label.
110         String prefix = this.getPropositionPrefix(prop);
111         return prefix + StringEscapeUtils.escapeHtml(this.getDescription(prop));
112     }
113 
114     @Override
115     public String getNaturalLanguageUsageKey() {
116         return null;
117     }
118 
119     public List<Object> getListItems(PropositionEditor propositionEditor) {
120         return null;
121     }
122 }