View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * {@link String}をキー、{@link Object}の配列を値にもつ{@link Map}からBeanに変換するコマンドです。
34   * 
35   * @author baba
36   * @since 1.0.0
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  		super(dxoClass, method, converterFactory, annotationReader, destClass);
45  	}
46  
47  	private Map<String, Object> normalize(
48  			final Map<String, Object[]> parameters, final Class<?> formType) {
49  		final Map<String, Object> normalized = new HashMap<String, Object>();
50  		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(formType);
51  		for (final Entry<String, Object[]> entry : parameters.entrySet()) {
52  			final String name = entry.getKey();
53  			if (beanDesc.hasPropertyDesc(name)) {
54  				final PropertyDesc propertyDesc = beanDesc
55  						.getPropertyDesc(name);
56  				if (propertyDesc.isReadable() && propertyDesc.isWritable()) {
57  					final Object[] values = entry.getValue();
58  					final Class<?> propertyType = propertyDesc
59  							.getPropertyType();
60  					if (propertyType.isArray()) {
61  						normalized.put(name, values);
62  					} else if (Collection.class.isAssignableFrom(propertyType)) {
63  						normalized.put(name, values);
64  					} else if (String.class.isAssignableFrom(propertyType)) {
65  						final String value = (String) values[0];
66  						if (!StringUtil.isEmpty(value)) {
67  							normalized.put(name, value);
68  						} else {
69  							normalized.put(name, null);
70  						}
71  					} else {
72  						normalized.put(name, values[0]);
73  					}
74  				}
75  			}
76  		}
77  		return normalized;
78  	}
79  
80  	@Override
81  	protected void convertScalar(final Object source, final Object dest) {
82  		final Map<String, Object> normalized = normalize(
83  				castToObjectArrayMap(source), dest.getClass());
84  		super.convertScalar(normalized, dest);
85  	}
86  
87  	@SuppressWarnings("unchecked")
88  	private Map<String, Object[]> castToObjectArrayMap(final Object source) {
89  		return (Map<String, Object[]>) source;
90  	}
91  
92  }