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