1 | |
package org.kuali.student.lum.program.client; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
|
8 | |
import org.kuali.student.common.assembly.data.Data; |
9 | |
import org.kuali.student.common.assembly.data.Metadata; |
10 | |
import org.kuali.student.common.dto.DtoConstants; |
11 | |
import org.kuali.student.common.rice.authorization.PermissionType; |
12 | |
import org.kuali.student.common.ui.client.application.ViewContext; |
13 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.MenuSectionController; |
14 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
15 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
16 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
17 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
18 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
19 | |
import org.kuali.student.common.ui.client.mvc.ModelProvider; |
20 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
21 | |
import org.kuali.student.common.ui.client.mvc.View; |
22 | |
import org.kuali.student.common.ui.client.mvc.dto.ReferenceModel; |
23 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
24 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
25 | |
import org.kuali.student.common.ui.client.util.ExportElement; |
26 | |
import org.kuali.student.common.ui.client.util.ExportUtils; |
27 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
28 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
29 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations; |
30 | |
import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog; |
31 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup; |
32 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup; |
33 | |
import org.kuali.student.common.ui.shared.IdAttributes; |
34 | |
import org.kuali.student.common.ui.shared.IdAttributes.IdType; |
35 | |
import org.kuali.student.core.comments.ui.client.widgets.commenttool.CommentTool; |
36 | |
import org.kuali.student.lum.common.client.helpers.RecentlyViewedHelper; |
37 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
38 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
39 | |
import org.kuali.student.lum.program.client.events.UpdateEvent; |
40 | |
import org.kuali.student.lum.program.client.properties.ProgramProperties; |
41 | |
import org.kuali.student.lum.program.client.rpc.AbstractCallback; |
42 | |
import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService; |
43 | |
import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcServiceAsync; |
44 | |
import org.kuali.student.lum.program.client.widgets.ProgramSideBar; |
45 | |
|
46 | |
import com.google.gwt.core.client.GWT; |
47 | |
import com.google.gwt.event.dom.client.ClickEvent; |
48 | |
import com.google.gwt.event.dom.client.ClickHandler; |
49 | |
import com.google.gwt.event.shared.HandlerManager; |
50 | |
import com.google.gwt.user.client.ui.Label; |
51 | |
import com.google.gwt.user.client.ui.Widget; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | public abstract class ProgramController extends MenuSectionController { |
57 | |
|
58 | |
protected MajorDisciplineRpcServiceAsync programRemoteService; |
59 | |
|
60 | 0 | protected boolean initialized = false; |
61 | |
|
62 | |
protected DataModel programModel; |
63 | |
|
64 | |
protected AbstractProgramConfigurer configurer; |
65 | |
|
66 | |
protected HandlerManager eventBus; |
67 | |
|
68 | 0 | protected Label statusLabel = new Label(); |
69 | |
|
70 | |
protected ProgramSideBar sideBar; |
71 | |
|
72 | 0 | private boolean needToLoadOldModel = false; |
73 | |
|
74 | |
private ProgramStatus lastLoadedStatus; |
75 | |
|
76 | 0 | protected boolean reloadMetadata = false; |
77 | |
|
78 | 0 | protected boolean processBeforeShow = true; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public ProgramController(String name, DataModel programModel, ViewContext viewContext, HandlerManager eventBus) { |
86 | 0 | super(); |
87 | 0 | programRemoteService = createProgramRemoteService(); |
88 | 0 | this.eventBus = eventBus; |
89 | 0 | this.programModel = programModel; |
90 | 0 | setViewContext(viewContext); |
91 | 0 | initializeModel(); |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
protected MajorDisciplineRpcServiceAsync createProgramRemoteService() { |
99 | 0 | return GWT.create(MajorDisciplineRpcService.class); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public void beforeViewChange(Enum<?> viewChangingTo, final Callback<Boolean> okToChange) { |
104 | 0 | if (processBeforeShow) { |
105 | 0 | super.beforeViewChange(viewChangingTo, new Callback<Boolean>() { |
106 | |
|
107 | |
@Override |
108 | |
public void exec(Boolean result) { |
109 | 0 | if (result) { |
110 | 0 | if (getCurrentView() instanceof SectionView && ((SectionView) getCurrentView()).isDirty()) { |
111 | 0 | ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup(); |
112 | 0 | final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog = new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes. Save changes?", buttonGroup); |
113 | 0 | buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() { |
114 | |
|
115 | |
@Override |
116 | |
public void exec(ButtonEnumerations.YesNoCancelEnum result) { |
117 | 0 | switch (result) { |
118 | |
case YES: |
119 | 0 | dialog.hide(); |
120 | 0 | fireUpdateEvent(okToChange); |
121 | 0 | break; |
122 | |
case NO: |
123 | 0 | dialog.hide(); |
124 | 0 | resetModel(); |
125 | 0 | needToLoadOldModel = true; |
126 | 0 | resetFieldInteractionFlag(); |
127 | 0 | okToChange.exec(true); |
128 | 0 | break; |
129 | |
case CANCEL: |
130 | 0 | okToChange.exec(false); |
131 | 0 | dialog.hide(); |
132 | |
|
133 | 0 | HistoryManager.logHistoryChange(); |
134 | |
break; |
135 | |
} |
136 | 0 | } |
137 | |
}); |
138 | 0 | dialog.show(); |
139 | 0 | } else { |
140 | 0 | okToChange.exec(true); |
141 | |
} |
142 | |
} else { |
143 | 0 | okToChange.exec(false); |
144 | |
} |
145 | 0 | } |
146 | |
}); |
147 | |
} else { |
148 | 0 | processBeforeShow = true; |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
protected void fireUpdateEvent(final Callback<Boolean> okToChange) { |
153 | 0 | eventBus.fireEvent(new UpdateEvent(okToChange)); |
154 | 0 | } |
155 | |
|
156 | |
protected void resetModel() { |
157 | 0 | programModel.resetRoot(); |
158 | 0 | } |
159 | |
|
160 | |
protected void resetFieldInteractionFlag() { |
161 | 0 | View currentView = getCurrentView(); |
162 | 0 | if (currentView instanceof Section) { |
163 | 0 | ((Section) currentView).resetFieldInteractionFlags(); |
164 | |
} |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private void initializeModel() { |
171 | 0 | setDefaultModelId(ProgramConstants.PROGRAM_MODEL_ID); |
172 | 0 | registerModel(ProgramConstants.PROGRAM_MODEL_ID, new ModelProvider<DataModel>() { |
173 | |
@Override |
174 | |
public void requestModel(final ModelRequestCallback<DataModel> callback) { |
175 | 0 | if (programModel.getRoot() == null || programModel.getRoot().size() == 0) { |
176 | 0 | loadModel(callback); |
177 | |
} else { |
178 | 0 | callback.onModelReady(programModel); |
179 | |
} |
180 | 0 | } |
181 | |
}); |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
@Override |
186 | |
public void requestModel(Class modelType, ModelRequestCallback callback) { |
187 | 0 | if (modelType == ReferenceModel.class) { |
188 | 0 | ReferenceModel referenceModel = new ReferenceModel(); |
189 | 0 | referenceModel.setReferenceId(ProgramUtils.getProgramId(programModel)); |
190 | 0 | referenceModel.setReferenceTypeKey(ProgramConstants.MAJOR_REFERENCE_TYPE_ID); |
191 | 0 | referenceModel.setReferenceType(ProgramConstants.MAJOR_LU_TYPE_ID); |
192 | 0 | Map<String, String> attributes = new HashMap<String, String>(); |
193 | 0 | attributes.put("name", getStringProperty("name")); |
194 | 0 | referenceModel.setReferenceAttributes(attributes); |
195 | 0 | callback.onModelReady(referenceModel); |
196 | 0 | } else { |
197 | 0 | super.requestModel(modelType, callback); |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected void loadModel(final ModelRequestCallback<DataModel> callback) { |
208 | 0 | programRemoteService.getData(getViewContext().getId(), new AbstractCallback<Data>(ProgramProperties.get().common_retrievingData()) { |
209 | |
|
210 | |
@Override |
211 | |
public void onFailure(Throwable caught) { |
212 | 0 | super.onFailure(caught); |
213 | 0 | callback.onRequestFail(caught); |
214 | 0 | } |
215 | |
|
216 | |
@Override |
217 | |
public void onSuccess(Data result) { |
218 | 0 | super.onSuccess(result); |
219 | 0 | programModel.setRoot(result); |
220 | 0 | setHeaderTitle(); |
221 | 0 | callback.onModelReady(programModel); |
222 | 0 | } |
223 | |
}); |
224 | 0 | } |
225 | |
|
226 | |
private void setModelData() { |
227 | 0 | setHeaderTitle(); |
228 | 0 | setStatus(); |
229 | 0 | configurer.applyPermissions(); |
230 | |
|
231 | 0 | if (needToLoadOldModel) { |
232 | 0 | needToLoadOldModel = false; |
233 | |
} else { |
234 | 0 | String id = ProgramUtils.getProgramId(programModel); |
235 | 0 | if (null != id) { |
236 | |
|
237 | 0 | ViewContext docContext = new ViewContext(); |
238 | 0 | docContext.setId(id); |
239 | 0 | docContext.setIdType(IdType.OBJECT_ID); |
240 | 0 | String pgmType = getStringProperty(ProgramConstants.TYPE); |
241 | 0 | docContext.setAttribute(ProgramConstants.TYPE, pgmType + '/' + ProgramSections.PROGRAM_DETAILS_VIEW); |
242 | 0 | RecentlyViewedHelper.addDocument(getProgramName(), |
243 | |
HistoryManager.appendContext(getProgramViewLocation(pgmType), docContext)); |
244 | |
} |
245 | 0 | eventBus.fireEvent(new ModelLoadedEvent(programModel)); |
246 | 0 | onModelLoadedEvent(); |
247 | |
} |
248 | 0 | } |
249 | |
|
250 | |
private String getProgramViewLocation(String pgmType) { |
251 | 0 | if (ProgramClientConstants.MAJOR_PROGRAM.equals(pgmType)) { |
252 | 0 | return AppLocations.Locations.VIEW_PROGRAM.getLocation(); |
253 | 0 | } else if (ProgramClientConstants.CORE_PROGRAM.equals(pgmType)) { |
254 | 0 | return AppLocations.Locations.VIEW_CORE_PROGRAM.getLocation(); |
255 | 0 | } else if (ProgramClientConstants.CREDENTIAL_PROGRAM_TYPES.contains(pgmType)) { |
256 | 0 | return AppLocations.Locations.VIEW_BACC_PROGRAM.getLocation(); |
257 | |
} |
258 | 0 | return null; |
259 | |
} |
260 | |
|
261 | |
protected void setStatus() { |
262 | 0 | statusLabel.setText(ProgramProperties.get().common_status(getStringProperty(ProgramConstants.STATE))); |
263 | 0 | } |
264 | |
|
265 | |
public String getProgramName() { |
266 | 0 | String name = getStringProperty(ProgramConstants.LONG_TITLE); |
267 | 0 | if (name == null) { |
268 | 0 | name = ProgramProperties.get().common_newProgram(); |
269 | |
} |
270 | 0 | return name; |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
@Override |
279 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
280 | 0 | if (programModel.getRoot() == null) { |
281 | 0 | loadModel(new ModelRequestCallback<DataModel>() { |
282 | |
@Override |
283 | |
public void onModelReady(DataModel model) { |
284 | 0 | if (loadMetadataCondition()) { |
285 | 0 | loadMetadata(onReadyCallback); |
286 | |
} else { |
287 | |
|
288 | 0 | onReadyCallback.exec(true); |
289 | |
} |
290 | 0 | } |
291 | |
|
292 | |
@Override |
293 | |
public void onRequestFail(Throwable cause) { |
294 | 0 | GWT.log(cause.getMessage()); |
295 | 0 | } |
296 | |
}); |
297 | |
} else { |
298 | 0 | afterMetadataLoaded(onReadyCallback); |
299 | |
} |
300 | 0 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
protected boolean loadMetadataCondition() { |
308 | 0 | return lastLoadedStatus == null || ProgramStatus.of(programModel) != lastLoadedStatus; |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
protected void loadMetadata(final Callback<Boolean> onReadyCallback) { |
317 | 0 | Map<String, String> idAttributes = new HashMap<String, String>(); |
318 | 0 | ViewContext viewContext = getViewContext(); |
319 | 0 | IdType idType = viewContext.getIdType(); |
320 | 0 | String viewContextId = null; |
321 | 0 | if (idType != null) { |
322 | 0 | idAttributes.put(IdAttributes.ID_TYPE, idType.toString()); |
323 | 0 | viewContextId = viewContext.getId(); |
324 | 0 | if (idType == IdType.COPY_OF_OBJECT_ID) { |
325 | 0 | viewContextId = null; |
326 | |
} |
327 | |
} |
328 | 0 | if (programModel.getRoot() != null) { |
329 | 0 | ProgramStatus programStatus = ProgramStatus.of(programModel); |
330 | 0 | idAttributes.put(DtoConstants.DTO_STATE, programStatus.getValue()); |
331 | 0 | if (programStatus.getNextStatus() != null) { |
332 | 0 | idAttributes.put(DtoConstants.DTO_NEXT_STATE, programStatus.getNextStatus().getValue()); |
333 | |
} |
334 | |
} |
335 | 0 | programRemoteService.getMetadata(viewContextId, idAttributes, new AbstractCallback<Metadata>() { |
336 | |
|
337 | |
@Override |
338 | |
public void onSuccess(Metadata result) { |
339 | 0 | super.onSuccess(result); |
340 | 0 | DataModelDefinition def = new DataModelDefinition(result); |
341 | 0 | programModel.setDefinition(def); |
342 | 0 | lastLoadedStatus = ProgramStatus.of(programModel); |
343 | 0 | afterMetadataLoaded(onReadyCallback); |
344 | 0 | } |
345 | |
|
346 | |
@Override |
347 | |
public void onFailure(Throwable caught) { |
348 | 0 | super.onFailure(caught); |
349 | 0 | onReadyCallback.exec(false); |
350 | 0 | } |
351 | |
}); |
352 | 0 | } |
353 | |
|
354 | |
protected void configureView() { |
355 | 0 | addStyleName("programController"); |
356 | 0 | configurer.setModelDefinition(programModel.getDefinition()); |
357 | 0 | configurer.configure(this); |
358 | 0 | addContentWidget(statusLabel); |
359 | 0 | setSideBarWidget(sideBar); |
360 | 0 | } |
361 | |
|
362 | |
@Override |
363 | |
public void setViewContext(ViewContext viewContext) { |
364 | 0 | super.setViewContext(viewContext); |
365 | 0 | if (viewContext.getId() != null && !viewContext.getId().isEmpty()) { |
366 | 0 | viewContext.setPermissionType(PermissionType.OPEN); |
367 | |
} else { |
368 | 0 | viewContext.setPermissionType(PermissionType.INITIATE); |
369 | |
} |
370 | 0 | } |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
protected void afterMetadataLoaded(Callback<Boolean> onReadyCallback) { |
378 | 0 | if (!reloadMetadata) { |
379 | 0 | configureView(); |
380 | 0 | onReadyCallback.exec(true); |
381 | 0 | reloadMetadata = true; |
382 | |
} else { |
383 | 0 | onReadyCallback.exec(true); |
384 | 0 | ProgramUtils.syncMetadata(configurer, programModel.getDefinition()); |
385 | |
} |
386 | 0 | if (programModel.getRoot() != null) { |
387 | 0 | setModelData(); |
388 | |
} |
389 | 0 | } |
390 | |
|
391 | |
protected void setHeaderTitle() { |
392 | 0 | String title = getProgramName(); |
393 | 0 | this.setContentTitle(title); |
394 | 0 | this.setName(title); |
395 | 0 | } |
396 | |
|
397 | |
protected Widget createCommentPanel() { |
398 | 0 | final CommentTool commentTool = new CommentTool(ProgramSections.COMMENTS, "Comments", "kuali.comment.type.generalRemarks", "Program Comments"); |
399 | 0 | commentTool.setController(this); |
400 | 0 | KSButton commentsButton = new KSButton(ProgramProperties.get().comments_button(), KSButtonAbstract.ButtonStyle.DEFAULT_ANCHOR, new ClickHandler() { |
401 | |
|
402 | |
@Override |
403 | |
public void onClick(ClickEvent event) { |
404 | 0 | commentTool.show(); |
405 | 0 | } |
406 | |
}); |
407 | 0 | return commentsButton; |
408 | |
} |
409 | |
|
410 | |
protected void doSave() { |
411 | 0 | } |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
protected void updateState(String state, final Callback<Boolean> okCallback) { |
438 | |
|
439 | 0 | programRemoteService.updateState(programModel.getRoot(), state, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) { |
440 | |
@Override |
441 | |
public void onSuccess(DataSaveResult result) { |
442 | 0 | super.onSuccess(result); |
443 | 0 | okCallback.exec(true); |
444 | 0 | refreshModelAndView(result); |
445 | 0 | } |
446 | |
}); |
447 | |
|
448 | 0 | } |
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
public void refreshModelAndView(DataSaveResult result){ |
455 | 0 | if (result != null) { |
456 | 0 | programModel.setRoot(result.getValue()); |
457 | |
} |
458 | 0 | setHeaderTitle(); |
459 | 0 | setStatus(); |
460 | 0 | } |
461 | |
|
462 | |
public DataModel getProgramModel() { |
463 | 0 | return programModel; |
464 | |
} |
465 | |
|
466 | |
public void onModelLoadedEvent() { |
467 | 0 | } |
468 | |
|
469 | |
protected String getStringProperty(String key) { |
470 | 0 | return programModel.get(key); |
471 | |
} |
472 | |
|
473 | |
protected Data getDataProperty(String key) { |
474 | 0 | return programModel.get(key); |
475 | |
} |
476 | |
|
477 | |
public boolean isExportButtonActive() { |
478 | 0 | if (this.getCurrentViewEnum() != null) { |
479 | 0 | if (this.getCurrentViewEnum().equals(ProgramSections.SUMMARY) |
480 | |
|| this.getCurrentViewEnum().equals(ProgramSections.VIEW_ALL)) { |
481 | 0 | return true; |
482 | |
} else { |
483 | 0 | return false; |
484 | |
} |
485 | |
|
486 | |
} else { |
487 | 0 | return false; |
488 | |
} |
489 | |
} |
490 | |
|
491 | |
@Override |
492 | |
public ArrayList<ExportElement> getExportElementsFromView() { |
493 | |
|
494 | 0 | String viewName = null; |
495 | 0 | String sectionTitle = null; |
496 | 0 | View currentView = this.getCurrentView(); |
497 | 0 | if (currentView != null) { |
498 | |
|
499 | 0 | ArrayList<ExportElement> exportElements = new ArrayList<ExportElement>(); |
500 | 0 | if (currentView != null && currentView instanceof Section) { |
501 | 0 | Section currentSection = (Section) currentView; |
502 | 0 | List<Section> nestedSections = currentSection.getSections(); |
503 | 0 | for (int i = 0; i < nestedSections.size(); i++) { |
504 | 0 | ExportElement sectionExportItem = new ExportElement(); |
505 | 0 | ArrayList<ExportElement> subList = null; |
506 | 0 | Section nestedSection = nestedSections.get(i); |
507 | 0 | if (nestedSection != null && nestedSection instanceof SectionView) { |
508 | 0 | SectionView nestedSectionView = (SectionView) nestedSection; |
509 | 0 | viewName = nestedSectionView.getName(); |
510 | 0 | sectionTitle = nestedSectionView.getTitle(); |
511 | 0 | sectionExportItem.setSectionName(sectionTitle + " " + i + " - " + viewName); |
512 | 0 | sectionExportItem.setViewName(sectionTitle + " " + i + " - " + viewName); |
513 | 0 | subList = ExportUtils.getExportElementsFromView(nestedSectionView, subList, viewName, sectionTitle); |
514 | 0 | if (subList != null && subList.size()> 0) { |
515 | 0 | sectionExportItem.setSubset(subList); |
516 | 0 | exportElements.add(sectionExportItem); |
517 | |
} |
518 | |
} |
519 | |
} |
520 | |
} |
521 | 0 | return exportElements; |
522 | |
|
523 | |
} else { |
524 | |
|
525 | |
} |
526 | 0 | return null; |
527 | |
|
528 | |
} |
529 | |
} |