1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lu.ui.tools.client.configuration; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
21 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.BasicLayout; |
22 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
23 | |
import org.kuali.student.common.ui.client.event.SaveActionEvent; |
24 | |
import org.kuali.student.common.ui.client.event.SaveActionHandler; |
25 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
26 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
27 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
28 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
29 | |
import org.kuali.student.common.ui.client.mvc.ModelProvider; |
30 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
31 | |
import org.kuali.student.common.ui.client.mvc.View; |
32 | |
import org.kuali.student.common.ui.client.mvc.WorkQueue; |
33 | |
import org.kuali.student.common.ui.client.mvc.WorkQueue.WorkItem; |
34 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
35 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
36 | |
import org.kuali.student.common.ui.client.widgets.KSLightBox; |
37 | |
import org.kuali.student.common.ui.client.widgets.KSProgressIndicator; |
38 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations; |
39 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.OkGroup; |
40 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.ButtonEnum; |
41 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.OkEnum; |
42 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ActionCancelGroup; |
43 | |
import org.kuali.student.common.ui.client.widgets.progress.BlockingTask; |
44 | |
import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator; |
45 | |
import org.kuali.student.core.assembly.data.Data; |
46 | |
import org.kuali.student.core.assembly.data.Metadata; |
47 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
48 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel; |
49 | |
import org.kuali.student.lum.common.client.widgets.CluSetHelper; |
50 | |
import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcService; |
51 | |
import org.kuali.student.lum.common.client.widgets.CluSetManagementRpcServiceAsync; |
52 | |
|
53 | |
import com.google.gwt.core.client.GWT; |
54 | |
import com.google.gwt.dom.client.Style; |
55 | |
import com.google.gwt.user.client.Window; |
56 | |
import com.google.gwt.user.client.ui.Widget; |
57 | |
|
58 | 0 | public class CluSetsManagementController extends BasicLayout { |
59 | |
|
60 | 0 | private final DataModel cluSetModel = new DataModel(); |
61 | |
private WorkQueue cluSetModelRequestQueue; |
62 | |
|
63 | |
private ClusetView mainView; |
64 | |
private ClusetView createClusetView; |
65 | |
private ClusetView editClusetView; |
66 | |
private ClusetView viewClusetView; |
67 | |
|
68 | 0 | private boolean initialized = false; |
69 | 0 | CluSetManagementRpcServiceAsync cluSetManagementRpcServiceAsync = GWT.create(CluSetManagementRpcService.class); |
70 | |
public static final String CLUSET_MGT_MODEL = "cluSetManagementModel"; |
71 | 0 | private BlockingTask retrievingTask = new BlockingTask("Retrieving ..."); |
72 | |
|
73 | 0 | final KSLightBox progressWindow = new KSLightBox(); |
74 | |
|
75 | |
|
76 | |
public CluSetsManagementController(){ |
77 | 0 | super(CluSetsManagementController.class.getName()); |
78 | 0 | setName("Course Set Management"); |
79 | 0 | initialize(); |
80 | 0 | } |
81 | |
|
82 | |
public ClusetView getMainView() { |
83 | 0 | return mainView; |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public <V extends Enum<?>> void showView(final V viewType) { |
88 | 0 | if (viewType == ClusetView.CluSetsManagementViews.EDIT) { |
89 | 0 | final String cluSetId = mainView.getSelectedCluSetId(); |
90 | 0 | editClusetView.setSelectedCluSetId(cluSetId); |
91 | 0 | viewClusetView.setSelectedCluSetId(cluSetId); |
92 | 0 | if (cluSetId != null) { |
93 | 0 | KSBlockingProgressIndicator.addTask(retrievingTask); |
94 | 0 | cluSetManagementRpcServiceAsync.getData(cluSetId, new KSAsyncCallback<Data>() { |
95 | |
@Override |
96 | |
public void handleFailure(Throwable caught) { |
97 | 0 | KSBlockingProgressIndicator.removeTask(retrievingTask); |
98 | 0 | Window.alert("Failed to retrieve cluset with id" + cluSetId); |
99 | 0 | } |
100 | |
@Override |
101 | |
public void onSuccess(Data result) { |
102 | |
try { |
103 | 0 | cluSetModel.setRoot(result); |
104 | 0 | editClusetView.updateView(cluSetModel); |
105 | 0 | CluSetsManagementController.super.showView(viewType); |
106 | |
} finally { |
107 | 0 | KSBlockingProgressIndicator.removeTask(retrievingTask); |
108 | 0 | } |
109 | 0 | } |
110 | |
}); |
111 | |
} |
112 | 0 | } else if (viewType == ClusetView.CluSetsManagementViews.VIEW) { |
113 | 0 | final String cluSetId = mainView.getSelectedCluSetId(); |
114 | 0 | editClusetView.setSelectedCluSetId(cluSetId); |
115 | 0 | viewClusetView.setSelectedCluSetId(cluSetId); |
116 | 0 | if (cluSetId != null) { |
117 | 0 | KSBlockingProgressIndicator.addTask(retrievingTask); |
118 | 0 | cluSetManagementRpcServiceAsync.getData(cluSetId, new KSAsyncCallback<Data>() { |
119 | |
@Override |
120 | |
public void handleFailure(Throwable caught) { |
121 | 0 | KSBlockingProgressIndicator.removeTask(retrievingTask); |
122 | 0 | Window.alert("Failed to retrieve cluset with id" + cluSetId); |
123 | 0 | } |
124 | |
@Override |
125 | |
public void onSuccess(Data result) { |
126 | |
try { |
127 | 0 | cluSetModel.setRoot(result); |
128 | 0 | viewClusetView.updateView(cluSetModel); |
129 | 0 | CluSetsManagementController.super.showView(viewType); |
130 | |
} finally { |
131 | 0 | KSBlockingProgressIndicator.removeTask(retrievingTask); |
132 | 0 | } |
133 | 0 | } |
134 | |
}); |
135 | |
} |
136 | 0 | } else { |
137 | 0 | cluSetModel.setRoot(new Data()); |
138 | 0 | super.showView(viewType); |
139 | |
} |
140 | 0 | } |
141 | |
|
142 | |
private Widget getButtonPanel() { |
143 | 0 | ActionCancelGroup actionCancel = new ActionCancelGroup( |
144 | |
ButtonEnumerations.SaveCancelEnum.SAVE, |
145 | |
ButtonEnumerations.SaveCancelEnum.CANCEL); |
146 | 0 | actionCancel.addCallback(new Callback<ButtonEnumerations.ButtonEnum>() { |
147 | |
@Override |
148 | |
public void exec(ButtonEnum result) { |
149 | 0 | if (result == ButtonEnumerations.SaveCancelEnum.SAVE) { |
150 | 0 | fireApplicationEvent(new SaveActionEvent()); |
151 | 0 | } else if (result == ButtonEnumerations.SaveCancelEnum.CANCEL) { |
152 | 0 | showView(ClusetView.CluSetsManagementViews.MAIN); |
153 | |
} |
154 | 0 | } |
155 | |
}); |
156 | 0 | return actionCancel; |
157 | |
} |
158 | |
|
159 | |
private void initialize() { |
160 | 0 | super.setDefaultModelId(CLUSET_MGT_MODEL); |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | 0 | createClusetView = new ClusetView(ClusetView.CluSetsManagementViews.CREATE, |
166 | 0 | "Build Course Set", CLUSET_MGT_MODEL, new Callback<Boolean>() { |
167 | |
@Override |
168 | |
public void exec(Boolean result) { |
169 | 0 | if (result == true) { |
170 | 0 | Widget buttonPanel = getButtonPanel(); |
171 | 0 | buttonPanel.getElement().getStyle().setPaddingTop(50, Style.Unit.PX); |
172 | 0 | createClusetView.addWidget(buttonPanel); |
173 | |
} |
174 | 0 | } |
175 | |
}); |
176 | 0 | editClusetView = new ClusetView(ClusetView.CluSetsManagementViews.EDIT, |
177 | 0 | "Edit Course Set", CLUSET_MGT_MODEL, new Callback<Boolean>() { |
178 | |
@Override |
179 | |
public void exec(Boolean result) { |
180 | 0 | if (result == true) { |
181 | 0 | Widget buttonPanel = getButtonPanel(); |
182 | 0 | buttonPanel.getElement().getStyle().setPaddingTop(50, Style.Unit.PX); |
183 | 0 | editClusetView.addWidget(buttonPanel); |
184 | |
} |
185 | 0 | } |
186 | |
}); |
187 | 0 | viewClusetView = new ClusetView(ClusetView.CluSetsManagementViews.VIEW, |
188 | |
"View Course Set", CLUSET_MGT_MODEL, null); |
189 | |
|
190 | 0 | mainView = new ClusetView(ClusetView.CluSetsManagementViews.MAIN, |
191 | |
"", CLUSET_MGT_MODEL, null); |
192 | |
|
193 | 0 | setDefaultView(ClusetView.CluSetsManagementViews.MAIN); |
194 | 0 | addView(createClusetView); |
195 | 0 | addView(editClusetView); |
196 | 0 | addView(viewClusetView); |
197 | 0 | addView(mainView); |
198 | |
|
199 | 0 | super.registerModel(CLUSET_MGT_MODEL, new ModelProvider<DataModel>() { |
200 | |
|
201 | |
@Override |
202 | |
public void requestModel(final ModelRequestCallback<DataModel> callback) { |
203 | 0 | if (cluSetModelRequestQueue == null){ |
204 | 0 | cluSetModelRequestQueue = new WorkQueue(); |
205 | |
} |
206 | |
|
207 | 0 | WorkItem workItem = new WorkItem(){ |
208 | |
@Override |
209 | |
public void exec(Callback<Boolean> workCompleteCallback) { |
210 | 0 | if (cluSetModel.getRoot() == null || cluSetModel.getRoot().size() == 0){ |
211 | 0 | cluSetModel.setRoot(new Data()); |
212 | |
} |
213 | 0 | callback.onModelReady(cluSetModel); |
214 | 0 | workCompleteCallback.exec(true); |
215 | |
|
216 | 0 | } |
217 | |
}; |
218 | 0 | cluSetModelRequestQueue.submit(workItem); |
219 | 0 | } |
220 | |
|
221 | |
}); |
222 | |
|
223 | 0 | } |
224 | |
|
225 | |
private void init(final Callback<Boolean> onReadyCallback) { |
226 | 0 | KSProgressIndicator progressInd = new KSProgressIndicator(); |
227 | 0 | progressInd.setText("Loading"); |
228 | 0 | progressInd.show(); |
229 | 0 | progressWindow.setWidget(progressInd); |
230 | |
|
231 | 0 | if (initialized) { |
232 | 0 | onReadyCallback.exec(true); |
233 | |
} else { |
234 | 0 | progressWindow.show(); |
235 | |
|
236 | 0 | cluSetManagementRpcServiceAsync.getMetadata("courseSet", null, new KSAsyncCallback<Metadata>(){ |
237 | |
|
238 | |
@Override |
239 | |
public void handleFailure(Throwable caught) { |
240 | 0 | onReadyCallback.exec(false); |
241 | 0 | progressWindow.hide(); |
242 | 0 | throw new RuntimeException("Failed to get model definition.", caught); |
243 | |
} |
244 | |
|
245 | |
@Override |
246 | |
public void onSuccess(Metadata result) { |
247 | 0 | DataModelDefinition def = new DataModelDefinition(result); |
248 | 0 | cluSetModel.setDefinition(def); |
249 | 0 | init(def); |
250 | 0 | initialized = true; |
251 | 0 | onReadyCallback.exec(true); |
252 | 0 | progressWindow.hide(); |
253 | 0 | } |
254 | |
}); |
255 | |
} |
256 | 0 | } |
257 | |
|
258 | |
private void init(DataModelDefinition modelDefinition){ |
259 | |
|
260 | 0 | if (!initialized){ |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | 0 | addApplicationEventHandler(SaveActionEvent.TYPE, new SaveActionHandler(){ |
268 | |
@Override |
269 | |
public void doSave(SaveActionEvent saveAction) { |
270 | 0 | GWT.log("CluSetManagementController received save action request.", null); |
271 | 0 | doSaveAction(saveAction); |
272 | 0 | } |
273 | |
}); |
274 | |
} |
275 | |
|
276 | 0 | initialized = true; |
277 | 0 | } |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
@Override |
283 | |
public Class<? extends Enum<?>> getViewsEnum() { |
284 | 0 | return ClusetView.CluSetsManagementViews.class; |
285 | |
} |
286 | |
|
287 | |
@Override |
288 | |
protected void renderView(View view) { |
289 | 0 | super.renderView(view); |
290 | |
|
291 | |
|
292 | 0 | } |
293 | |
|
294 | |
@SuppressWarnings("unchecked") |
295 | |
@Override |
296 | |
public void requestModel(Class modelType, final ModelRequestCallback callback) { |
297 | 0 | super.requestModel(modelType, callback); |
298 | 0 | } |
299 | |
|
300 | |
public void doSaveAction(final SaveActionEvent saveActionEvent){ |
301 | |
|
302 | |
|
303 | 0 | getCurrentView().updateModel(); |
304 | |
|
305 | 0 | if(cluSetModel!=null){ |
306 | 0 | cluSetModel.validate(new Callback<List<ValidationResultInfo>>() { |
307 | |
@Override |
308 | |
public void exec(List<ValidationResultInfo> result) { |
309 | |
|
310 | 0 | boolean save = true; |
311 | 0 | View v = getCurrentView(); |
312 | 0 | if(v instanceof Section){ |
313 | 0 | ((Section) v).setFieldHasHadFocusFlags(true); |
314 | 0 | ErrorLevel status = ((Section) v).processValidationResults(result); |
315 | 0 | if(status == ErrorLevel.ERROR){ |
316 | 0 | save = false; |
317 | |
} |
318 | |
} |
319 | |
|
320 | 0 | if(save){ |
321 | 0 | getCurrentView().updateModel(); |
322 | 0 | CluSetsManagementController.this.updateModel(); |
323 | |
|
324 | |
|
325 | 0 | CluSetHelper.wrap((Data)cluSetModel.getRoot()).setReusable(new Boolean(true)); |
326 | 0 | saveModel(cluSetModel, saveActionEvent); |
327 | |
} |
328 | |
else{ |
329 | 0 | Window.alert("Save failed. Please check fields for errors."); |
330 | |
} |
331 | |
|
332 | 0 | } |
333 | |
}); |
334 | |
} |
335 | 0 | } |
336 | |
|
337 | |
private void saveModel(final DataModel dataModel, final SaveActionEvent saveActionEvent) { |
338 | 0 | final KSLightBox saveWindow = new KSLightBox(); |
339 | 0 | final KSLabel saveMessage = new KSLabel(saveActionEvent.getMessage() + "..."); |
340 | 0 | final OkGroup buttonGroup = new OkGroup(new Callback<OkEnum>(){ |
341 | |
|
342 | |
@Override |
343 | |
public void exec(OkEnum result) { |
344 | 0 | saveWindow.hide(); |
345 | 0 | saveActionEvent.doActionComplete(); |
346 | 0 | } |
347 | |
}); |
348 | |
|
349 | 0 | buttonGroup.setWidth("250px"); |
350 | 0 | buttonGroup.getButton(OkEnum.Ok).setEnabled(false); |
351 | 0 | buttonGroup.setContent(saveMessage); |
352 | |
|
353 | |
|
354 | 0 | if (saveActionEvent.isAcknowledgeRequired()){ |
355 | 0 | saveWindow.setWidget(buttonGroup); |
356 | |
} else { |
357 | 0 | saveWindow.setWidget(saveMessage); |
358 | |
} |
359 | 0 | saveWindow.show(); |
360 | |
|
361 | 0 | final Callback<Throwable> saveFailedCallback = new Callback<Throwable>() { |
362 | |
|
363 | |
@Override |
364 | |
public void exec(Throwable caught) { |
365 | 0 | GWT.log("Save Failed.", caught); |
366 | 0 | saveWindow.setWidget(buttonGroup); |
367 | 0 | saveMessage.setText("Save Failed! Please Try Again."); |
368 | 0 | buttonGroup.getButton(OkEnum.Ok).setEnabled(true); |
369 | 0 | } |
370 | |
|
371 | |
}; |
372 | |
try { |
373 | 0 | cluSetManagementRpcServiceAsync.saveData(dataModel.getRoot(), new KSAsyncCallback<DataSaveResult>() { |
374 | |
@Override |
375 | |
public void handleFailure(Throwable caught) { |
376 | 0 | saveFailedCallback.exec(caught); |
377 | 0 | } |
378 | |
|
379 | |
@Override |
380 | |
public void onSuccess(DataSaveResult result) { |
381 | 0 | if (result.getValidationResults() != null && |
382 | |
!result.getValidationResults().isEmpty()) { |
383 | 0 | StringBuilder errorMessage = new StringBuilder(); |
384 | 0 | errorMessage.append("Validation error: "); |
385 | 0 | for (ValidationResultInfo validationError : result.getValidationResults()) { |
386 | 0 | errorMessage.append(validationError.getMessage()).append(" "); |
387 | |
} |
388 | 0 | saveMessage.setText(errorMessage.toString()); |
389 | 0 | buttonGroup.getButton(OkEnum.Ok).setEnabled(true); |
390 | 0 | } else { |
391 | 0 | dataModel.setRoot(result.getValue()); |
392 | 0 | if (saveActionEvent.isAcknowledgeRequired()){ |
393 | 0 | saveMessage.setText("Save Successful"); |
394 | 0 | buttonGroup.getButton(OkEnum.Ok).setEnabled(true); |
395 | |
} else { |
396 | 0 | saveWindow.hide(); |
397 | 0 | saveActionEvent.doActionComplete(); |
398 | |
} |
399 | |
} |
400 | 0 | } |
401 | |
}); |
402 | |
|
403 | 0 | } catch (Exception e) { |
404 | 0 | saveFailedCallback.exec(e); |
405 | 0 | } |
406 | 0 | } |
407 | |
|
408 | |
@Override |
409 | |
public void showDefaultView(final Callback<Boolean> onReadyCallback) { |
410 | 0 | init(new Callback<Boolean>() { |
411 | |
@Override |
412 | |
public void exec(Boolean result) { |
413 | 0 | if (result) { |
414 | 0 | doShowDefaultView(onReadyCallback); |
415 | |
} else { |
416 | 0 | onReadyCallback.exec(false); |
417 | |
} |
418 | 0 | } |
419 | |
}); |
420 | 0 | } |
421 | |
|
422 | |
@Override |
423 | |
public void beforeShow(Callback<Boolean> onReadyCallback) { |
424 | 0 | showDefaultView(onReadyCallback); |
425 | 0 | } |
426 | |
|
427 | |
private void doShowDefaultView(final Callback<Boolean> onReadyCallback) { |
428 | 0 | super.showDefaultView(onReadyCallback); |
429 | 0 | } |
430 | |
|
431 | |
@Override |
432 | |
public void setParentController(Controller controller) { |
433 | 0 | super.setParentController(controller); |
434 | 0 | } |
435 | |
|
436 | |
} |