1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
package org.kuali.student.common.assembly.dictionary; |
10 | |
|
11 | |
import java.text.DateFormat; |
12 | |
import java.text.ParseException; |
13 | |
import java.text.SimpleDateFormat; |
14 | |
import java.util.ArrayList; |
15 | |
import java.util.HashMap; |
16 | |
import java.util.List; |
17 | |
import java.util.Map; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.student.common.assembly.data.ConstraintMetadata; |
21 | |
import org.kuali.student.common.assembly.data.Data; |
22 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
23 | |
import org.kuali.student.common.assembly.data.LookupParamMetadata; |
24 | |
import org.kuali.student.common.assembly.data.Metadata; |
25 | |
import org.kuali.student.common.assembly.data.UILookupConfig; |
26 | |
import org.kuali.student.common.assembly.data.UILookupData; |
27 | |
import org.kuali.student.common.assembly.data.Data.DataType; |
28 | |
import org.kuali.student.common.assembly.data.Data.Value; |
29 | |
import org.kuali.student.common.assembly.data.Metadata.WriteAccess; |
30 | |
import org.kuali.student.common.dictionary.dto.CaseConstraint; |
31 | |
import org.kuali.student.common.dictionary.dto.CommonLookupParam; |
32 | |
import org.kuali.student.common.dictionary.dto.Constraint; |
33 | |
import org.kuali.student.common.dictionary.dto.FieldDefinition; |
34 | |
import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; |
35 | |
import org.kuali.student.common.dictionary.dto.WhenConstraint; |
36 | |
import org.kuali.student.common.dictionary.service.DictionaryService; |
37 | |
import org.kuali.student.common.dto.DtoConstants; |
38 | |
import org.kuali.student.common.dto.DtoConstants.DtoState; |
39 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
40 | |
import org.springframework.beans.BeanUtils; |
41 | |
import org.springframework.context.ApplicationContext; |
42 | |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class MetadataServiceImpl { |
50 | 4 | final Logger LOG = Logger.getLogger(MetadataServiceImpl.class); |
51 | |
|
52 | |
private Map<String, DictionaryService> dictionaryServiceMap; |
53 | |
private List<UILookupConfig> lookupObjectStructures; |
54 | |
private String uiLookupContext; |
55 | |
|
56 | 36 | private static class RecursionCounter { |
57 | |
public static final int MAX_DEPTH = 4; |
58 | |
|
59 | 18 | private Map<String, Integer> recursions = new HashMap<String, Integer>(); |
60 | |
|
61 | |
public int increment(String objectName) { |
62 | 18 | Integer hits = recursions.get(objectName); |
63 | |
|
64 | 18 | if (hits == null) { |
65 | 18 | hits = new Integer(1); |
66 | |
} else { |
67 | 0 | hits++; |
68 | |
} |
69 | 18 | recursions.put(objectName, hits); |
70 | 18 | return hits; |
71 | |
} |
72 | |
|
73 | |
public int decrement(String objectName) { |
74 | 18 | Integer hits = recursions.get(objectName); |
75 | 18 | if (hits >= 1) { |
76 | 18 | hits--; |
77 | |
} |
78 | |
|
79 | 18 | recursions.put(objectName, hits); |
80 | 18 | return hits; |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 4 | public MetadataServiceImpl(DictionaryService... dictionaryServices) { |
90 | 4 | if (dictionaryServices != null) { |
91 | 4 | this.dictionaryServiceMap = new HashMap<String, DictionaryService>(); |
92 | 8 | for (DictionaryService d : dictionaryServices) { |
93 | 4 | List<String> objectTypes = d.getObjectTypes(); |
94 | 4 | for (String objectType : objectTypes) { |
95 | 12 | dictionaryServiceMap.put(objectType, d); |
96 | |
} |
97 | |
} |
98 | |
} |
99 | 4 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public Metadata getMetadata(String objectKey, String type, String state, String nextState, String documentTypeName) { |
112 | 15 | nextState = (nextState == null || nextState.length() <=0 ? DtoState.getNextStateAsString(state):nextState); |
113 | 15 | state = state==null?null:state.toUpperCase(); |
114 | 15 | nextState = nextState==null?null:nextState.toUpperCase(); |
115 | |
|
116 | 15 | return getMetadataFromDictionaryService(objectKey, type, state, nextState, null, documentTypeName); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public Metadata getMetadataByWorkflowNode(String objectKey, String workflowNode, String documentTypeName) { |
125 | |
|
126 | 3 | return getMetadataFromDictionaryService(objectKey, null, DtoState.DRAFT.toString(), null, workflowNode, documentTypeName); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public Metadata getMetadata(String objectKey, String type, String state) { |
138 | 15 | state = (state == null ? DtoState.DRAFT.toString():state); |
139 | 15 | String nextState = DtoState.getNextStateAsString(state); |
140 | |
|
141 | 15 | return getMetadata(objectKey, type, state.toUpperCase(), nextState, null); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public Metadata getMetadata(String objectKey, String state) { |
152 | 1 | return getMetadata(objectKey, null, state); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public Metadata getMetadata(String objectKey) { |
163 | 9 | return getMetadata(objectKey, null, null); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
protected Metadata getMetadataFromDictionaryService(String objectKey, String type, String state, String nextState, String workflowNode, String documentTypeName) { |
177 | |
|
178 | 18 | Metadata metadata = new Metadata(); |
179 | |
|
180 | 18 | ObjectStructureDefinition objectStructure = getObjectStructure(objectKey); |
181 | |
|
182 | 18 | metadata.setProperties(getProperties(objectStructure, type, state, nextState, workflowNode, new RecursionCounter(), documentTypeName)); |
183 | |
|
184 | 18 | metadata.setWriteAccess(WriteAccess.ALWAYS); |
185 | 18 | metadata.setDataType(DataType.DATA); |
186 | 18 | addLookupstoMetadata(objectKey, metadata, type); |
187 | 18 | return metadata; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private Map<String, Metadata> getProperties(ObjectStructureDefinition objectStructure, String type, String state, String nextState, String workflowNode, RecursionCounter counter, String documentTypeName) { |
200 | 18 | String objectName = objectStructure.getName(); |
201 | 18 | int hits = counter.increment(objectName); |
202 | |
|
203 | 18 | Map<String, Metadata> properties = null; |
204 | |
|
205 | 18 | if (hits < RecursionCounter.MAX_DEPTH) { |
206 | 18 | properties = new HashMap<String, Metadata>(); |
207 | |
|
208 | 18 | List<FieldDefinition> attributes = objectStructure.getAttributes(); |
209 | 18 | for (FieldDefinition fd : attributes) { |
210 | |
|
211 | 93 | Metadata metadata = new Metadata(); |
212 | |
|
213 | |
|
214 | 93 | metadata.setWriteAccess(WriteAccess.ALWAYS); |
215 | 93 | metadata.setDataType(convertDictionaryDataType(fd.getDataType())); |
216 | |
|
217 | 93 | metadata.setConstraints(getConstraints(fd, type, state, nextState, workflowNode, documentTypeName)); |
218 | 93 | metadata.setCanEdit(!fd.isReadOnly()); |
219 | 93 | metadata.setCanUnmask(!fd.isMask()); |
220 | 93 | metadata.setCanView(!fd.isHide()); |
221 | 93 | metadata.setDynamic(fd.isDynamic()); |
222 | 93 | metadata.setLabelKey(fd.getLabelKey()); |
223 | 93 | metadata.setDefaultValue(convertDefaultValue(metadata.getDataType(), fd.getDefaultValue())); |
224 | 93 | metadata.setDefaultValuePath(fd.getDefaultValuePath()); |
225 | |
|
226 | 93 | if (fd.isPartialMask()){ |
227 | 11 | metadata.setPartialMaskFormatter(fd.getPartialMaskFormatter()); |
228 | |
} |
229 | |
|
230 | 93 | if (fd.isMask()){ |
231 | 11 | metadata.setMaskFormatter(fd.getMaskFormatter()); |
232 | |
} |
233 | |
|
234 | |
|
235 | 93 | Map<String, Metadata> nestedProperties = null; |
236 | 93 | if (fd.getDataType() == org.kuali.student.common.dictionary.dto.DataType.COMPLEX && fd.getDataObjectStructure() != null) { |
237 | 0 | nestedProperties = getProperties(fd.getDataObjectStructure(), type, state, nextState, workflowNode, counter, documentTypeName); |
238 | |
} |
239 | |
|
240 | |
|
241 | 93 | if (isRepeating(fd)) { |
242 | 0 | Metadata repeatingMetadata = new Metadata(); |
243 | 0 | metadata.setDataType(DataType.LIST); |
244 | |
|
245 | 0 | repeatingMetadata.setWriteAccess(WriteAccess.ALWAYS); |
246 | 0 | repeatingMetadata.setOnChangeRefreshMetadata(false); |
247 | 0 | repeatingMetadata.setDataType(convertDictionaryDataType(fd.getDataType())); |
248 | |
|
249 | 0 | if (nestedProperties != null) { |
250 | 0 | repeatingMetadata.setProperties(nestedProperties); |
251 | |
} |
252 | |
|
253 | 0 | Map<String, Metadata> repeatingProperty = new HashMap<String, Metadata>(); |
254 | 0 | repeatingProperty.put("*", repeatingMetadata); |
255 | 0 | metadata.setProperties(repeatingProperty); |
256 | 0 | } else if (nestedProperties != null) { |
257 | 0 | metadata.setProperties(nestedProperties); |
258 | |
} |
259 | |
|
260 | 93 | properties.put(fd.getName(), metadata); |
261 | |
|
262 | 93 | } |
263 | |
} |
264 | |
|
265 | 18 | counter.decrement(objectName); |
266 | 18 | return properties; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
protected boolean isRepeating(FieldDefinition fd) { |
276 | 93 | boolean isRepeating = false; |
277 | |
try { |
278 | 93 | int maxOccurs = Integer.parseInt(fd.getMaxOccurs()); |
279 | 28 | isRepeating = maxOccurs > 1; |
280 | 65 | } catch (NumberFormatException nfe) { |
281 | 65 | isRepeating = FieldDefinition.UNBOUNDED.equals(fd.getMaxOccurs()); |
282 | 28 | } |
283 | |
|
284 | 93 | return isRepeating; |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
protected ObjectStructureDefinition getObjectStructure(String objectKey) { |
294 | 18 | DictionaryService dictionaryService = dictionaryServiceMap.get(objectKey); |
295 | |
|
296 | 18 | if (dictionaryService == null) { |
297 | 0 | throw new RuntimeException("Dictionary service not provided for objectKey=[" + objectKey + "]."); |
298 | |
} |
299 | |
|
300 | 18 | return dictionaryService.getObjectStructure(objectKey); |
301 | |
} |
302 | |
|
303 | |
|
304 | |
protected List<ConstraintMetadata> getConstraints(FieldDefinition fd, String type, String state, String nextState, String workflowNode, String documentTypeName) { |
305 | 93 | List<ConstraintMetadata> constraints = new ArrayList<ConstraintMetadata>(); |
306 | |
|
307 | 93 | ConstraintMetadata constraintMetadata = new ConstraintMetadata(); |
308 | |
|
309 | 93 | updateConstraintMetadata(constraintMetadata, (Constraint) fd, type, state, nextState, workflowNode); |
310 | 93 | constraints.add(constraintMetadata); |
311 | |
|
312 | 93 | return constraints; |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
protected void updateConstraintMetadata(ConstraintMetadata constraintMetadata, Constraint constraint, String type, String state, String nextState, String workflowNode) { |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | 104 | if (constraint.getMinLength() != null) { |
327 | 60 | constraintMetadata.setMinLength(constraint.getMinLength()); |
328 | |
} |
329 | |
|
330 | |
|
331 | |
try { |
332 | 104 | if (constraint.getMaxLength() != null) { |
333 | 60 | constraintMetadata.setMaxLength(Integer.parseInt(constraint.getMaxLength())); |
334 | |
} |
335 | |
|
336 | 0 | } catch (NumberFormatException nfe) { |
337 | |
|
338 | |
|
339 | 0 | constraintMetadata.setMaxLength(9999); |
340 | 104 | } |
341 | |
|
342 | |
|
343 | 104 | if (constraint.getMinOccurs() != null) { |
344 | 83 | constraintMetadata.setMinOccurs(constraint.getMinOccurs()); |
345 | |
} |
346 | |
|
347 | |
|
348 | 104 | String maxOccurs = constraint.getMaxOccurs(); |
349 | 104 | if (maxOccurs != null) { |
350 | |
try { |
351 | 28 | constraintMetadata.setMaxOccurs(Integer.parseInt(maxOccurs)); |
352 | 28 | if (!FieldDefinition.SINGLE.equals(maxOccurs)) { |
353 | 0 | constraintMetadata.setId("repeating"); |
354 | |
} |
355 | 0 | } catch (NumberFormatException nfe) { |
356 | |
|
357 | 0 | if (FieldDefinition.UNBOUNDED.equals(maxOccurs)) { |
358 | 0 | constraintMetadata.setId("repeating"); |
359 | 0 | constraintMetadata.setMaxOccurs(9999); |
360 | |
} |
361 | 28 | } |
362 | |
} |
363 | |
|
364 | |
|
365 | 104 | if (constraint.getExclusiveMin() != null) { |
366 | 22 | constraintMetadata.setMinValue(constraint.getExclusiveMin()); |
367 | |
} |
368 | |
|
369 | |
|
370 | 104 | if (constraint.getInclusiveMax() != null) { |
371 | 11 | constraintMetadata.setMaxValue(constraint.getInclusiveMax()); |
372 | |
} |
373 | |
|
374 | 104 | if (constraint.getValidChars() != null) { |
375 | 25 | constraintMetadata.setValidChars(constraint.getValidChars().getValue()); |
376 | 25 | constraintMetadata.setValidCharsMessageId(constraint.getValidChars().getLabelKey()); |
377 | |
} |
378 | |
|
379 | |
|
380 | 104 | if (constraint.getCaseConstraint() != null) { |
381 | 35 | processCaseConstraint(constraintMetadata, constraint.getCaseConstraint(), type, state, nextState, workflowNode); |
382 | |
} |
383 | 104 | } |
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
protected void processCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String type, String state, String nextState, String workflowNode) { |
391 | 35 | String fieldPath = caseConstraint.getFieldPath(); |
392 | 35 | fieldPath = (fieldPath != null ? fieldPath.toUpperCase() : fieldPath); |
393 | |
|
394 | 35 | if (workflowNode != null && fieldPath != null && fieldPath.startsWith("PROPOSAL/WORKFLOWNODE")){ |
395 | 3 | processRequiredByNodeCaseConstraint(constraintMetadata, caseConstraint, workflowNode); |
396 | 32 | } else if ("STATE".equals(fieldPath)) { |
397 | 11 | processStateCaseConstraint(constraintMetadata, caseConstraint, type, state, nextState, workflowNode); |
398 | 21 | } else if ("TYPE".equals(fieldPath)) { |
399 | 0 | processTypeCaseConstraint(constraintMetadata, caseConstraint, type, state, nextState, workflowNode); |
400 | |
} |
401 | 35 | } |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
private void processRequiredByNodeCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String workflowNode) { |
412 | 3 | List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint(); |
413 | |
|
414 | 3 | if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) { |
415 | 3 | for (WhenConstraint whenConstraint : whenConstraints) { |
416 | 3 | List<Object> values = whenConstraint.getValues(); |
417 | 3 | Constraint constraint = whenConstraint.getConstraint(); |
418 | |
|
419 | 3 | if (constraint.getErrorLevel() == ErrorLevel.ERROR && constraint.getMinOccurs() != null && constraint.getMinOccurs() > 0){ |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | 3 | if (isWorkflowNodeFirstConstraintValue(workflowNode, values)) { |
426 | |
|
427 | |
|
428 | |
|
429 | 1 | constraintMetadata.setRequiredForNextState(true); |
430 | 1 | if (DtoConstants.WORKFLOW_NODE_PRE_ROUTE.equals(workflowNode)){ |
431 | 0 | constraintMetadata.setNextState(DtoState.SUBMITTED.toString()); |
432 | |
} else { |
433 | 1 | constraintMetadata.setNextState(DtoState.APPROVED.toString()); |
434 | |
} |
435 | 1 | constraintMetadata.setMinOccurs(0); |
436 | 2 | } else if (values.contains(workflowNode)){ |
437 | |
|
438 | 1 | constraintMetadata.setRequiredForNextState(false); |
439 | 1 | constraintMetadata.setNextState(null); |
440 | 1 | constraintMetadata.setMinOccurs(1); |
441 | |
} |
442 | |
} |
443 | 3 | } |
444 | |
} |
445 | 3 | } |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
private boolean isWorkflowNodeFirstConstraintValue(String workflowNode, List<Object> values){ |
453 | 3 | if (values != null && !values.isEmpty()){ |
454 | 3 | return values.get(0).equals(workflowNode); |
455 | |
} else { |
456 | 0 | return false; |
457 | |
} |
458 | |
} |
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
private void processStateCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String type, String state, String nextState, String workflowNode) { |
464 | 11 | List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint(); |
465 | |
|
466 | 11 | if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) { |
467 | 11 | for (WhenConstraint whenConstraint : whenConstraints) { |
468 | 22 | List<Object> values = whenConstraint.getValues(); |
469 | 22 | if (values != null) { |
470 | 22 | Constraint constraint = whenConstraint.getConstraint(); |
471 | |
|
472 | 22 | if (constraint.getErrorLevel() == ErrorLevel.ERROR){ |
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | 22 | if (values.contains(nextState)) { |
478 | 7 | if (constraint.getMinOccurs() != null && constraint.getMinOccurs() > 0) { |
479 | 7 | constraintMetadata.setRequiredForNextState(true); |
480 | 7 | constraintMetadata.setNextState(nextState); |
481 | |
} |
482 | |
} |
483 | |
|
484 | |
|
485 | 22 | if (values.contains(state)) { |
486 | 11 | updateConstraintMetadata(constraintMetadata, constraint, type, state, nextState, workflowNode); |
487 | |
} |
488 | |
} |
489 | |
} |
490 | 22 | } |
491 | |
} |
492 | 11 | } |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
private void processTypeCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String type, String state, String nextState, String workflowNode) { |
498 | 0 | List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint(); |
499 | |
|
500 | 0 | if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) { |
501 | 0 | for (WhenConstraint whenConstraint : whenConstraints) { |
502 | 0 | List<Object> values = whenConstraint.getValues(); |
503 | 0 | if (values != null && values.contains(type)) { |
504 | 0 | Constraint constraint = whenConstraint.getConstraint(); |
505 | 0 | updateConstraintMetadata(constraintMetadata, constraint, type, state, nextState, workflowNode); |
506 | |
} |
507 | 0 | } |
508 | |
} |
509 | 0 | } |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
protected Value convertDefaultValue(DataType dataType, Object value) { |
520 | 93 | Value v = null; |
521 | 93 | if (value instanceof String) { |
522 | 0 | String s = (String) value; |
523 | 1 | switch (dataType) { |
524 | |
case STRING: |
525 | 0 | v = new Data.StringValue(s); |
526 | 0 | break; |
527 | |
case BOOLEAN: |
528 | 0 | v = new Data.BooleanValue(Boolean.valueOf(s)); |
529 | 0 | break; |
530 | |
case FLOAT: |
531 | 0 | v = new Data.FloatValue(Float.valueOf(s)); |
532 | 0 | break; |
533 | |
case DATE: |
534 | 0 | DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
535 | |
try { |
536 | 0 | v = new Data.DateValue(format.parse(s)); |
537 | 0 | } catch (ParseException e) { |
538 | 0 | LOG.error("Unable to get default date value from metadata definition"); |
539 | 0 | } |
540 | 0 | break; |
541 | |
case LONG: |
542 | 0 | if (!s.isEmpty()) { |
543 | 0 | v = new Data.LongValue(Long.valueOf(s)); |
544 | |
} |
545 | |
break; |
546 | |
case DOUBLE: |
547 | 0 | v = new Data.DoubleValue(Double.valueOf(s)); |
548 | 0 | break; |
549 | |
case INTEGER: |
550 | 0 | v = new Data.IntegerValue(Integer.valueOf(s)); |
551 | |
break; |
552 | |
} |
553 | 0 | } else { |
554 | 93 | v = convertDefaultValue(value); |
555 | |
} |
556 | |
|
557 | 93 | return v; |
558 | |
} |
559 | |
|
560 | |
protected Value convertDefaultValue(Object value) { |
561 | 93 | Value v = null; |
562 | |
|
563 | 93 | if (value instanceof String) { |
564 | 0 | v = new Data.StringValue((String) value); |
565 | 93 | } else if (value instanceof Boolean) { |
566 | 0 | v = new Data.BooleanValue((Boolean) value); |
567 | 93 | } else if (value instanceof Integer) { |
568 | 0 | v = new Data.IntegerValue((Integer) value); |
569 | 93 | } else if (value instanceof Double) { |
570 | 0 | v = new Data.DoubleValue((Double) value); |
571 | 93 | } else if (value instanceof Long) { |
572 | 0 | v = new Data.LongValue((Long) value); |
573 | 93 | } else if (value instanceof Short) { |
574 | 0 | v = new Data.ShortValue((Short) value); |
575 | 93 | } else if (value instanceof Float) { |
576 | 0 | v = new Data.FloatValue((Float) value); |
577 | |
} |
578 | |
|
579 | 93 | return v; |
580 | |
} |
581 | |
|
582 | |
protected DataType convertDictionaryDataType(org.kuali.student.common.dictionary.dto.DataType dataType) { |
583 | 1 | switch (dataType) { |
584 | |
case STRING: |
585 | 71 | return DataType.STRING; |
586 | |
case BOOLEAN: |
587 | 0 | return DataType.BOOLEAN; |
588 | |
case INTEGER: |
589 | 0 | return DataType.INTEGER; |
590 | |
case FLOAT: |
591 | 0 | return DataType.FLOAT; |
592 | |
case COMPLEX: |
593 | 0 | return DataType.DATA; |
594 | |
case DATE: |
595 | 11 | return DataType.DATE; |
596 | |
case DOUBLE: |
597 | 11 | return DataType.DOUBLE; |
598 | |
case LONG: |
599 | 0 | return DataType.LONG; |
600 | |
} |
601 | |
|
602 | 0 | return null; |
603 | |
} |
604 | |
|
605 | |
public void setUiLookupContext(String uiLookupContext) { |
606 | 2 | this.uiLookupContext = uiLookupContext; |
607 | 2 | init(); |
608 | |
|
609 | 2 | } |
610 | |
|
611 | |
@SuppressWarnings("unchecked") |
612 | |
private void init() { |
613 | 2 | ApplicationContext ac = new ClassPathXmlApplicationContext(uiLookupContext); |
614 | |
|
615 | 2 | Map<String, UILookupConfig> beansOfType = (Map<String, UILookupConfig>) ac.getBeansOfType(UILookupConfig.class); |
616 | 2 | lookupObjectStructures = new ArrayList<UILookupConfig>(); |
617 | 2 | for (UILookupConfig objStr : beansOfType.values()) { |
618 | 4 | lookupObjectStructures.add(objStr); |
619 | |
} |
620 | 2 | System.out.println("UILookup loaded"); |
621 | 2 | } |
622 | |
|
623 | |
private String calcSimpleName(String objectKey) { |
624 | 26 | int lastDot = objectKey.lastIndexOf("."); |
625 | 26 | if (lastDot == -1) { |
626 | 26 | return objectKey; |
627 | |
} |
628 | 0 | return objectKey.substring(lastDot + 1); |
629 | |
|
630 | |
} |
631 | |
|
632 | |
private boolean matchesObjectKey(String objectKey, String path) { |
633 | 26 | String simpleName = calcSimpleName(objectKey); |
634 | 26 | if (path.toLowerCase().startsWith(simpleName.toLowerCase())) { |
635 | |
|
636 | 14 | return true; |
637 | |
} |
638 | |
|
639 | 12 | return false; |
640 | |
} |
641 | |
|
642 | |
private boolean matchesType(String paramType, String lookupType) { |
643 | |
|
644 | 14 | if (paramType == null && lookupType == null) { |
645 | 0 | return true; |
646 | |
} |
647 | |
|
648 | |
|
649 | 14 | if (paramType == null && lookupType != null) { |
650 | 4 | return false; |
651 | |
} |
652 | |
|
653 | |
|
654 | |
|
655 | |
|
656 | |
|
657 | 10 | if (paramType != null && lookupType == null) { |
658 | 0 | return true; |
659 | |
} |
660 | 10 | if (paramType.equalsIgnoreCase(lookupType)) { |
661 | |
|
662 | 5 | return true; |
663 | |
} |
664 | |
|
665 | 5 | return false; |
666 | |
} |
667 | |
|
668 | |
private void addLookupstoMetadata(String objectKey, Metadata metadata, String type) { |
669 | 18 | if (lookupObjectStructures != null) { |
670 | 13 | for (UILookupConfig lookup : lookupObjectStructures) { |
671 | 26 | if (!matchesObjectKey(objectKey, lookup.getPath())) { |
672 | 12 | continue; |
673 | |
} |
674 | 14 | if (!matchesType(type, lookup.getType())) { |
675 | 9 | continue; |
676 | |
} |
677 | |
|
678 | |
|
679 | 5 | Map<String, Metadata> parsedMetadataMap = metadata.getProperties(); |
680 | 5 | Metadata parsedMetadata = null; |
681 | 5 | String parsedMetadataKey = ""; |
682 | 5 | String lookupFieldPath = lookup.getPath(); |
683 | 5 | String[] lookupPathTokens = getPathTokens(lookupFieldPath); |
684 | 10 | for (int i = 1; i < lookupPathTokens.length; i++) { |
685 | 5 | if (parsedMetadataMap == null) { |
686 | 0 | break; |
687 | |
} |
688 | 5 | if (i == lookupPathTokens.length - 1) { |
689 | |
|
690 | 5 | parsedMetadata = parsedMetadataMap.get(lookupPathTokens[i]); |
691 | 5 | parsedMetadataKey = parsedMetadataKey + "." + lookupPathTokens[i]; |
692 | |
} |
693 | 5 | if (parsedMetadataMap.get(lookupPathTokens[i]) != null) { |
694 | 5 | parsedMetadataMap = parsedMetadataMap.get(lookupPathTokens[i]).getProperties(); |
695 | 0 | } else if (parsedMetadataMap.get("*") != null) { |
696 | |
|
697 | 0 | parsedMetadataMap = parsedMetadataMap.get("*").getProperties(); |
698 | 0 | i--; |
699 | |
} |
700 | |
|
701 | |
} |
702 | 5 | if (parsedMetadata != null) { |
703 | |
|
704 | |
|
705 | 5 | UILookupData initialLookup = lookup.getInitialLookup(); |
706 | 5 | if (initialLookup != null) { |
707 | 5 | mapLookupDatatoMeta(initialLookup); |
708 | 5 | parsedMetadata.setInitialLookup(mapLookupDatatoMeta(lookup.getInitialLookup())); |
709 | |
} |
710 | 5 | List<LookupMetadata> additionalLookupMetadata = null; |
711 | 5 | if (lookup.getAdditionalLookups() != null) { |
712 | 0 | additionalLookupMetadata = new ArrayList<LookupMetadata>(); |
713 | 0 | for (UILookupData additionallookup : lookup.getAdditionalLookups()) { |
714 | 0 | additionalLookupMetadata.add(mapLookupDatatoMeta(additionallookup)); |
715 | |
} |
716 | 0 | parsedMetadata.setAdditionalLookups(additionalLookupMetadata); |
717 | |
} |
718 | |
} |
719 | 5 | } |
720 | |
} |
721 | 18 | } |
722 | |
|
723 | |
private LookupMetadata mapLookupDatatoMeta(UILookupData lookupData) { |
724 | 10 | LookupMetadata lookupMetadata = new LookupMetadata(); |
725 | |
List<LookupParamMetadata> paramsMetadata; |
726 | 10 | BeanUtils.copyProperties(lookupData, lookupMetadata, new String[]{"widget", "usage", "widgetOptions", "params"}); |
727 | 10 | if (lookupData.getWidget() != null) { |
728 | 10 | lookupMetadata.setWidget(org.kuali.student.common.assembly.data.LookupMetadata.Widget.valueOf(lookupData.getWidget().toString())); |
729 | |
} |
730 | 10 | if (lookupData.getUsage() != null) { |
731 | 10 | lookupMetadata.setUsage(org.kuali.student.common.assembly.data.LookupMetadata.Usage.valueOf(lookupData.getUsage().toString())); |
732 | |
} |
733 | 10 | if (lookupData.getWidgetOptions () != null) { |
734 | 0 | lookupMetadata.setWidgetOptions (new HashMap ()); |
735 | 0 | for (UILookupData.WidgetOption wo: lookupData.getWidgetOptions ().keySet ()) { |
736 | 0 | String value = lookupData.getWidgetOptions ().get (wo); |
737 | 0 | LookupMetadata.WidgetOption key = LookupMetadata.WidgetOption.valueOf(wo.toString()); |
738 | 0 | lookupMetadata.getWidgetOptions ().put (key, value); |
739 | 0 | } |
740 | |
} |
741 | 10 | if (lookupData.getParams() != null) { |
742 | 10 | paramsMetadata = new ArrayList<LookupParamMetadata>(); |
743 | 10 | for (CommonLookupParam param : lookupData.getParams()) { |
744 | 50 | paramsMetadata.add(mapLookupParamMetadata(param)); |
745 | |
} |
746 | 10 | lookupMetadata.setParams(paramsMetadata); |
747 | |
} |
748 | |
|
749 | 10 | return lookupMetadata; |
750 | |
} |
751 | |
|
752 | |
private LookupParamMetadata mapLookupParamMetadata(CommonLookupParam param) { |
753 | 50 | LookupParamMetadata paramMetadata = new LookupParamMetadata(); |
754 | 50 | BeanUtils.copyProperties(param, paramMetadata, new String[]{"childLookup", "dataType", "writeAccess", "usage", "widget"}); |
755 | 50 | if (param.getChildLookup() != null) { |
756 | 0 | paramMetadata.setChildLookup(mapLookupDatatoMeta((UILookupData) param.getChildLookup())); |
757 | |
} |
758 | 50 | if (param.getDataType() != null) { |
759 | 50 | paramMetadata.setDataType(org.kuali.student.common.assembly.data.Data.DataType.valueOf(param.getDataType().toString())); |
760 | |
} |
761 | 50 | if (param.getWriteAccess() != null) { |
762 | 40 | paramMetadata.setWriteAccess(org.kuali.student.common.assembly.data.Metadata.WriteAccess.valueOf(param.getWriteAccess().toString())); |
763 | |
} |
764 | 50 | if (param.getUsage() != null) { |
765 | 0 | paramMetadata.setUsage(org.kuali.student.common.assembly.data.LookupMetadata.Usage.valueOf(param.getUsage().toString())); |
766 | |
} |
767 | 50 | if (param.getWidget() != null) { |
768 | 0 | paramMetadata.setWidget(org.kuali.student.common.assembly.data.LookupParamMetadata.Widget.valueOf(param.getWidget().toString())); |
769 | |
} |
770 | |
|
771 | 50 | return paramMetadata; |
772 | |
} |
773 | |
|
774 | |
private static String[] getPathTokens(String fieldPath) { |
775 | 5 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
776 | |
} |
777 | |
} |