1 | |
package org.kuali.student.lum.program.client.major.edit; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Iterator; |
5 | |
import java.util.List; |
6 | |
import java.util.Set; |
7 | |
import java.util.TreeSet; |
8 | |
|
9 | |
import org.kuali.student.common.assembly.data.Data; |
10 | |
import org.kuali.student.common.assembly.data.Data.Property; |
11 | |
import org.kuali.student.common.assembly.data.QueryPath; |
12 | |
import org.kuali.student.common.ui.client.application.Application; |
13 | |
import org.kuali.student.common.ui.client.application.ViewContext; |
14 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
15 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
16 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
17 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
18 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
19 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
20 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
21 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
22 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
23 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
24 | |
import org.kuali.student.common.ui.shared.IdAttributes; |
25 | |
import org.kuali.student.common.ui.shared.IdAttributes.IdType; |
26 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
27 | |
import org.kuali.student.lum.common.client.helpers.RecentlyViewedHelper; |
28 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
29 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
30 | |
import org.kuali.student.lum.program.client.ProgramRegistry; |
31 | |
import org.kuali.student.lum.program.client.ProgramSections; |
32 | |
import org.kuali.student.lum.program.client.ProgramStatus; |
33 | |
import org.kuali.student.lum.program.client.ProgramUtils; |
34 | |
import org.kuali.student.lum.program.client.events.AddSpecializationEvent; |
35 | |
import org.kuali.student.lum.program.client.events.AfterSaveEvent; |
36 | |
import org.kuali.student.lum.program.client.events.ChangeViewEvent; |
37 | |
import org.kuali.student.lum.program.client.events.MetadataLoadedEvent; |
38 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
39 | |
import org.kuali.student.lum.program.client.events.SpecializationCreatedEvent; |
40 | |
import org.kuali.student.lum.program.client.events.SpecializationSaveEvent; |
41 | |
import org.kuali.student.lum.program.client.events.SpecializationUpdateEvent; |
42 | |
import org.kuali.student.lum.program.client.events.StateChangeEvent; |
43 | |
import org.kuali.student.lum.program.client.events.StoreRequirementIDsEvent; |
44 | |
import org.kuali.student.lum.program.client.events.UpdateEvent; |
45 | |
import org.kuali.student.lum.program.client.major.MajorController; |
46 | |
import org.kuali.student.lum.program.client.properties.ProgramProperties; |
47 | |
import org.kuali.student.lum.program.client.rpc.AbstractCallback; |
48 | |
import org.kuali.student.lum.program.client.widgets.ProgramSideBar; |
49 | |
|
50 | |
import com.google.gwt.core.client.GWT; |
51 | |
import com.google.gwt.event.dom.client.ClickEvent; |
52 | |
import com.google.gwt.event.dom.client.ClickHandler; |
53 | |
import com.google.gwt.event.shared.HandlerManager; |
54 | |
import com.google.gwt.user.client.Window; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public class MajorEditController extends MajorController { |
60 | |
|
61 | 0 | private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save()); |
62 | 0 | private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED); |
63 | 0 | private final Set<String> existingVariationIds = new TreeSet<String>(); |
64 | |
|
65 | |
private ProgramStatus previousState; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public MajorEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) { |
73 | 0 | super(programModel, viewContext, eventBus); |
74 | 0 | configurer = GWT.create(MajorEditConfigurer.class); |
75 | 0 | sideBar.setState(ProgramSideBar.State.EDIT); |
76 | 0 | initHandlers(); |
77 | 0 | } |
78 | |
|
79 | |
@Override |
80 | |
protected void configureView() { |
81 | 0 | super.configureView(); |
82 | 0 | if (!initialized) { |
83 | 0 | eventBus.fireEvent(new MetadataLoadedEvent(programModel.getDefinition(), this)); |
84 | 0 | List<Enum<?>> excludedViews = new ArrayList<Enum<?>>(); |
85 | 0 | excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT); |
86 | 0 | excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT); |
87 | 0 | excludedViews.add(ProgramSections.SUMMARY); |
88 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews); |
89 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews); |
90 | 0 | initialized = true; |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
private void initHandlers() { |
95 | 0 | saveButton.addClickHandler(new ClickHandler() { |
96 | |
|
97 | |
@Override |
98 | |
public void onClick(ClickEvent event) { |
99 | 0 | doSave(); |
100 | 0 | } |
101 | |
}); |
102 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
103 | |
|
104 | |
@Override |
105 | |
public void onClick(ClickEvent event) { |
106 | 0 | doCancel(); |
107 | 0 | } |
108 | |
}); |
109 | 0 | eventBus.addHandler(UpdateEvent.TYPE, new UpdateEvent.Handler() { |
110 | |
@Override |
111 | |
public void onEvent(UpdateEvent event) { |
112 | 0 | Enum<?> view = event.getCurrentView(); |
113 | 0 | if (view != null) { |
114 | 0 | setCurrentViewEnum(view); |
115 | |
} |
116 | 0 | doSave(event.getOkCallback()); |
117 | |
|
118 | 0 | } |
119 | |
}); |
120 | 0 | eventBus.addHandler(StateChangeEvent.TYPE, new StateChangeEvent.Handler() { |
121 | |
@Override |
122 | |
public void onEvent(final StateChangeEvent event) { |
123 | 0 | programModel.validateNextState(new Callback<List<ValidationResultInfo>>() { |
124 | |
@Override |
125 | |
public void exec(List<ValidationResultInfo> result) { |
126 | 0 | boolean isSectionValid = isValid(result, true); |
127 | 0 | if (isSectionValid) { |
128 | 0 | Callback<Boolean> callback = new Callback<Boolean>() { |
129 | |
@Override |
130 | |
public void exec(Boolean result) { |
131 | 0 | if (result) { |
132 | 0 | reloadMetadata = true; |
133 | 0 | loadMetadata(new Callback<Boolean>() { |
134 | |
@Override |
135 | |
public void exec(Boolean result) { |
136 | 0 | if (result) { |
137 | 0 | ProgramUtils.syncMetadata(configurer, programModel.getDefinition()); |
138 | 0 | HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM.getLocation(), context); |
139 | |
} |
140 | 0 | } |
141 | |
}); |
142 | |
} |
143 | 0 | } |
144 | |
}; |
145 | 0 | previousState = ProgramStatus.of(programModel); |
146 | 0 | ProgramUtils.setStatus(programModel, event.getProgramStatus().getValue()); |
147 | 0 | saveData(callback); |
148 | 0 | } else { |
149 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
150 | |
} |
151 | 0 | } |
152 | |
}); |
153 | 0 | } |
154 | |
}); |
155 | |
|
156 | 0 | eventBus.addHandler(SpecializationSaveEvent.TYPE, new SpecializationSaveEvent.Handler() { |
157 | |
@Override |
158 | |
public void onEvent(SpecializationSaveEvent event) { |
159 | |
|
160 | 0 | Data currentVariations = getDataProperty(ProgramConstants.VARIATIONS); |
161 | |
|
162 | 0 | existingVariationIds.clear(); |
163 | |
|
164 | 0 | for (Data.Property prop : currentVariations) { |
165 | 0 | String existingId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
166 | 0 | existingVariationIds.add(existingId); |
167 | 0 | } |
168 | 0 | String updatedId = event.getData().get(ProgramConstants.ID); |
169 | 0 | Integer updatedKey = null; |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 0 | if (updatedId != null) { |
176 | 0 | for (Data.Property prop : currentVariations) { |
177 | 0 | String id = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
178 | 0 | if (updatedId.equals(id)) { |
179 | 0 | updatedKey = prop.getKey(); |
180 | 0 | Data currentMetaInfo = ((Data) prop.getValue()).get("metaInfo"); |
181 | 0 | String latestVersionInd = currentMetaInfo.get("versionInd"); |
182 | 0 | Data newMetaInfo = event.getData().get("metaInfo"); |
183 | 0 | if (newMetaInfo == null) { |
184 | 0 | newMetaInfo = new Data(); |
185 | 0 | event.getData().set("metaInfo", newMetaInfo); |
186 | |
} |
187 | 0 | newMetaInfo.set("versionInd", latestVersionInd); |
188 | 0 | break; |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | 0 | currentVariations.set(updatedKey, event.getData()); |
193 | |
} else { |
194 | 0 | currentVariations.add(event.getData()); |
195 | |
|
196 | |
} |
197 | 0 | doSave(); |
198 | 0 | } |
199 | |
}); |
200 | 0 | eventBus.addHandler(AddSpecializationEvent.TYPE, new AddSpecializationEvent.Handler() { |
201 | |
@Override |
202 | |
public void onEvent(AddSpecializationEvent event) { |
203 | 0 | String id = getStringProperty(ProgramConstants.ID); |
204 | 0 | ProgramRegistry.setRow((getDataProperty(ProgramConstants.VARIATIONS)).size()); |
205 | 0 | ProgramRegistry.setData(ProgramUtils.createNewSpecializationBasedOnMajor(programModel)); |
206 | 0 | ViewContext viewContext = new ViewContext(); |
207 | 0 | viewContext.setId(id); |
208 | 0 | viewContext.setIdType(IdAttributes.IdType.OBJECT_ID); |
209 | 0 | HistoryManager.navigate(AppLocations.Locations.EDIT_VARIATION.getLocation(), viewContext); |
210 | |
|
211 | 0 | } |
212 | |
}); |
213 | |
|
214 | 0 | eventBus.addHandler(StoreRequirementIDsEvent.TYPE, new StoreRequirementIDsEvent.Handler() { |
215 | |
@Override |
216 | |
public void onEvent(StoreRequirementIDsEvent event) { |
217 | 0 | List<String> ids = event.getProgramRequirementIds(); |
218 | |
|
219 | 0 | programModel.set(QueryPath.parse(ProgramConstants.PROGRAM_REQUIREMENTS), new Data()); |
220 | 0 | Data programRequirements = programModel.get(ProgramConstants.PROGRAM_REQUIREMENTS); |
221 | |
|
222 | 0 | if (programRequirements == null) { |
223 | 0 | Window.alert("Cannot find program requirements in data model."); |
224 | 0 | GWT.log("Cannot find program requirements in data model", null); |
225 | 0 | return; |
226 | |
} |
227 | |
|
228 | 0 | for (String id : ids) { |
229 | 0 | programRequirements.add(id); |
230 | |
} |
231 | 0 | doSave(); |
232 | 0 | } |
233 | |
}); |
234 | 0 | eventBus.addHandler(ChangeViewEvent.TYPE, new ChangeViewEvent.Handler() { |
235 | |
@Override |
236 | |
public void onEvent(ChangeViewEvent event) { |
237 | 0 | showView(event.getViewToken()); |
238 | 0 | } |
239 | |
}); |
240 | 0 | } |
241 | |
|
242 | |
@Override |
243 | |
protected void loadModel(ModelRequestCallback<DataModel> callback) { |
244 | 0 | ViewContext viewContext = getViewContext(); |
245 | 0 | if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) { |
246 | 0 | createNewVersionAndLoadModel(callback, viewContext); |
247 | |
} else { |
248 | 0 | super.loadModel(callback); |
249 | |
} |
250 | 0 | } |
251 | |
|
252 | |
protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) { |
253 | 0 | Data data = new Data(); |
254 | 0 | Data versionData = new Data(); |
255 | 0 | versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId()); |
256 | 0 | versionData.set(new Data.StringKey("versionComment"), "Major Disicpline Version"); |
257 | 0 | data.set(new Data.StringKey("versionInfo"), versionData); |
258 | |
|
259 | 0 | programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_retrievingData()) { |
260 | |
@Override |
261 | |
public void onSuccess(DataSaveResult result) { |
262 | 0 | super.onSuccess(result); |
263 | 0 | programModel.setRoot(result.getValue()); |
264 | 0 | viewContext.setId(ProgramUtils.getProgramId(programModel)); |
265 | 0 | viewContext.setIdType(IdType.OBJECT_ID); |
266 | 0 | setHeaderTitle(); |
267 | 0 | setStatus(); |
268 | 0 | callback.onModelReady(programModel); |
269 | 0 | eventBus.fireEvent(new ModelLoadedEvent(programModel)); |
270 | 0 | } |
271 | |
|
272 | |
@Override |
273 | |
public void onFailure(Throwable caught) { |
274 | 0 | super.onFailure(caught); |
275 | 0 | callback.onRequestFail(caught); |
276 | 0 | } |
277 | |
}); |
278 | |
|
279 | 0 | } |
280 | |
|
281 | |
private void doSave(final Callback<Boolean> okCallback) { |
282 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
283 | |
@Override |
284 | |
public void onModelReady(DataModel model) { |
285 | 0 | MajorEditController.this.updateModelFromCurrentView(); |
286 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
287 | |
@Override |
288 | |
public void exec(List<ValidationResultInfo> result) { |
289 | 0 | boolean isSectionValid = isValid(result, true); |
290 | 0 | if (isSectionValid) { |
291 | 0 | saveData(okCallback); |
292 | |
} else { |
293 | 0 | okCallback.exec(false); |
294 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
295 | |
} |
296 | 0 | } |
297 | |
}); |
298 | |
|
299 | 0 | } |
300 | |
|
301 | |
@Override |
302 | |
public void onRequestFail(Throwable cause) { |
303 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
304 | 0 | } |
305 | |
}); |
306 | 0 | } |
307 | |
|
308 | |
private void doCancel() { |
309 | 0 | showView(ProgramSections.SUMMARY); |
310 | 0 | } |
311 | |
|
312 | |
@Override |
313 | |
protected void doSave() { |
314 | 0 | doSave(NO_OP_CALLBACK); |
315 | 0 | } |
316 | |
|
317 | |
private void saveData(final Callback<Boolean> okCallback) { |
318 | 0 | programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) { |
319 | |
@Override |
320 | |
public void onSuccess(DataSaveResult result) { |
321 | 0 | super.onSuccess(result); |
322 | |
|
323 | 0 | List<ValidationResultInfo> validationResults = result.getValidationResults(); |
324 | 0 | if (validationResults != null && !validationResults.isEmpty()) { |
325 | 0 | if (previousState != null) { |
326 | 0 | ProgramUtils.setStatus(programModel, previousState.getValue()); |
327 | |
} |
328 | 0 | ProgramUtils.retrofitValidationResults(validationResults); |
329 | 0 | isValid(validationResults, false, true); |
330 | 0 | ProgramUtils.handleValidationErrorsForSpecializations(validationResults, programModel); |
331 | |
|
332 | |
|
333 | 0 | Data currentVariations = getDataProperty(ProgramConstants.VARIATIONS); |
334 | |
|
335 | 0 | existingVariationIds.clear(); |
336 | |
|
337 | 0 | for (Iterator<Property> iter = currentVariations.iterator();iter.hasNext();) { |
338 | 0 | Property prop = iter.next(); |
339 | 0 | String existingId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
340 | 0 | if(existingId==null){ |
341 | 0 | iter.remove(); |
342 | |
}else{ |
343 | 0 | existingVariationIds.add(existingId); |
344 | |
} |
345 | 0 | } |
346 | |
|
347 | 0 | okCallback.exec(false); |
348 | 0 | } else { |
349 | 0 | Data data = result.getValue(); |
350 | 0 | if (data != null) { |
351 | 0 | programModel.setRoot(result.getValue()); |
352 | |
} |
353 | 0 | previousState = null; |
354 | 0 | setHeaderTitle(); |
355 | 0 | setStatus(); |
356 | 0 | resetFieldInteractionFlag(); |
357 | 0 | configurer.applyPermissions(); |
358 | 0 | handleSpecializations(); |
359 | 0 | throwAfterSaveEvent(); |
360 | 0 | HistoryManager.logHistoryChange(); |
361 | 0 | ViewContext viewContext = getViewContext(); |
362 | 0 | viewContext.setId(getStringProperty(ProgramConstants.ID)); |
363 | 0 | viewContext.setIdType(IdType.OBJECT_ID); |
364 | |
|
365 | |
|
366 | 0 | ViewContext docContext = new ViewContext(); |
367 | 0 | docContext.setId(getStringProperty(ProgramConstants.ID)); |
368 | 0 | docContext.setIdType(IdType.OBJECT_ID); |
369 | 0 | docContext.setAttribute(ProgramConstants.TYPE, ProgramConstants.MAJOR_LU_TYPE_ID + '/' + ProgramSections.PROGRAM_DETAILS_VIEW); |
370 | 0 | RecentlyViewedHelper.addDocument(getProgramName(), |
371 | |
HistoryManager.appendContext(AppLocations.Locations.VIEW_PROGRAM.getLocation(), docContext)); |
372 | 0 | KSNotifier.show(ProgramProperties.get().common_successfulSave()); |
373 | 0 | okCallback.exec(true); |
374 | 0 | processCurrentView(); |
375 | |
} |
376 | 0 | } |
377 | |
}); |
378 | 0 | } |
379 | |
|
380 | |
private void processCurrentView() { |
381 | 0 | Enum<?> currentView = getCurrentViewEnum(); |
382 | 0 | if (currentView.name().equals(ProgramSections.VIEW_ALL.name())) { |
383 | 0 | HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM.getLocation(), getViewContext()); |
384 | |
} else { |
385 | 0 | showView(currentView); |
386 | |
} |
387 | 0 | } |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
private void handleSpecializations() { |
393 | 0 | String newVariationId = null; |
394 | 0 | Data variations = programModel.get(ProgramConstants.VARIATIONS); |
395 | 0 | for (Data.Property prop : variations) { |
396 | 0 | String varId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
397 | 0 | if (!existingVariationIds.contains(varId)) { |
398 | 0 | newVariationId = varId; |
399 | 0 | existingVariationIds.add(newVariationId); |
400 | 0 | break; |
401 | |
} |
402 | 0 | } |
403 | 0 | if (newVariationId != null) { |
404 | 0 | eventBus.fireEvent(new SpecializationCreatedEvent(newVariationId)); |
405 | |
} else { |
406 | 0 | eventBus.fireEvent(new SpecializationUpdateEvent(variations)); |
407 | |
} |
408 | 0 | } |
409 | |
|
410 | |
private void throwAfterSaveEvent() { |
411 | 0 | eventBus.fireEvent(new AfterSaveEvent(programModel, this)); |
412 | 0 | } |
413 | |
|
414 | |
@Override |
415 | |
public void onModelLoadedEvent() { |
416 | 0 | Enum<?> changeSection = ProgramRegistry.getSection(); |
417 | 0 | if (changeSection != null) { |
418 | 0 | showView(changeSection); |
419 | 0 | ProgramRegistry.setSection(null); |
420 | |
} else { |
421 | 0 | String id = getStringProperty(ProgramConstants.ID); |
422 | 0 | if (id == null) { |
423 | 0 | showView(ProgramSections.PROGRAM_DETAILS_EDIT); |
424 | |
} else { |
425 | 0 | showView(ProgramSections.SUMMARY); |
426 | |
} |
427 | |
} |
428 | |
|
429 | 0 | for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){ |
430 | 0 | crossConstraint.reprocessWithUpdatedConstraints(); |
431 | |
} |
432 | 0 | } |
433 | |
|
434 | |
@Override |
435 | |
public void beforeShow(Callback<Boolean> onReadyCallback) { |
436 | 0 | if(!initialized){ |
437 | 0 | Application.getApplicationContext().clearCrossConstraintMap(null); |
438 | 0 | Application.getApplicationContext().clearPathToFieldMapping(null); |
439 | |
} |
440 | 0 | super.beforeShow(onReadyCallback); |
441 | 0 | } |
442 | |
} |