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