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