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