1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.configurable.mvc; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.LinkedHashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Map.Entry; |
23 | |
|
24 | |
import org.kuali.student.common.assembly.data.QueryPath; |
25 | |
import org.kuali.student.common.ui.client.application.Application; |
26 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.MenuSectionController; |
27 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.TabMenuController; |
28 | |
import org.kuali.student.common.ui.client.configurable.mvc.layouts.ViewLayoutController; |
29 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
30 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
31 | |
import org.kuali.student.common.ui.client.event.ActionEvent; |
32 | |
import org.kuali.student.common.ui.client.event.SaveActionEvent; |
33 | |
import org.kuali.student.common.ui.client.event.SectionUpdateEvent; |
34 | |
import org.kuali.student.common.ui.client.event.SectionUpdateHandler; |
35 | |
import org.kuali.student.common.ui.client.event.ValidateRequestEvent; |
36 | |
import org.kuali.student.common.ui.client.event.ValidateRequestHandler; |
37 | |
import org.kuali.student.common.ui.client.mvc.ActionCompleteCallback; |
38 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
39 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
40 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
41 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
42 | |
import org.kuali.student.common.ui.client.mvc.View; |
43 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
44 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
45 | |
import org.kuali.student.common.ui.client.widgets.KSLightBox; |
46 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
47 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
48 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
49 | |
|
50 | |
import com.google.gwt.core.client.GWT; |
51 | |
import com.google.gwt.event.dom.client.ClickEvent; |
52 | |
import com.google.gwt.event.dom.client.ClickHandler; |
53 | |
import com.google.gwt.user.client.ui.FlowPanel; |
54 | |
import com.google.gwt.user.client.ui.Widget; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public abstract class LayoutController extends Controller implements ViewLayoutController, View { |
65 | |
|
66 | 0 | protected Map<Enum<?>, View> viewMap = new LinkedHashMap<Enum<?>, View>(); |
67 | 0 | protected Map<String, Enum<?>> viewEnumMap = new HashMap<String, Enum<?>>(); |
68 | |
protected Enum<?> defaultView; |
69 | |
|
70 | |
protected String name; |
71 | |
protected Enum<?> viewType; |
72 | |
|
73 | |
protected View startPopupView; |
74 | |
protected KSLightBox startViewWindow; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public LayoutController(){ |
81 | 0 | super(); |
82 | |
|
83 | 0 | addApplicationEventHandler(SectionUpdateEvent.TYPE, new SectionUpdateHandler(){ |
84 | |
|
85 | |
@Override |
86 | |
public void onSectionUpdate(final SectionUpdateEvent event) { |
87 | 0 | LayoutController.this.requestModel(new ModelRequestCallback<DataModel>(){ |
88 | |
|
89 | |
@Override |
90 | |
public void onRequestFail(Throwable cause) { |
91 | 0 | GWT.log("Unable to retrieve model for section update", cause); |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public void onModelReady(DataModel model) { |
96 | 0 | event.getSection().updateModel(model); |
97 | 0 | event.getSection().updateWidgetData(model); |
98 | |
|
99 | 0 | } |
100 | |
}); |
101 | |
|
102 | 0 | } |
103 | |
}); |
104 | |
|
105 | 0 | addApplicationEventHandler(ValidateRequestEvent.TYPE, new ValidateRequestHandler() { |
106 | |
|
107 | |
@Override |
108 | |
public void onValidateRequest(final ValidateRequestEvent event) { |
109 | 0 | FieldDescriptor originatingField = event.getFieldDescriptor(); |
110 | 0 | String modelId = null; |
111 | 0 | if (originatingField != null) { |
112 | 0 | modelId = originatingField.getModelId(); |
113 | |
} |
114 | 0 | if (modelId == null) { |
115 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
116 | |
@Override |
117 | |
public void onModelReady(DataModel model) { |
118 | 0 | validate(model, event); |
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
public void onRequestFail(Throwable cause) { |
123 | 0 | GWT.log("Unable to retrieve model for validation", cause); |
124 | 0 | } |
125 | |
|
126 | |
}); |
127 | |
} else { |
128 | 0 | requestModel(modelId, new ModelRequestCallback<DataModel>() { |
129 | |
@Override |
130 | |
public void onModelReady(DataModel model) { |
131 | 0 | validate(model, event); |
132 | 0 | } |
133 | |
|
134 | |
@Override |
135 | |
public void onRequestFail(Throwable cause) { |
136 | 0 | GWT.log("Unable to retrieve model for validation", cause); |
137 | 0 | } |
138 | |
|
139 | |
}); |
140 | |
} |
141 | 0 | } |
142 | |
|
143 | |
}); |
144 | 0 | } |
145 | |
|
146 | |
private void validate(DataModel model, final ValidateRequestEvent event) { |
147 | 0 | if(event.validateSingleField()){ |
148 | 0 | model.validateField(event.getFieldDescriptor(), new Callback<List<ValidationResultInfo>>() { |
149 | |
@Override |
150 | |
public void exec(List<ValidationResultInfo> result) { |
151 | 0 | if(event.getFieldDescriptor() != null) { |
152 | |
|
153 | |
|
154 | 0 | FieldElement element = event.getFieldDescriptor().getFieldElement(); |
155 | 0 | Widget w = event.getFieldDescriptor().getFieldWidget(); |
156 | 0 | if(element != null) { |
157 | 0 | element.clearValidationErrors(); |
158 | |
|
159 | 0 | if ((w instanceof CanProcessValidationResults) && ((CanProcessValidationResults) w).doesOnTheFlyValidation()) { |
160 | 0 | ((CanProcessValidationResults) w).Validate(event, result); |
161 | |
} else { |
162 | 0 | for(int i = 0; i < result.size(); i++) { |
163 | 0 | ValidationResultInfo vr = result.get(i); |
164 | 0 | if (vr.getElement().equals(event.getFieldDescriptor().getFieldKey()) && event.getFieldDescriptor().hasHadFocus()) { |
165 | 0 | element.processValidationResult(vr); |
166 | |
} |
167 | |
} |
168 | |
} |
169 | |
} |
170 | |
} |
171 | |
|
172 | 0 | } |
173 | |
}); |
174 | |
} else { |
175 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
176 | |
@Override |
177 | |
public void exec(List<ValidationResultInfo> result) { |
178 | 0 | isValid(result, false, true); |
179 | 0 | } |
180 | |
}); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public ErrorLevel checkForErrors(List<ValidationResultInfo> list){ |
190 | 0 | ErrorLevel errorLevel = ErrorLevel.OK; |
191 | |
|
192 | 0 | for(ValidationResultInfo vr: list){ |
193 | 0 | if(vr.getErrorLevel().getLevel() > errorLevel.getLevel()){ |
194 | 0 | errorLevel = vr.getErrorLevel(); |
195 | |
} |
196 | 0 | if(errorLevel.equals(ErrorLevel.ERROR)){ |
197 | 0 | break; |
198 | |
} |
199 | |
} |
200 | |
|
201 | 0 | return errorLevel; |
202 | |
|
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public static LayoutController findParentLayout(Widget w){ |
212 | 0 | LayoutController result = null; |
213 | |
while (true) { |
214 | 0 | if (w == null) { |
215 | 0 | break; |
216 | 0 | } else if (w instanceof HasLayoutController) { |
217 | 0 | result = ((HasLayoutController)w).getLayoutController(); |
218 | 0 | if (result != null) { |
219 | 0 | break; |
220 | |
} |
221 | 0 | } else if (w instanceof LayoutController) { |
222 | 0 | result = (LayoutController) w; |
223 | 0 | break; |
224 | |
} |
225 | 0 | w = w.getParent(); |
226 | |
|
227 | |
} |
228 | 0 | return result; |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public void addStartViewPopup(final View view){ |
235 | 0 | startPopupView = view; |
236 | 0 | if(startViewWindow == null){ |
237 | 0 | startViewWindow = new KSLightBox(); |
238 | |
} |
239 | |
|
240 | 0 | FlowPanel panel = new FlowPanel(); |
241 | 0 | panel.add(view.asWidget()); |
242 | 0 | KSButton save = new KSButton("Save",new ClickHandler(){ |
243 | |
public void onClick(ClickEvent event) { |
244 | 0 | view.updateModel(); |
245 | 0 | SaveActionEvent saveActionEvent = new SaveActionEvent(true); |
246 | |
|
247 | 0 | saveActionEvent.setActionCompleteCallback(new ActionCompleteCallback(){ |
248 | |
public void onActionComplete(ActionEvent action) { |
249 | 0 | startViewWindow.hide(); |
250 | 0 | } |
251 | |
}); |
252 | |
|
253 | |
|
254 | 0 | fireApplicationEvent(saveActionEvent); |
255 | 0 | } |
256 | |
}); |
257 | 0 | startViewWindow.addButton(save); |
258 | |
|
259 | 0 | KSButton cancel = new KSButton("Cancel", new ClickHandler(){ |
260 | |
public void onClick(ClickEvent event) { |
261 | 0 | startViewWindow.hide(); |
262 | 0 | } |
263 | |
}); |
264 | 0 | startViewWindow.addButton(cancel); |
265 | |
|
266 | 0 | if(view instanceof SectionView){ |
267 | 0 | ((SectionView) view).setController(this); |
268 | |
} |
269 | 0 | startViewWindow.setWidget(panel); |
270 | 0 | } |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public boolean isStartViewShowing(){ |
276 | 0 | if(startViewWindow == null){ |
277 | 0 | return false; |
278 | |
} |
279 | 0 | return startViewWindow.isShowing(); |
280 | |
} |
281 | |
|
282 | |
public View getStartPopupView(){ |
283 | 0 | return startPopupView; |
284 | |
} |
285 | |
|
286 | |
public void showStartPopup(final Callback<Boolean> onReadyCallback){ |
287 | 0 | startPopupView.beforeShow(new Callback<Boolean>() { |
288 | |
@Override |
289 | |
public void exec(Boolean result) { |
290 | 0 | if (result) { |
291 | 0 | startViewWindow.show(); |
292 | |
} |
293 | 0 | onReadyCallback.exec(result); |
294 | 0 | } |
295 | |
}); |
296 | 0 | } |
297 | |
|
298 | |
public KSLightBox getStartPopup(){ |
299 | 0 | return startViewWindow; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public void addView(View view){ |
309 | 0 | viewMap.put(view.getViewEnum(), view); |
310 | 0 | viewEnumMap.put(view.getViewEnum().toString(), view.getViewEnum()); |
311 | 0 | if(view instanceof SectionView){ |
312 | 0 | ((SectionView) view).setController(this); |
313 | |
} |
314 | 0 | else if(view instanceof ToolView){ |
315 | 0 | ((ToolView) view).setController(this); |
316 | |
} |
317 | 0 | } |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public <V extends Enum<?>> void setDefaultView(V viewType){ |
323 | 0 | this.defaultView = viewType; |
324 | 0 | } |
325 | |
|
326 | |
public Enum<?> getDefaultView(){ |
327 | 0 | return this.defaultView; |
328 | |
} |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public abstract void updateModel(); |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public void updateModelFromView(Enum<?> viewType){ |
340 | 0 | View v = viewMap.get(viewType); |
341 | 0 | if(v != null){ |
342 | 0 | v.updateModel(); |
343 | |
} |
344 | 0 | } |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
public void updateModelFromCurrentView(){ |
350 | 0 | if(this.getCurrentView() != null){ |
351 | 0 | this.getCurrentView().updateModel(); |
352 | |
} |
353 | 0 | } |
354 | |
|
355 | |
@Override |
356 | |
public <V extends Enum<?>> void getView(V viewType, Callback<View> callback) { |
357 | 0 | callback.exec(viewMap.get(viewType)); |
358 | 0 | } |
359 | |
|
360 | |
@Override |
361 | |
public Enum<?> getViewEnumValue(String enumValue) { |
362 | 0 | return viewEnumMap.get(enumValue); |
363 | |
} |
364 | |
|
365 | |
@Override |
366 | |
public void showDefaultView(final Callback<Boolean> onReadyCallback) { |
367 | 0 | HistoryManager.setLogNavigationHistory(false); |
368 | |
|
369 | 0 | if(defaultView != null){ |
370 | 0 | showView(defaultView, onReadyCallback); |
371 | |
} |
372 | 0 | else if(!viewMap.isEmpty()){ |
373 | 0 | if(defaultView == null){ |
374 | 0 | showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); |
375 | |
} |
376 | |
} |
377 | |
|
378 | 0 | } |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public void showFirstView(Callback<Boolean> onReadyCallback){ |
387 | 0 | HistoryManager.setLogNavigationHistory(false); |
388 | 0 | if(!viewMap.isEmpty()){ |
389 | 0 | showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); |
390 | |
} |
391 | |
else{ |
392 | 0 | showDefaultView(onReadyCallback); |
393 | |
} |
394 | 0 | } |
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly){ |
405 | 0 | return isValid(validationResults, checkCurrentSectionOnly, true); |
406 | |
} |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly, boolean allFields){ |
416 | |
|
417 | 0 | boolean isValid = true; |
418 | |
|
419 | 0 | clearAllWarnings(); |
420 | |
|
421 | 0 | if (checkCurrentSectionOnly){ |
422 | |
|
423 | 0 | View v = getCurrentView(); |
424 | 0 | if(v instanceof Section){ |
425 | 0 | isValid = isValid(validationResults, (Section)v, allFields); |
426 | |
} |
427 | 0 | if(this.isStartViewShowing()){ |
428 | 0 | if(startPopupView instanceof Section){ |
429 | 0 | isValid = isValid(validationResults, ((Section) startPopupView), allFields) && isValid; |
430 | |
} |
431 | |
} |
432 | 0 | } else { |
433 | |
|
434 | 0 | String errorSections = ""; |
435 | 0 | StringBuilder errorSectionsbuffer = new StringBuilder(); |
436 | 0 | errorSectionsbuffer.append(errorSections); |
437 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
438 | 0 | View v = entry.getValue(); |
439 | 0 | if (v instanceof Section){ |
440 | 0 | if (!isValid(validationResults, (Section)v, allFields)){ |
441 | 0 | isValid = false; |
442 | 0 | errorSectionsbuffer.append(((SectionView)v).getName() + ", "); |
443 | |
} |
444 | |
} |
445 | 0 | } |
446 | 0 | if(this.isStartViewShowing()){ |
447 | 0 | if(startPopupView instanceof Section){ |
448 | 0 | isValid = isValid(validationResults, ((Section) startPopupView), allFields) && isValid; |
449 | |
} |
450 | |
} |
451 | 0 | errorSections = errorSectionsbuffer.toString(); |
452 | 0 | if (!errorSections.isEmpty()){ |
453 | 0 | errorSections = errorSections.substring(0, errorSections.length()-2); |
454 | |
|
455 | |
} |
456 | |
} |
457 | |
|
458 | 0 | return isValid; |
459 | |
} |
460 | |
|
461 | |
private boolean isValid(List<ValidationResultInfo> validationResults, Section section, boolean allFields){ |
462 | |
ErrorLevel status; |
463 | 0 | if(allFields){ |
464 | 0 | section.setFieldHasHadFocusFlags(true); |
465 | 0 | status = section.processValidationResults(validationResults); |
466 | |
} |
467 | |
else{ |
468 | 0 | status = section.processValidationResults(validationResults, false); |
469 | |
} |
470 | |
|
471 | 0 | return (status != ErrorLevel.ERROR); |
472 | |
} |
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
protected void clearAllWarnings(){ |
478 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
479 | 0 | View v = entry.getValue(); |
480 | 0 | if (v instanceof Section){ |
481 | 0 | ((Section)v).clearValidationWarnings(); |
482 | |
} |
483 | 0 | } |
484 | 0 | } |
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
@Override |
496 | |
public void beforeViewChange(Enum<?> viewChangingTo, Callback<Boolean> okToChange) { |
497 | |
|
498 | 0 | if(this.getCurrentView() instanceof Controller){ |
499 | 0 | ((Controller)this.getCurrentView()).beforeViewChange(viewChangingTo, okToChange); |
500 | |
} |
501 | |
else{ |
502 | 0 | okToChange.exec(true); |
503 | |
} |
504 | |
|
505 | 0 | this.showExport(isExportButtonActive()); |
506 | 0 | } |
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
protected void showWarnings(){ |
513 | 0 | if (!Application.getApplicationContext().getValidationWarnings().isEmpty()){ |
514 | 0 | isValid(Application.getApplicationContext().getValidationWarnings(), true); |
515 | |
} |
516 | 0 | } |
517 | |
|
518 | |
@Override |
519 | |
public Widget asWidget() { |
520 | 0 | return this; |
521 | |
} |
522 | |
|
523 | |
@Override |
524 | |
public boolean beforeHide() { |
525 | 0 | return true; |
526 | |
} |
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
@Override |
534 | |
public void beforeShow(Callback<Boolean> onReadyCallback) { |
535 | 0 | onReadyCallback.exec(true); |
536 | 0 | } |
537 | |
|
538 | |
@Override |
539 | |
public Controller getController() { |
540 | 0 | return parentController; |
541 | |
} |
542 | |
|
543 | |
@Override |
544 | |
public String getName() { |
545 | 0 | if(name == null && viewType != null){ |
546 | 0 | return viewType.toString(); |
547 | |
} |
548 | |
else{ |
549 | 0 | return name; |
550 | |
} |
551 | |
} |
552 | |
|
553 | |
@Override |
554 | |
public Enum<?> getViewEnum() { |
555 | 0 | return viewType; |
556 | |
} |
557 | |
|
558 | |
public void setViewEnum(Enum<?> viewType){ |
559 | 0 | this.viewType= viewType; |
560 | 0 | } |
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
|
567 | |
public void setName(String name){ |
568 | 0 | this.name = name; |
569 | 0 | } |
570 | |
|
571 | |
public void setController(Controller controller){ |
572 | 0 | parentController = controller; |
573 | 0 | } |
574 | |
|
575 | |
@Override |
576 | |
public void collectBreadcrumbNames(List<String> names) { |
577 | 0 | names.add(this.getName()); |
578 | 0 | if(this.getCurrentView() != null){ |
579 | 0 | this.getCurrentView().collectBreadcrumbNames(names); |
580 | |
} |
581 | 0 | } |
582 | |
|
583 | |
@Override |
584 | |
public void clear() { |
585 | |
|
586 | 0 | } |
587 | |
|
588 | |
public boolean isExportButtonActive() { |
589 | 0 | return false; |
590 | |
} |
591 | |
|
592 | |
@Override |
593 | |
public void showExport(boolean show) { |
594 | |
|
595 | |
|
596 | 0 | } |
597 | |
} |