1 | |
package org.seasar.cubby.controller.impl; |
2 | |
|
3 | |
import java.util.Collection; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.Map; |
6 | |
import java.util.Map.Entry; |
7 | |
|
8 | |
import org.seasar.cubby.controller.Populator; |
9 | |
import org.seasar.cubby.dxo.HttpRequestDxo; |
10 | |
import org.seasar.framework.beans.BeanDesc; |
11 | |
import org.seasar.framework.beans.PropertyDesc; |
12 | |
import org.seasar.framework.beans.PropertyNotFoundRuntimeException; |
13 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
14 | |
import org.seasar.framework.util.StringUtil; |
15 | |
|
16 | 21 | public class PopulatorImpl implements Populator { |
17 | |
|
18 | |
private HttpRequestDxo httpRequestDxo; |
19 | |
|
20 | |
public void setHttpRequestDxo(final HttpRequestDxo httpRequestDxo) { |
21 | 21 | this.httpRequestDxo = httpRequestDxo; |
22 | 21 | } |
23 | |
|
24 | |
public void populate(final Map<String, Object[]> src, final Object dest) { |
25 | |
if (src == null) { |
26 | |
return; |
27 | |
} |
28 | |
|
29 | |
final Map<String, Object> normalized = normalize(src, dest); |
30 | |
|
31 | |
httpRequestDxo.convert(normalized, dest); |
32 | |
} |
33 | |
|
34 | |
private Map<String, Object> normalize(final Map<String, Object[]> src, |
35 | |
final Object dest) { |
36 | |
final Map<String, Object> normalized = new HashMap<String, Object>(); |
37 | |
final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(dest.getClass()); |
38 | |
for (final Entry<String, Object[]> entry : src.entrySet()) { |
39 | |
try { |
40 | |
final String name = entry.getKey(); |
41 | |
final PropertyDesc propertyDesc = beanDesc |
42 | |
.getPropertyDesc(name); |
43 | |
if (propertyDesc.isWritable()) { |
44 | |
final Object[] values = entry.getValue(); |
45 | |
final Class<?> propertyType = propertyDesc |
46 | |
.getPropertyType(); |
47 | |
if (propertyType.isArray()) { |
48 | |
normalized.put(name, values); |
49 | |
} else if (Collection.class.isAssignableFrom(propertyType)) { |
50 | |
normalized.put(name, values); |
51 | |
} else if (String.class.isAssignableFrom(propertyType)) { |
52 | |
final String value = (String) values[0]; |
53 | |
if (!StringUtil.isEmpty(value)) { |
54 | |
normalized.put(name, value); |
55 | |
} else { |
56 | |
normalized.put(name, null); |
57 | |
} |
58 | |
} else { |
59 | |
normalized.put(name, values[0]); |
60 | |
} |
61 | |
} |
62 | |
} catch (final PropertyNotFoundRuntimeException e) { |
63 | |
|
64 | |
} |
65 | |
} |
66 | |
return normalized; |
67 | |
} |
68 | |
|
69 | |
public Map<String, String> describe(final Object src) { |
70 | |
final Map<String, String> dest = new HashMap<String, String>(); |
71 | |
if (src == null) { |
72 | |
return dest; |
73 | |
} |
74 | |
httpRequestDxo.convert(src, dest); |
75 | |
return dest; |
76 | |
} |
77 | |
|
78 | |
} |