1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.application; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Map.Entry; |
25 | |
|
26 | |
import org.kuali.student.common.messages.dto.Message; |
27 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
28 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
29 | |
import org.kuali.student.common.ui.client.security.SecurityContext; |
30 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService; |
31 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync; |
32 | |
|
33 | |
import com.google.gwt.core.client.GWT; |
34 | |
import com.google.gwt.user.client.rpc.AsyncCallback; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class ApplicationContext { |
45 | 0 | private ServerPropertiesRpcServiceAsync serverPropertiesRpcService = GWT.create(ServerPropertiesRpcService.class); |
46 | |
|
47 | 0 | private boolean loggedIn = true; |
48 | 0 | private String userId = "testuser"; |
49 | 0 | private String version = "KS"; |
50 | 0 | private List<String> roles = new ArrayList<String>(); |
51 | |
|
52 | 0 | private Map<String, Map<String, String>> messages = new HashMap<String, Map<String,String>>(); |
53 | 0 | private Map<String, String> flatMessages = new HashMap<String, String>(); |
54 | 0 | private List<Message> messagesList = new ArrayList<Message>(); |
55 | |
|
56 | |
private SecurityContext securityContext; |
57 | |
private String applicationContextUrl; |
58 | |
|
59 | |
|
60 | 0 | private String parentPath = ""; |
61 | 0 | private HashMap<String,HashMap<String,FieldDescriptor>> pathToFieldMapping = new HashMap<String,HashMap<String,FieldDescriptor>>(); |
62 | 0 | private HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>> crossConstraints = new HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>>(); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | protected ApplicationContext() { |
69 | 0 | roles.add("role1"); |
70 | 0 | roles.add("role2"); |
71 | |
|
72 | 0 | serverPropertiesRpcService.getContextPath(new AsyncCallback<String>(){ |
73 | |
|
74 | |
@Override |
75 | |
public void onFailure(Throwable caught) { |
76 | 0 | throw new RuntimeException("Fatal - Unable to initialze application context"); |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
public void onSuccess(String result) { |
81 | 0 | applicationContextUrl = result; |
82 | 0 | } |
83 | |
}); |
84 | 0 | } |
85 | |
|
86 | |
public void setLoggedIn(boolean loggedIn) { |
87 | 0 | this.loggedIn = loggedIn; |
88 | 0 | } |
89 | |
|
90 | |
public void setUserId(String userId) { |
91 | 0 | this.userId = userId; |
92 | 0 | } |
93 | |
|
94 | |
public void setRoles(List<String> roles) { |
95 | 0 | this.roles = roles; |
96 | 0 | } |
97 | |
|
98 | |
public boolean isLoggedIn() { |
99 | 0 | return loggedIn; |
100 | |
} |
101 | |
|
102 | |
public String getUserId() { |
103 | 0 | return userId; |
104 | |
} |
105 | |
|
106 | |
public List<String> getRoles() { |
107 | 0 | return roles; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public void addMessages(List<Message> messages) { |
115 | 0 | messagesList.addAll(messages); |
116 | 0 | for (Message m : messages) { |
117 | 0 | String groupName = m.getGroupName(); |
118 | 0 | Map<String, String> group = this.messages.get(groupName); |
119 | 0 | if (group == null) { |
120 | 0 | group = new HashMap<String, String>(); |
121 | 0 | this.messages.put(groupName, group); |
122 | |
} |
123 | 0 | group.put(m.getId(), m.getValue()); |
124 | 0 | flatMessages.put(m.getId(), m.getValue()); |
125 | 0 | } |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public String getMessage(String messageId) { |
132 | 0 | return flatMessages.get(messageId); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public List<Message> getMessages() { |
139 | 0 | return messagesList; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public String getMessage(String groupName, String messageId) { |
147 | |
|
148 | 0 | String result = null; |
149 | |
|
150 | 0 | Map<String, String> group = this.messages.get(groupName); |
151 | 0 | if (group != null) { |
152 | 0 | result = group.get(messageId); |
153 | |
} |
154 | |
|
155 | 0 | return result; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public String getUILabel(String groupName, String type, String state, String fieldId) { |
173 | |
|
174 | 0 | String label = getMessage(groupName, type + ":" + state + ":" + fieldId); |
175 | |
|
176 | 0 | if (label == null) |
177 | 0 | label = getMessage(groupName, fieldId); |
178 | |
|
179 | 0 | if (label == null) |
180 | 0 | label = fieldId; |
181 | |
|
182 | 0 | return label; |
183 | |
|
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public String getUILabel(String groupName, String fieldId) { |
191 | |
|
192 | 0 | String label = getMessage(groupName, fieldId); |
193 | |
|
194 | 0 | if (label == null) |
195 | 0 | label = fieldId; |
196 | |
|
197 | 0 | return label; |
198 | |
|
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public SecurityContext getSecurityContext() { |
206 | 0 | return securityContext; |
207 | |
} |
208 | |
|
209 | |
public void setSecurityContext(SecurityContext securityContext) { |
210 | 0 | this.securityContext = securityContext; |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public String getApplicationContextUrl() { |
217 | 0 | return applicationContextUrl; |
218 | |
} |
219 | |
|
220 | |
public void setVersion(String version) { |
221 | 0 | this.version = version; |
222 | 0 | } |
223 | |
|
224 | |
public String getVersion() { |
225 | 0 | return version; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public void putCrossConstraint(String namespace, String path, HasCrossConstraints fd){ |
235 | 0 | if(namespace==null){ |
236 | 0 | namespace="_default"; |
237 | |
} |
238 | |
|
239 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
240 | 0 | if(crossConstraintMap==null){ |
241 | 0 | crossConstraintMap = new HashMap<String,HashSet<HasCrossConstraints>>(); |
242 | 0 | crossConstraints.put(namespace, crossConstraintMap); |
243 | |
} |
244 | 0 | HashSet<HasCrossConstraints> fieldDescriptors = crossConstraintMap.get(path); |
245 | 0 | if(fieldDescriptors == null){ |
246 | 0 | fieldDescriptors = new HashSet<HasCrossConstraints>(); |
247 | 0 | crossConstraintMap.put(path, fieldDescriptors); |
248 | |
} |
249 | 0 | fieldDescriptors.add(fd); |
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
public HashSet<HasCrossConstraints> getCrossConstraint(String namespace, String path){ |
255 | 0 | if(namespace==null){ |
256 | 0 | namespace="_default"; |
257 | |
} |
258 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
259 | 0 | if(crossConstraintMap!=null){ |
260 | 0 | return crossConstraintMap.get(path); |
261 | |
} |
262 | 0 | return null; |
263 | |
} |
264 | |
public void clearCrossConstraintMap(String namespace){ |
265 | 0 | if(namespace==null){ |
266 | 0 | namespace="_default"; |
267 | |
} |
268 | 0 | crossConstraints.remove(namespace); |
269 | 0 | } |
270 | |
public void putPathToFieldMapping(String namespace, String path, FieldDescriptor fd){ |
271 | 0 | if(namespace==null){ |
272 | 0 | namespace="_default"; |
273 | |
} |
274 | |
|
275 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
276 | 0 | if(pathToField==null){ |
277 | 0 | pathToField = new HashMap<String,FieldDescriptor>(); |
278 | 0 | pathToFieldMapping.put(namespace, pathToField); |
279 | |
} |
280 | 0 | pathToField.put(path, fd); |
281 | 0 | } |
282 | |
|
283 | |
public FieldDescriptor getPathToFieldMapping(String namespace, String path){ |
284 | 0 | if(namespace==null){ |
285 | 0 | namespace="_default"; |
286 | |
} |
287 | |
|
288 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
289 | 0 | if(pathToField!=null){ |
290 | 0 | return pathToField.get(path); |
291 | |
} |
292 | 0 | return null; |
293 | |
} |
294 | |
public void clearPathToFieldMapping(String namespace){ |
295 | 0 | if(namespace==null){ |
296 | 0 | namespace="_default"; |
297 | |
} |
298 | 0 | pathToFieldMapping.remove(namespace); |
299 | 0 | } |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public void clearCrossConstraintsWithStartingPath(String namespace, String pathPrefix){ |
309 | 0 | if(namespace==null){ |
310 | 0 | namespace="_default"; |
311 | |
} |
312 | |
|
313 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
314 | 0 | if(crossConstraintMap!=null){ |
315 | 0 | Iterator<Map.Entry<String,HashSet<HasCrossConstraints>>> constraintMapIter = crossConstraintMap.entrySet().iterator(); |
316 | 0 | while(constraintMapIter.hasNext()){ |
317 | 0 | Map.Entry<String,HashSet<HasCrossConstraints>> entry = constraintMapIter.next(); |
318 | 0 | if(entry.getKey().startsWith(pathPrefix)){ |
319 | 0 | constraintMapIter.remove(); |
320 | |
} |
321 | 0 | } |
322 | |
|
323 | |
|
324 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
325 | 0 | if(pathToField!=null){ |
326 | 0 | Iterator<Entry<String, FieldDescriptor>> pathMapIter = pathToField.entrySet().iterator(); |
327 | 0 | while(pathMapIter.hasNext()){ |
328 | 0 | Entry<String, FieldDescriptor> entry = pathMapIter.next(); |
329 | 0 | if(entry.getKey().startsWith(pathPrefix)){ |
330 | 0 | FieldDescriptor fd = entry.getValue(); |
331 | 0 | if(fd.getFieldWidget()!=null && fd.getFieldWidget() instanceof HasCrossConstraints && ((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()!=null){ |
332 | |
|
333 | 0 | for(String path:((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()){ |
334 | 0 | HashSet<HasCrossConstraints> set = crossConstraintMap.get(path); |
335 | 0 | if(set!=null){ |
336 | 0 | set.remove(fd.getFieldWidget()); |
337 | |
} |
338 | 0 | } |
339 | |
} |
340 | |
} |
341 | 0 | } |
342 | |
} |
343 | |
} |
344 | 0 | } |
345 | |
|
346 | |
|
347 | |
public HashSet<HasCrossConstraints> getCrossConstraints(String namespace) { |
348 | 0 | if(namespace==null){ |
349 | 0 | namespace="_default"; |
350 | |
} |
351 | 0 | HashSet<HasCrossConstraints> results = new HashSet<HasCrossConstraints>(); |
352 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
353 | 0 | if(crossConstraintMap!=null){ |
354 | 0 | for(HashSet<HasCrossConstraints> fds: crossConstraintMap.values()){ |
355 | 0 | results.addAll(fds); |
356 | |
} |
357 | |
} |
358 | 0 | return results; |
359 | |
} |
360 | |
|
361 | |
public String getParentPath() { |
362 | 0 | return parentPath; |
363 | |
} |
364 | |
|
365 | |
public void setParentPath(String parentPath) { |
366 | 0 | this.parentPath = parentPath; |
367 | 0 | } |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
} |