Coverage Report - org.kuali.student.lum.lu.ui.course.client.controllers.CourseProposalController
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseProposalController
0%
0/177
0%
0/68
2.028
CourseProposalController$1
0%
0/6
0%
0/2
2.028
CourseProposalController$1$1
0%
0/6
0%
0/4
2.028
CourseProposalController$10
0%
0/16
N/A
2.028
CourseProposalController$11
0%
0/9
0%
0/2
2.028
CourseProposalController$11$1
0%
0/9
0%
0/4
2.028
CourseProposalController$12
0%
0/5
N/A
2.028
CourseProposalController$13
0%
0/35
0%
0/14
2.028
CourseProposalController$14
0%
0/10
0%
0/2
2.028
CourseProposalController$15
0%
0/11
0%
0/6
2.028
CourseProposalController$15$1
0%
0/13
0%
0/4
2.028
CourseProposalController$15$1$1
0%
0/5
0%
0/2
2.028
CourseProposalController$15$1$2
0%
0/10
0%
0/2
2.028
CourseProposalController$16
0%
0/3
N/A
2.028
CourseProposalController$17
0%
0/3
N/A
2.028
CourseProposalController$18
0%
0/5
0%
0/2
2.028
CourseProposalController$19
0%
0/1
N/A
2.028
CourseProposalController$2
0%
0/5
0%
0/4
2.028
CourseProposalController$3
0%
0/3
N/A
2.028
CourseProposalController$4
0%
0/4
N/A
2.028
CourseProposalController$5
0%
0/19
0%
0/10
2.028
CourseProposalController$5$1
0%
0/14
N/A
2.028
CourseProposalController$6
0%
0/15
0%
0/10
2.028
CourseProposalController$7
0%
0/8
N/A
2.028
CourseProposalController$8
0%
0/10
N/A
2.028
CourseProposalController$8$1
0%
0/4
0%
0/2
2.028
CourseProposalController$9
0%
0/11
0%
0/2
2.028
 
 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.lum.lu.ui.course.client.controllers;
 17  
 
 18  
 import java.text.DateFormat;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Date;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.kuali.student.common.assembly.data.Data;
 26  
 import org.kuali.student.common.assembly.data.Metadata;
 27  
 import org.kuali.student.common.assembly.data.QueryPath;
 28  
 import org.kuali.student.common.rice.StudentIdentityConstants;
 29  
 import org.kuali.student.common.rice.authorization.PermissionType;
 30  
 import org.kuali.student.common.ui.client.application.Application;
 31  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 32  
 import org.kuali.student.common.ui.client.application.ViewContext;
 33  
 import org.kuali.student.common.ui.client.configurable.mvc.layouts.MenuEditableSectionController;
 34  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
 35  
 import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
 36  
 import org.kuali.student.common.ui.client.event.ActionEvent;
 37  
 import org.kuali.student.common.ui.client.event.ContentDirtyEvent;
 38  
 import org.kuali.student.common.ui.client.event.ContentDirtyEventHandler;
 39  
 import org.kuali.student.common.ui.client.event.SaveActionEvent;
 40  
 import org.kuali.student.common.ui.client.event.SaveActionHandler;
 41  
 import org.kuali.student.common.ui.client.mvc.ActionCompleteCallback;
 42  
 import org.kuali.student.common.ui.client.mvc.Callback;
 43  
 import org.kuali.student.common.ui.client.mvc.Controller;
 44  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 45  
 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
 46  
 import org.kuali.student.common.ui.client.mvc.HasCrossConstraints;
 47  
 import org.kuali.student.common.ui.client.mvc.ModelProvider;
 48  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 49  
 import org.kuali.student.common.ui.client.mvc.View;
 50  
 import org.kuali.student.common.ui.client.mvc.WorkQueue;
 51  
 import org.kuali.student.common.ui.client.mvc.WorkQueue.WorkItem;
 52  
 import org.kuali.student.common.ui.client.mvc.dto.ReferenceModel;
 53  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 54  
 import org.kuali.student.common.ui.client.security.AuthorizationCallback;
 55  
 import org.kuali.student.common.ui.client.security.RequiresAuthorization;
 56  
 import org.kuali.student.common.ui.client.service.DataSaveResult;
 57  
 import org.kuali.student.common.ui.client.util.WindowTitleUtils;
 58  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 59  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 60  
 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.YesNoCancelEnum;
 61  
 import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog;
 62  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup;
 63  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup;
 64  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotification;
 65  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
 66  
 import org.kuali.student.common.ui.client.widgets.progress.BlockingTask;
 67  
 import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator;
 68  
 import org.kuali.student.common.ui.shared.IdAttributes;
 69  
 import org.kuali.student.common.ui.shared.IdAttributes.IdType;
 70  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 71  
 import org.kuali.student.core.statement.dto.StatementTypeInfo;
 72  
 import org.kuali.student.core.workflow.ui.client.widgets.WorkflowEnhancedNavController;
 73  
 import org.kuali.student.core.workflow.ui.client.widgets.WorkflowUtilities;
 74  
 import org.kuali.student.lum.common.client.helpers.RecentlyViewedHelper;
 75  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 76  
 import org.kuali.student.lum.lu.ui.course.client.configuration.CourseConfigurer;
 77  
 import org.kuali.student.lum.lu.ui.course.client.requirements.CourseRequirementsDataModel;
 78  
 import org.kuali.student.lum.lu.ui.course.client.requirements.HasRequirements;
 79  
 import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcService;
 80  
 import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcServiceAsync;
 81  
 import org.kuali.student.lum.lu.ui.course.client.service.CreditCourseProposalRpcService;
 82  
 import org.kuali.student.lum.lu.ui.course.client.service.CreditCourseProposalRpcServiceAsync;
 83  
 
 84  
 import com.google.gwt.core.client.GWT;
 85  
 import com.google.gwt.event.dom.client.ClickEvent;
 86  
 import com.google.gwt.event.dom.client.ClickHandler;
 87  
 import com.google.gwt.user.client.Window;
 88  
 import com.google.gwt.user.client.rpc.AsyncCallback;
 89  
 
 90  
 /**
 91  
  * Controller for course proposal screens.  This controller controls all functions of the course proposal process
 92  
  * and contains the data model and is responsible for retrieving its data and metadata from the server. In
 93  
  * addition, this controller is responsible for course proposal save events and updating its ui accordingly.
 94  
  * 
 95  
  *
 96  
  * @author Kuali Student Team
 97  
  *
 98  
  */
 99  
 
 100  0
 public class CourseProposalController extends MenuEditableSectionController implements RequiresAuthorization, WorkflowEnhancedNavController, HasRequirements {
 101  
 
 102  
         //RPC Services
 103  0
         CreditCourseProposalRpcServiceAsync cluProposalRpcServiceAsync = GWT.create(CreditCourseProposalRpcService.class);
 104  0
         CourseRpcServiceAsync courseServiceAsync = GWT.create(CourseRpcService.class);
 105  
         //Models
 106  0
         private final DataModel cluProposalModel = new DataModel("Proposal");
 107  0
         private final DataModel comparisonModel = new DataModel("Original Course");
 108  
 
 109  0
         CourseConfigurer cfg = GWT.create(CourseConfigurer.class);
 110  
         
 111  
         private WorkQueue modelRequestQueue;
 112  
 
 113  
     private WorkflowUtilities workflowUtil;
 114  
 
 115  0
         private boolean initialized = false;
 116  0
         private boolean isNew = false;
 117  
 
 118  
         private static final String UPDATED_KEY = "metaInfo/updateTime";
 119  
         private static final String VERSION_KEY  = "versionInfo/versionedFromId";
 120  
         private static final String MODIFY_TYPE = "kuali.proposal.type.course.modify";
 121  
         public static final String CREATE_TYPE = "kuali.proposal.type.course.create";
 122  
         public static final String INITIAL_SAVE_VERSION = "1";
 123  0
         private String currentDocType = CREATE_TYPE;
 124  0
         private String proposalPath = "";
 125  
         private String currentTitle;
 126  
 
 127  0
         private final DateFormat df = DateFormat.getInstance();
 128  
 
 129  0
         private final BlockingTask initializingTask = new BlockingTask("Loading");
 130  0
         private final BlockingTask loadDataTask = new BlockingTask("Retrieving Data");
 131  0
         private final BlockingTask saving = new BlockingTask("Saving");
 132  
     final CourseRequirementsDataModel reqDataModel;
 133  
    
 134  
     public CourseProposalController(){
 135  0
         super();
 136  0
         reqDataModel = new CourseRequirementsDataModel(this);
 137  0
         initialize();
 138  0
         addStyleName("courseProposal");
 139  0
     }
 140  
 
 141  
     @Override
 142  
     public void setViewContext(ViewContext viewContext) {
 143  0
             super.setViewContext(viewContext);
 144  0
             if(viewContext.getId() != null && !viewContext.getId().isEmpty()){
 145  0
                     if(viewContext.getIdType() != IdType.COPY_OF_OBJECT_ID){
 146  0
                             viewContext.setPermissionType(PermissionType.OPEN);
 147  
                     }
 148  
                     else{
 149  
                             //they are trying to make a modification
 150  0
                             viewContext.setPermissionType(PermissionType.INITIATE);
 151  
                     }
 152  
             }
 153  
             else{
 154  0
                     viewContext.setPermissionType(PermissionType.INITIATE);
 155  
             }
 156  0
     }
 157  
 
 158  
     private void initialize() {
 159  
             //TODO get from messages
 160  0
                    proposalPath = cfg.getProposalPath();
 161  0
                    workflowUtil = new WorkflowUtilities(CourseProposalController.this ,proposalPath);
 162  
 
 163  0
                    super.setDefaultModelId(cfg.getModelId());
 164  0
         super.registerModel(cfg.getModelId(), new ModelProvider<DataModel>() {
 165  
 
 166  
             @Override
 167  
             public void requestModel(final ModelRequestCallback<DataModel> callback) {
 168  0
                 if (modelRequestQueue == null){
 169  0
                     modelRequestQueue = new WorkQueue();
 170  
                 }
 171  
 
 172  0
                 WorkItem workItem = new WorkItem(){
 173  
                     @Override
 174  
                     public void exec(Callback<Boolean> workCompleteCallback) {
 175  0
                         if (cluProposalModel.getRoot() == null || initialized == false){
 176  0
                             initModel(callback, workCompleteCallback);
 177  
                         } else {
 178  0
                             callback.onModelReady(cluProposalModel);
 179  0
                             workCompleteCallback.exec(true);
 180  
                         }
 181  0
                     }
 182  
                 };
 183  0
                 modelRequestQueue.submit(workItem);
 184  
 
 185  0
             }
 186  
 
 187  
         });
 188  0
         super.registerModel("ComparisonModel", new ModelProvider<DataModel>() {
 189  
             @Override
 190  
             public void requestModel(final ModelRequestCallback<DataModel> callback) {
 191  0
                     if(comparisonModel.getRoot() != null && comparisonModel.getRoot().size() != 0){
 192  0
                             callback.onModelReady(comparisonModel);
 193  
                             
 194  
                     }
 195  
                     else{
 196  0
                             callback.onModelReady(null);
 197  
                     }
 198  
                 
 199  0
             }
 200  
         });
 201  0
         super.addApplicationEventHandler(ContentDirtyEvent.TYPE, new ContentDirtyEventHandler(){
 202  
                         public void onContentDirty(ContentDirtyEvent event) {
 203  0
                         setContentWarning("You have unsaved changes");                                
 204  0
                         }                
 205  
         });
 206  
 
 207  0
         addApplicationEventHandler(SaveActionEvent.TYPE, new SaveActionHandler(){
 208  
             public void doSave(SaveActionEvent saveAction) {
 209  0
                 GWT.log("CluProposalController received save action request.", null);
 210  0
                 doSaveAction(saveAction);
 211  0
             }
 212  
         });
 213  
 
 214  0
     }
 215  
 
 216  
     private void initModel(final ModelRequestCallback<DataModel> callback, Callback<Boolean> workCompleteCallback){
 217  0
             if(getViewContext().getIdType() == IdType.DOCUMENT_ID){
 218  0
             getCluProposalFromWorkflowId(callback, workCompleteCallback);
 219  0
         } else if (getViewContext().getIdType() == IdType.KS_KEW_OBJECT_ID){
 220  0
             getCluProposalFromProposalId(getViewContext().getId(), callback, workCompleteCallback);
 221  0
         } else if (getViewContext().getIdType() == IdType.COPY_OF_OBJECT_ID){
 222  0
             createModifyCluProposalModel("versionComment", callback, workCompleteCallback);
 223  
         } else{
 224  0
             createNewCluProposalModel(callback, workCompleteCallback);
 225  
         }
 226  0
     }
 227  
 
 228  
     private void getCurrentModel(final ModelRequestCallback<DataModel> callback, Callback<Boolean> workCompleteCallback){
 229  0
             if (cluProposalModel.getRoot() != null && cluProposalModel.getRoot().size() > 0){
 230  0
                 String id = cluProposalModel.get(cfg.getProposalPath()+"/id");
 231  0
                 if(id != null){
 232  0
                         getCluProposalFromProposalId(id, callback, workCompleteCallback);
 233  
                 }
 234  
                 else{
 235  0
                         initModel(callback, workCompleteCallback);
 236  
                 }
 237  0
             }
 238  
             else{
 239  0
                     initModel(callback, workCompleteCallback);
 240  
             }
 241  0
     }
 242  
 
 243  
     private void init(final Callback<Boolean> onReadyCallback) {
 244  0
             if (initialized) {
 245  0
                     onReadyCallback.exec(true);
 246  
             } else {
 247  0
                     initialized = true;
 248  0
                     KSBlockingProgressIndicator.addTask(initializingTask);
 249  0
                     setContentWarning("");
 250  0
             this.requestModel(new ModelRequestCallback<DataModel>(){
 251  
 
 252  
                                 @Override
 253  
                                 public void onModelReady(DataModel model) {
 254  
                                         //Setup View Context
 255  0
                                         String idType = null;
 256  0
                                     String viewContextId = "";
 257  0
                                     if(getViewContext().getIdType() != null){
 258  0
                                 idType = getViewContext().getIdType().toString();
 259  0
                                 viewContextId = getViewContext().getId();
 260  0
                                 if(getViewContext().getIdType()==IdAttributes.IdType.COPY_OF_OBJECT_ID){
 261  0
                                         viewContextId = null;
 262  
                                 }
 263  
 
 264  
                                     }
 265  0
                                         HashMap<String, String> idAttributes = new HashMap<String, String>();
 266  0
                                     if(idType != null){
 267  0
                                             idAttributes.put(IdAttributes.ID_TYPE, idType);
 268  
                                     }
 269  0
                                     if(cluProposalModel.get(VERSION_KEY) != null && !((String)cluProposalModel.get(VERSION_KEY)).equals("")){
 270  0
                                             currentDocType = MODIFY_TYPE;
 271  
                                     }
 272  0
                                     idAttributes.put(StudentIdentityConstants.DOCUMENT_TYPE_NAME, currentDocType);
 273  
 
 274  
                                     //Get metadata and complete initializing the screen
 275  0
                                     cluProposalRpcServiceAsync.getMetadata(viewContextId, idAttributes, new KSAsyncCallback<Metadata>(){
 276  
                                                 @Override
 277  
                         public void handleTimeout(Throwable caught) {
 278  0
                                         initializeFailed(); 
 279  0
                                                 }
 280  
 
 281  
                                                 @Override
 282  
                         public void handleFailure(Throwable caught) {
 283  0
                                                         initializeFailed();
 284  0
                                     throw new RuntimeException("Failed to get model definition.", caught);
 285  
                                 }
 286  
 
 287  
                                                 public void initializeFailed(){
 288  0
                                                 initialized = false;
 289  0
                                         onReadyCallback.exec(false);
 290  0
                                         KSBlockingProgressIndicator.removeTask(initializingTask);                                                        
 291  0
                                                 }
 292  
                                                 
 293  
                                 public void onSuccess(Metadata result) {
 294  0
                                         DataModelDefinition def = new DataModelDefinition(result);
 295  0
                                     cluProposalModel.setDefinition(def);
 296  0
                                     comparisonModel.setDefinition(def);
 297  
 
 298  0
                                     configureScreens(def, onReadyCallback);
 299  
 
 300  0
                                 }
 301  
                                   });
 302  
                                         
 303  0
                                 }
 304  
 
 305  
                                 @Override
 306  
                                 public void onRequestFail(Throwable cause) {
 307  0
                                         GWT.log("Failed to get modeld for proposal controller init");
 308  0
                                         onReadyCallback.exec(false);
 309  0
                                 }
 310  
                         });
 311  
                                 
 312  
             }
 313  0
     }
 314  
 
 315  
     private void configureScreens(final DataModelDefinition modelDefinition, final Callback<Boolean> onReadyCallback){
 316  0
         workflowUtil.requestAndSetupModel();
 317  
 
 318  0
         CourseRequirementsDataModel.getStatementTypes(new Callback<List<StatementTypeInfo>>() {
 319  
 
 320  
             @Override
 321  
             public void exec(List<StatementTypeInfo> stmtTypes) {
 322  0
                 List<StatementTypeInfo> stmtTypesOut = new ArrayList<StatementTypeInfo>();
 323  0
                 if (stmtTypes != null) {
 324  0
                     for (StatementTypeInfo stmtType : stmtTypes) {
 325  0
                         if (stmtType.getId().contains("kuali.statement.type.course.enrollmentEligibility") ||
 326  
                             stmtType.getId().contains("kuali.statement.type.course.creditConstraints")) {
 327  0
                             continue;
 328  
                         }
 329  0
                         stmtTypesOut.add(stmtType);
 330  
                     }
 331  
                 }
 332  
 
 333  0
                 cfg.setStatementTypes(stmtTypesOut);
 334  0
                 cfg.setModelDefinition(modelDefinition);
 335  0
                 cfg.configure(CourseProposalController.this);
 336  
                 
 337  
                 //Update any cross fields
 338  0
                 for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){
 339  0
                         crossConstraint.reprocessWithUpdatedConstraints();
 340  
                 }
 341  
                 
 342  0
                 onReadyCallback.exec(true);
 343  0
                 KSBlockingProgressIndicator.removeTask(initializingTask);
 344  0
             }
 345  
         });
 346  0
     }
 347  
 
 348  
     @Override
 349  
     @SuppressWarnings("unchecked")
 350  
     public void requestModel(Class modelType, final ModelRequestCallback callback) {
 351  0
         if(modelType == ReferenceModel.class){
 352  0
                 if (cluProposalModel != null){
 353  0
                         ReferenceModel ref = new ReferenceModel();
 354  
 
 355  0
                         if(cluProposalModel.get(cfg.getProposalPath()) != null){
 356  0
                             ref.setReferenceId((String)cluProposalModel.get(cfg.getProposalPath()+"/id"));
 357  
                         } else {
 358  0
                                 ref.setReferenceId(null);
 359  
                         }
 360  
                         
 361  
                         //Use the referenceAttribute to store misc data from the parent model like reference name, etc
 362  0
                         if(cluProposalModel.get(cfg.getProposalPath()) != null){
 363  0
                                 Map<String, String> attributes = new HashMap<String, String>();
 364  0
                                 attributes.put("name", (String)cluProposalModel.get(cfg.getProposalPath()+"/name"));
 365  0
                                 ref.setReferenceAttributes(attributes);
 366  0
                         } else {
 367  0
                                 ref.setReferenceAttributes(null);
 368  
                         }
 369  
 
 370  0
                         ref.setReferenceTypeKey(cfg.getProposalReferenceTypeKey());
 371  0
                         ref.setReferenceType(cfg.getProposalReferenceObjectType());
 372  0
                         ref.setReferenceState(getViewContext().getState());
 373  
 
 374  0
                         callback.onModelReady(ref);
 375  0
                 }
 376  0
         } else if (modelType == Data.class){
 377  0
                 requestModel(cfg.getModelId(), callback);
 378  
         } else {
 379  0
             super.requestModel(modelType, callback);
 380  
         }
 381  
 
 382  0
     }
 383  
 
 384  
     @SuppressWarnings("unchecked")
 385  
     private void getCluProposalFromWorkflowId(final ModelRequestCallback callback, final Callback<Boolean> workCompleteCallback){
 386  0
         KSBlockingProgressIndicator.addTask(loadDataTask);
 387  0
         workflowUtil.getDataIdFromWorkflowId(getViewContext().getId(), new KSAsyncCallback<String>(){
 388  
                         @Override
 389  
                         public void handleFailure(Throwable caught) {
 390  0
                 Window.alert("Error loading Proposal from Workflow Document: "+caught.getMessage());
 391  0
                 createNewCluProposalModel(callback, workCompleteCallback);
 392  0
                 KSBlockingProgressIndicator.removeTask(loadDataTask);
 393  0
                         }
 394  
 
 395  
                         @Override
 396  
                         public void onSuccess(String proposalId) {
 397  0
                                 KSBlockingProgressIndicator.removeTask(loadDataTask);
 398  0
                                 getCluProposalFromProposalId(proposalId, callback, workCompleteCallback);
 399  0
                         }
 400  
         });
 401  0
     }
 402  
 
 403  
     @SuppressWarnings("unchecked")
 404  
     private void getCluProposalFromProposalId(String id, final ModelRequestCallback callback, final Callback<Boolean> workCompleteCallback){
 405  0
             KSBlockingProgressIndicator.addTask(loadDataTask);
 406  0
             cluProposalRpcServiceAsync.getData(id, new KSAsyncCallback<Data>(){
 407  
 
 408  
                         @Override
 409  
                         public void handleFailure(Throwable caught) {
 410  0
                 Window.alert("Error loading Proposal: "+caught.getMessage());
 411  0
                 createNewCluProposalModel(callback, workCompleteCallback);
 412  0
                 KSBlockingProgressIndicator.removeTask(loadDataTask);
 413  0
                         }
 414  
 
 415  
                         @Override
 416  
                         public void onSuccess(Data result) {
 417  0
                                 cluProposalModel.setRoot(result);
 418  0
                         setProposalHeaderTitle();
 419  0
                         setLastUpdated();
 420  0
                 reqDataModel.retrieveStatementTypes(cluProposalModel.<String>get("id"), new Callback<Boolean>() {
 421  
                     @Override
 422  
                     public void exec(Boolean result) {
 423  0
                        if(result){
 424  0
                           getCourseComparisonModel(callback, workCompleteCallback);
 425  
                        }
 426  0
                     }
 427  
                 });
 428  0
                         }
 429  
 
 430  
             });
 431  0
     }
 432  
 
 433  
     @SuppressWarnings("unchecked")
 434  
         private void getCourseComparisonModel(final ModelRequestCallback proposalModelRequestCallback, final Callback<Boolean> workCompleteCallback){
 435  0
                 if(cluProposalModel.get(VERSION_KEY) != null && !((String)cluProposalModel.get(VERSION_KEY)).equals("")){
 436  0
                         courseServiceAsync.getData((String)cluProposalModel.get(VERSION_KEY), new KSAsyncCallback<Data>(){
 437  
         
 438  
                             @Override
 439  
                     public void handleFailure(Throwable caught) {
 440  0
                         Window.alert("Error loading Proposal: "+caught.getMessage());
 441  0
                         createNewCluProposalModel(proposalModelRequestCallback, workCompleteCallback);
 442  0
                         KSBlockingProgressIndicator.removeTask(loadDataTask);
 443  0
                     }
 444  
                             
 445  
                                 @Override
 446  
                                 public void onSuccess(Data result) {
 447  0
                                         if(result != null){
 448  0
                                                 comparisonModel.setRoot(result);
 449  
                                         }
 450  0
                                         proposalModelRequestCallback.onModelReady(cluProposalModel);
 451  0
                                         workCompleteCallback.exec(true);
 452  0
                                         KSBlockingProgressIndicator.removeTask(loadDataTask);
 453  0
                                 }
 454  
                         });
 455  
                 }
 456  
                 else{
 457  0
                         proposalModelRequestCallback.onModelReady(cluProposalModel);
 458  0
                         workCompleteCallback.exec(true);
 459  0
                         KSBlockingProgressIndicator.removeTask(loadDataTask);
 460  
                 }
 461  0
         }
 462  
 
 463  
     @SuppressWarnings("unchecked")
 464  
     private void createNewCluProposalModel(final ModelRequestCallback callback, final Callback<Boolean> workCompleteCallback){
 465  0
         cluProposalModel.setRoot(new Data());
 466  0
         isNew = true;
 467  0
         setProposalHeaderTitle();
 468  0
         setLastUpdated();
 469  0
         callback.onModelReady(cluProposalModel);
 470  0
         workCompleteCallback.exec(true);
 471  0
     }
 472  
 
 473  
     @SuppressWarnings("unchecked")
 474  
     private void createModifyCluProposalModel(String versionComment, final ModelRequestCallback callback, final Callback<Boolean> workCompleteCallback){
 475  0
         Data data = new Data();
 476  
         
 477  0
         Data proposalData = new Data();
 478  0
         proposalData.set(new Data.StringKey("type"), MODIFY_TYPE);
 479  0
         data.set(new Data.StringKey("proposal"), proposalData);
 480  
         
 481  0
         Data versionData = new Data();
 482  0
         versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId());
 483  0
         versionData.set(new Data.StringKey("versionComment"), versionComment);
 484  0
         data.set(new Data.StringKey("versionInfo"), versionData);
 485  
         
 486  0
         cluProposalModel.setRoot(data);
 487  0
         cluProposalRpcServiceAsync.saveData(cluProposalModel.getRoot(), new AsyncCallback<DataSaveResult>() {
 488  
                         public void onSuccess(DataSaveResult result) {
 489  0
                                 cluProposalModel.setRoot(result.getValue());
 490  0
                                 setProposalHeaderTitle();
 491  0
                         setLastUpdated();
 492  
                         //add to recently viewed now that we know the id of proposal
 493  0
                         ViewContext docContext = new ViewContext();
 494  0
                         docContext.setId((String) cluProposalModel.get(cfg.getProposalPath()+"/id"));
 495  0
                         docContext.setIdType(IdType.KS_KEW_OBJECT_ID);
 496  0
                         RecentlyViewedHelper.addDocument(getProposalTitle(), 
 497  
                                         HistoryManager.appendContext(AppLocations.Locations.COURSE_PROPOSAL.getLocation(), docContext)
 498  
                                         + "/SUMMARY");
 499  0
                         getCourseComparisonModel(callback, workCompleteCallback);
 500  
                         
 501  
                         // We need to update the current view context so that if the user clicks the back button it doesn't 
 502  
                         // create a duplicate course proposal. 
 503  0
                         getViewContext().setIdType(docContext.getIdType());
 504  0
                         getViewContext().setId(docContext.getId());
 505  
                         
 506  0
                         }
 507  
                         
 508  
                         public void onFailure(Throwable caught) {
 509  0
                 Window.alert("Error loading Proposal: "+caught.getMessage());
 510  0
                 createNewCluProposalModel(callback, workCompleteCallback);
 511  0
                 KSBlockingProgressIndicator.removeTask(loadDataTask);
 512  0
                         }
 513  
                 });
 514  0
     }
 515  
 
 516  
     public void doSaveAction(final SaveActionEvent saveActionEvent){
 517  0
         requestModel(new ModelRequestCallback<DataModel>() {
 518  
             @Override
 519  
             public void onModelReady(DataModel model) {
 520  0
                 CourseProposalController.this.updateModelFromCurrentView();
 521  
 
 522  0
                 if (isStartViewShowing()){
 523  
                         //This call required so fields in start section, which also appear in
 524  
                         //other sections don't get overridden from updateModel call above.
 525  0
                         getStartPopupView().updateModel();
 526  
                 }
 527  
 
 528  0
                     model.validate(new Callback<List<ValidationResultInfo>>() {
 529  
                     @Override
 530  
                     public void exec(List<ValidationResultInfo> result) {
 531  
 
 532  0
                             boolean isSectionValid = isValid(result, true);
 533  
 
 534  0
                             if(isSectionValid){
 535  0
                             if (startSectionRequired()){
 536  0
                                 showStartPopup(NO_OP_CALLBACK);
 537  0
                                 saveActionEvent.doActionComplete();
 538  
                             }
 539  
                             else{
 540  0
                                     saveProposalClu(saveActionEvent);
 541  
                             }
 542  
                             }
 543  
                             else{
 544  
                                     //saveActionEvent.doActionComplete();
 545  0
                                     KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000));
 546  
                             }
 547  
 
 548  0
                     }
 549  
                 });
 550  0
             }
 551  
 
 552  
             @Override
 553  
             public void onRequestFail(Throwable cause) {
 554  0
                     saveActionEvent.doActionComplete();
 555  0
                 GWT.log("Unable to retrieve model for validation and save", cause);
 556  0
             }
 557  
 
 558  
         });
 559  
 
 560  0
     }
 561  
 
 562  
     public boolean startSectionRequired(){
 563  0
         String proposalId = cluProposalModel.get(cfg.getProposalPath()+"/id");
 564  
         
 565  
         //Defaulting the proposalTitle to courseTitle, this way course data gets set and assembler doesn't
 566  
         //complain. This may not be the correct approach.
 567  0
         String proposalTitle = cluProposalModel.get(cfg.getProposalTitlePath());
 568  0
         String courseTitle = cluProposalModel.get(cfg.getCourseTitlePath());
 569  0
         if (proposalTitle == null || proposalTitle.isEmpty()){
 570  0
             cluProposalModel.set(QueryPath.parse(cfg.getProposalTitlePath()), courseTitle);
 571  
         }
 572  
         
 573  0
             return proposalId==null && !CourseProposalController.this.isStartViewShowing() && !hasTitles(proposalTitle, courseTitle);
 574  
     }
 575  
 
 576  
     private boolean hasTitles(String proposalTitle, String courseTitle){
 577  0
             return (proposalTitle != null && !proposalTitle.isEmpty()) && (courseTitle != null && !courseTitle.isEmpty());
 578  
     }
 579  
     
 580  
     public void saveProposalClu(final SaveActionEvent saveActionEvent){
 581  0
             KSBlockingProgressIndicator.addTask(saving);
 582  0
         final Callback<Throwable> saveFailedCallback = new Callback<Throwable>() {
 583  
 
 584  
                         @Override
 585  
                         public void exec(Throwable caught) {
 586  0
                                  GWT.log("Save Failed.", caught);
 587  0
                                  KSBlockingProgressIndicator.removeTask(saving);
 588  0
                  KSNotifier.add(new KSNotification("Save Failed on server. Please try again.", false, true, 5000));
 589  0
                         }
 590  
 
 591  
         };
 592  
         try {
 593  0
             cluProposalRpcServiceAsync.saveData(cluProposalModel.getRoot(), new KSAsyncCallback<DataSaveResult>(){
 594  
                 @Override
 595  
                 public void handleFailure(Throwable caught) {
 596  0
                    saveFailedCallback.exec(caught);
 597  0
                 }
 598  
 
 599  
                 public void onSuccess(DataSaveResult result) {
 600  0
                         KSBlockingProgressIndicator.removeTask(saving);
 601  
 
 602  0
                         if(result.getValidationResults()!=null && !result.getValidationResults().isEmpty()){
 603  0
                                 isValid(result.getValidationResults(), false, true);
 604  0
                             saveActionEvent.setGotoNextView(false);
 605  0
                         saveActionEvent.doActionComplete();
 606  0
                         KSNotifier.add(new KSNotification("Save Failed. There were validation errors.", false, 5000));
 607  
                         }else{
 608  
                                 
 609  0
                                 saveActionEvent.setSaveSuccessful(true);
 610  0
                                 cluProposalModel.setRoot(result.getValue());
 611  0
                                 String title = getProposalTitle();
 612  
 
 613  0
                                 View currentView = getCurrentView();
 614  0
                                             if (currentView instanceof SectionView){
 615  0
                                                     ((SectionView)currentView).updateView(cluProposalModel);
 616  0
                                                     ((SectionView) currentView).resetDirtyFlags();
 617  
                                 }
 618  0
                             saveActionEvent.doActionComplete();
 619  
                             
 620  0
                                             ViewContext context = CourseProposalController.this.getViewContext();
 621  0
                                             context.setId((String)cluProposalModel.get(proposalPath+"/id"));
 622  0
                                             context.setIdType(IdType.KS_KEW_OBJECT_ID);
 623  
                                             
 624  
                                             //Ensure workflow doc status gets updated from draft, only done on intial save
 625  
                                             //to reduce workflow rpc calls.
 626  0
                                             String proposalVersion = cluProposalModel.get(proposalPath+"/metaInfo/versionInd");
 627  0
                                             if (INITIAL_SAVE_VERSION.equals(proposalVersion)){
 628  0
                                                     workflowUtil.refresh();
 629  
                                             }
 630  
                                             
 631  0
                                             setProposalHeaderTitle();
 632  0
                                             setLastUpdated();
 633  0
                                             HistoryManager.logHistoryChange();
 634  0
                                 if(isNew){
 635  0
                                         RecentlyViewedHelper.addCurrentDocument(title);
 636  
                                 }
 637  0
                                 else if(!currentTitle.equals(title)){
 638  0
                                         RecentlyViewedHelper.updateTitle(currentTitle, title, (String)cluProposalModel.get(proposalPath+"/id"));
 639  
                                 }
 640  0
                                 isNew = false;
 641  
                                             
 642  0
                                             if(saveActionEvent.gotoNextView()){
 643  0
                                                     CourseProposalController.this.showNextViewOnMenu();
 644  
                                             }
 645  0
                                             KSNotifier.add(new KSNotification("Save Successful", false, 4000));
 646  
                         }
 647  0
                 }
 648  
             });
 649  0
         } catch (Exception e) {
 650  0
                 saveFailedCallback.exec(e);
 651  0
         }
 652  
 
 653  0
     }
 654  
 
 655  
     public void setLastUpdated(){
 656  0
             Date lastUpdated = (Date)cluProposalModel.get(UPDATED_KEY);
 657  0
             if(lastUpdated != null){
 658  0
                     setContentInfo("Last Updated: " + df.format(lastUpdated));
 659  
             }
 660  
             else{
 661  0
                     setContentInfo("");
 662  
             }
 663  0
     }
 664  
 
 665  
     @Override
 666  
         public void beforeShow(final Callback<Boolean> onReadyCallback){
 667  0
             Application.getApplicationContext().clearCrossConstraintMap(null);
 668  0
             Application.getApplicationContext().clearPathToFieldMapping(null);
 669  0
             Application.getApplicationContext().setParentPath("");
 670  
             
 671  0
             init(onReadyCallback);
 672  0
         }
 673  
     
 674  
    @Override
 675  
    public void showDefaultView(Callback<Boolean> onReadyCallback) {
 676  0
            if(isNew){
 677  0
                    super.showFirstView(onReadyCallback);
 678  
            }
 679  
            else{
 680  0
                    super.showDefaultView(onReadyCallback);
 681  
            }
 682  0
    }
 683  
 
 684  
         @Override
 685  
     public void setParentController(Controller controller) {
 686  0
         super.setParentController(controller);
 687  0
     }
 688  
 
 689  
         @Override
 690  
         public void checkAuthorization(final PermissionType permissionType, final AuthorizationCallback authCallback) {
 691  0
                 Map<String,String> attributes = new HashMap<String,String>();
 692  
 //                if (StringUtils.isNotBlank(getViewContext().getId())) {
 693  0
                 GWT.log("Attempting Auth Check.", null);
 694  0
                 if ( (getViewContext().getId() != null) && (!"".equals(getViewContext().getId())) ) {
 695  0
                         attributes.put(getViewContext().getIdType().toString(), getViewContext().getId());
 696  
                 }
 697  
 
 698  0
                 cluProposalRpcServiceAsync.isAuthorized(permissionType, attributes, new KSAsyncCallback<Boolean>(){
 699  
 
 700  
                         @Override
 701  
                         public void handleFailure(Throwable caught) {
 702  0
                                 authCallback.isNotAuthorized("Error checking authorization.");
 703  0
                                 GWT.log("Error checking proposal authorization.", caught);
 704  0
                 Window.alert("Error Checking Proposal Authorization: "+caught.getMessage());
 705  0
                         }
 706  
 
 707  
                         @Override
 708  
                         public void onSuccess(Boolean result) {
 709  0
                                 GWT.log("Succeeded checking auth for permission type '" + permissionType + "' with result: " + result, null);
 710  0
                                 if (Boolean.TRUE.equals(result)) {
 711  0
                                         authCallback.isAuthorized();
 712  
                                 }
 713  
                                 else {
 714  0
                                         authCallback.isNotAuthorized("User is not authorized: " + permissionType);
 715  
                                 }
 716  0
                         }
 717  
             });
 718  0
         }
 719  
 
 720  
         @Override
 721  
         public boolean isAuthorizationRequired() {
 722  0
                 return true;
 723  
         }
 724  
 
 725  
         @Override
 726  
         public void setAuthorizationRequired(boolean required) {
 727  0
                 throw new UnsupportedOperationException();
 728  
         }
 729  
 
 730  
     protected void setProposalHeaderTitle(){
 731  
             String title;
 732  0
             if (cluProposalModel.get(cfg.getProposalTitlePath()) != null){
 733  0
                     title = getProposalTitle();
 734  
             }
 735  
             else{
 736  0
                     title = "New Course (Proposal)";
 737  
             }
 738  0
             this.setContentTitle(title);
 739  0
             this.setName(title);
 740  0
             WindowTitleUtils.setContextTitle(title);
 741  0
                 currentTitle = title;
 742  0
     }
 743  
 
 744  
         @Override
 745  
         public WorkflowUtilities getWfUtilities() {
 746  0
                 return workflowUtil;
 747  
         }
 748  
 
 749  
         @Override
 750  
         public void beforeViewChange(Enum<?> viewChangingTo, final Callback<Boolean> okToChange) {
 751  
                 //We do this check here because theoretically the subcontroller views
 752  
                 //will display their own messages to the user to give them a reason why the view
 753  
                 //change has been cancelled, otherwise continue to check for reasons not to change
 754  
                 //with this controller
 755  0
                 super.beforeViewChange(viewChangingTo, new Callback<Boolean>(){
 756  
 
 757  
                         @Override
 758  
                         public void exec(Boolean result) {
 759  0
                                 if(result){
 760  0
                                         if(getCurrentView() instanceof SectionView && ((SectionView)getCurrentView()).isDirty()){
 761  0
                                                 ButtonGroup<YesNoCancelEnum> buttonGroup = new YesNoCancelGroup();
 762  0
                                                 final ButtonMessageDialog<YesNoCancelEnum> dialog = new ButtonMessageDialog<YesNoCancelEnum>("Warning", "You may have unsaved changes.  Save changes?", buttonGroup);
 763  0
                                                 buttonGroup.addCallback(new Callback<YesNoCancelEnum>(){
 764  
 
 765  
                                                         @Override
 766  
                                                         public void exec(YesNoCancelEnum result) {
 767  0
                                                                 switch(result){
 768  
                                                                         case YES:
 769  0
                                                                                 dialog.hide();
 770  0
                                                                                 final SaveActionEvent e = new SaveActionEvent();
 771  0
                                                                                 e.setActionCompleteCallback(new ActionCompleteCallback(){
 772  
 
 773  
                                                                                         @Override
 774  
                                                                                         public void onActionComplete(ActionEvent action) {
 775  0
                                                                                                 if(e.isSaveSuccessful()){
 776  0
                                                                                                         okToChange.exec(true);
 777  
                                                                                                 }
 778  
                                                                                                 else{
 779  0
                                                                                                         okToChange.exec(false);
 780  
                                                                                                 }
 781  0
                                                                                         }
 782  
                                                                                         
 783  
                                                                                 });
 784  0
                                                                                 fireApplicationEvent(e);
 785  0
                                                                                 break;
 786  
                                                                         case NO:
 787  
                                                                                 //Force a model request from server
 788  0
                                                                                 getCurrentModel(new ModelRequestCallback<DataModel>(){
 789  
 
 790  
                                                                                         @Override
 791  
                                                                                         public void onModelReady(DataModel model) {
 792  0
                                                                                                 if (getCurrentView()instanceof Section){
 793  0
                                                                                                     ((Section) getCurrentView()).resetFieldInteractionFlags();
 794  
                                                                                                 }
 795  0
                                                                                                 okToChange.exec(true);
 796  0
                                                                                                 dialog.hide();
 797  0
                                                                                         }
 798  
 
 799  
                                                                                         @Override
 800  
                                                                                         public void onRequestFail(Throwable cause) {
 801  
                                                                                                 //TODO Is this correct... do we want to stop view change if we can't restore the data?  Possibly traps the user
 802  
                                                                                                 //if we don't it messes up saves, possibly warn the user that it failed and continue?
 803  0
                                                                                                 okToChange.exec(false);
 804  0
                                                                                                 dialog.hide();
 805  0
                                                                                                 GWT.log("Unable to retrieve model for data restore on view change with no save", cause);
 806  0
                                                                                         }},
 807  
                                                                                         NO_OP_CALLBACK);
 808  
 
 809  0
                                                                                 break;
 810  
                                                                         case CANCEL:
 811  0
                                                                                 okToChange.exec(false);
 812  0
                                                                                 dialog.hide();
 813  
                                                                                 // Because this event fires after the history change event we need to "undo" the history events. 
 814  0
                                                                                 HistoryManager.logHistoryChange();  
 815  
                                                                                 break;
 816  
                                                                 }
 817  0
                                                         }
 818  
                                                 });
 819  0
                                                 dialog.show();
 820  0
                                         }
 821  
                                         else{
 822  0
                                                 okToChange.exec(true);
 823  
                                         }
 824  
                                 }
 825  
                                 else{
 826  0
                                         okToChange.exec(false);
 827  
                                 }
 828  0
                         }
 829  
                 });
 830  0
         }
 831  
         
 832  
     public KSButton getSaveButton(){
 833  0
             if(currentDocType != MODIFY_TYPE){
 834  0
                 return new KSButton("Save and Continue", new ClickHandler(){
 835  
                             public void onClick(ClickEvent event) {
 836  0
                                     CourseProposalController.this.fireApplicationEvent(new SaveActionEvent(true));
 837  0
                             }
 838  
                         });
 839  
             }
 840  
             else{
 841  0
                     return new KSButton("Save", new ClickHandler(){
 842  
                 public void onClick(ClickEvent event) {
 843  0
                     CourseProposalController.this.fireApplicationEvent(new SaveActionEvent(false));
 844  0
                 }
 845  
             });
 846  
             }
 847  
     }
 848  
     
 849  
     public KSButton getCancelButton(final Enum<?> summaryView){
 850  
             
 851  0
         return new KSButton("Cancel", ButtonStyle.ANCHOR_LARGE_CENTERED, new ClickHandler(){
 852  
                     public void onClick(ClickEvent event) {
 853  0
                             if(!isNew){
 854  0
                                     CourseProposalController.this.showView(summaryView);
 855  
                             }
 856  
                             else{
 857  0
                                     Application.navigate(AppLocations.Locations.CURRICULUM_MANAGEMENT.getLocation());
 858  
                             }
 859  0
                     }
 860  
                 });
 861  
 
 862  
     }
 863  
         
 864  
         @Override
 865  
         public void onHistoryEvent(String historyStack) {
 866  0
                 super.onHistoryEvent(historyStack);
 867  
                 //we dont want to add proposals that are brand new before saving, or copy addresses (as they will initiate
 868  
                 //the modify/copy logic again if called)
 869  0
                 if(cluProposalModel.get(cfg.getProposalTitlePath()) != null && 
 870  
                                 this.getViewContext().getIdType() != IdType.COPY_OF_OBJECT_ID){
 871  0
                         RecentlyViewedHelper.addCurrentDocument(getProposalTitle());
 872  
                 }
 873  0
         }
 874  
         
 875  
         private String getProposalTitle(){
 876  0
                 StringBuffer sb = new StringBuffer();
 877  0
                 sb.append(cluProposalModel.get(cfg.getProposalTitlePath()));
 878  0
                 sb.append(" (Proposal)");
 879  0
                 return sb.toString();
 880  
         }
 881  
 
 882  
     public String getCourseId(){
 883  0
         return cluProposalModel.<String>get("id");
 884  
     }
 885  
 
 886  
     public boolean isNew() {
 887  0
         return isNew;
 888  
     }
 889  
 
 890  
     public CourseRequirementsDataModel getReqDataModel() {
 891  0
         return reqDataModel;
 892  
     }
 893  
 }