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