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