1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.dxo.impl; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.Map; |
22 | |
import java.util.Map.Entry; |
23 | |
|
24 | |
import org.seasar.extension.dxo.annotation.AnnotationReader; |
25 | |
import org.seasar.extension.dxo.command.impl.MapToBeanDxoCommand; |
26 | |
import org.seasar.extension.dxo.converter.ConverterFactory; |
27 | |
import org.seasar.framework.beans.BeanDesc; |
28 | |
import org.seasar.framework.beans.PropertyDesc; |
29 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
30 | |
import org.seasar.framework.util.StringUtil; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
class ObjectArrayMapToBeanDxoCommand extends MapToBeanDxoCommand { |
39 | |
|
40 | |
@SuppressWarnings("unchecked") |
41 | |
ObjectArrayMapToBeanDxoCommand(final Class dxoClass, final Method method, |
42 | |
final ConverterFactory converterFactory, |
43 | |
final AnnotationReader annotationReader, final Class destClass) { |
44 | 36 | super(dxoClass, method, converterFactory, annotationReader, destClass); |
45 | 36 | } |
46 | |
|
47 | |
private Map<String, Object> normalize( |
48 | |
final Map<String, Object[]> parameters, final Class<?> formType) { |
49 | 5 | final Map<String, Object> normalized = new HashMap<String, Object>(); |
50 | 5 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(formType); |
51 | 5 | for (final Entry<String, Object[]> entry : parameters.entrySet()) { |
52 | 8 | final String name = entry.getKey(); |
53 | 8 | if (beanDesc.hasPropertyDesc(name)) { |
54 | 8 | final PropertyDesc propertyDesc = beanDesc |
55 | |
.getPropertyDesc(name); |
56 | 8 | if (propertyDesc.isReadable() && propertyDesc.isWritable()) { |
57 | 8 | final Object[] values = entry.getValue(); |
58 | 8 | final Class<?> propertyType = propertyDesc |
59 | |
.getPropertyType(); |
60 | 8 | if (propertyType.isArray()) { |
61 | 3 | normalized.put(name, values); |
62 | 5 | } else if (Collection.class.isAssignableFrom(propertyType)) { |
63 | 2 | normalized.put(name, values); |
64 | 3 | } else if (String.class.isAssignableFrom(propertyType)) { |
65 | 0 | final String value = (String) values[0]; |
66 | 0 | if (!StringUtil.isEmpty(value)) { |
67 | 0 | normalized.put(name, value); |
68 | |
} else { |
69 | 0 | normalized.put(name, null); |
70 | |
} |
71 | 0 | } else { |
72 | 3 | normalized.put(name, values[0]); |
73 | |
} |
74 | |
} |
75 | |
} |
76 | 8 | } |
77 | 5 | return normalized; |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
protected void convertScalar(final Object source, final Object dest) { |
82 | 5 | final Map<String, Object> normalized = normalize( |
83 | |
castToObjectArrayMap(source), dest.getClass()); |
84 | 5 | super.convertScalar(normalized, dest); |
85 | 5 | } |
86 | |
|
87 | |
@SuppressWarnings("unchecked") |
88 | |
private Map<String, Object[]> castToObjectArrayMap(final Object source) { |
89 | 5 | return (Map<String, Object[]>) source; |
90 | |
} |
91 | |
|
92 | |
} |