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.kuali.rice.core.api.util.tree.Node;
19  import org.kuali.rice.core.api.util.tree.Tree;
20  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
21  import org.kuali.rice.krms.dto.PropositionEditor;
22  import org.kuali.rice.krms.dto.RuleEditor;
23  import org.kuali.rice.krms.tree.node.TreeNode;
24  import org.kuali.rice.krms.util.KRMSConstants;
25  import org.kuali.rice.krms.util.PropositionTreeUtil;
26  
27  import java.util.List;
28  
29  /**
30   * This is a helper class to build the view tree to be displayed on the manage requisites page on the ui to display
31   * a readonly tree of the rule.
32   *
33   * @author Kuali Student Team
34   */
35  public class RuleViewTreeBuilder extends AbstractTreeBuilder {
36  
37      private static final long serialVersionUID = 1L;
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          if (rule.getPropositionEditor() != null) {
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                      firstNode.setNodeLabel("<u>" + firstNode.getNodeLabel() + ":</u>");
64                  }
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.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
76              addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
77  
78              if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
79                  newNode.setNodeLabel(this.buildNodeLabel(prop));
80              } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
81                  newNode.setNodeLabel(this.buildNodeLabel(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(prop.getCompoundOpCode()));
90                          newNode.getChildren().add(opNode);
91                      }
92                      first = false;
93                      // call to build the child nodes
94                      buildPreviewTree(rule, newNode, child);
95                  }
96              }
97  
98              newNode.setData(this.createTreeNode(rule, newNode.getNodeLabel(), prop));
99              currentNode.getChildren().add(newNode);
100 
101         }
102     }
103 
104     public TreeNode createTreeNode(RuleEditor rule, String data, PropositionEditor prop){
105         TreeNode tNode = new TreeNode(data);
106         tNode.setKey(prop.getKey());
107         return tNode;
108     }
109 
110     @Override
111     public String getNaturalLanguageUsageKey() {
112         return null;
113     }
114 }