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