Coverage Report - org.kuali.student.lum.program.client.credential.edit.CredentialEditController
 
Classes in this File Line Coverage Branch Coverage Complexity
CredentialEditController
0%
0/56
0%
0/8
1.615
CredentialEditController$1
0%
0/3
N/A
1.615
CredentialEditController$2
0%
0/3
N/A
1.615
CredentialEditController$3
0%
0/12
0%
0/4
1.615
CredentialEditController$4
0%
0/3
N/A
1.615
CredentialEditController$5
0%
0/3
N/A
1.615
CredentialEditController$6
0%
0/3
N/A
1.615
CredentialEditController$6$1
0%
0/10
0%
0/2
1.615
CredentialEditController$6$1$1
0%
0/5
0%
0/2
1.615
CredentialEditController$6$1$1$1
0%
0/5
0%
0/2
1.615
CredentialEditController$7
0%
0/6
N/A
1.615
CredentialEditController$7$1
0%
0/7
0%
0/2
1.615
CredentialEditController$8
0%
0/23
0%
0/10
1.615
CredentialEditController$9
0%
0/13
N/A
1.615
 
 1  
 package org.kuali.student.lum.program.client.credential.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.credential.CredentialController;
 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 CredentialEditController extends CredentialController {
 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 CredentialEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) {
 60  0
         super(programModel, viewContext, eventBus);
 61  0
         configurer = GWT.create(CredentialEditConfigurer.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_BACC_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  
     private void doSave(final Callback<Boolean> okCallback) {
 174  0
         requestModel(new ModelRequestCallback<DataModel>() {
 175  
             @Override
 176  
             public void onModelReady(DataModel model) {
 177  0
                 CredentialEditController.this.updateModelFromCurrentView();
 178  0
                 model.validate(new Callback<List<ValidationResultInfo>>() {
 179  
                     @Override
 180  
                     public void exec(List<ValidationResultInfo> result) {
 181  0
                         boolean isSectionValid = isValid(result, true);
 182  0
                         if (isSectionValid) {
 183  0
                             saveData(okCallback);
 184  
                         } else {
 185  0
                             okCallback.exec(false);
 186  0
                             KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000));
 187  
                         }
 188  0
                     }
 189  
                 });
 190  
 
 191  0
             }
 192  
 
 193  
             @Override
 194  
             public void onRequestFail(Throwable cause) {
 195  0
                 GWT.log("Unable to retrieve model for validation and save", cause);
 196  0
             }
 197  
         });
 198  0
     }
 199  
 
 200  
     private void saveData(final Callback<Boolean> okCallback) {
 201  0
         programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) {
 202  
             @Override
 203  
             public void onSuccess(DataSaveResult result) {
 204  0
                 super.onSuccess(result);
 205  0
                 if (result.getValidationResults() != null && !result.getValidationResults().isEmpty()) {
 206  0
                     if (previousState != null) {
 207  0
                         ProgramUtils.setStatus(programModel, previousState.getValue());
 208  
                     }
 209  0
                     isValid(result.getValidationResults(), false, true);
 210  0
                     StringBuilder msg = new StringBuilder();
 211  0
                     for (ValidationResultInfo vri : result.getValidationResults()) {
 212  0
                         msg.append(vri.getMessage());
 213  
                     }
 214  0
                     okCallback.exec(false);
 215  0
                 } else {
 216  0
                     previousState = null;
 217  0
                     programModel.setRoot(result.getValue());
 218  0
                     setHeaderTitle();
 219  0
                     setStatus();
 220  0
                     resetFieldInteractionFlag();
 221  0
                     throwAfterSaveEvent();
 222  0
                     if (ProgramSections.getViewForUpdate().contains(getCurrentViewEnum().name())) {
 223  0
                         showView(getCurrentViewEnum());
 224  
                     }
 225  0
                     HistoryManager.logHistoryChange();
 226  0
                     KSNotifier.show(ProgramProperties.get().common_successfulSave());
 227  0
                     okCallback.exec(true);
 228  
                 }
 229  0
             }
 230  
         });
 231  0
     }
 232  
 
 233  
     @Override
 234  
     protected void loadModel(ModelRequestCallback<DataModel> callback) {
 235  0
         ViewContext viewContext = getViewContext();
 236  0
         if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) {
 237  0
             createNewVersionAndLoadModel(callback, viewContext);
 238  
         } else {
 239  0
             super.loadModel(callback);
 240  
         }
 241  0
     }
 242  
 
 243  
     protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) {
 244  0
         Data data = new Data();
 245  0
         Data versionData = new Data();
 246  0
         versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId());
 247  0
         versionData.set(new Data.StringKey("versionComment"), "Credential Program Version");
 248  0
         data.set(new Data.StringKey("versionInfo"), versionData);
 249  
 
 250  0
         programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_retrievingData()) {
 251  
             @Override
 252  
             public void onSuccess(DataSaveResult result) {
 253  0
                 super.onSuccess(result);
 254  0
                 programModel.setRoot(result.getValue());
 255  0
                 viewContext.setIdType(IdType.OBJECT_ID);
 256  0
                 viewContext.setId((String) programModel.get(ProgramConstants.ID));
 257  0
                 setHeaderTitle();
 258  0
                 setStatus();
 259  0
                 callback.onModelReady(programModel);
 260  0
                 eventBus.fireEvent(new ModelLoadedEvent(programModel));
 261  0
             }
 262  
 
 263  
             @Override
 264  
             public void onFailure(Throwable caught) {
 265  0
                 super.onFailure(caught);
 266  0
                 callback.onRequestFail(caught);
 267  0
             }
 268  
         });
 269  
 
 270  0
     }
 271  
 
 272  
     private void throwAfterSaveEvent() {
 273  0
         eventBus.fireEvent(new AfterSaveEvent(programModel, this));
 274  0
     }
 275  
 
 276  
     @Override
 277  
     public void onModelLoadedEvent() {
 278  0
         Enum<?> changeSection = ProgramRegistry.getSection();
 279  0
         if (changeSection != null) {
 280  0
             showView(changeSection);
 281  0
             ProgramRegistry.setSection(null);
 282  
         } else {
 283  0
             String id = getStringProperty(ProgramConstants.ID);
 284  0
             if (id == null) {
 285  0
                 showView(ProgramSections.PROGRAM_DETAILS_EDIT);
 286  
             } else {
 287  0
                 showView(ProgramSections.SUMMARY);
 288  
             }
 289  
         }
 290  0
     }
 291  
 }