/connect-web/src/main/java/org/osforce/connect/web/module/discussion/ForumWidget.java

http://focus-sns.googlecode.com/ · Java · 116 lines · 95 code · 13 blank · 8 comment · 8 complexity · edc03843443a446133a6e90c98c9dd4f MD5 · raw file

  1. package org.osforce.connect.web.module.discussion;
  2. import java.util.List;
  3. import javax.validation.Valid;
  4. import org.osforce.connect.entity.commons.Statistic;
  5. import org.osforce.connect.entity.discussion.Forum;
  6. import org.osforce.connect.entity.discussion.Topic;
  7. import org.osforce.connect.entity.system.Project;
  8. import org.osforce.connect.entity.system.ProjectFeature;
  9. import org.osforce.connect.entity.system.User;
  10. import org.osforce.connect.service.commons.StatisticService;
  11. import org.osforce.connect.service.discussion.ForumService;
  12. import org.osforce.connect.service.discussion.TopicService;
  13. import org.osforce.connect.web.AttributeKeys;
  14. import org.osforce.connect.web.security.annotation.Permission;
  15. import org.osforce.spring4me.dao.Page;
  16. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  17. import org.osforce.spring4me.web.stereotype.Widget;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.ui.Model;
  20. import org.springframework.validation.BindingResult;
  21. import org.springframework.web.bind.annotation.ModelAttribute;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestMethod;
  24. import org.springframework.web.bind.annotation.RequestParam;
  25. /**
  26. *
  27. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  28. * @since 1.1.0
  29. * @create May 21, 2011 - 12:05:40 PM
  30. * <a href="http://www.opensourceforce.org">????</a>
  31. */
  32. @Widget
  33. @RequestMapping("/discussion/forum")
  34. public class ForumWidget {
  35. private TopicService topicService;
  36. private ForumService forumService;
  37. private StatisticService statisticService;
  38. public ForumWidget() {
  39. }
  40. @Autowired
  41. public void setTopicService(TopicService topicService) {
  42. this.topicService = topicService;
  43. }
  44. @Autowired
  45. public void setForumService(ForumService forumService) {
  46. this.forumService = forumService;
  47. }
  48. @Autowired
  49. public void setStatisticService(StatisticService statisticService) {
  50. this.statisticService = statisticService;
  51. }
  52. @RequestMapping("/list-view")
  53. @Permission({"forum-view"})
  54. public String doListView(Page<Topic> page,
  55. @RequestAttr Project project, Model model) {
  56. List<Forum> forums = forumService.getForumList(project);
  57. for(Forum forum : forums) {
  58. page = topicService.getTopicPage(page, forum.getId());
  59. for(Topic topic : page.getResult()) {
  60. Statistic statistic = statisticService.getStatistic(Statistic.TYPE_VIEW, topic.getId(), Topic.NAME);
  61. if(statistic!=null) {
  62. topic.setViews(statistic.getCount());
  63. }
  64. }
  65. forum.setTopics(page.getResult());
  66. }
  67. model.addAttribute(AttributeKeys.FORUM_LIST_KEY_READABLE, forums);
  68. return "discussion/forum-list";
  69. }
  70. @RequestMapping("/form-view")
  71. @Permission(value={"forum-add", "forum-edit"}, userRequired=true, projectRequired=true)
  72. public String doFormView(@RequestParam(required=false) Long forumId,
  73. @ModelAttribute @Valid Forum forum, BindingResult result,
  74. @RequestAttr User user, @RequestAttr Project project, Model model, Boolean showErrors) {
  75. if(!showErrors) {
  76. forum.setEnteredBy(user);
  77. forum.setModifiedBy(user);
  78. forum.setProject(project);
  79. if(forumId!=null) {
  80. forum = forumService.getForum(forumId);
  81. }
  82. model.addAttribute(AttributeKeys.FORUM_KEY_READABLE, forum);
  83. }
  84. return "discussion/forum-form";
  85. }
  86. @RequestMapping(value="/form-action", method=RequestMethod.POST)
  87. @Permission(value={"forum-add", "forum-edit"}, userRequired=true, projectRequired=true)
  88. public String doFormAction(@ModelAttribute @Valid Forum forum,
  89. BindingResult result, Model model, @RequestAttr Project project) {
  90. if(result.hasErrors()) {
  91. model.addAttribute(AttributeKeys.SHOW_ERRORS_KEY_READABLE, true);
  92. model.addAttribute(AttributeKeys.FEATURE_CODE_KEY_READABLE, ProjectFeature.FEATURE_DISCUSSION);
  93. return "page:/discussion/forum-form";
  94. }
  95. //
  96. if(forum.getId()==null) {
  97. forumService.createForum(forum);
  98. } else {
  99. forumService.updateForum(forum);
  100. }
  101. return String.format("redirect:/%s/discussion/forum/list", project.getUniqueId());
  102. }
  103. }