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              TreeNode tNode = null;
80              if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
81                  tNode = new TreeNode(this.buildNodeLabel(prop));
82              } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
83                  tNode = new TreeNode(this.getDescription(prop));
84                  tNode.setCompound(true);
85                  boolean first = true;
86                  for (PropositionEditor child : prop.getCompoundEditors()) {
87                      // add an opcode node in between each of the children.
88                      if (!first) {
89                          //addOpCodeNode(newNode, propositionEditor);
90                          Node<TreeNode, String> opNode = new Node<TreeNode, String>();
91                          opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
92                          opNode.setData(new TreeNode(null));
93                          newNode.getChildren().add(opNode);
94                      }
95                      first = false;
96                      // call to build the child nodes
97                      buildPreviewTree(rule, newNode, child);
98                  }
99              }
100 
101             tNode.setListItems(this.getListItems(prop));
102             newNode.setData(tNode);
103             tNode.setKey(prop.getKey());
104             currentNode.getChildren().add(newNode);
105 
106         }
107     }
108 
109     @Override
110     protected String buildNodeLabel(PropositionEditor prop){
111         //Build the node label.
112         String prefix = this.getPropositionPrefix(prop);
113         return prefix + this.getDescription(prop);
114     }
115 
116     @Override
117     public String getNaturalLanguageUsageKey() {
118         return null;
119     }
120 
121     public List<Object> getListItems(PropositionEditor propositionEditor) {
122         return null;
123     }
124 }