##### Coverage Report - org.seasar.cubby.internal.controller.impl.FormWrapperFactoryImpl

Classes in this File | Line Coverage | Branch Coverage | Complexity
FormWrapperFactoryImpl |
100% | 5/5
N/A | N/A
| 6.5;6.5
FormWrapperFactoryImpl$1 |
N/A | N/A
N/A | N/A
| 6.5;6.5
FormWrapperFactoryImpl$FormWrapperImpl |
95% | 67/70
88% | 39/44
| 6.5;6.5

1 |  |
     /*
2 |  |
      * Copyright 2004-2009 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.internal.controller.impl;
17 |  |
18 |  |
     import java.lang.reflect.Array;
19 |  |
     import java.util.Collection;
20 |  |
21 |  |
     import org.seasar.cubby.action.RequestParameter;
22 |  |
     import org.seasar.cubby.controller.FormWrapper;
23 |  |
     import org.seasar.cubby.controller.FormWrapperFactory;
24 |  |
     import org.seasar.cubby.converter.ConversionHelper;
25 |  |
     import org.seasar.cubby.converter.Converter;
26 |  |
     import org.seasar.cubby.converter.impl.ConversionHelperImpl;
27 |  |
     import org.seasar.cubby.spi.ConverterProvider;
28 |  |
     import org.seasar.cubby.spi.ProviderFactory;
29 |  |
     import org.seasar.cubby.spi.beans.Attribute;
30 |  |
     import org.seasar.cubby.spi.beans.BeanDesc;
31 |  |
     import org.seasar.cubby.spi.beans.BeanDescFactory;
32 |  |
33 |  |
     /**
34 |  |
      * フォームオブジェクトのラッパーファクトリの実装です。
35 |  |
      *
36 |  |
      * @author baba
37 |  |
      */
38 | 16 |
     public class FormWrapperFactoryImpl implements FormWrapperFactory {
39 |  |
40 |  |
             /** 変換のヘルパクラス。 */
41 | 9 |
             private final ConversionHelper conversionHelper = new ConversionHelperImpl();
42 |  |
43 |  |
             /**
44 |  |
              * {@inheritDoc}
45 |  |
              */
46 |  |
             public FormWrapper create(final Object form) {
47 | 9 |
                     final FormWrapper formObject = new FormWrapperImpl(form);
48 | 9 |
                     return formObject;
49 |  |
             }
50 |  |
51 |  |
             /**
52 |  |
              * フォームオブジェクトのラッパーの実装です。
53 |  |
              *
54 |  |
              * @author baba
55 |  |
              */
56 | 9 |
             private class FormWrapperImpl implements FormWrapper {
57 |  |
58 |  |
                     /** フォームオブジェクト */
59 |  |
                     private final Object form;
60 |  |
61 |  |
                     /**
62 |  |
                      * インスタンス化します。
63 |  |
                      *
64 |  |
                      * @param form
65 |  |
                      *            フォームオブジェクト
66 |  |
                      * @param context
67 |  |
                      *            変換中のコンテキスト
68 |  |
                      */
69 | 9 |
                     private FormWrapperImpl(final Object form) {
70 | 9 |
                             this.form = form;
71 | 9 |
                     }
72 |  |
73 |  |
                     /**
74 |  |
                      * {@inheritDoc}
75 |  |
                      */
76 |  |
                     public boolean hasValues(final String name) {
77 | 2 |
                             if (this.form == null) {
78 | 0 |
                                     return false;
79 |  |
                             }
80 | 2 |
                             final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form
81 |  |
                                             .getClass());
82 | 2 |
                             final Attribute attribute = findAttribute(beanDesc, name);
83 | 2 |
                             return attribute != null;
84 |  |
                     }
85 |  |
86 |  |
                     /**
87 |  |
                      * {@inheritDoc}
88 |  |
                      */
89 |  |
                     public String[] getValues(final String name) {
90 | 20 |
                             if (this.form == null) {
91 | 0 |
                                     return null;
92 |  |
                             }
93 |  |
94 | 20 |
                             final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form
95 |  |
                                             .getClass());
96 | 20 |
                             final Attribute attribute = findAttribute(beanDesc, name);
97 | 20 |
                             if (attribute == null) {
98 | 4 |
                                     return null;
99 |  |
                             }
100 | 16 |
                             final Object value = attribute.getValue(this.form);
101 |  |
102 | 16 |
                             if (value == null) {
103 | 2 |
                                     return null;
104 | 14 |
                             } else if (value instanceof String[]) {
105 | 0 |
                                     return (String[]) value;
106 |  |
                             } else {
107 |  |
                                     final Class<? extends Converter> converterType;
108 | 14 |
                                     if (attribute.isAnnotationPresent(RequestParameter.class)) {
109 | 4 |
                                             final RequestParameter requestParameter = attribute
110 |  |
                                                             .getAnnotation(RequestParameter.class);
111 | 4 |
                                             if (Converter.class.equals(requestParameter.converter())) {
112 | 2 |
                                                     converterType = null;
113 |  |
                                             } else {
114 | 2 |
                                                     converterType = requestParameter.converter();
115 |  |
                                             }
116 | 4 |
                                     } else {
117 | 10 |
                                             converterType = null;
118 |  |
                                     }
119 | 14 |
                                     if (value.getClass().isArray()) {
120 | 2 |
                                             final int length = Array.getLength(value);
121 | 2 |
                                             final String[] array = (String[]) Array.newInstance(
122 |  |
                                                             String.class, length);
123 | 8 |
                                             for (int i = 0; i < length; i++) {
124 | 6 |
                                                     final Object element = Array.get(value, i);
125 | 6 |
                                                     final String converted = convert(element, converterType);
126 | 6 |
                                                     Array.set(array, i, converted);
127 |  |
                                             }
128 | 2 |
                                             return array;
129 | 12 |
                                     } else if (value instanceof Collection) {
130 | 2 |
                                             final Collection<?> collection = (Collection<?>) value;
131 | 2 |
                                             final String[] array = (String[]) Array.newInstance(
132 |  |
                                                             String.class, collection.size());
133 | 2 |
                                             int i = 0;
134 | 2 |
                                             for (final Object element : collection) {
135 | 4 |
                                                     final String converted = convert(element, converterType);
136 | 4 |
                                                     Array.set(array, i++, converted);
137 | 4 |
                                             }
138 | 2 |
                                             return array;
139 |  |
                                     } else {
140 | 10 |
                                             final String[] array = (String[]) Array.newInstance(
141 |  |
                                                             String.class, 1);
142 | 10 |
                                             final String converted = convert(value, converterType);
143 | 10 |
                                             Array.set(array, 0, converted);
144 | 10 |
                                             return array;
145 |  |
                                     }
146 |  |
                             }
147 |  |
                     }
148 |  |
149 |  |
                     /**
150 |  |
                      * 指定された名前に対応する属性を検索します。
151 |  |
                      *
152 |  |
                      * @param beanDesc
153 |  |
                      *            Java Beans の定義
154 |  |
                      * @param name
155 |  |
                      *            名前
156 |  |
                      * @return 属性の定義
157 |  |
                      */
158 |  |
                     private Attribute findAttribute(final BeanDesc beanDesc,
159 |  |
                                     final String name) {
160 |  |
161 | 22 |
                             for (final Attribute attribute : beanDesc.findAllAttributes()) {
162 | 118 |
                                     if (attribute.isAnnotationPresent(RequestParameter.class)) {
163 | 26 |
                                             final RequestParameter requestParameter = attribute
164 |  |
                                                             .getAnnotation(RequestParameter.class);
165 | 26 |
                                             final String parameterName = requestParameter.name();
166 | 26 |
                                             if (parameterName == null || parameterName.length() == 0) {
167 | 15 |
                                                     if (name.equals(attribute.getName())) {
168 | 2 |
                                                             return attribute;
169 |  |
                                                     }
170 |  |
                                             } else {
171 | 11 |
                                                     if (name.equals(parameterName)) {
172 | 2 |
                                                             return attribute;
173 |  |
                                                     }
174 |  |
                                             }
175 | 22 |
                                     } else {
176 | 92 |
                                             if (name.equals(attribute.getName())) {
177 | 14 |
                                                     return attribute;
178 |  |
                                             }
179 |  |
                                     }
180 |  |
                             }
181 |  |
182 | 4 |
                             return null;
183 |  |
                     }
184 |  |
185 |  |
                     /**
186 |  |
                      * 指定されたオブジェクトを文字列に変換します。
187 |  |
                      *
188 |  |
                      * @param value
189 |  |
                      *            値
190 |  |
                      * @param converterType
191 |  |
                      *            コンバータの型
192 |  |
                      * @return <code>value</code>を変換した文字列
193 |  |
                      */
194 |  |
                     private String convert(final Object value,
195 |  |
                                     final Class<? extends Converter> converterType) {
196 | 20 |
                             if (value == null) {
197 | 5 |
                                     return null;
198 |  |
                             }
199 | 15 |
                             final ConverterProvider converterProvider = ProviderFactory
200 |  |
                                             .get(ConverterProvider.class);
201 |  |
                             final Converter converter;
202 | 15 |
                             if (converterType == null) {
203 | 13 |
                                     converter = converterProvider.getConverter(null, value
204 |  |
                                                     .getClass());
205 |  |
                             } else {
206 | 2 |
                                     converter = converterProvider.getConverter(converterType);
207 |  |
                             }
208 | 15 |
                             if (converter == null) {
209 | 8 |
                                     return value.toString();
210 |  |
                             } else {
211 | 7 |
                                     return converter.convertToString(value, conversionHelper);
212 |  |
                             }
213 |  |
                     }
214 |  |
215 |  |
             }
216 |  |
     }