Coverage Report - org.kuali.student.lum.lu.ui.course.client.requirements.CourseRequirementsSummaryView
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseRequirementsSummaryView
0%
0/133
0%
0/56
2.414
CourseRequirementsSummaryView$1
0%
0/5
0%
0/2
2.414
CourseRequirementsSummaryView$2
0%
0/12
0%
0/4
2.414
CourseRequirementsSummaryView$3
0%
0/3
N/A
2.414
CourseRequirementsSummaryView$3$1
0%
0/10
0%
0/2
2.414
CourseRequirementsSummaryView$3$1$1
0%
0/5
N/A
2.414
CourseRequirementsSummaryView$4
0%
0/3
N/A
2.414
CourseRequirementsSummaryView$4$1
0%
0/4
N/A
2.414
CourseRequirementsSummaryView$5
0%
0/6
N/A
2.414
CourseRequirementsSummaryView$6
0%
0/7
0%
0/4
2.414
CourseRequirementsSummaryView$6$1
0%
0/6
0%
0/2
2.414
CourseRequirementsSummaryView$7
0%
0/17
0%
0/4
2.414
CourseRequirementsSummaryView$7$1
0%
0/3
N/A
2.414
CourseRequirementsSummaryView$7$2
0%
0/5
0%
0/2
2.414
 
 1  
 package org.kuali.student.lum.lu.ui.course.client.requirements;
 2  
 
 3  
 import java.util.*;
 4  
 
 5  
 import org.kuali.student.common.dto.RichTextInfo;
 6  
 import org.kuali.student.common.ui.client.application.Application;
 7  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 8  
 import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
 9  
 import org.kuali.student.common.ui.client.configurable.mvc.views.VerticalSectionView;
 10  
 import org.kuali.student.common.ui.client.mvc.*;
 11  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 12  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract;
 13  
 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations;
 14  
 import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog;
 15  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.ActionCancelGroup;
 16  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton;
 17  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.LabelPanel;
 18  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 19  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType;
 20  
 import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
 21  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 22  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 23  
 import org.kuali.student.core.statement.dto.StatementTypeInfo;
 24  
 import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil;
 25  
 import org.kuali.student.core.statement.ui.client.widgets.rules.SubrulePreviewWidget;
 26  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 27  
 import org.kuali.student.lum.common.client.widgets.CluSetDetailsWidget;
 28  
 import org.kuali.student.lum.common.client.widgets.CluSetRetriever;
 29  
 import org.kuali.student.lum.common.client.widgets.CluSetRetrieverImpl;
 30  
 import org.kuali.student.lum.lu.ui.course.client.configuration.AbstractCourseConfigurer;
 31  
 import org.kuali.student.lum.lu.ui.course.client.configuration.CourseProposalConfigurer;
 32  
 import org.kuali.student.lum.lu.ui.course.client.controllers.CourseProposalController;
 33  
 
 34  
 import com.google.gwt.core.client.GWT;
 35  
 import com.google.gwt.event.dom.client.ClickEvent;
 36  
 import com.google.gwt.event.dom.client.ClickHandler;
 37  
 import com.google.gwt.user.client.Window;
 38  
 import com.google.gwt.user.client.ui.FlowPanel;
 39  
 import com.google.gwt.user.client.ui.HorizontalPanel;
 40  
 import com.google.gwt.user.client.ui.VerticalPanel;
 41  
 import com.google.gwt.user.client.ui.Widget;
 42  
 
 43  0
 public class CourseRequirementsSummaryView extends VerticalSectionView {
 44  
 
 45  0
     private static CluSetRetriever cluSetRetriever = new CluSetRetrieverImpl();
 46  
 
 47  
     //view's widgets
 48  0
     private FlowPanel layout = new FlowPanel();
 49  0
     private ActionCancelGroup actionCancelButtons = new ActionCancelGroup(ButtonEnumerations.SaveContinueCancelEnum.SAVE_CONTINUE, ButtonEnumerations.SaveContinueCancelEnum.CANCEL);
 50  0
     private Map<String, Widget> addButtonsList = new HashMap<String, Widget>();
 51  
 
 52  
     //view's data
 53  
     private CourseRequirementsViewController parentController;
 54  
     private CourseRequirementsDataModel rules;
 55  
     private boolean isReadOnly;
 56  
     private boolean showSaveButtons;
 57  0
     private static int tempProgReqInfoID = 9999;
 58  
     public static final String NEW_STMT_TREE_ID = "NEWSTMTTREE";
 59  
     public static final String NEW_REQ_COMP_ID = "NEWREQCOMP";    
 60  
 
 61  0
     private Map<String, SpanPanel> perCourseRequisiteTypePanel = new LinkedHashMap<String, SpanPanel>();
 62  
 
 63  
     public CourseRequirementsSummaryView(final CourseRequirementsViewController parentController, Enum<?> viewEnum, String name,
 64  
                                                             String modelId, CourseRequirementsDataModel rulesData, boolean isReadOnly, boolean showSaveButtons) {
 65  0
         super(viewEnum, name, modelId);
 66  0
         this.parentController = parentController;
 67  0
         rules = rulesData;
 68  0
         rules.setInitialized(false);
 69  0
         this.isReadOnly = isReadOnly;
 70  0
         this.showSaveButtons = showSaveButtons;
 71  0
         if (!isReadOnly && showSaveButtons) {
 72  0
             setupSaveCancelButtons();
 73  
         }
 74  0
     }
 75  
 
 76  
     @Override
 77  
     public boolean isDirty() {
 78  0
         return rules.isDirty();
 79  
     }
 80  
 
 81  
     @Override
 82  
     public void beforeShow(final Callback<Boolean> onReadyCallback) {
 83  
 
 84  
         //only when user wants to see rules then load requirements from database if they haven't been loaded yet
 85  0
         if (!rules.isInitialized()) {
 86  0
             rules.retrieveCourseRequirements(AbstractCourseConfigurer.COURSE_PROPOSAL_MODEL, new Callback<Boolean>() {
 87  
                 @Override
 88  
                 public void exec(Boolean result) {
 89  0
                     if (result) {
 90  0
                         displayRules();
 91  
                     }
 92  0
                     onReadyCallback.exec(result);
 93  0
                 }
 94  
             });
 95  0
             return;
 96  
         }
 97  
 
 98  
         //for read-only view, we don't need to worry about rules being added or modified
 99  0
         if (isReadOnly) {
 100  0
             displayRules();            
 101  0
             onReadyCallback.exec(true);
 102  0
             return;
 103  
         }
 104  
 
 105  
         //see if we need to update a rule if user is returning from rule manage screen
 106  0
         parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){
 107  
                         @Override
 108  
                         public void exec(View result) {
 109  0
                                 CourseRequirementsManageView manageView = (CourseRequirementsManageView) result;
 110  
                 
 111  
                                 //return if user did not added or updated a rule
 112  0
                 if (!manageView.isDirty() || !manageView.isUserClickedSaveButton()) {
 113  0
                     rules.removeEmptyRules();
 114  0
                     onReadyCallback.exec(true);
 115  0
                     return;
 116  
                 }
 117  
 
 118  
                 //update the rule because user added or edited the rule
 119  0
                 ((SectionView)parentController.getCurrentView()).setIsDirty(false);
 120  0
                 manageView.setUserClickedSaveButton(false);
 121  
 
 122  
                 //if rule storage updated successfully, update the display as well
 123  0
                 StatementTreeViewInfo affectedRule = rules.updateRules(manageView.getRuleTree(), manageView.getInternalCourseReqID(), manageView.isNewRule());
 124  0
                 updateRequirementWidgets(affectedRule);
 125  
                 
 126  0
                         onReadyCallback.exec(true);
 127  0
                         }
 128  
                 });        
 129  0
     }
 130  
 
 131  
     public void revertRuleChanges() {
 132  0
         rules.revertRuleChanges();
 133  0
         displayRules();
 134  0
     }
 135  
 
 136  
     private void updateRequirementWidgets(StatementTreeViewInfo rule) {
 137  0
         if (rule != null) {
 138  0
             StatementTypeInfo affectedStatementTypeInfo = rules.getStmtTypeInfo(rule.getType());
 139  0
             SpanPanel reqPanel = perCourseRequisiteTypePanel.get(affectedStatementTypeInfo.getId());
 140  
 
 141  
             //don't show 'add rule' button if we have one rule already
 142  0
             addButtonsList.get(rule.getType()).setVisible(false);
 143  
 
 144  
             //if this is a new rule then add it to the panel
 145  0
             if (reqPanel.getWidgetCount() == 0) {
 146  0
                 SubrulePreviewWidget newRulePreviewWidget = addCourseRequisite(reqPanel, rule);
 147  0
                 reqPanel.add(newRulePreviewWidget);
 148  0
                 return;
 149  
             }
 150  
 
 151  0
             for (int i = 0; i < reqPanel.getWidgetCount(); i++) {
 152  0
                 SubrulePreviewWidget subrulePreviewWidget = (SubrulePreviewWidget)reqPanel.getWidget(i);
 153  0
                 SubrulePreviewWidget newRulePreviewWidget = addCourseRequisite(reqPanel, rule);
 154  0
                 reqPanel.insert(newRulePreviewWidget, i);
 155  0
                 reqPanel.remove(subrulePreviewWidget);
 156  0
                 break; //there should be only one rule per requisite type
 157  
             }
 158  
         }
 159  0
     }    
 160  
 
 161  
     public void displayRules() {       
 162  0
         remove(layout);
 163  0
         layout.clear();
 164  
 
 165  
         //display 'Course Requirements' page title (don't add if read only because the section itself will display the title)
 166  0
         if (!isReadOnly) {
 167  0
             SectionTitle pageTitle = SectionTitle.generateH2Title("Course Requisites");
 168  0
             pageTitle.addStyleName("ks-layout-header");  //make the header orange
 169  0
             layout.add(pageTitle);
 170  
         }
 171  
 
 172  
         //iterate and display rules for each Course Requirement type e.g. Pre-Requisites, Co-Requisites, Anti-Requisites
 173  0
         boolean firstSubHeader = true;
 174  0
         for (StatementTypeInfo stmtTypeInfo : rules.getStmtTypes()) {
 175  
 
 176  
             //Show only headers for top statement types
 177  0
             if (isTopStatement(stmtTypeInfo)) {
 178  0
                 SectionTitle title = SectionTitle.generateH3Title(stmtTypeInfo.getName());
 179  0
                 title.addStyleName("KS-Course-Requisites-Top-Stmt-Header");
 180  0
                 layout.add(title);
 181  0
                 firstSubHeader = true;
 182  0
                 continue;
 183  
             }
 184  
 
 185  
             //create and display one type of Course Requisites section
 186  0
             SpanPanel requirementsPanel = new SpanPanel();
 187  0
             perCourseRequisiteTypePanel.put(stmtTypeInfo.getId(), requirementsPanel);
 188  0
             displayRequirementSectionForGivenType(requirementsPanel, stmtTypeInfo, firstSubHeader);
 189  0
             firstSubHeader = false;
 190  
 
 191  
             //now display each requirement for this Course Requisites type; should be only one for courses
 192  0
             for (StatementTreeViewInfo ruleInfo : rules.getCourseReqInfo(stmtTypeInfo.getId())) {
 193  0
                 SubrulePreviewWidget subrulePreviewWidget = addCourseRequisite(requirementsPanel, ruleInfo);
 194  0
                 requirementsPanel.add(subrulePreviewWidget);
 195  0
             }
 196  0
         }
 197  
 
 198  
         //save and cancel buttons
 199  0
         if (!isReadOnly && showSaveButtons) {
 200  0
             layout.add(actionCancelButtons);
 201  
         }
 202  
 
 203  0
         addWidget(layout);
 204  0
     }
 205  
 
 206  
     private void displayRequirementSectionForGivenType(final SpanPanel requirementsPanel, final StatementTypeInfo stmtTypeInfo, boolean firstSubHeader) {
 207  
 
 208  
         //display header for this Course Requisites type e.g. Enrollment Eligibility
 209  0
         SectionTitle title = SectionTitle.generateH3Title(stmtTypeInfo.getName());
 210  0
         title.setStyleName((firstSubHeader ? "KS-Course-Requisites-Preview-Rule-Type-First-Header" : "KS-Course-Requisites-Preview-Rule-Type-Header"));
 211  0
         layout.add(title);
 212  
 
 213  0
         LabelPanel labelExamples = new LabelPanel(stmtTypeInfo.getDescr());
 214  0
         labelExamples.getElement().setAttribute("style", "font-weight: normal; width: 80%;");
 215  
         
 216  0
         String examplesHtml = Application.getApplicationContext().getMessage(stmtTypeInfo.getId());
 217  
         
 218  0
         if (examplesHtml != null && !examplesHtml.equals("")) {
 219  0
                 AbbrButton examples = new AbbrButton(AbbrButtonType.EXAMPLES);
 220  0
                 examples.setVisible(true);
 221  0
                 examples.setHoverHTML(examplesHtml);
 222  0
                 examples.getHoverPopup().addStyleName("ks-example-popup");
 223  0
                 labelExamples.add(examples);
 224  
         }
 225  
         
 226  0
         HorizontalPanel spacer0 = new HorizontalPanel();
 227  0
         spacer0.addStyleName("KS-Course-Requisites-Button-Spacer");
 228  0
         labelExamples.add(spacer0);
 229  
  
 230  0
         layout.add(labelExamples);
 231  
         
 232  
         //display "Add Rule" button if user is in 'edit' mode OR a rule is already defined
 233  0
         final String stmtId = stmtTypeInfo.getId();
 234  0
         if (!isReadOnly) { // && rules.getCourseReqInfo(stmtId).isEmpty()) {
 235  0
             KSButton addCourseReqButton = new KSButton("Add " + stmtTypeInfo.getName(), KSButtonAbstract.ButtonStyle.FORM_SMALL);
 236  0
             addCourseReqButton.addClickHandler(new ClickHandler(){
 237  
                 public void onClick(ClickEvent event) {
 238  
 
 239  0
                     storeRules(false, new Callback<Boolean>() {
 240  
                         @Override
 241  
                         public void exec(Boolean courseExists) {
 242  0
                             if (courseExists) {
 243  0
                                 final StatementTreeViewInfo newRule = new StatementTreeViewInfo();
 244  0
                                 newRule.setId(generateStatementTreeId());
 245  0
                                 newRule.setType(stmtId);
 246  0
                                 RichTextInfo text = new RichTextInfo();
 247  0
                                 text.setPlain("");
 248  0
                                 newRule.setDesc(text);
 249  0
                                 parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){
 250  
                                     @Override
 251  
                                     public void exec(View result) {
 252  0
                                         rules.addRule(newRule);
 253  0
                                         ((CourseRequirementsManageView) result).setRuleTree(newRule, true, rules.getInternalCourseReqID(newRule));
 254  0
                                         parentController.showView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE);
 255  0
                                     }
 256  
                                 });
 257  
                             }
 258  0
                         }
 259  
                     });
 260  0
                 };
 261  
             });
 262  0
             addCourseReqButton.setVisible(rules.getCourseReqInfo(stmtId).isEmpty());
 263  0
             addButtonsList.put(stmtId, addCourseReqButton);
 264  0
             layout.add(addCourseReqButton);
 265  0
             VerticalPanel spacer = new VerticalPanel();
 266  0
             spacer.addStyleName("KS-Course-Requisites-Button-Spacer");            
 267  0
             layout.add(spacer);
 268  
         }
 269  
 
 270  0
         layout.add(requirementsPanel);
 271  0
     }
 272  
 
 273  
     private SubrulePreviewWidget addCourseRequisite(final SpanPanel requirementsPanel,final StatementTreeViewInfo rule) {
 274  
 
 275  0
         Integer internalProgReqID =  rules.getInternalCourseReqID(rule);
 276  0
         String stmtTypeId = rule.getType();
 277  
         
 278  0
         final SubrulePreviewWidget rulePreviewWidget = new SubrulePreviewWidget(rule, isReadOnly, getCluSetWidgetList(rule));
 279  
 
 280  0
         addRulePreviewWidgetHandlers(requirementsPanel, rulePreviewWidget, stmtTypeId, internalProgReqID);
 281  0
         return rulePreviewWidget;
 282  
     }
 283  
 
 284  
     private void addRulePreviewWidgetHandlers(final SpanPanel requirementsPanel, final SubrulePreviewWidget subRuleWidget, final String stmtTypeId, final Integer internalProgReqID) {
 285  
 
 286  0
         subRuleWidget.addEditButtonClickHandler(new ClickHandler() {
 287  
             public void onClick(ClickEvent event) {
 288  0
                 parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){
 289  
                     @Override
 290  
                     public void exec(View result) {
 291  0
                         ((CourseRequirementsManageView) result).setRuleTree(rules.getRule(internalProgReqID), false, internalProgReqID);
 292  0
                         parentController.showView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE);
 293  0
                     }
 294  
                 });                
 295  0
             }
 296  
         });
 297  
                 
 298  0
         subRuleWidget.addDeleteButtonClickHandler(new ClickHandler() {
 299  
             public void onClick(ClickEvent event) {
 300  
                 //deleting subrule does not delete the requirement (rule) itself
 301  0
                 rules.markRuleAsEdited(internalProgReqID);
 302  
                 //remove rule from display and storage
 303  0
                 rules.deleteRule(internalProgReqID);
 304  0
                 requirementsPanel.remove(subRuleWidget);
 305  0
                 addButtonsList.get(stmtTypeId).setVisible(true);
 306  0
             }
 307  
         });                   
 308  0
     }
 309  
 
 310  
     static public boolean isTopStatement(StatementTypeInfo stmtInfo) {
 311  0
         return ((stmtInfo.getAllowedStatementTypes() != null) && !stmtInfo.getAllowedStatementTypes().isEmpty());
 312  
     }
 313  
 
 314  
     static public Map<String, Widget> getCluSetWidgetList(StatementTreeViewInfo rule) {
 315  0
         Map<String, Widget> widgetList = new HashMap<String, Widget>();
 316  0
         Set<String> cluSetIds = new HashSet<String>();
 317  0
         findCluSetIds(rule, cluSetIds);
 318  0
         for (String clusetId : cluSetIds) {
 319  0
             widgetList.put(clusetId, new CluSetDetailsWidget(clusetId, cluSetRetriever));
 320  
         }
 321  
 
 322  0
         return widgetList;
 323  
     }
 324  
 
 325  
     static private void findCluSetIds(StatementTreeViewInfo rule, Set<String> list) {
 326  
 
 327  0
         List<StatementTreeViewInfo> statements = rule.getStatements();
 328  0
         List<ReqComponentInfo> reqComponentInfos = rule.getReqComponents();
 329  
 
 330  0
         if ((statements != null) && (statements.size() > 0)) {
 331  
             // retrieve all statements
 332  0
             for (StatementTreeViewInfo statement : statements) {
 333  0
                 findCluSetIds(statement, list); // inside set the children of this statementTreeViewInfo
 334  
             }
 335  0
         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
 336  
             // retrieve all req. component LEAFS
 337  0
             for (ReqComponentInfo reqComponent : reqComponentInfos) {
 338  0
                 List<ReqCompFieldInfo> fieldInfos = reqComponent.getReqCompFields();
 339  0
                 for (ReqCompFieldInfo fieldInfo : fieldInfos) {
 340  0
                     if (RulesUtil.isCluSetWidget(fieldInfo.getType())) {
 341  0
                         list.add(fieldInfo.getValue());
 342  
                     }
 343  
                 }
 344  0
             }
 345  
         }
 346  0
     }
 347  
 
 348  
     private void setupSaveCancelButtons() {
 349  0
         actionCancelButtons.addStyleName("KS-Course-Requisites-Save-Button");
 350  0
         actionCancelButtons.addCallback(new Callback<ButtonEnumerations.ButtonEnum>(){
 351  
              @Override
 352  
             public void exec(ButtonEnumerations.ButtonEnum result) {
 353  0
                 if (result == ButtonEnumerations.SaveContinueCancelEnum.SAVE_CONTINUE) {
 354  0
                     storeRules(true, new Callback<Boolean>() {
 355  
                         @Override
 356  
                         public void exec(Boolean result) {
 357  0
                             if (result) {
 358  0
                                 CourseProposalController courseController = ((CourseProposalController) parentController.getParentController());
 359  0
                                 courseController.showNextViewOnMenu();
 360  0
                                 courseController.getReqDataModel().retrieveStatementTypes(courseController.getCourseId(), Controller.NO_OP_CALLBACK);
 361  
                             }
 362  0
                         }
 363  
                     });                    
 364  
                 } else {
 365  0
                     if(! ((CourseProposalController)parentController.getController()).isNew()){
 366  0
                         (parentController.getController()).showView(CourseProposalConfigurer.CourseSections.SUMMARY);
 367  
                     }
 368  
                     else{
 369  0
                         Application.navigate(AppLocations.Locations.CURRICULUM_MANAGEMENT.getLocation());
 370  
                     }
 371  
                 }
 372  0
             }
 373  
         });
 374  0
     }
 375  
 
 376  
     public void storeRules(final boolean storeRules, final Callback<Boolean> callback) {
 377  0
         parentController.requestModel(CourseRequirementsViewController.COURSE_PROPOSAL_MODEL, new ModelRequestCallback() {
 378  
             @Override
 379  
             public void onRequestFail(Throwable cause) {
 380  0
                 Window.alert(cause.getMessage());
 381  0
                 GWT.log("Unable to retrieve model for course requirements view", cause);
 382  0
                 callback.exec(false);
 383  0
             }
 384  
             @Override
 385  
             public void onModelReady(Model model) {
 386  0
                 String courseId = ((DataModel)model).getRoot().get("id");
 387  0
                 String courseState = ((DataModel)model).getRoot().get("state");
 388  0
                 if (courseId == null) {
 389  0
                     final ConfirmationDialog dialog = new ConfirmationDialog("Submit Course Title", "Before saving rules please submit course proposal title");
 390  0
                     dialog.getConfirmButton().addClickHandler(new ClickHandler(){
 391  
                         @Override
 392  
                         public void onClick(ClickEvent event) {
 393  0
                             dialog.hide();
 394  0
                         }
 395  
                     });
 396  0
                     dialog.show();
 397  0
                     callback.exec(false);
 398  0
                 } else {
 399  0
                     if (storeRules) {
 400  0
                         rules.updateCourseRequisites(courseId, courseState, new Callback<List<StatementTreeViewInfo>>() {
 401  
                             @Override
 402  
                             public void exec(List<StatementTreeViewInfo> rules) {
 403  0
                                 for (StatementTreeViewInfo rule : rules) {
 404  0
                                     updateRequirementWidgets(rule);
 405  
                                 }
 406  0
                                 callback.exec(true);                                
 407  0
                             }
 408  
                         });
 409  
                     } else {
 410  0
                         callback.exec(true);                         
 411  
                     }
 412  
                 }
 413  0
             }
 414  
         });
 415  0
     }
 416  
     
 417  
     static public String generateStatementTreeId() {
 418  0
         return (NEW_STMT_TREE_ID + Integer.toString(tempProgReqInfoID++));
 419  
     }
 420  
 }