/connect-web/src/main/java/org/osforce/connect/web/bind/support/CustomArgumentResolver.java

http://focus-sns.googlecode.com/ · Java · 114 lines · 72 code · 11 blank · 31 comment · 11 complexity · 54d1d40ed277b7515e5850a1d90af3b8 MD5 · raw file

  1. package org.osforce.connect.web.bind.support;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.osforce.connect.web.AttributeKeys;
  5. import org.osforce.spring4me.commons.collection.CollectionUtil;
  6. import org.osforce.spring4me.dao.Page;
  7. import org.osforce.spring4me.web.widget.config.WidgetConfig;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.core.MethodParameter;
  10. import org.springframework.core.convert.ConversionService;
  11. import org.springframework.web.bind.support.WebArgumentResolver;
  12. import org.springframework.web.context.request.NativeWebRequest;
  13. import org.springframework.web.context.request.WebRequest;
  14. /**
  15. *
  16. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  17. * @since 1.1.0
  18. * @create May 13, 2011 - 4:37:04 PM
  19. * <a href="http://www.opensourceforce.org">????</a>
  20. */
  21. public class CustomArgumentResolver implements WebArgumentResolver {
  22. private static List<Class<?>> classes = CollectionUtil.newArrayList();
  23. private ConversionService conversionService;
  24. static {
  25. //classes.add(Site.class);
  26. classes.add(Page.class);
  27. //classes.add(Project.class);
  28. //classes.add(User.class);
  29. //classes.add(TeamMember.class);
  30. }
  31. public CustomArgumentResolver() {
  32. }
  33. @Autowired
  34. public void setConversionService(ConversionService conversionService) {
  35. this.conversionService = conversionService;
  36. }
  37. public Object resolveArgument(MethodParameter methodParameter,
  38. NativeWebRequest webRequest) throws Exception {
  39. Class<?> paramType = methodParameter.getParameterType();
  40. WidgetConfig widgetConfig = (WidgetConfig) webRequest
  41. .getAttribute(WidgetConfig.KEY, WebRequest.SCOPE_REQUEST);
  42. Object value = null;
  43. if(paramType.isAssignableFrom(Page.class)) {
  44. value = getPage(widgetConfig, webRequest.getNativeRequest(HttpServletRequest.class));
  45. return returnValue(webRequest, value, Page.class);
  46. } /*else if(paramType.isAssignableFrom(Site.class)) {
  47. value = webRequest.getAttribute(AttributeKeys.SITE_KEY, WebRequest.SCOPE_REQUEST);
  48. return returnValue(webRequest, value, Site.class);
  49. } else if(paramType.isAssignableFrom(Project.class)) {
  50. value = webRequest.getAttribute(AttributeKeys.PROJECT_KEY, WebRequest.SCOPE_REQUEST);
  51. return returnValue(webRequest, value, Project.class);
  52. } else if(paramType.isAssignableFrom(User.class)) {
  53. value = webRequest.getAttribute(AttributeKeys.USER_KEY, WebRequest.SCOPE_REQUEST);
  54. return returnValue(webRequest, value, User.class);
  55. } else if(paramType.isAssignableFrom(TeamMember.class)) {
  56. value = webRequest.getAttribute(AttributeKeys.TEAM_MEMBER_KEY, WebRequest.SCOPE_REQUEST);
  57. return returnValue(webRequest, value, TeamMember.class);
  58. }*/ else if(AttributeKeys.SHOW_ERRORS_KEY_READABLE.equals(methodParameter.getParameterName())) {
  59. value = (Boolean) webRequest.getAttribute(
  60. AttributeKeys.SHOW_ERRORS_KEY_READABLE, WebRequest.SCOPE_REQUEST);
  61. return returnValue(webRequest, value!=null?value:false, Boolean.class);
  62. }
  63. //
  64. return WebArgumentResolver.UNRESOLVED;
  65. }
  66. private Object returnValue(NativeWebRequest webRequest, Object value, Class<?> clazz) {
  67. HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
  68. if(value==null && "POST".equals(request.getMethod())
  69. && classes.contains(clazz)) {
  70. return WebArgumentResolver.UNRESOLVED;
  71. }
  72. return value;
  73. }
  74. // TODO need to refactor
  75. private Page<?> getPage(WidgetConfig widgetConfig, HttpServletRequest request) {
  76. //String fragmentId = widgetConfig.getId();
  77. //String pageKey = fragmentId + "_page";
  78. //Page<?> page = (Page<?>) request.getSession().getAttribute(pageKey);
  79. Page<?> page = null;
  80. if(page==null) {
  81. // new page
  82. Integer pageSize = 10;
  83. if(widgetConfig.getPrefs().get("limit")!=null) {
  84. String prefValue =widgetConfig.getPrefs().get("limit");
  85. pageSize = conversionService.convert(prefValue, Integer.class);
  86. } else if(widgetConfig.getPrefs().get("pageSize")!=null) {
  87. String prefValue = widgetConfig.getPrefs().get("pageSize");
  88. pageSize = conversionService.convert(prefValue, Integer.class);
  89. }
  90. page = new Page<Object>(pageSize);
  91. }
  92. // set page no
  93. Integer pageNo = 1;
  94. if(request.getParameter("pageNo")!=null) {
  95. String paramValue = request.getParameter("pageNo");
  96. pageNo = conversionService.convert(paramValue, Integer.class);
  97. }
  98. page.setPageNo(pageNo);
  99. // set page to session
  100. //request.getSession().setAttribute(pageKey, page);
  101. return page;
  102. }
  103. }