| 1 | |
package org.kuali.student.lum.program.client.variation.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.ui.client.application.Application; |
| 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.HasCrossConstraints; |
| 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.widgets.KSButton; |
| 15 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
| 16 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
| 17 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
| 18 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
| 19 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
| 20 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
| 21 | |
import org.kuali.student.lum.program.client.ProgramMsgConstants; |
| 22 | |
import org.kuali.student.lum.program.client.ProgramRegistry; |
| 23 | |
import org.kuali.student.lum.program.client.ProgramSections; |
| 24 | |
import org.kuali.student.lum.program.client.events.ChangeViewEvent; |
| 25 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
| 26 | |
import org.kuali.student.lum.program.client.events.SpecializationCreatedEvent; |
| 27 | |
import org.kuali.student.lum.program.client.events.SpecializationSaveEvent; |
| 28 | |
import org.kuali.student.lum.program.client.events.SpecializationUpdateEvent; |
| 29 | |
import org.kuali.student.lum.program.client.events.StoreSpecRequirementIDsEvent; |
| 30 | |
import org.kuali.student.lum.program.client.major.MajorController; |
| 31 | |
import org.kuali.student.lum.program.client.major.proposal.MajorProposalController; |
| 32 | |
import org.kuali.student.lum.program.client.variation.VariationController; |
| 33 | |
import org.kuali.student.lum.program.client.widgets.ProgramSideBar; |
| 34 | |
|
| 35 | |
import com.google.gwt.core.client.GWT; |
| 36 | |
import com.google.gwt.event.dom.client.ClickEvent; |
| 37 | |
import com.google.gwt.event.dom.client.ClickHandler; |
| 38 | |
import com.google.gwt.event.shared.HandlerManager; |
| 39 | |
import com.google.gwt.user.client.Window; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | public class VariationEditController extends VariationController { |
| 45 | |
|
| 46 | 0 | private final KSButton saveButton = new KSButton(getLabel(ProgramMsgConstants.COMMON_SAVE)); |
| 47 | 0 | private final KSButton cancelButton = new KSButton(getLabel(ProgramMsgConstants.COMMON_CANCEL), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED); |
| 48 | |
|
| 49 | |
private String currentId; |
| 50 | |
|
| 51 | |
public VariationEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus, MajorController majorController) { |
| 52 | 0 | super(programModel, viewContext, eventBus, majorController); |
| 53 | 0 | configurer = GWT.create(VariationEditConfigurer.class); |
| 54 | 0 | sideBar.setState(ProgramSideBar.State.EDIT); |
| 55 | 0 | if (getStringProperty(ProgramConstants.ID) != null) { |
| 56 | 0 | setDefaultView(ProgramSections.SUMMARY); |
| 57 | |
} |
| 58 | 0 | initHandlers(); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
private void initHandlers() { |
| 62 | 0 | saveButton.addClickHandler(new ClickHandler() { |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public void onClick(ClickEvent event) { |
| 66 | 0 | doSave(); |
| 67 | 0 | } |
| 68 | |
}); |
| 69 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public void onClick(ClickEvent event) { |
| 73 | 0 | doCancel(); |
| 74 | 0 | } |
| 75 | |
}); |
| 76 | 0 | ModelLoadedEvent.Handler modelLoadedHandler = new ModelLoadedEvent.Handler() { |
| 77 | |
@Override |
| 78 | |
public void onEvent(ModelLoadedEvent event) { |
| 79 | 0 | DataModel dataModel = event.getModel(); |
| 80 | 0 | Data variationMap = dataModel.get(ProgramConstants.VARIATIONS); |
| 81 | 0 | if (variationMap != null) { |
| 82 | 0 | int row = 0; |
| 83 | 0 | for (Data.Property property : variationMap) { |
| 84 | 0 | final Data variationData = property.getValue(); |
| 85 | 0 | if (variationData.get(ProgramConstants.ID).equals(currentId)) { |
| 86 | 0 | programModel.setRoot(variationData); |
| 87 | 0 | ProgramRegistry.setData(variationData); |
| 88 | 0 | ProgramRegistry.setRow(row); |
| 89 | 0 | setContentTitle(getProgramName()); |
| 90 | 0 | row++; |
| 91 | 0 | return; |
| 92 | |
} |
| 93 | 0 | } |
| 94 | |
} |
| 95 | 0 | } |
| 96 | |
}; |
| 97 | 0 | ProgramRegistry.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
| 98 | 0 | eventBus.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
| 99 | |
|
| 100 | 0 | ChangeViewEvent.Handler changeViewHandler = new ChangeViewEvent.Handler() { |
| 101 | |
@Override |
| 102 | |
public void onEvent(ChangeViewEvent event) { |
| 103 | 0 | Enum<?> viewToken = event.getViewToken(); |
| 104 | 0 | if (!viewToken.name().equals(ProgramSections.SPECIALIZATIONS_EDIT.name())) { |
| 105 | 0 | showView(viewToken); |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
}; |
| 109 | 0 | ProgramRegistry.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
| 110 | 0 | eventBus.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
| 111 | 0 | eventBus.addHandler(SpecializationCreatedEvent.TYPE, new SpecializationCreatedEvent.Handler() { |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public void onEvent(SpecializationCreatedEvent event) { |
| 115 | 0 | programModel.getRoot().set(ProgramConstants.ID, event.getSpecializationId()); |
| 116 | 0 | showWarnings(); |
| 117 | 0 | } |
| 118 | |
}); |
| 119 | |
|
| 120 | 0 | eventBus.addHandler(SpecializationUpdateEvent.TYPE, new SpecializationUpdateEvent.Handler() { |
| 121 | |
@Override |
| 122 | |
public void onEvent(SpecializationUpdateEvent event) { |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | 0 | String currSpecializationId = programModel.getRoot().get(ProgramConstants.ID); |
| 127 | 0 | for (Data.Property prop : event.getSpecializations()) { |
| 128 | 0 | String specId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
| 129 | |
|
| 130 | 0 | if (null != specId && specId.equals(currSpecializationId) && prop.getValueType().equals(Data.class)) { |
| 131 | |
|
| 132 | 0 | programModel.setRoot((Data) prop.getValue()); |
| 133 | 0 | showView(getCurrentViewEnum()); |
| 134 | |
} |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 138 | 0 | showWarnings(); |
| 139 | 0 | } |
| 140 | |
}); |
| 141 | |
|
| 142 | 0 | StoreSpecRequirementIDsEvent.Handler requirementsHandler = new StoreSpecRequirementIDsEvent.Handler() { |
| 143 | |
@Override |
| 144 | |
public void onEvent(StoreSpecRequirementIDsEvent event) { |
| 145 | 0 | final String programId = event.getProgramId(); |
| 146 | 0 | final List<String> ids = event.getProgramRequirementIds(); |
| 147 | |
|
| 148 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
| 149 | |
@Override |
| 150 | |
public void onModelReady(final DataModel model) { |
| 151 | 0 | Data programRequirements = null; |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | 0 | Data variationData = model.getRoot(); |
| 156 | 0 | if (variationData.get(ProgramConstants.ID).equals(programId)) { |
| 157 | 0 | variationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data()); |
| 158 | 0 | programRequirements = variationData.get(ProgramConstants.PROGRAM_REQUIREMENTS); |
| 159 | |
|
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | 0 | if (programRequirements == null) { |
| 164 | 0 | Window.alert("Cannot find program requirements in data model."); |
| 165 | 0 | GWT.log("Cannot find program requirements in data model", null); |
| 166 | 0 | return; |
| 167 | |
} |
| 168 | |
|
| 169 | 0 | for (String id : ids) { |
| 170 | 0 | programRequirements.add(id); |
| 171 | |
} |
| 172 | 0 | doSave(); |
| 173 | 0 | } |
| 174 | |
|
| 175 | |
@Override |
| 176 | |
public void onRequestFail(Throwable cause) { |
| 177 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
| 178 | 0 | } |
| 179 | |
|
| 180 | |
}); |
| 181 | 0 | } |
| 182 | |
}; |
| 183 | 0 | ProgramRegistry.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
| 184 | 0 | eventBus.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
| 185 | 0 | } |
| 186 | |
|
| 187 | |
@Override |
| 188 | |
protected void fireUpdateEvent(Callback<Boolean> okToChange) { |
| 189 | 0 | doSave(okToChange); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
private void doSave(final Callback<Boolean> okToChange) { |
| 193 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
| 194 | |
@Override |
| 195 | |
public void onModelReady(final DataModel model) { |
| 196 | 0 | VariationEditController.this.updateModelFromCurrentView(); |
| 197 | 0 | model.setParentPath(ProgramConstants.VARIATIONS + "/" + ProgramRegistry.getRow()); |
| 198 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
| 199 | |
@Override |
| 200 | |
public void exec(List<ValidationResultInfo> results) { |
| 201 | 0 | boolean isSectionValid = isValid(results, true); |
| 202 | 0 | if (isSectionValid) { |
| 203 | 0 | saveData(model); |
| 204 | 0 | okToChange.exec(true); |
| 205 | |
} else { |
| 206 | 0 | okToChange.exec(false); |
| 207 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
| 208 | |
} |
| 209 | 0 | } |
| 210 | |
}); |
| 211 | 0 | } |
| 212 | |
|
| 213 | |
@Override |
| 214 | |
public void onRequestFail(Throwable cause) { |
| 215 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
| 216 | 0 | } |
| 217 | |
|
| 218 | |
}); |
| 219 | 0 | } |
| 220 | |
|
| 221 | |
@Override |
| 222 | |
protected void configureView() { |
| 223 | 0 | super.configureView(); |
| 224 | 0 | if (!initialized) { |
| 225 | 0 | List<Enum<?>> excludedViews = new ArrayList<Enum<?>>(); |
| 226 | 0 | excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT); |
| 227 | 0 | excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT); |
| 228 | 0 | excludedViews.add(ProgramSections.SUMMARY); |
| 229 | 0 | addCommonButton(getLabel(ProgramMsgConstants.PROGRAM_MENU_SECTIONS), saveButton, excludedViews); |
| 230 | 0 | addCommonButton(getLabel(ProgramMsgConstants.PROGRAM_MENU_SECTIONS), cancelButton, excludedViews); |
| 231 | 0 | initialized = true; |
| 232 | |
} |
| 233 | |
|
| 234 | 0 | } |
| 235 | |
|
| 236 | |
@Override |
| 237 | |
protected void resetModel() { |
| 238 | 0 | currentId = getStringProperty(ProgramConstants.ID); |
| 239 | 0 | programModel.resetRoot(); |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
private void doCancel() { |
| 243 | 0 | navigateToParent(ProgramSections.SUMMARY); |
| 244 | 0 | } |
| 245 | |
|
| 246 | |
@Override |
| 247 | |
protected void doSave() { |
| 248 | 0 | doSave(NO_OP_CALLBACK); |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
private void saveData(DataModel model) { |
| 252 | 0 | currentId = model.get(ProgramConstants.ID); |
| 253 | 0 | eventBus.fireEvent(new SpecializationSaveEvent(model.getRoot())); |
| 254 | 0 | setContentTitle(getProgramName()); |
| 255 | 0 | setName(getProgramName()); |
| 256 | 0 | resetFieldInteractionFlag(); |
| 257 | 0 | } |
| 258 | |
|
| 259 | |
@Override |
| 260 | |
protected void navigateToParent() { |
| 261 | 0 | navigateToParent(ProgramSections.SPECIALIZATIONS_EDIT); |
| 262 | 0 | } |
| 263 | |
|
| 264 | |
private void navigateToParent(ProgramSections parentSection) { |
| 265 | 0 | String appLoc = ""; |
| 266 | |
|
| 267 | 0 | if(!(majorController instanceof MajorProposalController)) |
| 268 | 0 | appLoc = AppLocations.Locations.EDIT_PROGRAM_SPEC.getLocation(); |
| 269 | |
else |
| 270 | 0 | appLoc = AppLocations.Locations.PROGRAM_PROPOSAL.getLocation(); |
| 271 | 0 | String path = HistoryManager.appendContext(appLoc, getViewContext()) + "/" + parentSection; |
| 272 | 0 | HistoryManager.navigate(path); |
| 273 | 0 | } |
| 274 | |
|
| 275 | |
|
| 276 | |
@Override |
| 277 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
| 278 | |
|
| 279 | 0 | Application.getApplicationContext().clearCrossConstraintsWithStartingPath(null,ProgramConstants.VARIATIONS); |
| 280 | |
|
| 281 | |
|
| 282 | 0 | String newParentPath = ProgramConstants.VARIATIONS+"/"+org.kuali.student.lum.program.client.ProgramRegistry.getRow()+"/"; |
| 283 | 0 | Application.getApplicationContext().setParentPath(newParentPath); |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | 0 | Callback<Boolean> finalizeVariationView = new Callback<Boolean>(){ |
| 289 | |
public void exec(Boolean result) { |
| 290 | |
|
| 291 | 0 | for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){ |
| 292 | 0 | crossConstraint.reprocessWithUpdatedConstraints(); |
| 293 | |
} |
| 294 | |
|
| 295 | 0 | onReadyCallback.exec(result); |
| 296 | 0 | } |
| 297 | |
}; |
| 298 | 0 | super.beforeShow(finalizeVariationView); |
| 299 | 0 | } |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
@Override |
| 304 | |
public <V extends Enum<?>> void showView(V viewType, |
| 305 | |
final Callback<Boolean> onReadyCallback) { |
| 306 | 0 | Callback<Boolean> updateCrossConstraintsCallback = new Callback<Boolean>(){ |
| 307 | |
public void exec(Boolean result) { |
| 308 | 0 | onReadyCallback.exec(result); |
| 309 | 0 | showWarnings(); |
| 310 | 0 | } |
| 311 | |
}; |
| 312 | 0 | super.showView(viewType, updateCrossConstraintsCallback); |
| 313 | 0 | } |
| 314 | |
|
| 315 | |
} |