1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
37 | |
|
38 | 48 | public class FormWrapperFactoryImpl implements FormWrapperFactory { |
39 | |
|
40 | |
|
41 | 27 | private final ConversionHelper conversionHelper = new ConversionHelperImpl(); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public FormWrapper create(final Object form) { |
47 | 27 | final FormWrapper formObject = new FormWrapperImpl(form); |
48 | 27 | return formObject; |
49 | |
} |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 27 | private class FormWrapperImpl implements FormWrapper { |
58 | |
|
59 | |
|
60 | |
private final Object form; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 27 | private FormWrapperImpl(final Object form) { |
71 | 27 | this.form = form; |
72 | 27 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public boolean hasValues(final String name) { |
78 | 6 | if (this.form == null) { |
79 | 0 | return false; |
80 | |
} |
81 | 6 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form |
82 | |
.getClass()); |
83 | 6 | final Attribute attribute = findAttribute(beanDesc, name); |
84 | 6 | return attribute != null; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public String[] getValues(final String name) { |
91 | 60 | if (this.form == null) { |
92 | 0 | return null; |
93 | |
} |
94 | |
|
95 | 60 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(this.form |
96 | |
.getClass()); |
97 | 60 | final Attribute attribute = findAttribute(beanDesc, name); |
98 | 60 | if (attribute == null) { |
99 | 12 | return null; |
100 | |
} |
101 | 48 | final Object value = attribute.getValue(this.form); |
102 | |
|
103 | 48 | if (value == null) { |
104 | 6 | return null; |
105 | 42 | } else if (value instanceof String[]) { |
106 | 0 | return (String[]) value; |
107 | |
} else { |
108 | |
final Class<? extends Converter> converterType; |
109 | 42 | if (attribute.isAnnotationPresent(RequestParameter.class)) { |
110 | 12 | final RequestParameter requestParameter = attribute |
111 | |
.getAnnotation(RequestParameter.class); |
112 | 12 | if (Converter.class.equals(requestParameter.converter())) { |
113 | 6 | converterType = null; |
114 | |
} else { |
115 | 6 | converterType = requestParameter.converter(); |
116 | |
} |
117 | 12 | } else { |
118 | 30 | converterType = null; |
119 | |
} |
120 | 42 | if (value.getClass().isArray()) { |
121 | 6 | final int length = Array.getLength(value); |
122 | 6 | final String[] array = (String[]) Array.newInstance( |
123 | |
String.class, length); |
124 | 24 | for (int i = 0; i < length; i++) { |
125 | 18 | final Object element = Array.get(value, i); |
126 | 18 | final String converted = convert(element, converterType); |
127 | 18 | Array.set(array, i, converted); |
128 | |
} |
129 | 6 | return array; |
130 | 36 | } else if (value instanceof Collection) { |
131 | 6 | final Collection<?> collection = (Collection<?>) value; |
132 | 6 | final String[] array = (String[]) Array.newInstance( |
133 | |
String.class, collection.size()); |
134 | 6 | int i = 0; |
135 | 6 | for (final Object element : collection) { |
136 | 12 | final String converted = convert(element, converterType); |
137 | 12 | Array.set(array, i++, converted); |
138 | 12 | } |
139 | 6 | return array; |
140 | |
} else { |
141 | 30 | final String[] array = (String[]) Array.newInstance( |
142 | |
String.class, 1); |
143 | 30 | final String converted = convert(value, converterType); |
144 | 30 | Array.set(array, 0, converted); |
145 | 30 | return array; |
146 | |
} |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
private Attribute findAttribute(final BeanDesc beanDesc, |
160 | |
final String name) { |
161 | |
|
162 | 66 | for (final Attribute attribute : beanDesc.findAllAttributes()) { |
163 | 354 | if (attribute.isAnnotationPresent(RequestParameter.class)) { |
164 | 78 | final RequestParameter requestParameter = attribute |
165 | |
.getAnnotation(RequestParameter.class); |
166 | 78 | final String parameterName = requestParameter.name(); |
167 | 78 | if (parameterName == null || parameterName.length() == 0) { |
168 | 45 | if (name.equals(attribute.getName())) { |
169 | 6 | return attribute; |
170 | |
} |
171 | |
} else { |
172 | 33 | if (name.equals(parameterName)) { |
173 | 6 | return attribute; |
174 | |
} |
175 | |
} |
176 | 66 | } else { |
177 | 276 | if (name.equals(attribute.getName())) { |
178 | 42 | return attribute; |
179 | |
} |
180 | |
} |
181 | |
} |
182 | |
|
183 | 12 | return null; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
private String convert(final Object value, |
196 | |
final Class<? extends Converter> converterType) { |
197 | 60 | if (value == null) { |
198 | 15 | return null; |
199 | |
} |
200 | 45 | final ConverterProvider converterProvider = ProviderFactory |
201 | |
.get(ConverterProvider.class); |
202 | |
final Converter converter; |
203 | 45 | if (converterType == null) { |
204 | 39 | converter = converterProvider.getConverter(null, value |
205 | |
.getClass()); |
206 | |
} else { |
207 | 6 | converter = converterProvider.getConverter(converterType); |
208 | |
} |
209 | 45 | if (converter == null) { |
210 | 24 | return value.toString(); |
211 | |
} else { |
212 | 21 | return converter.convertToString(value, conversionHelper); |
213 | |
} |
214 | |
} |
215 | |
|
216 | |
} |
217 | |
} |