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