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.StringUtils;
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.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.CompareTreeNode;
25  import org.kuali.rice.krms.util.KRMSConstants;
26  import org.kuali.rice.krms.util.PropositionTreeUtil;
27  
28  import java.util.List;
29  
30  /**
31   * Build a tree to display to different rules next to each other.
32   *
33   * Used on the compare lightbox in KRMS.
34   *
35   * @author Kuali Student Team
36   */
37  public class RuleCompareTreeBuilder extends AbstractTreeBuilder{
38  
39      private static final long serialVersionUID = 1L;
40  
41      private static final int margin = 18;
42  
43      public Tree<CompareTreeNode, String> buildTree(RuleEditor firstElement, RuleEditor secondElement) {
44          Tree<CompareTreeNode, String> myTree = initCompareTree();
45          Node<CompareTreeNode, String> firstNode = myTree.getRootElement().getChildren().get(0);
46  
47          //Add the nodes recursively.
48          addTreeNode(firstNode, getRootProposition(firstElement), getRootProposition(secondElement));
49  
50          //Underline the first node in the preview.
51          if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)){
52              Node<CompareTreeNode, String> childNode = firstNode.getChildren().get(0);
53              if(childNode.getData() != null){
54                  CompareTreeNode compareTreeNode = childNode.getData();
55  
56                  if(colonRequired(firstElement)) {
57                      if(!compareTreeNode.getFirstElement().trim().isEmpty()){
58                          compareTreeNode.setFirstElement(compareTreeNode.getFirstElement() + ":");
59                      }
60                  }
61  
62                  if(colonRequired(secondElement)) {
63                      if(!compareTreeNode.getSecondElement().trim().isEmpty()){
64                          compareTreeNode.setSecondElement(compareTreeNode.getSecondElement() + ":");
65                      }
66                  }
67              }
68          }
69  
70          return myTree;
71      }
72  
73      protected boolean colonRequired(RuleEditor element) {
74          if(element != null && element.getProposition() != null) {
75              if(element.getProposition().getPropositionTypeCode().equals(PropositionType.COMPOUND.getCode())) {
76                  return true;
77              }
78          }
79          return false;
80      }
81  
82      protected PropositionEditor getRootProposition(RuleEditor rule){
83          if(rule!=null){
84              return rule.getPropositionEditor();
85          }
86          return null;
87      }
88  
89      public static Tree<CompareTreeNode, String> initCompareTree() {
90          Tree<CompareTreeNode, String> myTree = new Tree<CompareTreeNode, String>();
91  
92          Node<CompareTreeNode, String> rootNode = createCompareNode();
93          myTree.setRootElement(rootNode);
94  
95          rootNode.getChildren().add(createCompareNode());
96  
97          return myTree;
98      }
99  
100     private static Node<CompareTreeNode, String> createCompareNode() {
101         Node<CompareTreeNode, String> rootNode = new Node<CompareTreeNode, String>();
102         rootNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
103         rootNode.setData(new CompareTreeNode(350));
104         return rootNode;
105     }
106 
107     protected void addTreeNode(Node<CompareTreeNode, String> currentNode, PropositionEditor firstElement, PropositionEditor secondElement) {
108         if ((firstElement == null) && (secondElement == null)) {
109             return;
110         }
111 
112         Node<CompareTreeNode, String> newNode = new Node<CompareTreeNode, String>();
113         CompareTreeNode tNode = new CompareTreeNode(this.getNodeWidth(currentNode), this.getDescription(firstElement), this.getDescription(secondElement));
114         tNode.setFirstElementItems(this.getListItems(firstElement));
115         tNode.setSecondElementItems(this.getListItems(secondElement));
116         newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
117         if (!tNode.getFirstElement().equals(tNode.getSecondElement())){
118             addNodeType(newNode, KRMSConstants.NODE_TYPE_COMPAREELEMENT);
119         }
120 
121         newNode.setData(tNode);
122         currentNode.getChildren().add(newNode);
123 
124         this.addCompoundTreeNode(newNode, firstElement, secondElement);
125     }
126 
127     protected void addCompoundTreeNode(Node<CompareTreeNode, String> newNode, PropositionEditor firstElement, PropositionEditor secondElement) {
128 
129         // Retrieve the opreator code of the propositions
130         String firstOpCode = this.getLabelForOperator(firstElement);
131         String secondOpCode = this.getLabelForOperator(secondElement);
132 
133         // Get the size of the biggest children list
134         int size = Math.max(getChildrenSize(firstElement), getChildrenSize(secondElement));
135 
136         for (int i = 0; i < size; i++) {
137 
138             PropositionEditor first = getChildForIndex(firstElement, i);
139             PropositionEditor second = getChildForIndex(secondElement, i);
140 
141             // add an opcode node in between each of the children.
142             if (i>0) {
143                 this.addOperatorTreeNode(newNode, getLabelForChild(first, firstOpCode), getLabelForChild(second, secondOpCode));
144             }
145 
146             // call to build the childs node
147             addTreeNode(newNode, first, second);
148         }
149 
150     }
151 
152     protected String getLabelForChild(PropositionEditor proposition, String label){
153         if (proposition!=null){
154             return label;
155         }
156         return StringUtils.EMPTY;
157     }
158 
159     protected String getLabelForOperator(PropositionEditor proposition){
160         if(proposition!=null){
161             return PropositionTreeUtil.getLabelForOperator(proposition.getCompoundOpCode());
162         }
163         return " ";
164     }
165 
166     protected int getChildrenSize(PropositionEditor parent) {
167         if ((parent != null) && (parent.getCompoundComponents()!=null)){
168             return parent.getCompoundEditors().size();
169         }
170         return 0;
171     }
172 
173     protected PropositionEditor getChildForIndex(PropositionEditor parent, int index) {
174         if ((parent != null) && (parent.getCompoundComponents()!=null)){
175             if(parent.getCompoundComponents().size() > index){
176                 return parent.getCompoundEditors().get(index);
177             }
178         }
179         return null;
180     }
181 
182     protected int getNodeWidth(Node<CompareTreeNode, String> parent){
183         return parent.getData().getWidth() - margin;
184     }
185 
186     protected void addOperatorTreeNode(Node<CompareTreeNode, String> newNode, String firstElement, String secondElement) {
187         Node<CompareTreeNode, String> opNode = new Node<CompareTreeNode, String>();
188         if (!firstElement.equals(secondElement)){
189             opNode.setNodeType(KRMSConstants.NODE_TYPE_COMPAREELEMENT);
190         }
191         opNode.setData(new CompareTreeNode(this.getNodeWidth(newNode), firstElement, secondElement));
192         newNode.getChildren().add(opNode);
193     }
194 
195     public List<String> getListItems(PropositionEditor propositionEditor) {
196         return null;
197     }
198 
199     public String getNaturalLanguageUsageKey(){
200         return null;
201     }
202 
203 }