/connect-web/src/main/java/org/osforce/connect/web/module/list/LinkWidget.java

http://focus-sns.googlecode.com/ · Java · 166 lines · 135 code · 17 blank · 14 comment · 13 complexity · 1cf40b6b2f915abc1ff868dfc4c4f802 MD5 · raw file

  1. package org.osforce.connect.web.module.list;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.osforce.connect.entity.blog.Post;
  7. import org.osforce.connect.entity.commons.Statistic;
  8. import org.osforce.connect.entity.discussion.Topic;
  9. import org.osforce.connect.entity.document.File;
  10. import org.osforce.connect.entity.gallery.Photo;
  11. import org.osforce.connect.entity.knowledge.Question;
  12. import org.osforce.connect.entity.list.Link;
  13. import org.osforce.connect.entity.system.Project;
  14. import org.osforce.connect.service.blog.PostService;
  15. import org.osforce.connect.service.commons.StatisticService;
  16. import org.osforce.connect.service.discussion.TopicService;
  17. import org.osforce.connect.service.document.FileService;
  18. import org.osforce.connect.service.gallery.PhotoService;
  19. import org.osforce.connect.service.knowledge.QuestionService;
  20. import org.osforce.connect.service.list.LinkService;
  21. import org.osforce.connect.web.AttributeKeys;
  22. import org.osforce.connect.web.security.annotation.Permission;
  23. import org.osforce.spring4me.dao.Page;
  24. import org.osforce.spring4me.web.bind.annotation.PrefParam;
  25. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  26. import org.osforce.spring4me.web.bind.annotation.SessionParam;
  27. import org.osforce.spring4me.web.stereotype.Widget;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.ui.Model;
  30. import org.springframework.web.bind.annotation.PathVariable;
  31. import org.springframework.web.bind.annotation.RequestMapping;
  32. import org.springframework.web.bind.annotation.ResponseBody;
  33. /**
  34. *
  35. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  36. * @since 1.1.0
  37. * @create May 30, 2011 - 12:30:13 PM
  38. * <a href="http://www.opensourceforce.org">????</a>
  39. */
  40. @Widget
  41. @RequestMapping("/list/link")
  42. public class LinkWidget {
  43. private LinkService linkService;
  44. private FileService fileService;
  45. private PostService postService;
  46. private TopicService topicService;
  47. private PhotoService photoService;
  48. private QuestionService questionService;
  49. private StatisticService statisticService;
  50. public LinkWidget() {
  51. }
  52. @Autowired
  53. public void setLinkService(LinkService linkService) {
  54. this.linkService = linkService;
  55. }
  56. @Autowired
  57. public void setFileService(FileService fileService) {
  58. this.fileService = fileService;
  59. }
  60. @Autowired
  61. public void setPostService(PostService postService) {
  62. this.postService = postService;
  63. }
  64. @Autowired
  65. public void setTopicService(TopicService topicService) {
  66. this.topicService = topicService;
  67. }
  68. @Autowired
  69. public void setPhotoService(PhotoService photoService) {
  70. this.photoService = photoService;
  71. }
  72. @Autowired
  73. public void setQuestionService(QuestionService questionService) {
  74. this.questionService = questionService;
  75. }
  76. @Autowired
  77. public void setStatisticService(StatisticService statisticService) {
  78. this.statisticService = statisticService;
  79. }
  80. @RequestMapping("/recent-view")
  81. @Permission(value={"link-view"}, projectRequired=true)
  82. public String doRecentView(Page<Link> page, Model model,
  83. @RequestAttr Project project, @PrefParam String linkTypes) {
  84. page.desc("l.entered");
  85. List<String> types = Arrays.asList(StringUtils.split(linkTypes, ","));
  86. page = linkService.getLinkPage(page, project, types);
  87. associateLinkedEntity(page);
  88. model.addAttribute(AttributeKeys.PAGE_KEY_READABLE, page);
  89. return "list/link-recent";
  90. }
  91. @RequestMapping("/list-view")
  92. @Permission(value={"link-view"}, projectRequired=true)
  93. public String doListView(Page<Link> page,
  94. @PrefParam("Question") String entity, Model model,
  95. @RequestAttr Project project, @SessionParam String linkEntity) {
  96. // if blank use default entity
  97. if(StringUtils.isBlank(linkEntity)) {
  98. linkEntity = entity;
  99. }
  100. page = linkService.getLinkPage(page, project, linkEntity);
  101. associateLinkedEntity(page);
  102. model.addAttribute(AttributeKeys.PAGE_KEY_READABLE, page);
  103. return "list/link-list";
  104. }
  105. private void associateLinkedEntity(Page<Link> page) {
  106. for(Link link : page.getResult()) {
  107. Object entity = null;
  108. if(StringUtils.equals(link.getEntity(), Question.NAME)) {
  109. entity = questionService.getQuestion(link.getToId());
  110. Statistic statistic = statisticService.getStatistic(Statistic.TYPE_VIEW, link.getToId(), Question.NAME);
  111. Long views = statistic!=null ? statistic.getCount() : 0L;
  112. ((Question)entity).setViews(views);
  113. } else if (StringUtils.equals(link.getEntity(), Post.NAME)) {
  114. entity = postService.getPost(link.getToId());
  115. Statistic statistic = statisticService.getStatistic(Statistic.TYPE_VIEW, link.getToId(), Post.NAME);
  116. Long views = statistic!=null ? statistic.getCount() : 0L;
  117. ((Post)entity).setViews(views);
  118. } else if (StringUtils.equals(link.getEntity(), Topic.NAME)) {
  119. entity = topicService.getTopic(link.getToId());
  120. Statistic statistic = statisticService.getStatistic(Statistic.TYPE_VIEW, link.getToId(), Topic.NAME);
  121. Long views = statistic!=null ? statistic.getCount() : 0L;
  122. ((Topic)entity).setViews(views);
  123. } else if (StringUtils.equals(link.getEntity(), Photo.NAME)) {
  124. entity = photoService.getPhoto(link.getToId());
  125. } else if (StringUtils.equals(link.getEntity(), File.NAME)) {
  126. entity = fileService.getFile(link.getToId());
  127. }
  128. link.setLinkedEntity(entity);
  129. }
  130. }
  131. /**
  132. *
  133. * @param type such as focus, favorite ...
  134. * @param link
  135. * @return
  136. */
  137. @RequestMapping(value={"/{type}"})
  138. public @ResponseBody Object focus(@PathVariable String type, Link link) {
  139. link.setType(type);
  140. Link tmp = linkService.getLink(link.getFromId(), link.getToId(), link.getEntity());
  141. if(tmp==null) {
  142. linkService.createLink(link);
  143. } else {
  144. tmp.setType(link.getType());
  145. linkService.updateLink(tmp);
  146. }
  147. return Collections.singletonMap("id", link.getId());
  148. }
  149. }