Coverage Report - org.kuali.student.lum.program.client.major.edit.MajorEditController
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorEditController
0%
0/78
0%
0/16
1.833
MajorEditController$1
0%
0/3
N/A
1.833
MajorEditController$10
0%
0/6
N/A
1.833
MajorEditController$10$1
0%
0/7
0%
0/2
1.833
MajorEditController$11
0%
0/33
0%
0/8
1.833
MajorEditController$2
0%
0/3
N/A
1.833
MajorEditController$3
0%
0/6
0%
0/2
1.833
MajorEditController$4
0%
0/3
N/A
1.833
MajorEditController$4$1
0%
0/10
0%
0/2
1.833
MajorEditController$4$1$1
0%
0/5
0%
0/2
1.833
MajorEditController$4$1$1$1
0%
0/5
0%
0/2
1.833
MajorEditController$5
0%
0/27
0%
0/10
1.833
MajorEditController$6
0%
0/9
N/A
1.833
MajorEditController$7
0%
0/12
0%
0/4
1.833
MajorEditController$8
0%
0/3
N/A
1.833
MajorEditController$9
0%
0/13
N/A
1.833
 
 1  
 package org.kuali.student.lum.program.client.major.edit;
 2  
 
 3  
 import com.google.gwt.core.client.GWT;
 4  
 import com.google.gwt.event.dom.client.ClickEvent;
 5  
 import com.google.gwt.event.dom.client.ClickHandler;
 6  
 import com.google.gwt.event.shared.HandlerManager;
 7  
 import com.google.gwt.user.client.Window;
 8  
 import org.kuali.student.common.ui.client.application.ViewContext;
 9  
 import org.kuali.student.common.ui.client.mvc.Callback;
 10  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 11  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 12  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 13  
 import org.kuali.student.common.ui.client.service.DataSaveResult;
 14  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 15  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract;
 16  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
 17  
 import org.kuali.student.common.ui.shared.IdAttributes;
 18  
 import org.kuali.student.common.ui.shared.IdAttributes.IdType;
 19  
 import org.kuali.student.core.assembly.data.Data;
 20  
 import org.kuali.student.core.assembly.data.QueryPath;
 21  
 import org.kuali.student.core.validation.dto.ValidationResultInfo;
 22  
 import org.kuali.student.lum.common.client.helpers.RecentlyViewedHelper;
 23  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 24  
 import org.kuali.student.lum.program.client.*;
 25  
 import org.kuali.student.lum.program.client.events.*;
 26  
 import org.kuali.student.lum.program.client.major.MajorController;
 27  
 import org.kuali.student.lum.program.client.properties.ProgramProperties;
 28  
 import org.kuali.student.lum.program.client.rpc.AbstractCallback;
 29  
 import org.kuali.student.lum.program.client.widgets.ProgramSideBar;
 30  
 
 31  
 import java.util.ArrayList;
 32  
 import java.util.List;
 33  
 import java.util.Set;
 34  
 import java.util.TreeSet;
 35  
 
 36  
 /**
 37  
  * @author Igor
 38  
  */
 39  0
 public class MajorEditController extends MajorController {
 40  
 
 41  0
     private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save());
 42  0
     private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED);
 43  0
     private final Set<String> existingVariationIds = new TreeSet<String>();
 44  
 
 45  
     private ProgramStatus previousState;
 46  
 
 47  
     /**
 48  
      * Constructor.
 49  
      *
 50  
      * @param programModel
 51  
      */
 52  
     public MajorEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) {
 53  0
         super(programModel, viewContext, eventBus);
 54  0
         configurer = GWT.create(MajorEditConfigurer.class);
 55  0
         sideBar.setState(ProgramSideBar.State.EDIT);
 56  0
         initHandlers();
 57  0
     }
 58  
 
 59  
     @Override
 60  
     protected void configureView() {
 61  0
         super.configureView();
 62  0
         if (!initialized) {
 63  0
             eventBus.fireEvent(new MetadataLoadedEvent(programModel.getDefinition(), this));
 64  0
             List<Enum<?>> excludedViews = new ArrayList<Enum<?>>();
 65  0
             excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT);
 66  0
             excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT);
 67  0
             excludedViews.add(ProgramSections.SUMMARY);
 68  0
             addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews);
 69  0
             addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews);
 70  0
             initialized = true;
 71  
         }
 72  0
     }
 73  
 
 74  
     private void initHandlers() {
 75  0
         saveButton.addClickHandler(new ClickHandler() {
 76  
 
 77  
             @Override
 78  
             public void onClick(ClickEvent event) {
 79  0
                 doSave();
 80  0
             }
 81  
         });
 82  0
         cancelButton.addClickHandler(new ClickHandler() {
 83  
 
 84  
             @Override
 85  
             public void onClick(ClickEvent event) {
 86  0
                 doCancel();
 87  0
             }
 88  
         });
 89  0
         eventBus.addHandler(UpdateEvent.TYPE, new UpdateEvent.Handler() {
 90  
             @Override
 91  
             public void onEvent(UpdateEvent event) {
 92  0
                 Enum<?> view = event.getCurrentView();
 93  0
                 if (view != null) {
 94  0
                     setCurrentViewEnum(view);
 95  
                 }
 96  0
                 doSave(event.getOkCallback());
 97  
 
 98  0
             }
 99  
         });
 100  0
         eventBus.addHandler(StateChangeEvent.TYPE, new StateChangeEvent.Handler() {
 101  
             @Override
 102  
             public void onEvent(final StateChangeEvent event) {
 103  0
                 programModel.validateNextState(new Callback<List<ValidationResultInfo>>() {
 104  
                     @Override
 105  
                     public void exec(List<ValidationResultInfo> result) {
 106  0
                         boolean isSectionValid = isValid(result, true);
 107  0
                         if (isSectionValid) {
 108  0
                             Callback<Boolean> callback = new Callback<Boolean>() {
 109  
                                 @Override
 110  
                                 public void exec(Boolean result) {
 111  0
                                     if (result) {
 112  0
                                         reloadMetadata = true;
 113  0
                                         loadMetadata(new Callback<Boolean>() {
 114  
                                             @Override
 115  
                                             public void exec(Boolean result) {
 116  0
                                                 if (result) {
 117  0
                                                     ProgramUtils.syncMetadata(configurer, programModel.getDefinition());
 118  0
                                                     HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM.getLocation(), context);
 119  
                                                 }
 120  0
                                             }
 121  
                                         });
 122  
                                     }
 123  0
                                 }
 124  
                             };
 125  0
                             previousState = ProgramStatus.of(programModel.<String>get(ProgramConstants.STATE));
 126  0
                             ProgramUtils.setStatus(programModel, event.getProgramStatus().getValue());
 127  0
                             saveData(callback);
 128  0
                         } else {
 129  0
                             Window.alert("Save failed.  Please check fields for errors.");
 130  
                         }
 131  0
                     }
 132  
                 });
 133  0
             }
 134  
         });
 135  
 
 136  0
         eventBus.addHandler(SpecializationSaveEvent.TYPE, new SpecializationSaveEvent.Handler() {
 137  
             @Override
 138  
             public void onEvent(SpecializationSaveEvent event) {
 139  
 
 140  0
                 Data currentVariations = (Data) programModel.get(ProgramConstants.VARIATIONS);
 141  
 
 142  0
                 existingVariationIds.clear();
 143  
 
 144  0
                 for (Data.Property prop : currentVariations) {
 145  0
                     String existingId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID);
 146  0
                     existingVariationIds.add(existingId);
 147  0
                 }
 148  0
                 String updatedId = event.getData().get(ProgramConstants.ID);
 149  0
                 Integer updatedKey = null;
 150  
 
 151  
                 //FIXME: This is ugly but gets us past a  blocker issue. 
 152  
                 // Theres something wrong with the way the models are
 153  
                 // handled in the major and variation controllers  so they get out of sync.
 154  
                 // This is a temporary workaround
 155  0
                 if (updatedId != null) { // this is an update of an existing variation
 156  0
                     for (Data.Property prop : currentVariations) {
 157  0
                         String id = (String) ((Data) prop.getValue()).get(ProgramConstants.ID);
 158  0
                         if (updatedId.equals(id)) {
 159  0
                             updatedKey = prop.getKey();
 160  0
                             Data currentMetaInfo = ((Data) prop.getValue()).get("metaInfo");
 161  0
                             String latestVersionInd = currentMetaInfo.get("versionInd");
 162  0
                             Data newMetaInfo = event.getData().get("metaInfo");
 163  0
                             if (newMetaInfo == null) {
 164  0
                                 newMetaInfo = new Data();
 165  0
                                 event.getData().set("metaInfo", newMetaInfo);
 166  
                             }
 167  0
                             newMetaInfo.set("versionInd", latestVersionInd);
 168  0
                             break;
 169  
                         }
 170  0
                     }
 171  
 
 172  0
                     currentVariations.set(updatedKey, event.getData());
 173  
                 } else {
 174  0
                     currentVariations.add(event.getData());
 175  
 
 176  
                 }
 177  0
                 doSave();
 178  0
             }
 179  
         });
 180  0
         eventBus.addHandler(AddSpecializationEvent.TYPE, new AddSpecializationEvent.Handler() {
 181  
             @Override
 182  
             public void onEvent(AddSpecializationEvent event) {
 183  0
                 String id = (String) programModel.get(ProgramConstants.ID);
 184  0
                 ProgramRegistry.setRow(programModel.<Data>get(ProgramConstants.VARIATIONS).size());
 185  0
                 ProgramRegistry.setData(ProgramUtils.createNewSpecializationBasedOnMajor(programModel));
 186  0
                 ViewContext viewContext = new ViewContext();
 187  0
                 viewContext.setId(id);
 188  0
                 viewContext.setIdType(IdAttributes.IdType.OBJECT_ID);
 189  0
                 HistoryManager.navigate(AppLocations.Locations.EDIT_VARIATION.getLocation(), viewContext);
 190  
 
 191  0
             }
 192  
         });
 193  
 
 194  0
         eventBus.addHandler(StoreRequirementIDsEvent.TYPE, new StoreRequirementIDsEvent.Handler() {
 195  
             @Override
 196  
             public void onEvent(StoreRequirementIDsEvent event) {
 197  0
                 List<String> ids = event.getProgramRequirementIds();
 198  
 
 199  0
                 programModel.set(QueryPath.parse(ProgramConstants.PROGRAM_REQUIREMENTS), new Data());
 200  0
                 Data programRequirements = programModel.get(ProgramConstants.PROGRAM_REQUIREMENTS);
 201  
 
 202  0
                 if (programRequirements == null) {
 203  0
                     Window.alert("Cannot find program requirements in data model.");
 204  0
                     GWT.log("Cannot find program requirements in data model", null);
 205  0
                     return;
 206  
                 }
 207  
 
 208  0
                 for (String id : ids) {
 209  0
                     programRequirements.add(id);
 210  
                 }
 211  0
                 doSave();
 212  0
             }
 213  
         });
 214  0
         eventBus.addHandler(ChangeViewEvent.TYPE, new ChangeViewEvent.Handler() {
 215  
             @Override
 216  
             public void onEvent(ChangeViewEvent event) {
 217  0
                 showView(event.getViewToken());
 218  0
             }
 219  
         });
 220  0
     }
 221  
 
 222  
     @Override
 223  
     protected void loadModel(ModelRequestCallback<DataModel> callback) {
 224  0
         ViewContext viewContext = getViewContext();
 225  0
         if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) {
 226  0
             createNewVersionAndLoadModel(callback, viewContext);
 227  
         } else {
 228  0
             super.loadModel(callback);
 229  
         }
 230  0
     }
 231  
 
 232  
     protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) {
 233  0
         Data data = new Data();
 234  0
         Data versionData = new Data();
 235  0
         versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId());
 236  0
         versionData.set(new Data.StringKey("versionComment"), "Major Disicpline Version");
 237  0
         data.set(new Data.StringKey("versionInfo"), versionData);
 238  
 
 239  0
         programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_retrievingData()) {
 240  
             @Override
 241  
             public void onSuccess(DataSaveResult result) {
 242  0
                 super.onSuccess(result);
 243  0
                 programModel.setRoot(result.getValue());
 244  0
                 viewContext.setId((String) programModel.get(ProgramConstants.ID));
 245  0
                 viewContext.setIdType(IdType.OBJECT_ID);
 246  0
                 setHeaderTitle();
 247  0
                 setStatus();
 248  0
                 callback.onModelReady(programModel);
 249  0
                 eventBus.fireEvent(new ModelLoadedEvent(programModel));
 250  0
             }
 251  
 
 252  
             @Override
 253  
             public void onFailure(Throwable caught) {
 254  0
                 super.onFailure(caught);
 255  0
                 callback.onRequestFail(caught);
 256  0
             }
 257  
         });
 258  
 
 259  0
     }
 260  
 
 261  
     private void doSave(final Callback<Boolean> okCallback) {
 262  0
         requestModel(new ModelRequestCallback<DataModel>() {
 263  
             @Override
 264  
             public void onModelReady(DataModel model) {
 265  0
                 MajorEditController.this.updateModelFromCurrentView();
 266  0
                 model.validate(new Callback<List<ValidationResultInfo>>() {
 267  
                     @Override
 268  
                     public void exec(List<ValidationResultInfo> result) {
 269  0
                         boolean isSectionValid = isValid(result, true);
 270  0
                         if (isSectionValid) {
 271  0
                             saveData(okCallback);
 272  
                         } else {
 273  0
                             okCallback.exec(false);
 274  0
                             Window.alert("Save failed.  Please check fields for errors.");
 275  
                         }
 276  0
                     }
 277  
                 });
 278  
 
 279  0
             }
 280  
 
 281  
             @Override
 282  
             public void onRequestFail(Throwable cause) {
 283  0
                 GWT.log("Unable to retrieve model for validation and save", cause);
 284  0
             }
 285  
         });
 286  0
     }
 287  
 
 288  
     private void doCancel() {
 289  0
         showView(ProgramSections.SUMMARY);
 290  0
     }
 291  
 
 292  
     @Override
 293  
     protected void doSave() {
 294  0
         doSave(NO_OP_CALLBACK);
 295  0
     }
 296  
 
 297  
     private void saveData(final Callback<Boolean> okCallback) {
 298  0
         programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) {
 299  
             @Override
 300  
             public void onSuccess(DataSaveResult result) {
 301  0
                 super.onSuccess(result);
 302  0
                 Data data = result.getValue();
 303  0
                 if (data != null) {
 304  0
                     programModel.setRoot(result.getValue());
 305  
                 }
 306  0
                 List<ValidationResultInfo> validationResults = result.getValidationResults();
 307  0
                 if (validationResults != null && !validationResults.isEmpty()) {
 308  0
                     if (previousState != null) {
 309  0
                         ProgramUtils.setStatus(programModel, previousState.getValue());
 310  
                     }
 311  0
                     ProgramUtils.retrofitValidationResults(validationResults);
 312  0
                     isValid(validationResults, false, true);
 313  0
                     ProgramUtils.handleValidationErrorsForSpecializations(validationResults, programModel);
 314  0
                     okCallback.exec(false);
 315  
                 } else {
 316  0
                     previousState = null;
 317  0
                     setHeaderTitle();
 318  0
                     setStatus();
 319  0
                     resetFieldInteractionFlag();
 320  0
                     configurer.applyPermissions();
 321  0
                     handleSpecializations();
 322  0
                     throwAfterSaveEvent();
 323  0
                     HistoryManager.logHistoryChange();
 324  0
                     ViewContext viewContext = getViewContext();
 325  0
                     viewContext.setId((String) programModel.get(ProgramConstants.ID));
 326  0
                     viewContext.setIdType(IdType.OBJECT_ID);
 327  
 
 328  
                     // add to recently viewed now that we're sure to know the program's id
 329  0
                     ViewContext docContext = new ViewContext();
 330  0
                     docContext.setId((String) programModel.get(ProgramConstants.ID));
 331  0
                     docContext.setIdType(IdType.OBJECT_ID);
 332  0
                     docContext.setAttribute(ProgramConstants.TYPE, ProgramConstants.MAJOR_LU_TYPE_ID + '/' + ProgramSections.PROGRAM_DETAILS_VIEW);
 333  0
                     RecentlyViewedHelper.addDocument(getProgramName(),
 334  
                             HistoryManager.appendContext(AppLocations.Locations.VIEW_PROGRAM.getLocation(), docContext));
 335  0
                     KSNotifier.show(ProgramProperties.get().common_successfulSave());
 336  0
                     okCallback.exec(true);
 337  0
                     processCurrentView();
 338  
                 }
 339  0
             }
 340  
         });
 341  0
     }
 342  
 
 343  
     private void processCurrentView() {
 344  0
         Enum<?> currentView = getCurrentViewEnum();
 345  0
         if (currentView.name().equals(ProgramSections.VIEW_ALL.name())) {
 346  0
             HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM.getLocation(), getViewContext());
 347  
         } else {
 348  0
             showView(currentView);
 349  
         }
 350  0
     }
 351  
 
 352  
     /**
 353  
      * Handles after save work for specializations.
 354  
      */
 355  
     private void handleSpecializations() {
 356  0
         String newVariationId = null;
 357  0
         Data variations = programModel.get(ProgramConstants.VARIATIONS);
 358  0
         for (Data.Property prop : variations) {
 359  0
             String varId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID);
 360  0
             if (!existingVariationIds.contains(varId)) {
 361  0
                 newVariationId = varId;
 362  0
                 existingVariationIds.add(newVariationId);
 363  0
                 break;
 364  
             }
 365  0
         }
 366  0
         if (newVariationId != null) {
 367  0
             eventBus.fireEvent(new SpecializationCreatedEvent(newVariationId));
 368  
         } else {
 369  0
             eventBus.fireEvent(new SpecializationUpdateEvent(variations));
 370  
         }
 371  0
     }
 372  
 
 373  
     private void throwAfterSaveEvent() {
 374  0
         eventBus.fireEvent(new AfterSaveEvent(programModel, this));
 375  0
     }
 376  
 
 377  
     @Override
 378  
     public void onModelLoadedEvent() {
 379  0
         Enum<?> changeSection = ProgramRegistry.getSection();
 380  0
         if (changeSection != null) {
 381  0
             showView(changeSection);
 382  0
             ProgramRegistry.setSection(null);
 383  
         } else {
 384  0
             String id = (String) programModel.get(ProgramConstants.ID);
 385  0
             if (id == null) {
 386  0
                 showView(ProgramSections.PROGRAM_DETAILS_EDIT);
 387  
             } else {
 388  0
                 showView(ProgramSections.SUMMARY);
 389  
             }
 390  
         }
 391  0
     }
 392  
 }