PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/confeito/src/org/t2framework/confeito/spi/impl/FormResolverImpl.java

http://t-2.googlecode.com/
Java | 186 lines | 146 code | 10 blank | 30 comment | 35 complexity | 98b4ed8e5e21645f6ec854a47b921530 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, MIT
  1. /*
  2. * Copyright 2008-2010 the T2 Project ant 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, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.t2framework.confeito.spi.impl;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import org.t2framework.confeito.Constants;
  24. import org.t2framework.confeito.action.ErrorInfo;
  25. import org.t2framework.confeito.annotation.Form;
  26. import org.t2framework.confeito.contexts.Request;
  27. import org.t2framework.confeito.contexts.WebContext;
  28. import org.t2framework.confeito.internal.RequestParseUtil;
  29. import org.t2framework.confeito.model.Component;
  30. import org.t2framework.confeito.model.Property;
  31. import org.t2framework.confeito.spi.FormResolver;
  32. import org.t2framework.confeito.util.ConverterUtil;
  33. import org.t2framework.confeito.util.Logger;
  34. import org.t2framework.confeito.util.Reflections.MethodUtil;
  35. /**
  36. * <#if locale="en">
  37. * <p>
  38. * Default implementation of {@code FormResolver}. The constraint of this
  39. * implementation is that multiple request parameters converts to List<String>.
  40. * </p>
  41. * <#else>
  42. * <p>
  43. *
  44. * </p>
  45. * </#if>
  46. *
  47. * @author shot
  48. *
  49. */
  50. public class FormResolverImpl implements FormResolver {
  51. protected static Logger LOG = Logger.getLogger(FormResolverImpl.class);
  52. @Override
  53. public void resolve(Form form, WebContext context, Component<?> component,
  54. ErrorInfo errorInfo) {
  55. final Request request = context.getRequest();
  56. Map<String, Map<Integer, Object>> paramMap = RequestParseUtil
  57. .createParamMap(request);
  58. final String className = component.getName();
  59. for (Property property : component.getProperties()) {
  60. final String name = property.propertyName;
  61. if (property.writeMethod == null) {
  62. LOG.debug(className + "#" + name
  63. + " does not have setter method.");
  64. continue;
  65. }
  66. final boolean containsList = paramMap.containsKey(name);
  67. if (request.hasMultipleParameters(name) == false
  68. && containsList == false) {
  69. final Object param = request.getParameter(name);
  70. try {
  71. Object instance = component.getInstance();
  72. setSingleValue(request, property, instance, param);
  73. } catch (Throwable t) {
  74. errorInfo.addErrorInfo(className + "#" + name, t);
  75. continue;
  76. }
  77. } else {
  78. Object params = null;
  79. if (containsList) {
  80. Map<Integer, Object> map = paramMap.get(name);
  81. params = RequestParseUtil.convertAsList(map);
  82. } else {
  83. params = request.getParameters(name);
  84. }
  85. if (params == null) {
  86. continue;
  87. }
  88. try {
  89. Object instance = component.getInstance();
  90. setMultipleValues(request, property, instance, params);
  91. } catch (Throwable t) {
  92. errorInfo.addErrorInfo(className + "#" + name + "[]", t);
  93. continue;
  94. }
  95. }
  96. }
  97. }
  98. protected void setMultipleValues(Request request, Property pd,
  99. Object target, Object src) {
  100. final Class<?> destType = pd.writeMethod.getParameterTypes()[0];
  101. final Class<? extends Object> srcType = src.getClass();
  102. final Locale locale = request.getLocale();
  103. if (destType.isArray()) {
  104. setArrayValue(pd, target, src, destType, srcType, locale);
  105. } else if (List.class.isAssignableFrom(destType)) {
  106. setListValue(pd, target, src, destType, srcType, locale);
  107. } else {
  108. MethodUtil.invoke(pd.writeMethod, target, pd.convertArgs(target));
  109. }
  110. }
  111. @SuppressWarnings("unchecked")
  112. protected void setListValue(Property pd, Object target, Object src,
  113. Class<? extends Object> destType, Class<? extends Object> srcType,
  114. Locale locale) {
  115. List<Object> list = null;
  116. if (List.class.isAssignableFrom(srcType)) {
  117. list = (List<Object>) src;
  118. } else if (srcType.isArray()) {
  119. list = Arrays.asList((Object[]) src);
  120. }
  121. Object[] args = Constants.EMPTY_ARRAY;
  122. if (list != null) {
  123. List<Object> ret = new ArrayList<Object>();
  124. for (Object o : list) {
  125. if (o != null && Date.class.isAssignableFrom(o.getClass())) {
  126. o = convertToDate(o.toString(), locale);
  127. }
  128. ret.add(o);
  129. }
  130. args = pd.convertArgs(ret);
  131. } else {
  132. args = pd.convertArgs(src);
  133. }
  134. MethodUtil.invoke(pd.writeMethod, target, args);
  135. }
  136. @SuppressWarnings("unchecked")
  137. protected void setArrayValue(Property pd, Object target, Object src,
  138. Class<? extends Object> destType, Class<? extends Object> srcType,
  139. Locale locale) {
  140. final Class<?> componentType = destType.getComponentType();
  141. Object[] params = null;
  142. if (List.class.isAssignableFrom(srcType)) {
  143. List<Object> list = (List) src;
  144. params = list.toArray(new Object[list.size()]);
  145. } else if (srcType.isArray()) {
  146. params = (Object[]) src;
  147. }
  148. if (params != null) {
  149. if (Date.class.isAssignableFrom(componentType)) {
  150. final int length = params.length;
  151. final Date[] dates = new Date[length];
  152. for (int i = 0; i < length; i++) {
  153. dates[i] = convertToDate(params[i].toString(), locale);
  154. }
  155. params = dates;
  156. }
  157. MethodUtil.invoke(pd.writeMethod, target, pd.convertArgs(params));
  158. } else {
  159. MethodUtil.invoke(pd.writeMethod, target, pd.convertArgs(src));
  160. }
  161. }
  162. protected Date convertToDate(String param, Locale locale) {
  163. return ConverterUtil.DATE_CONVERTER.toDate(param, null, locale);
  164. }
  165. protected void setSingleValue(Request request, Property pd, Object target,
  166. Object param) {
  167. final Class<?> type = pd.writeMethod.getParameterTypes()[0];
  168. Object value = param;
  169. if (Date.class.isAssignableFrom(type)) {
  170. final Locale locale = request.getLocale();
  171. value = ConverterUtil.DATE_CONVERTER.toDate(param.toString(), null,
  172. locale);
  173. }
  174. MethodUtil.invoke(pd.writeMethod, target, pd.convertArgs(value));
  175. }
  176. }