Coverage Report - org.kuali.student.lum.lu.ui.course.client.requirements.CourseRequirementsDataModel
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseRequirementsDataModel
0%
0/134
0%
0/80
2.568
CourseRequirementsDataModel$1
0%
0/8
N/A
2.568
CourseRequirementsDataModel$2
0%
0/9
0%
0/2
2.568
CourseRequirementsDataModel$3
0%
0/16
0%
0/6
2.568
CourseRequirementsDataModel$4
0%
0/3
N/A
2.568
CourseRequirementsDataModel$5
0%
0/30
0%
0/7
2.568
CourseRequirementsDataModel$6
0%
0/10
0%
0/2
2.568
CourseRequirementsDataModel$7
0%
0/1
N/A
2.568
CourseRequirementsDataModel$requirementState
0%
0/1
N/A
2.568
 
 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  
 package org.kuali.student.lum.lu.ui.course.client.requirements;
 16  
 
 17  
 import java.util.*;
 18  
 
 19  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 20  
 import org.kuali.student.common.ui.client.mvc.*;
 21  
 import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog;
 22  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotification;
 23  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
 24  
 import org.kuali.student.common.ui.client.widgets.rules.RulesUtil;
 25  
 import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
 26  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 27  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 28  
 import org.kuali.student.core.statement.dto.StatementTypeInfo;
 29  
 import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcService;
 30  
 import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcServiceAsync;
 31  
 import org.kuali.student.lum.program.client.rpc.StatementRpcService;
 32  
 import org.kuali.student.lum.program.client.rpc.StatementRpcServiceAsync;
 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  
 
 39  0
 public class CourseRequirementsDataModel {
 40  
 
 41  0
     private final CourseRpcServiceAsync courseRemoteService = GWT.create(CourseRpcService.class);
 42  0
     private final StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class);
 43  
     private Controller parentController;
 44  
 
 45  
     //keeping track of rules and rule state
 46  0
     public enum requirementState {STORED, ADDED, EDITED, DELETED;}
 47  0
     private Map<Integer, StatementTreeViewInfo> courseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>();
 48  0
     private Map<Integer, StatementTreeViewInfo> origCourseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>();
 49  0
     private Map<Integer, requirementState> courseReqState = new HashMap<Integer, requirementState>();
 50  0
     private Map<Integer, requirementState> origCourseReqState = new HashMap<Integer, requirementState>();
 51  0
     private List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>();
 52  0
     private boolean isInitialized = false;
 53  0
     private static Integer courseReqIDs = 111111;
 54  
 
 55  0
     public CourseRequirementsDataModel(Controller parentController) {
 56  0
         this.parentController = parentController;
 57  0
     }
 58  
 
 59  
     //retrieve rules based on IDs associated with this course
 60  
     public void retrieveCourseRequirements(String modelId, final Callback<Boolean> onReadyCallback) {
 61  
         //initialize data
 62  0
         courseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>();
 63  0
         origCourseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>();
 64  0
         courseReqState = new HashMap<Integer, requirementState>();
 65  0
         origCourseReqState = new HashMap<Integer, requirementState>();
 66  0
         stmtTypes = new ArrayList<StatementTypeInfo>();        
 67  0
         isInitialized = false;
 68  
 
 69  0
         parentController.requestModel(modelId, new ModelRequestCallback() {
 70  
 
 71  
             @Override
 72  
             public void onRequestFail(Throwable cause) {
 73  0
                 Window.alert(cause.getMessage());
 74  0
                 GWT.log("Unable to retrieve model for course requirements view", cause);
 75  0
                 onReadyCallback.exec(false);
 76  0
             }
 77  
 
 78  
             @Override
 79  
             public void onModelReady(Model model) {
 80  0
                 String courseId = ((DataModel)model).getRoot().get("id");
 81  0
                 retrieveStatementTypes(courseId, onReadyCallback);
 82  0
             }
 83  
         });    
 84  0
     }
 85  
 
 86  
     public void retrieveStatementTypes(final String courseId, final Callback<Boolean> onReadyCallback) {
 87  
 
 88  
         //retrieve available course requirement types
 89  0
         statementRpcServiceAsync.getStatementTypesForStatementTypeForCourse("kuali.statement.type.course", new KSAsyncCallback<List<StatementTypeInfo>>() {
 90  
             @Override
 91  
             public void handleFailure(Throwable caught) {
 92  0
                     Window.alert(caught.getMessage());
 93  0
                     GWT.log("getStatementTypes failed", caught);
 94  0
                 onReadyCallback.exec(false);
 95  0
             }
 96  
 
 97  
             @Override
 98  
             public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) {
 99  
                 //store the statement types
 100  0
                 for (StatementTypeInfo stmtInfoType : stmtInfoTypes) {
 101  0
                     stmtTypes.add(stmtInfoType);
 102  
                 }
 103  
 
 104  
                 //now retrieve the actual rules
 105  0
                 retrieveRules(courseId, onReadyCallback);
 106  0
             }
 107  
         });
 108  0
     }
 109  
 
 110  
     private void retrieveRules(String courseId, final Callback<Boolean> onReadyCallback) {
 111  
 
 112  
         //true if no course exists yet
 113  0
         if ((courseId == null) || courseId.isEmpty()) {
 114  0
             isInitialized = true;
 115  0
             onReadyCallback.exec(true);
 116  0
             return;
 117  
         }
 118  
 
 119  0
         courseRemoteService.getCourseStatements(courseId, CourseRequirementsManageView.RULEEDIT_TEMLATE,
 120  0
                                                     CourseRequirementsManageView.TEMLATE_LANGUAGE, new KSAsyncCallback<List<StatementTreeViewInfo>>() {
 121  
             @Override
 122  
             public void handleFailure(Throwable caught) {
 123  0
                 Window.alert(caught.getMessage());
 124  0
                 GWT.log("getCourseStatements failed", caught);
 125  0
                 onReadyCallback.exec(false);
 126  0
             }
 127  
 
 128  
             @Override
 129  
             public void onSuccess(List<StatementTreeViewInfo> foundRules) {
 130  
                 //update rules list with new course requirements
 131  0
                     if (foundRules != null) {
 132  0
                             for (StatementTreeViewInfo foundRule : foundRules) {
 133  
 
 134  0
                                     if (getStmtTypeInfo(foundRule.getType()) == null) {
 135  
                                             // Window.alert("Did not find corresponding statement type for course requirement of type: " + foundRule.getType());
 136  0
                                             GWT.log("Did not find corresponding statement type for course requirement of type: " + foundRule.getType(), null);
 137  
                                     }
 138  
 
 139  0
                                     origCourseReqInfos.put(courseReqIDs, RulesUtil.clone(foundRule));
 140  0
                                     origCourseReqState.put(courseReqIDs, requirementState.STORED);
 141  0
                                     courseReqInfos.put(courseReqIDs, foundRule);
 142  0
                                     courseReqState.put(courseReqIDs++, requirementState.STORED);
 143  
                             }
 144  
                     }
 145  
 
 146  0
                 isInitialized = true;
 147  0
                 onReadyCallback.exec(true);
 148  0
             }
 149  
         });     
 150  0
     }
 151  
 
 152  
     public StatementTreeViewInfo updateRules(StatementTreeViewInfo newSubRule, Integer internalCourseReqID, boolean isNewRule) {
 153  
 
 154  0
         StatementTreeViewInfo affectedRule = courseReqInfos.get(internalCourseReqID);
 155  
 
 156  0
         if (affectedRule == null) {
 157  0
             Window.alert("Cannot find course requisite with a statement that has id: '" + newSubRule.getId() + "'");
 158  0
             GWT.log("Cannot find course requisite with a statement that has id: '" + newSubRule.getId() + "'", null);
 159  0
             return null;
 160  
         }
 161  
 
 162  0
         if (courseReqState.get(internalCourseReqID) == CourseRequirementsDataModel.requirementState.STORED) {
 163  0
             courseReqState.put(internalCourseReqID, CourseRequirementsDataModel.requirementState.EDITED);
 164  
         }
 165  
 
 166  0
         courseReqInfos.put(internalCourseReqID, newSubRule);
 167  
 
 168  0
         return newSubRule;
 169  
     }
 170  
 
 171  
     public void updateCourseRequisites(final String courseId, final Callback<List<StatementTreeViewInfo>> callback) {
 172  
 
 173  
         //course proposal has to be in the database before we can save rules
 174  0
         if (courseId == null) {
 175  0
             final ConfirmationDialog dialog = new ConfirmationDialog("Submit Course Title", "Before saving rules please submit course proposal title");
 176  0
             dialog.getConfirmButton().addClickHandler(new ClickHandler(){
 177  
                 @Override
 178  
                 public void onClick(ClickEvent event) {
 179  0
                     dialog.hide();    
 180  0
                 }
 181  
             });
 182  0
             dialog.show();
 183  0
             return;
 184  
         }
 185  
 
 186  0
         final List<String> referencedProgReqIds = new ArrayList<String>();
 187  
 
 188  0
         courseRemoteService.storeCourseStatements(courseId.toString(), courseReqState, courseReqInfos, new KSAsyncCallback<Map<Integer, StatementTreeViewInfo>>() {
 189  
             @Override
 190  
             public void handleFailure(Throwable caught) {
 191  0
                 Window.alert(caught.getMessage());
 192  0
                 GWT.log("storeProgramRequirements failed", caught);
 193  0
             }
 194  
             @Override
 195  
             public void onSuccess(Map<Integer, StatementTreeViewInfo> storedRules) {
 196  
 
 197  0
                 for (Integer internalProgReqID : storedRules.keySet()) {
 198  0
                     StatementTreeViewInfo storedRule = storedRules.get(internalProgReqID);
 199  0
                     switch (courseReqState.get(internalProgReqID)) {
 200  
                         case STORED:
 201  
                             //rule was not changed so continue
 202  0
                             referencedProgReqIds.add(internalProgReqID.toString());
 203  0
                             break;
 204  
                         case ADDED:
 205  0
                             referencedProgReqIds.add(storedRule.getId());
 206  0
                             courseReqInfos.put(internalProgReqID, storedRule);
 207  0
                             origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule));
 208  0
                             origCourseReqState.put(internalProgReqID, requirementState.STORED);
 209  0
                             courseReqState.put(internalProgReqID, requirementState.STORED);
 210  0
                             break;
 211  
                         case EDITED:
 212  0
                             referencedProgReqIds.add(storedRule.getId());
 213  0
                             courseReqInfos.put(internalProgReqID, storedRule);
 214  0
                             origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule));
 215  0
                             origCourseReqState.put(internalProgReqID, requirementState.STORED);
 216  0
                             courseReqState.put(internalProgReqID, requirementState.STORED);
 217  0
                             break;
 218  
                         case DELETED:
 219  0
                             courseReqInfos.remove(internalProgReqID);
 220  0
                             origCourseReqInfos.remove(internalProgReqID);
 221  0
                             origCourseReqState.remove(internalProgReqID);
 222  0
                             courseReqState.remove(internalProgReqID);
 223  0
                             break;
 224  
                         default:
 225  
                             break;
 226  
                     }
 227  0
                 }
 228  
 
 229  0
                 KSNotifier.add(new KSNotification("Save Successful", false, 4000));
 230  
                 //MajorManager.getEventBus().fireEvent(new StoreRequirementIDsEvent(referencedProgReqIds));
 231  0
                 callback.exec(new ArrayList(storedRules.values()));  //update display widgets
 232  0
             }
 233  
         });        
 234  0
     }
 235  
 
 236  
     public static void stripStatementIds(StatementTreeViewInfo tree) {
 237  0
         List<StatementTreeViewInfo> statements = tree.getStatements();
 238  0
         List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
 239  
 
 240  0
         if ((tree.getId() != null) && (tree.getId().indexOf(CourseRequirementsSummaryView.NEW_STMT_TREE_ID)) >= 0) {
 241  0
             tree.setId(null);
 242  
         }
 243  0
         tree.setState("Active");
 244  
 
 245  0
         if ((statements != null) && (statements.size() > 0)) {
 246  
             // retrieve all statements
 247  0
             for (StatementTreeViewInfo statement : statements) {
 248  0
                 stripStatementIds(statement); // inside set the children of this statementTreeViewInfo
 249  
             }
 250  0
         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
 251  
             // retrieve all req. component LEAFS
 252  0
             for (ReqComponentInfo reqComponent : reqComponentInfos) {
 253  0
                 if ((reqComponent.getId() != null) && (reqComponent.getId().indexOf(CourseRequirementsSummaryView.NEW_REQ_COMP_ID) >= 0)) {
 254  0
                     reqComponent.setId(null);
 255  
                 }
 256  
 
 257  0
                 for (ReqCompFieldInfo field : reqComponent.getReqCompFields()) {
 258  0
                     field.setId(null);
 259  
                 }
 260  
 
 261  0
                 reqComponent.setState("Active");
 262  
             }
 263  
         }
 264  0
     }
 265  
 
 266  
     public List<StatementTreeViewInfo> getCourseReqInfo(String stmtTypeId) {
 267  0
         List<StatementTreeViewInfo> rules = new ArrayList<StatementTreeViewInfo>();
 268  0
         for(StatementTreeViewInfo rule : courseReqInfos.values()) {
 269  0
             if (rule.getType().equals(stmtTypeId)) {
 270  0
                 rules.add(rule);
 271  
             }
 272  
         }
 273  0
         return rules;
 274  
     }
 275  
 
 276  
     public Integer getInternalCourseReqID(StatementTreeViewInfo rule) {
 277  0
         for(Integer key : courseReqInfos.keySet()) {
 278  0
             if (courseReqInfos.get(key) ==  rule) {
 279  0
                 return key;
 280  
             }
 281  
         }
 282  
 
 283  0
         Window.alert("Problem retrieving key for course requisite: " +  rule.getId());
 284  0
         GWT.log("Problem retrieving key for course requisite: " +  rule.getId(), null);        
 285  
 
 286  0
         return null;
 287  
     }
 288  
 
 289  
     public StatementTypeInfo getStmtTypeInfo(String stmtTypeId) {
 290  0
         for (StatementTypeInfo stmtInfo : stmtTypes) {
 291  0
             if (stmtInfo.getId().equals(stmtTypeId)) {
 292  0
                 return stmtInfo;
 293  
             }
 294  
         }
 295  
 
 296  
         try {
 297  
 
 298  0
         Window.alert("Did not find StatementTypeInfo based on typeA: " + stmtTypeId + new Throwable().getStackTrace().toString());
 299  0
         GWT.log("Did not find StatementTypeInfo based on type: " + stmtTypeId + new Throwable().getStackTrace());
 300  
 
 301  
 
 302  0
         throw new Exception();
 303  
 
 304  0
         } catch (Exception e) {
 305  0
             Window.alert("Exception" + e.getStackTrace().toString());   
 306  
         }
 307  0
         return null;
 308  
     }
 309  
 
 310  
     public void deleteRule(Integer internalProgReqID) {
 311  0
         if (courseReqState.get(internalProgReqID) == requirementState.ADDED) {
 312  
             //user added a rule, didn't save it and now wants to delete it
 313  0
             courseReqState.remove(internalProgReqID);
 314  0
             courseReqInfos.remove(internalProgReqID);
 315  
         } else {
 316  0
             markRuleAsDeleted(internalProgReqID);
 317  
         }
 318  0
     }
 319  
 
 320  
     public void addRule(StatementTreeViewInfo rule) {
 321  0
         courseReqInfos.put(courseReqIDs, rule);
 322  0
         courseReqState.put(courseReqIDs++, requirementState.ADDED);
 323  0
     }
 324  
 
 325  
     public void updateRule(Integer internalProgReqID, StatementTreeViewInfo rule) {
 326  0
         courseReqInfos.put(internalProgReqID, rule);
 327  0
         markRuleAsEdited(internalProgReqID);
 328  0
     }    
 329  
 
 330  
     public void markRuleAsDeleted(Integer internalCourseReqID) {
 331  0
         if ((courseReqState.get(internalCourseReqID) == requirementState.STORED) ||
 332  
             (courseReqState.get(internalCourseReqID) == requirementState.EDITED)) {
 333  0
             courseReqState.put(internalCourseReqID, requirementState.DELETED);
 334  
         }
 335  0
     }
 336  
 
 337  
     public void markRuleAsEdited(Integer internalCourseReqID) {
 338  0
         if (courseReqState.get(internalCourseReqID) == requirementState.STORED) {
 339  0
             courseReqState.put(internalCourseReqID, requirementState.EDITED);
 340  
         }
 341  0
     }
 342  
 
 343  
     public String getStmtTypeName(String stmtTypeId) {
 344  0
         String name = getStmtTypeInfo(stmtTypeId).getName();
 345  0
         return (name == null ? "" : name);
 346  
     }
 347  
     
 348  
     public boolean isRuleExists(String stmtTypeId) {
 349  0
         boolean showNoRuleText = true;
 350  0
         for(StatementTreeViewInfo ruleInfo : courseReqInfos.values()) {
 351  0
             if ((ruleInfo.getType().equals(stmtTypeId)) && (courseReqState.get(getInternalCourseReqID(ruleInfo)) != requirementState.DELETED)) {
 352  0
                 showNoRuleText = false;
 353  
             }
 354  
         }
 355  0
         return showNoRuleText;
 356  
     }
 357  
 
 358  
     public void removeEmptyRules() {
 359  0
         for(StatementTreeViewInfo rule : courseReqInfos.values()) {
 360  0
             if (isEmpty(rule)) {
 361  0
                 Integer ruleId = getInternalCourseReqID(rule);
 362  0
                 courseReqInfos.remove(ruleId);
 363  0
                 courseReqState.remove(ruleId);
 364  0
             }
 365  
         }
 366  0
     }
 367  
 
 368  
     public boolean isDirty() {
 369  
 
 370  0
         if (courseReqState.size() != origCourseReqState.size()) {
 371  0
             return true;
 372  
         }
 373  
 
 374  0
         for(Integer key : courseReqState.keySet()) {
 375  0
             if (!courseReqState.get(key).equals(origCourseReqState.get(key))) {
 376  0
                 return true;
 377  
             }
 378  
         }
 379  
 
 380  0
         return false;
 381  
     }
 382  
 
 383  
     public void revertRuleChanges() {
 384  0
         courseReqInfos = new HashMap<Integer, StatementTreeViewInfo>();
 385  0
         courseReqState = new HashMap<Integer, requirementState>();
 386  0
         for(Integer key : origCourseReqInfos.keySet()) {
 387  0
             courseReqInfos.put(key, RulesUtil.clone(origCourseReqInfos.get(key)));
 388  0
             courseReqState.put(key, origCourseReqState.get(key));
 389  
         }
 390  0
     }
 391  
     
 392  
     public StatementTreeViewInfo getRule(Integer internalCourseReqID) {
 393  0
         return courseReqInfos.get(internalCourseReqID);
 394  
     }
 395  
 
 396  
     public static void getStatementTypes(final Callback<List<StatementTypeInfo>> onReadyCallback) {
 397  
 
 398  0
         StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class);
 399  
 
 400  
         //retrieve available course requirement types
 401  0
         statementRpcServiceAsync.getStatementTypesForStatementTypeForCourse("kuali.statement.type.course", new KSAsyncCallback<List<StatementTypeInfo>>() {
 402  
             @Override
 403  
             public void handleFailure(Throwable caught) {
 404  0
                     Window.alert(caught.getMessage());
 405  0
                     GWT.log("getStatementTypes failed", caught);
 406  0
                 onReadyCallback.exec(new ArrayList<StatementTypeInfo>());
 407  0
             }
 408  
 
 409  
             @Override
 410  
             public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) {
 411  
                 //store the statement types
 412  0
                 List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>();                
 413  0
                 for (StatementTypeInfo stmtInfoType : stmtInfoTypes) {
 414  0
                     stmtTypes.add(stmtInfoType);
 415  
                 }
 416  0
                 onReadyCallback.exec(stmtTypes);
 417  0
             }
 418  
         });
 419  0
     }
 420  
 
 421  
     public static boolean isEmpty(StatementTreeViewInfo rule) {
 422  0
         return (((rule.getStatements() == null) || rule.getStatements().isEmpty()) && ((rule.getReqComponents() == null) || rule.getReqComponents().isEmpty()));
 423  
     }
 424  
 
 425  
     public boolean isInitialized() {
 426  0
         return isInitialized;
 427  
     }
 428  
 
 429  
     public void setInitialized(boolean initialized) {
 430  0
         isInitialized = initialized;
 431  0
     }
 432  
 
 433  
     public List<StatementTypeInfo> getStmtTypes() {
 434  0
         return stmtTypes;
 435  
     }
 436  
 }