Coverage Report - org.kuali.student.core.statement.ui.client.widgets.rules.RuleInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleInfo
0%
0/182
0%
0/152
4.478
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.core.statement.ui.client.widgets.rules;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.student.core.statement.dto.StatementInfo;
 21  
 import org.kuali.student.core.statement.dto.StatementOperatorTypeKey;
 22  
 import org.kuali.student.core.statement.ui.client.widgets.table.Node;
 23  
 
 24  
 public class RuleInfo {
 25  
 
 26  
     private StatementVO statementVO;        //top-level statement (tree ROOT)
 27  
     private EditHistory editHistory;
 28  
     private boolean simplifying;
 29  
 
 30  0
     public RuleInfo() {
 31  0
         setEditHistory(new EditHistory(statementVO));
 32  0
         setStatementVO(createNewStatementVO());
 33  0
     }
 34  
 
 35  
     public StatementVO createNewStatementVO() {
 36  0
         StatementInfo newStatementTree = new StatementInfo();
 37  0
         newStatementTree.setOperator(StatementOperatorTypeKey.AND);
 38  0
         newStatementTree.setType(getStatementTypeKey());
 39  0
         StatementVO statementVO = new StatementVO();                            
 40  0
         statementVO.setStatementInfo(newStatementTree);
 41  0
         return statementVO;
 42  
     }
 43  
     
 44  
     public Node getStatementTree() {
 45  0
         return (statementVO != null ? statementVO.getTree() : null);
 46  
     }
 47  
     
 48  
     public EditHistory getEditHistory() {
 49  0
         return editHistory;
 50  
     }
 51  
 
 52  
     public void setEditHistory(EditHistory editHistory) {
 53  0
         this.editHistory = editHistory;
 54  0
     }
 55  
     
 56  
     private boolean statementVOIsGroupAble(List<StatementVO> selectedStatementVOs, List<ReqComponentVO> selectedReqComponentVOs) {
 57  0
         boolean orable = false;
 58  0
         StatementVO enclosingStatement = null;
 59  0
         boolean reqComponentsInSameGroup = true;
 60  0
         int numStatementsSelection = (selectedStatementVOs == null)? 0 : selectedStatementVOs.size();
 61  0
         int numReqComponentSelection = (selectedReqComponentVOs == null)? 0 : selectedReqComponentVOs.size();
 62  0
         int numSelection = numStatementsSelection + numReqComponentSelection;
 63  
         
 64  
         // At least 2 items (StatementVO or RC) must be selected.
 65  0
         if (numSelection >= 2) {
 66  0
             if (numStatementsSelection > 0) {
 67  0
                 enclosingStatement = this.statementVO.getParentStatementVO(this.statementVO, selectedStatementVOs.get(0));
 68  
             } else {
 69  0
                 enclosingStatement = this.statementVO.getEnclosingStatementVO(this.statementVO, selectedReqComponentVOs.get(0));
 70  
             }
 71  
             
 72  0
             if (numStatementsSelection > 0) {
 73  0
                 for (StatementVO selectedStatementVO : selectedStatementVOs) {
 74  0
                     StatementVO enclosingStatement2 = this.statementVO.getParentStatementVO(this.statementVO, selectedStatementVO);
 75  0
                     if (enclosingStatement != enclosingStatement2) {
 76  0
                         reqComponentsInSameGroup = false;
 77  0
                         break;
 78  
                     }
 79  0
                 }
 80  
             }
 81  0
             if (numReqComponentSelection > 0) {
 82  0
                 for (ReqComponentVO selectedReqCompoentVO : selectedReqComponentVOs) {
 83  0
                     StatementVO enclosingStatement2 = this.statementVO.getEnclosingStatementVO(this.statementVO, selectedReqCompoentVO);
 84  0
                     if (enclosingStatement != enclosingStatement2) {
 85  0
                         reqComponentsInSameGroup = false;
 86  0
                         break;
 87  
                     }
 88  0
                 }
 89  
             }
 90  
             
 91  
             // the items selected must all belong to the same group
 92  0
             if (!reqComponentsInSameGroup) return false;
 93  
             
 94  0
             int childCount = (enclosingStatement == null)? 0 : enclosingStatement.getChildCount();
 95  
             
 96  
             // number of selected requirement components must be less the the total number of
 97  
             // requirement components of the enclosing statement
 98  0
             if (selectedReqComponentVOs != null && !selectedReqComponentVOs.isEmpty() && childCount > numSelection) {
 99  0
                 orable = true;
 100  
             } else {
 101  0
                 orable = false;
 102  
             }
 103  0
         } else {
 104  0
             orable = false;
 105  
         }
 106  
 
 107  0
         return orable;
 108  
     }
 109  
 
 110  
     public boolean statementVOIsGroupAble() {
 111  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 112  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 113  0
         return statementVOIsGroupAble(selectedStatementVOs, selectedReqComponentVOs);
 114  
     }
 115  
     
 116  
     public void insertOR() {
 117  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 118  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 119  
 
 120  
         // check if the selections can be grouped by OR operator.
 121  0
         if (!statementVOIsGroupAble(selectedStatementVOs, selectedReqComponentVOs)) return;
 122  0
         StatementVO enclosingStatementVO = statementVO.getEnclosingStatementVO(statementVO, selectedReqComponentVOs.get(0));
 123  
 
 124  
         // create new statement to hold the new OR group
 125  0
         StatementVO newStatementVO = createNewStatementVO();
 126  0
         StatementInfo newLuStatementInfo = newStatementVO.getStatementInfo();
 127  0
         newLuStatementInfo.setOperator(StatementOperatorTypeKey.OR);
 128  0
         newStatementVO.setStatementInfo(newLuStatementInfo);
 129  
 
 130  
         // remove the selected RCs from original statement and move them into the new StatementVO
 131  0
         for (ReqComponentVO selectedReqComponentVO : selectedReqComponentVOs) {
 132  0
             enclosingStatementVO.removeReqComponentVO(selectedReqComponentVO);
 133  0
             newStatementVO.addReqComponentVO(selectedReqComponentVO);
 134  
         }
 135  
 
 136  
         // remove the selected StatementVOs from original statement and move them into the new StatementVO
 137  0
         for (StatementVO selectedStatementVO : selectedStatementVOs) {
 138  0
             enclosingStatementVO.removeStatementVO(selectedStatementVO);
 139  0
             newStatementVO.addStatementVO(selectedStatementVO);
 140  
         }
 141  0
         enclosingStatementVO.addStatementVO(newStatementVO);
 142  0
     }
 143  
     
 144  
     public void insertAND() {
 145  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 146  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 147  
 
 148  
         // check if the selections can be grouped by OR operator.
 149  0
         if (!statementVOIsGroupAble(selectedStatementVOs, selectedReqComponentVOs)) return;
 150  
 
 151  0
         StatementVO enclosingStatementVO = statementVO.getEnclosingStatementVO(statementVO, selectedReqComponentVOs.get(0));
 152  
         // create new statement to hold the new OR group
 153  0
         StatementVO newStatementVO = createNewStatementVO();
 154  0
         StatementInfo newLuStatementInfo = newStatementVO.getStatementInfo();
 155  0
         newLuStatementInfo.setOperator(StatementOperatorTypeKey.AND);
 156  0
         newStatementVO.setStatementInfo(newLuStatementInfo);
 157  
 
 158  
         // remove the selected RCs from original statement and move them into the new StatementVO
 159  0
         for (ReqComponentVO selectedReqComponentVO : selectedReqComponentVOs) {
 160  0
             enclosingStatementVO.removeReqComponentVO(selectedReqComponentVO);
 161  0
             newStatementVO.addReqComponentVO(selectedReqComponentVO);
 162  
         }
 163  
 
 164  
         // remove the selected StatementVOs from original statement and move them into the new StatementVO
 165  0
         for (StatementVO selectedStatementVO : selectedStatementVOs) {
 166  0
             enclosingStatementVO.removeStatementVO(selectedStatementVO);
 167  0
             newStatementVO.addStatementVO(selectedStatementVO);
 168  
         }
 169  
         
 170  0
         enclosingStatementVO.addStatementVO(newStatementVO);
 171  0
     }
 172  
     
 173  
     public boolean statementVOIsDegroupAble() {
 174  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 175  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 176  0
         return statementVOIsDegroupAble(selectedStatementVOs, selectedReqComponentVOs);
 177  
     }
 178  
     
 179  
     private boolean statementVOIsDegroupAble(List<StatementVO> selectedStatementVOs, List<ReqComponentVO> selectedReqComponentVOs) {
 180  0
         boolean degroupAble = false;
 181  0
         boolean selectedRootStatementVO = false;
 182  0
         boolean otherItemsExist = false;
 183  
 
 184  
         // at least one item is selected
 185  0
         if ((selectedStatementVOs != null && !selectedStatementVOs.isEmpty()) || 
 186  
             (selectedReqComponentVOs != null && !selectedReqComponentVOs.isEmpty())) {
 187  0
             degroupAble = true;
 188  
         } else {
 189  0
             return false;
 190  
         }
 191  
         
 192  
         // root statement should not be selected if there are other items.
 193  0
         for (StatementVO selectedStatement : selectedStatementVOs) {
 194  0
             if (selectedStatement == this.statementVO) {
 195  0
                 selectedRootStatementVO = true;
 196  0
                 break;
 197  
             }
 198  
         }
 199  
 
 200  0
         otherItemsExist = this.statementVO.getChildCount() > 0;
 201  0
         if (selectedRootStatementVO && otherItemsExist) {
 202  0
             degroupAble = degroupAble && false;
 203  
         } else{
 204  0
             degroupAble = degroupAble && true;
 205  
         }
 206  
         
 207  
         // must leave at least 2 children in a group unselected or all children are RCs and are selected.
 208  
         // Except for root.  For root, if root statement does not have any sub statements then it is okay to delete
 209  0
         for (ReqComponentVO selectedRC : selectedReqComponentVOs) {
 210  0
             StatementVO parentStatementVO = this.statementVO.getEnclosingStatementVO(this.statementVO, selectedRC);
 211  0
             int numSelectedChildren = 0;
 212  0
             int numSelectedChildrenRC = 0;
 213  0
             if (parentStatementVO.getStatementVOCount() > 0) {
 214  
                 for (StatementVO subStatementVO : 
 215  0
                     parentStatementVO.getStatementVOs()) {
 216  0
                     if (subStatementVO.isWrapperStatementVO() &&
 217  
                             subStatementVO.getReqComponentVOs().get(0).isCheckBoxOn()) {
 218  0
                         numSelectedChildren++;
 219  0
                     } else if (subStatementVO.isCheckBoxOn()) {
 220  0
                         numSelectedChildren++;
 221  
                     }
 222  
                 }
 223  0
             } else if (parentStatementVO.getReqComponentVOCount() > 0) {
 224  0
                 for (ReqComponentVO rcVO : parentStatementVO.getReqComponentVOs()) {
 225  0
                     if (rcVO.isCheckBoxOn()) {
 226  0
                         numSelectedChildren++;
 227  0
                         numSelectedChildrenRC++;
 228  
                     }
 229  
                 }
 230  
             }
 231  0
             if (parentStatementVO.getChildCount() - numSelectedChildren > 1) {
 232  0
                 degroupAble = degroupAble && true;
 233  0
             } else if (numSelectedChildrenRC == parentStatementVO.getChildCount()) {
 234  0
                 degroupAble = degroupAble && true;
 235  0
             } else if (parentStatementVO == this.statementVO && parentStatementVO.getStatementVOCount() == 0) {
 236  0
                 degroupAble = degroupAble && true;
 237  
             } else {
 238  0
                 degroupAble = false;
 239  
             }
 240  0
         }
 241  0
         return degroupAble;
 242  
     }
 243  
     
 244  
     public void deleteItem() {
 245  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 246  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 247  
 
 248  0
         if (!statementVOIsDegroupAble(selectedStatementVOs, selectedReqComponentVOs)) return;
 249  
 
 250  
         // remove the selected RCs
 251  0
         for (ReqComponentVO selectedReqComponent : selectedReqComponentVOs) {
 252  0
             StatementVO parentStatementVO = null;
 253  0
             parentStatementVO = this.statementVO.getEnclosingStatementVO(this.statementVO, selectedReqComponent);
 254  0
             parentStatementVO.removeReqComponentVO(selectedReqComponent);
 255  0
         }
 256  
 
 257  
         // remove the selected operator cells/statements.
 258  0
         for (StatementVO selectedStatement : selectedStatementVOs) {
 259  0
             StatementVO parentStatementVO = null; 
 260  0
             parentStatementVO = this.statementVO.getParentStatementVO(this.statementVO, selectedStatement);
 261  
             
 262  0
             if (selectedStatement == statementVO) {
 263  0
                 statementVO = null;
 264  0
             } else if (parentStatementVO != null) {
 265  0
                 parentStatementVO.removeStatementVO(selectedStatement);
 266  0
                 if (selectedStatement.getStatementVOCount() > 0) {
 267  0
                     for (StatementVO subStatement : selectedStatement.getStatementVOs()) {
 268  0
                         parentStatementVO.addStatementVO(subStatement);
 269  
                     }
 270  0
                 } else if (selectedStatement.getReqComponentVOCount() > 0) {
 271  0
                     for (ReqComponentVO leafReqComponent : selectedStatement.getReqComponentVOs()) {
 272  0
                         parentStatementVO.addReqComponentVO(leafReqComponent);
 273  
                     }
 274  
                 }
 275  
             }
 276  0
         }
 277  
 
 278  
         // clears off the root if the root no longer has any children after the deletions
 279  
      //   if (statementVO.getChildCount() == 0) {
 280  
      //       statementVO = null;
 281  
      //   }
 282  0
     }
 283  
 
 284  
     public boolean isAddToGroupOK() {
 285  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 286  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 287  0
         return isAddToGroupOK(selectedStatementVOs, selectedReqComponentVOs);
 288  
     }
 289  
 
 290  
     private boolean isAddToGroupOK(List<StatementVO> selectedStatementVOs, List<ReqComponentVO> selectedReqComponentVOs) {
 291  0
         int numSelectedS = (selectedStatementVOs == null)? 0 : selectedStatementVOs.size();
 292  0
         int numSelectedRC = (selectedReqComponentVOs == null)? 0 : selectedReqComponentVOs.size();
 293  
         // one or more rules are selected as well as a single AND or OR cell
 294  0
         return (numSelectedS == 1 && numSelectedRC > 0);
 295  
     }
 296  
     
 297  
     public void addToGroup() {
 298  0
         List<StatementVO> selectedStatementVOs = getSelectedStatementVOs();
 299  0
         List<ReqComponentVO> selectedReqComponentVOs = getSelectedReqComponentVOs();
 300  0
         StatementVO selectedS = null;
 301  
 
 302  0
         if (!isAddToGroupOK(selectedStatementVOs, selectedReqComponentVOs)) return;
 303  
 
 304  0
         selectedS = selectedStatementVOs.get(0);
 305  0
         if (selectedReqComponentVOs != null && !selectedReqComponentVOs.isEmpty()) {
 306  0
             for (ReqComponentVO selectedRC : selectedReqComponentVOs) {
 307  0
                 StatementVO parentS = this.statementVO.getEnclosingStatementVO(this.statementVO, selectedRC);
 308  0
                 if (parentS == null) continue;
 309  
 
 310  0
                 parentS.removeReqComponentVO(selectedRC);
 311  
                 // The following if else statement cleans up parent statements that
 312  
                 // has no more children other than the selectedS
 313  0
                 if (parentS.getChildCount() == 1 &&
 314  
                         parentS.getSelectedStatementVOs() != null &&
 315  
                         parentS.getSelectedStatementVOs().contains(selectedS)) {
 316  
                     // if parent is root replace the current root with the selected statement.
 317  
                     // otherwise remove the parent from grand parent and add the selected statement
 318  0
                     if (parentS == statementVO) {
 319  0
                         this.statementVO = selectedS;
 320  
                     } else {
 321  0
                         StatementVO grandParentS = this.statementVO.getParentStatementVO(this.statementVO, parentS);
 322  0
                         grandParentS.removeStatementVO(parentS);
 323  0
                         grandParentS.addStatementVO(selectedS);
 324  
                     }
 325  
                 }
 326  0
                 selectedS.addReqComponentVO(selectedRC);
 327  0
             }
 328  0
             statementVO.simplify();
 329  
         }
 330  0
     }
 331  
     
 332  
     public List<StatementVO> getSelectedStatementVOs() {
 333  0
         return (statementVO == null)? null : statementVO.getSelectedStatementVOs();
 334  
     }
 335  
     
 336  
     public List<ReqComponentVO> getSelectedReqComponentVOs() {
 337  0
         return (statementVO == null)? null : statementVO.getSelectedReqComponentVOs();
 338  
     }
 339  
 
 340  
     public StatementVO getStatementVO() {
 341  0
         return statementVO;
 342  
     }
 343  
 
 344  
     public void setStatementVO(StatementVO statementVO) {
 345  0
         this.statementVO = statementVO;
 346  0
     }
 347  
     
 348  
     public String getExpression() {
 349  0
         return (statementVO == null)? null : statementVO.convertToExpression();
 350  
     }
 351  
 
 352  
     public boolean isSimplifying() {
 353  0
         return simplifying;
 354  
     }
 355  
 
 356  
     public void setSimplifying(boolean simplifying) {
 357  0
         this.simplifying = simplifying;
 358  0
     }
 359  
 
 360  
     public String getStatementTypeKey() {
 361  0
         String statementType = (statementVO == null || statementVO.getStatementInfo() == null)? null :
 362  
             statementVO.getStatementInfo().getType();
 363  0
             return statementType;
 364  
     }
 365  
 }