1 | |
package org.kuali.student.common.ui.client.configurable.mvc.layouts; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.LinkedHashMap; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
import java.util.Map.Entry; |
8 | |
|
9 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
10 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
11 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
12 | |
import org.kuali.student.common.ui.client.mvc.Controller; |
13 | |
import org.kuali.student.common.ui.client.mvc.View; |
14 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
15 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel; |
16 | |
|
17 | |
public abstract class ViewLayout extends Controller{ |
18 | |
|
19 | 0 | public Map<Enum<?>, View> viewMap = new LinkedHashMap<Enum<?>, View>(); |
20 | 0 | public Map<String, Enum<?>> viewEnumMap = new HashMap<String, Enum<?>>(); |
21 | |
public Enum<?> defaultView; |
22 | |
|
23 | |
public ViewLayout(String controllerId) { |
24 | 0 | super(controllerId); |
25 | 0 | } |
26 | |
|
27 | |
public void addView(View view){ |
28 | 0 | viewMap.put(view.getViewEnum(), view); |
29 | 0 | viewEnumMap.put(view.getViewEnum().toString(), view.getViewEnum()); |
30 | 0 | } |
31 | |
|
32 | |
public <V extends Enum<?>> void setDefaultView(V viewType){ |
33 | 0 | this.defaultView = viewType; |
34 | 0 | } |
35 | |
|
36 | |
@Override |
37 | |
protected <V extends Enum<?>> void getView(V viewType, Callback<View> callback) { |
38 | 0 | callback.exec(viewMap.get(viewType)); |
39 | 0 | } |
40 | |
|
41 | |
@Override |
42 | |
public Enum<?> getViewEnumValue(String enumValue) { |
43 | 0 | return viewEnumMap.get(enumValue); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
@Override |
48 | |
public Class<? extends Enum<?>> getViewsEnum() { |
49 | 0 | return null; |
50 | |
} |
51 | |
|
52 | |
@Override |
53 | |
public void showDefaultView(Callback<Boolean> onReadyCallback) { |
54 | 0 | if(!viewMap.isEmpty()){ |
55 | 0 | if(defaultView == null){ |
56 | 0 | showView(viewMap.entrySet().iterator().next().getKey(), onReadyCallback); |
57 | |
} |
58 | |
else{ |
59 | 0 | showView(defaultView, onReadyCallback); |
60 | |
} |
61 | |
} |
62 | |
|
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public boolean isValid(List<ValidationResultInfo> validationResults, boolean checkCurrentSectionOnly){ |
73 | 0 | boolean isValid = true; |
74 | |
|
75 | 0 | if (checkCurrentSectionOnly){ |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 0 | View v = getCurrentView(); |
81 | 0 | if(v instanceof Section){ |
82 | 0 | isValid = isValid(validationResults, (Section)v); |
83 | |
|
84 | |
} |
85 | 0 | } else { |
86 | |
|
87 | |
|
88 | 0 | String errorSections = ""; |
89 | 0 | StringBuilder errorSectionsbuffer = new StringBuilder(); |
90 | 0 | errorSectionsbuffer.append(errorSections); |
91 | 0 | for (Entry<Enum<?>, View> entry:viewMap.entrySet()) { |
92 | 0 | View v = entry.getValue(); |
93 | 0 | if (v instanceof Section){ |
94 | 0 | if (!isValid(validationResults, (Section)v)){ |
95 | 0 | isValid = false; |
96 | 0 | errorSectionsbuffer.append(((SectionView)v).getName() + ", "); |
97 | |
} |
98 | |
} |
99 | 0 | } |
100 | 0 | errorSections = errorSectionsbuffer.toString(); |
101 | 0 | if (!errorSections.isEmpty()){ |
102 | 0 | errorSections = errorSections.substring(0, errorSections.length()-2); |
103 | |
|
104 | |
} |
105 | |
} |
106 | |
|
107 | 0 | return isValid; |
108 | |
} |
109 | |
|
110 | |
private boolean isValid(List<ValidationResultInfo> validationResults, Section section){ |
111 | 0 | section.setFieldHasHadFocusFlags(true); |
112 | 0 | ErrorLevel status = section.processValidationResults(validationResults); |
113 | |
|
114 | 0 | return (status != ErrorLevel.ERROR); |
115 | |
} |
116 | |
} |