Coverage Report - org.kuali.student.core.statement.ui.client.widgets.rules.RuleExpressionEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleExpressionEditor
0%
0/101
0%
0/16
2.714
RuleExpressionEditor$1
0%
0/11
0%
0/4
2.714
RuleExpressionEditor$2
0%
0/5
0%
0/8
2.714
RuleExpressionEditor$3
0%
0/23
0%
0/10
2.714
RuleExpressionEditor$4
0%
0/9
0%
0/2
2.714
 
 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.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.ui.client.mvc.Callback;
 22  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 23  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 24  
 import org.kuali.student.common.ui.client.widgets.KSTextArea;
 25  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 26  
 
 27  
 import com.google.gwt.event.dom.client.ClickEvent;
 28  
 import com.google.gwt.event.dom.client.ClickHandler;
 29  
 import com.google.gwt.event.dom.client.KeyUpEvent;
 30  
 import com.google.gwt.event.dom.client.KeyUpHandler;
 31  
 import com.google.gwt.user.client.ui.*;
 32  
 
 33  0
 public class RuleExpressionEditor extends FlowPanel {
 34  
 
 35  
     //view's widgets
 36  0
     private KSTextArea expressionTextBox = new KSTextArea();
 37  0
     private HTML htmlErrorMessage = new HTML();    
 38  0
     private KSButton btnUpdate = new KSButton("Update");
 39  0
     private KSButton btnUndo = new KSButton("Undo");
 40  0
     private FlowPanel ruleTablePanel = new FlowPanel();
 41  0
     private KSLabel noRuleText = new KSLabel("");
 42  0
     private Panel pnlMissingExpressions = new VerticalPanel();
 43  
 
 44  
     //rule table
 45  0
     private RuleTable  ruleTable = new RuleTable(false);
 46  0
     private ClickHandler ruleTableEditClauseHandler = null;
 47  
 
 48  
     //view's data
 49  
     private RuleInfo rule;
 50  
     private String previewExpression;
 51  
     private Callback reqCompEditCallback;
 52  
     private Callback ruleChangedCallback;
 53  
     private RuleManageWidget parent;
 54  0
     private boolean ruleChanged = false;
 55  
 
 56  
     // helper object
 57  0
     private RuleExpressionParser ruleExpressionParser = new RuleExpressionParser();
 58  
        
 59  0
     public RuleExpressionEditor(RuleManageWidget parent) {
 60  0
         this.parent = parent;
 61  
 
 62  0
         FlowPanel expressionBox = new FlowPanel();
 63  0
         HorizontalPanel expressionAndMsg = new HorizontalPanel();
 64  0
         expressionBox.setStyleName("KS-Program-Rule-LogicView-ExpressionPanel");
 65  0
         expressionTextBox.addStyleName("KS-Program-Rule-LogicView-ExpressionText");
 66  0
         expressionAndMsg.add(expressionTextBox);
 67  0
         expressionAndMsg.add(htmlErrorMessage);
 68  0
         expressionBox.add(expressionAndMsg);
 69  0
         btnUpdate.addStyleName("KS-Program-Rule-LogicView-Update-Button");
 70  0
         FlowPanel buttonsPanel = new FlowPanel();
 71  0
         buttonsPanel.add(btnUpdate);
 72  0
         btnUndo.addStyleName("KS-Program-Rule-LogicView-Undo-Button");
 73  0
         buttonsPanel.add(btnUndo);
 74  0
         buttonsPanel.addStyleName("KS-Program-Rule-LogicView-ButtonPanel");
 75  0
         expressionBox.add(buttonsPanel);
 76  0
         add(expressionBox);
 77  
 
 78  0
         ruleTablePanel.setStyleName("KS-Program-Rule-LogicView-RulePanel");
 79  0
         ruleTablePanel.add(ruleTable);
 80  0
         ruleTablePanel.add(noRuleText);
 81  0
         ruleTablePanel.add(pnlMissingExpressions);
 82  0
         add(ruleTablePanel);
 83  
 
 84  0
         previewExpression = "";
 85  
 
 86  0
         setupHandlers();
 87  0
     }
 88  
 
 89  
     public void setRule(RuleInfo rule) {
 90  0
         this.rule = rule;
 91  0
         redraw();
 92  0
     }
 93  
 
 94  
     private void setupHandlers() {
 95  0
         ruleTableEditClauseHandler = new ClickHandler() {
 96  
             @Override
 97  
             public void onClick(ClickEvent event) {
 98  0
                 HTMLTable.Cell cell = ruleTable.getCellForEvent(event);
 99  0
                 if (cell == null) {
 100  0
                     return;
 101  
                 }
 102  
 
 103  0
                 RuleNodeWidget widget = (RuleNodeWidget) ruleTable.getWidget(cell.getRowIndex(), cell.getCellIndex());
 104  0
                 Object userObject = widget.getNode().getUserObject();
 105  0
                 if (userObject instanceof ReqComponentVO) {
 106  0
                     widget.setEditMode(true);
 107  0
                     final ReqComponentVO tempRule = (ReqComponentVO) userObject;
 108  0
                     reqCompEditCallback.exec(tempRule.getReqComponentInfo());
 109  
                 }
 110  0
             }
 111  
         };                        
 112  0
         expressionTextBox.addKeyUpHandler(new KeyUpHandler() {
 113  
             public void onKeyUp(KeyUpEvent event) {
 114  
                 // escape error keys
 115  0
                 if(event.getNativeKeyCode() == 37 
 116  
                         ||event.getNativeKeyCode() == 38
 117  
                         ||event.getNativeKeyCode() == 39
 118  
                         ||event.getNativeKeyCode() == 40){
 119  0
                         return;
 120  
                 }
 121  0
                 btnUpdate.setEnabled(true);
 122  0
             }
 123  
         });
 124  0
         btnUpdate.addClickHandler(new ClickHandler() {
 125  
             public void onClick(ClickEvent event) {
 126  
 
 127  
                 //get and format logic expression of the rule
 128  0
                 previewExpression = expressionTextBox.getText();
 129  0
                 previewExpression = previewExpression.replaceAll("\n", " ");
 130  0
                 previewExpression = previewExpression.replaceAll("\r", " ");
 131  0
                 ruleExpressionParser.setExpression(previewExpression);
 132  
 
 133  
                 //validate the expression
 134  0
                 List<ReqComponentVO> rcs = (rule.getStatementVO() == null || rule.getStatementVO().getAllReqComponentVOs() == null) ?
 135  
                                                 new ArrayList<ReqComponentVO>() : rule.getStatementVO().getAllReqComponentVOs();
 136  0
                 List<String> errorMessages = new ArrayList<String>();
 137  0
                 boolean validExpression = ruleExpressionParser.validateExpression(errorMessages, rcs);
 138  
 
 139  
                 //show errors and don't change anything else
 140  0
                 if (!validExpression) {
 141  
 
 142  
                     //show missing 'subrules' that were removed from the expression box by the user
 143  0
                     List<ReqComponentVO> missingRCs = new ArrayList<ReqComponentVO>();
 144  0
                     rcs = (rule.getStatementVO() == null || rule.getStatementVO().getAllReqComponentVOs() == null) ?
 145  
                                                     new ArrayList<ReqComponentVO>() : rule.getStatementVO().getAllReqComponentVOs();
 146  0
                     ruleExpressionParser.checkMissingRCs(missingRCs, rcs);
 147  0
                     showMissingRCs(missingRCs);
 148  
 
 149  0
                     showErrors(errorMessages);
 150  0
                     return;
 151  
                 }
 152  
 
 153  
                 //update the rule based on the updated logic expression
 154  0
                 StatementVO newStatementVO = ruleExpressionParser.parseExpressionIntoStatementVO(previewExpression, rule.getStatementVO(), rule.getStatementTypeKey());
 155  0
                 rule.setStatementVO(newStatementVO);
 156  0
                 rule.getEditHistory().save(newStatementVO);
 157  
 
 158  
                 //display rule table for updated rule
 159  0
                 redraw();
 160  
 
 161  0
                 parent.updateObjectRule(rule);
 162  0
                 ruleChanged = true;
 163  0
                 ruleChangedCallback.exec(true);
 164  0
             }
 165  
         });
 166  
         
 167  0
         btnUndo.addClickHandler(new ClickHandler() {
 168  
             public void onClick(ClickEvent event) {
 169  0
                 StatementVO previousState = rule.getEditHistory().undo();
 170  0
                 if (previousState != null) {
 171  0
                     rule.setStatementVO(previousState);
 172  
                 }
 173  0
                 redraw();
 174  0
                 parent.updateObjectRule(rule);
 175  0
                 ruleChanged = true;
 176  0
                 ruleChangedCallback.exec(true);
 177  0
             }
 178  
         });
 179  0
     }
 180  
 
 181  
     private void redraw() {
 182  0
         htmlErrorMessage.setHTML("");
 183  0
         ruleTable.clear();
 184  0
         noRuleText.setText("");
 185  0
         ruleChanged = false;
 186  0
         setEnabledView(true);
 187  0
         expressionTextBox.setText(rule.getExpression().trim().isEmpty() ? "" : rule.getExpression().trim());        
 188  
 
 189  0
         if (rule.getExpression().trim().isEmpty()) {
 190  0
             noRuleText.setText("No rules have been added");
 191  0
             btnUpdate.setEnabled(false);
 192  0
             return;
 193  
         }
 194  
 
 195  0
         btnUpdate.setEnabled(false);    //redraw rule means no outstanding change to the expression    
 196  0
         ruleTable.buildTable(rule.getStatementTree());
 197  0
         ruleTable.addEditClauseHandler(ruleTableEditClauseHandler);
 198  0
     }
 199  
 
 200  
     private void showMissingRCs(List<ReqComponentVO> missingRCs) {
 201  0
         VerticalPanel pnlRCList = new VerticalPanel();
 202  0
         boolean missingExprFound = false;       
 203  0
         pnlMissingExpressions.clear();
 204  
 
 205  0
         for (ReqComponentVO rc : missingRCs) {
 206  0
             HorizontalPanel pnlRcListRow = new HorizontalPanel();
 207  0
             if (rc.getGuiReferenceLabelId() != null) {
 208  0
                 KSLabel rcLabel = new KSLabel(rc.getGuiReferenceLabelId());
 209  0
                 rcLabel.setStyleName("KS-Program-Rule-LogicView-Rule-ID");
 210  0
                 pnlRcListRow.add(rcLabel);
 211  0
                 missingExprFound = true;
 212  
             }
 213  0
             pnlRCList.add(pnlRcListRow);
 214  0
         }
 215  
         
 216  0
         if (missingExprFound) {
 217  0
             KSLabel missingExprTitle = new KSLabel("The following rules do not exist");
 218  0
             missingExprTitle.setStyleName("KS-Program-Rule-LogicView-Missing-Rules");
 219  0
             pnlMissingExpressions.add(missingExprTitle);                        
 220  0
             pnlMissingExpressions.add(pnlRCList);
 221  
         }        
 222  0
     }
 223  
     
 224  
     private void showErrors(List<String> errorMessages) {
 225  0
         StringBuilder sb = new StringBuilder("");
 226  0
         boolean firstRow = true;
 227  0
         for (String errorMessage : errorMessages) {
 228  0
             if (!firstRow) {
 229  0
                 sb.append(",<BR>");
 230  
             }
 231  0
             firstRow = false;
 232  0
             sb.append(errorMessage);
 233  
         }
 234  0
         htmlErrorMessage.getElement().getStyle().setProperty("color", "red");
 235  0
         htmlErrorMessage.setHTML(sb.toString());
 236  0
     }
 237  
 
 238  
     private void setEnableButtons(boolean enabled) {
 239  0
         if (enabled) {
 240  0
             btnUpdate.setEnabled(enabled);
 241  0
             btnUndo.setEnabled(rule.getEditHistory().isUndoable()); 
 242  
         } else {
 243  0
             btnUpdate.setEnabled(false);
 244  0
             btnUndo.setEnabled(false);
 245  
         }
 246  0
     }
 247  
 
 248  
     public void setEnabledView(boolean enabled) {
 249  0
         setEnableButtons(enabled);
 250  0
         ruleTable.setEnabled(enabled);
 251  0
         expressionTextBox.setEnabled(enabled);
 252  0
     }
 253  
 
 254  
     public void addReqCompEditButtonClickCallback(Callback<ReqComponentInfo> callback) {
 255  0
         reqCompEditCallback = callback;
 256  0
     }
 257  
 
 258  
     public void addRuleChangedButtonClickCallback(Callback<Boolean> callback) {
 259  0
         ruleChangedCallback = callback;
 260  0
     }
 261  
 }