Coverage Report - org.kuali.student.lum.program.client.core.edit.CoreEditController
 
Classes in this File Line Coverage Branch Coverage Complexity
CoreEditController
0%
0/56
0%
0/8
1.615
CoreEditController$1
0%
0/3
N/A
1.615
CoreEditController$2
0%
0/3
N/A
1.615
CoreEditController$3
0%
0/12
0%
0/4
1.615
CoreEditController$4
0%
0/3
N/A
1.615
CoreEditController$5
0%
0/3
N/A
1.615
CoreEditController$6
0%
0/3
N/A
1.615
CoreEditController$6$1
0%
0/10
0%
0/2
1.615
CoreEditController$6$1$1
0%
0/5
0%
0/2
1.615
CoreEditController$6$1$1$1
0%
0/5
0%
0/2
1.615
CoreEditController$7
0%
0/13
N/A
1.615
CoreEditController$8
0%
0/6
N/A
1.615
CoreEditController$8$1
0%
0/7
0%
0/2
1.615
CoreEditController$9
0%
0/23
0%
0/10
1.615
 
 1  
 package org.kuali.student.lum.program.client.core.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.IdType;
 18  
 import org.kuali.student.core.assembly.data.Data;
 19  
 import org.kuali.student.core.assembly.data.QueryPath;
 20  
 import org.kuali.student.core.validation.dto.ValidationResultInfo;
 21  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 22  
 import org.kuali.student.lum.program.client.*;
 23  
 import org.kuali.student.lum.program.client.core.CoreController;
 24  
 import org.kuali.student.lum.program.client.events.*;
 25  
 import org.kuali.student.lum.program.client.properties.ProgramProperties;
 26  
 import org.kuali.student.lum.program.client.rpc.AbstractCallback;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * @author Igor
 33  
  */
 34  0
 public class CoreEditController extends CoreController {
 35  
 
 36  0
     private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save());
 37  0
     private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED);
 38  
 
 39  
     private ProgramStatus previousState;
 40  
 
 41  
     /**
 42  
      * Constructor.
 43  
      *
 44  
      * @param programModel
 45  
      */
 46  
     public CoreEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) {
 47  0
         super(programModel, viewContext, eventBus);
 48  0
         configurer = GWT.create(CoreEditConfigurer.class);
 49  0
         bind();
 50  0
     }
 51  
 
 52  
     @Override
 53  
     protected void configureView() {
 54  0
         super.configureView();
 55  0
         if (!initialized) {
 56  0
             eventBus.fireEvent(new MetadataLoadedEvent(programModel.getDefinition(), this));
 57  0
             List<Enum<?>> excludedViews = new ArrayList<Enum<?>>();
 58  0
             excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT);
 59  0
             excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT);
 60  0
             excludedViews.add(ProgramSections.SUMMARY);
 61  0
             addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews);
 62  0
             addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews);
 63  0
             initialized = true;
 64  
         }
 65  0
     }
 66  
 
 67  
     private void bind() {
 68  0
         saveButton.addClickHandler(new ClickHandler() {
 69  
 
 70  
             @Override
 71  
             public void onClick(ClickEvent event) {
 72  0
                 doSave();
 73  0
             }
 74  
         });
 75  0
         cancelButton.addClickHandler(new ClickHandler() {
 76  
 
 77  
             @Override
 78  
             public void onClick(ClickEvent event) {
 79  0
                 doCancel();
 80  0
             }
 81  
         });
 82  0
         eventBus.addHandler(StoreRequirementIDsEvent.TYPE, new StoreRequirementIDsEvent.Handler() {
 83  
             @Override
 84  
             public void onEvent(StoreRequirementIDsEvent event) {
 85  0
                 List<String> ids = event.getProgramRequirementIds();
 86  
 
 87  0
                 programModel.set(QueryPath.parse(ProgramConstants.PROGRAM_REQUIREMENTS), new Data());
 88  0
                 Data programRequirements = programModel.get(ProgramConstants.PROGRAM_REQUIREMENTS);
 89  
 
 90  0
                 if (programRequirements == null) {
 91  0
                     Window.alert("Cannot find program requirements in data model.");
 92  0
                     GWT.log("Cannot find program requirements in data model", null);
 93  0
                     return;
 94  
                 }
 95  
 
 96  0
                 for (String id : ids) {
 97  0
                     programRequirements.add(id);
 98  
                 }
 99  0
                 doSave();
 100  0
             }
 101  
         });
 102  0
         eventBus.addHandler(ChangeViewEvent.TYPE, new ChangeViewEvent.Handler() {
 103  
             @Override
 104  
             public void onEvent(ChangeViewEvent event) {
 105  0
                 showView(event.getViewToken());
 106  0
             }
 107  
         });
 108  0
         eventBus.addHandler(UpdateEvent.TYPE, new UpdateEvent.Handler() {
 109  
             @Override
 110  
             public void onEvent(UpdateEvent event) {
 111  0
                 doSave(event.getOkCallback());
 112  0
             }
 113  
         });
 114  0
         eventBus.addHandler(StateChangeEvent.TYPE, new StateChangeEvent.Handler() {
 115  
             @Override
 116  
             public void onEvent(final StateChangeEvent event) {
 117  0
                 programModel.validateNextState(new Callback<List<ValidationResultInfo>>() {
 118  
                     @Override
 119  
                     public void exec(List<ValidationResultInfo> result) {
 120  0
                         boolean isSectionValid = isValid(result, true);
 121  0
                         if (isSectionValid) {
 122  0
                             Callback<Boolean> callback = new Callback<Boolean>() {
 123  
                                 @Override
 124  
                                 public void exec(Boolean result) {
 125  0
                                     if (result) {
 126  0
                                         reloadMetadata = true;
 127  0
                                         loadMetadata(new Callback<Boolean>() {
 128  
                                             @Override
 129  
                                             public void exec(Boolean result) {
 130  0
                                                 if (result) {
 131  0
                                                     ProgramUtils.syncMetadata(configurer, programModel.getDefinition());
 132  0
                                                     HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_PROGRAM.getLocation(), context);
 133  
                                                 }
 134  0
                                             }
 135  
                                         });
 136  
                                     }
 137  0
                                 }
 138  
                             };
 139  0
                             previousState = ProgramStatus.of(programModel.<String>get(ProgramConstants.STATE));
 140  0
                             ProgramUtils.setStatus(programModel, event.getProgramStatus().getValue());
 141  0
                             saveData(callback);
 142  0
                         } else {
 143  0
                             Window.alert("Save failed.  Please check fields for errors.");
 144  
                         }
 145  0
                     }
 146  
                 });
 147  0
             }
 148  
         });
 149  0
     }
 150  
 
 151  
     private void doCancel() {
 152  0
         showView(ProgramSections.SUMMARY);
 153  0
     }
 154  
 
 155  
     @Override
 156  
     protected void doSave() {
 157  0
         doSave(NO_OP_CALLBACK);
 158  0
     }
 159  
 
 160  
     @Override
 161  
     protected void loadModel(ModelRequestCallback<DataModel> callback) {
 162  0
         ViewContext viewContext = getViewContext();
 163  0
         if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) {
 164  0
             createNewVersionAndLoadModel(callback, viewContext);
 165  
         } else {
 166  0
             super.loadModel(callback);
 167  
         }
 168  0
     }
 169  
 
 170  
     protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) {
 171  0
         Data data = new Data();
 172  0
         Data versionData = new Data();
 173  0
         versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId());
 174  0
         versionData.set(new Data.StringKey("versionComment"), "Core Program Version");
 175  0
         data.set(new Data.StringKey("versionInfo"), versionData);
 176  
 
 177  0
         programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_retrievingData()) {
 178  
             @Override
 179  
             public void onSuccess(DataSaveResult result) {
 180  0
                 super.onSuccess(result);
 181  0
                 programModel.setRoot(result.getValue());
 182  0
                 viewContext.setIdType(IdType.OBJECT_ID);
 183  0
                 viewContext.setId((String) programModel.get(ProgramConstants.ID));
 184  0
                 setHeaderTitle();
 185  0
                 setStatus();
 186  0
                 callback.onModelReady(programModel);
 187  0
                 eventBus.fireEvent(new ModelLoadedEvent(programModel));
 188  0
             }
 189  
 
 190  
             @Override
 191  
             public void onFailure(Throwable caught) {
 192  0
                 super.onFailure(caught);
 193  0
                 callback.onRequestFail(caught);
 194  0
             }
 195  
         });
 196  
 
 197  0
     }
 198  
 
 199  
     private void doSave(final Callback<Boolean> okCallback) {
 200  0
         requestModel(new ModelRequestCallback<DataModel>() {
 201  
             @Override
 202  
             public void onModelReady(DataModel model) {
 203  0
                 CoreEditController.this.updateModelFromCurrentView();
 204  0
                 model.validate(new Callback<List<ValidationResultInfo>>() {
 205  
                     @Override
 206  
                     public void exec(List<ValidationResultInfo> result) {
 207  0
                         boolean isSectionValid = isValid(result, true);
 208  0
                         if (isSectionValid) {
 209  0
                             saveData(okCallback);
 210  
                         } else {
 211  0
                             okCallback.exec(false);
 212  0
                             Window.alert("Save failed.  Please check fields for errors.");
 213  
                         }
 214  0
                     }
 215  
                 });
 216  
 
 217  0
             }
 218  
 
 219  
             @Override
 220  
             public void onRequestFail(Throwable cause) {
 221  0
                 GWT.log("Unable to retrieve model for validation and save", cause);
 222  0
             }
 223  
         });
 224  0
     }
 225  
 
 226  
     private void saveData(final Callback<Boolean> okCallback) {
 227  0
         programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) {
 228  
             @Override
 229  
             public void onSuccess(DataSaveResult result) {
 230  0
                 super.onSuccess(result);
 231  0
                 if (result.getValidationResults() != null && !result.getValidationResults().isEmpty()) {
 232  0
                     if (previousState != null) {
 233  0
                         ProgramUtils.setStatus(programModel, previousState.getValue());
 234  
                     }
 235  0
                     isValid(result.getValidationResults(), false, true);
 236  0
                     StringBuilder msg = new StringBuilder();
 237  0
                     for (ValidationResultInfo vri : result.getValidationResults()) {
 238  0
                         msg.append(vri.getMessage());
 239  
                     }
 240  0
                     okCallback.exec(false);
 241  0
                 } else {
 242  0
                     previousState = null;
 243  0
                     programModel.setRoot(result.getValue());
 244  0
                     setHeaderTitle();
 245  0
                     setStatus();
 246  0
                     if (ProgramSections.getViewForUpdate().contains(getCurrentViewEnum().name())) {
 247  0
                         showView(getCurrentViewEnum());
 248  
                     }
 249  0
                     resetFieldInteractionFlag();
 250  0
                     throwAfterSaveEvent();
 251  0
                     HistoryManager.logHistoryChange();
 252  0
                     KSNotifier.show(ProgramProperties.get().common_successfulSave());
 253  0
                     okCallback.exec(true);
 254  
                 }
 255  0
             }
 256  
         });
 257  0
     }
 258  
 
 259  
     private void throwAfterSaveEvent() {
 260  0
         eventBus.fireEvent(new AfterSaveEvent(programModel, this));
 261  0
     }
 262  
 
 263  
     @Override
 264  
     public void onModelLoadedEvent() {
 265  0
         Enum<?> changeSection = ProgramRegistry.getSection();
 266  0
         if (changeSection != null) {
 267  0
             showView(changeSection);
 268  0
             ProgramRegistry.setSection(null);
 269  
         } else {
 270  0
             String id = (String) programModel.get(ProgramConstants.ID);
 271  0
             if (id == null) {
 272  0
                 showView(ProgramSections.PROGRAM_DETAILS_EDIT);
 273  
             } else {
 274  0
                 showView(ProgramSections.SUMMARY);
 275  
             }
 276  
         }
 277  0
     }
 278  
 }