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 | |
class ObjectArrayMapToBeanDxoCommand extends MapToBeanDxoCommand { |
38 | |
|
39 | |
@SuppressWarnings("unchecked") |
40 | |
public ObjectArrayMapToBeanDxoCommand(final Class dxoClass, |
41 | |
final Method method, final ConverterFactory converterFactory, |
42 | |
final AnnotationReader annotationReader, final Class destClass) { |
43 | 38 | super(dxoClass, method, converterFactory, annotationReader, destClass); |
44 | 38 | } |
45 | |
|
46 | |
private Map<String, Object> normalize( |
47 | |
final Map<String, Object[]> parameters, final Class<?> formType) { |
48 | 5 | final Map<String, Object> normalized = new HashMap<String, Object>(); |
49 | 5 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(formType); |
50 | 5 | for (final Entry<String, Object[]> entry : parameters.entrySet()) { |
51 | 8 | final String name = entry.getKey(); |
52 | 8 | if (beanDesc.hasPropertyDesc(name)) { |
53 | 8 | final PropertyDesc propertyDesc = beanDesc |
54 | |
.getPropertyDesc(name); |
55 | 8 | if (propertyDesc.isReadable() && propertyDesc.isWritable()) { |
56 | 8 | final Object[] values = entry.getValue(); |
57 | 8 | final Class<?> propertyType = propertyDesc |
58 | |
.getPropertyType(); |
59 | 8 | if (propertyType.isArray()) { |
60 | 3 | normalized.put(name, values); |
61 | 5 | } else if (Collection.class.isAssignableFrom(propertyType)) { |
62 | 2 | normalized.put(name, values); |
63 | 3 | } else if (String.class.isAssignableFrom(propertyType)) { |
64 | 0 | final String value = (String) values[0]; |
65 | 0 | if (!StringUtil.isEmpty(value)) { |
66 | 0 | normalized.put(name, value); |
67 | |
} else { |
68 | 0 | normalized.put(name, null); |
69 | |
} |
70 | 0 | } else { |
71 | 3 | normalized.put(name, values[0]); |
72 | |
} |
73 | |
} |
74 | |
} |
75 | 8 | } |
76 | 5 | return normalized; |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
protected void convertScalar(final Object source, final Object dest) { |
81 | 5 | final Map<String, Object> normalized = normalize( |
82 | |
castToObjectArrayMap(source), dest.getClass()); |
83 | 5 | super.convertScalar(normalized, dest); |
84 | 5 | } |
85 | |
|
86 | |
@SuppressWarnings("unchecked") |
87 | |
private Map<String, Object[]> castToObjectArrayMap(final Object source) { |
88 | 5 | return (Map<String, Object[]>) source; |
89 | |
} |
90 | |
|
91 | |
} |