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