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