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.LogicalOperator;
21  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
22  import org.kuali.rice.krms.dto.PropositionEditor;
23  import org.kuali.rice.krms.dto.RuleEditor;
24  import org.kuali.rice.krms.tree.node.TreeNode;
25  import org.kuali.rice.krms.util.KRMSConstants;
26  import org.kuali.rice.krms.util.PropositionTreeUtil;
27  
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * This is a helper class to build the view tree to be displayed on the manage requisites page on the ui to display
34   * a readonly tree of the rule.
35   *
36   * @author Kuali Student Team
37   */
38  public class RuleViewTreeBuilder extends AbstractTreeBuilder {
39  
40      private static final long serialVersionUID = 1L;
41  
42      public Tree<TreeNode, String> buildTree(RuleEditor rule) {
43  
44          Tree myTree = new Tree<TreeNode, String>();
45  
46          Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
47          rootNode.setNodeLabel("root");
48          rootNode.setNodeType("rootNode");
49          rootNode.setData(new TreeNode("Rule:"));
50          myTree.setRootElement(rootNode);
51  
52          if (rule == null) {
53              return myTree;
54          }
55  
56          if (rule.getPropositionEditor() != null) {
57  
58              buildPreviewTree(rule, rootNode, rule.getPropositionEditor());
59  
60              //Underline the first node in the preview.
61              if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
62                  Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
63                  if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
64                      firstNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEHEADER);
65                      addNodeType(firstNode, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
66                      firstNode.setNodeLabel("<u>" + firstNode.getNodeLabel() + ":</u>");
67                  }
68              }
69          }
70  
71          return myTree;
72      }
73  
74      private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
75          if (prop != null) {
76  
77              Node<TreeNode, String> newNode = new Node<TreeNode, String>();
78              newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
79              addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
80  
81              if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
82                  newNode.setNodeLabel(this.buildNodeLabel(prop));
83              } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
84                  newNode.setNodeLabel(this.buildNodeLabel(prop));
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(prop.getCompoundOpCode()));
93                          newNode.getChildren().add(opNode);
94                      }
95                      first = false;
96                      // call to build the childs node
97                      buildPreviewTree(rule, newNode, child);
98                  }
99              }
100 
101             TreeNode tNode = new TreeNode(newNode.getNodeLabel());
102             tNode.setListItems(this.getListItems(prop));
103             tNode.setKey(prop.getKey());
104             newNode.setData(tNode);
105             currentNode.getChildren().add(newNode);
106 
107         }
108     }
109 
110     public List<Object> getListItems(PropositionEditor propositionEditor) {
111         return null;
112     }
113 
114     @Override
115     public String getNaturalLanguageUsageKey() {
116         return null;
117     }
118 }