/src/main/java/com/google/ie/web/controller/TagController.java

http://thoughtsite.googlecode.com/ · Java · 266 lines · 163 code · 20 blank · 83 comment · 39 complexity · fb9c0805ad15d5de258a2b58cc6bf788 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.web.controller;
  16. import com.google.ie.business.domain.Tag;
  17. import com.google.ie.business.domain.User;
  18. import com.google.ie.business.service.TagService;
  19. import com.google.ie.common.constants.IdeaExchangeConstants;
  20. import com.google.ie.common.util.SearchUtility;
  21. import com.google.ie.dto.RetrievalInfo;
  22. import com.google.ie.dto.SearchResult;
  23. import com.google.ie.dto.ViewStatus;
  24. import org.apache.log4j.Logger;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.web.bind.annotation.ModelAttribute;
  28. import org.springframework.web.bind.annotation.PathVariable;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import org.springframework.web.bind.annotation.RequestParam;
  31. import org.springframework.web.bind.annotation.SessionAttributes;
  32. import java.util.Collections;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import javax.servlet.http.HttpSession;
  37. /**
  38. * A controller that handles request for idea tags.
  39. *
  40. * @author Sachneet
  41. *
  42. */
  43. @Controller
  44. @RequestMapping("/tags")
  45. @SessionAttributes("user")
  46. public class TagController {
  47. private static Logger logger = Logger.getLogger(TagController.class);
  48. @Autowired
  49. private TagService tagService;
  50. @RequestMapping("/list")
  51. public String showTags() {
  52. // List<Tag> tags = tagService.getTagsForAutoSuggestion(initial,
  53. // retrievalInfo);
  54. return "tags/list";
  55. }
  56. /**
  57. * Get tags for auto-suggestion
  58. *
  59. * @param initial of the tag
  60. * @param retrievalInfo the {@link RetrievalInfo} object containing the
  61. * query parameters
  62. * @param model the map object
  63. */
  64. @RequestMapping("/suggest/{initial}")
  65. public void showSimilarTags(@PathVariable String initial,
  66. @ModelAttribute RetrievalInfo retrievalInfo,
  67. Map<String, Object> model) {
  68. List<Tag> tags = tagService.getTagsForAutoSuggestion(initial,
  69. retrievalInfo);
  70. ViewStatus viewStatus = ViewStatus
  71. .createTheViewStatus(tags, WebConstants.TAGS, null);
  72. /* Remove the RetrievalInfo object from model */
  73. if (model.containsKey("retrievalInfo")) {
  74. model.remove("retrievalInfo");
  75. }
  76. /* Put the ViewStatus object containing the tag data into the model */
  77. model.put(WebConstants.VIEW_STATUS, viewStatus);
  78. }
  79. /**
  80. * Get tags starting with a specific string
  81. *
  82. * @param startString
  83. * @param retrievalInfo the {@link RetrievalInfo} object containing the
  84. * query parameters
  85. * @param model the map object
  86. */
  87. @RequestMapping("/list/{startString}")
  88. public void showTagsStaringWithSpecificString(@PathVariable String startString,
  89. @ModelAttribute RetrievalInfo retrievalInfo,
  90. Map<String, Object> model) {
  91. List<Tag> tags = tagService.getTagsWithSpecificStartString(startString.toLowerCase(),
  92. retrievalInfo);
  93. Map<String, Object> parameters = new HashMap<String, Object>();
  94. if (tags != null && tags.size() > WebConstants.ZERO) {
  95. parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(tags));
  96. }
  97. ViewStatus viewStatus = ViewStatus.createTheViewStatus(tags, WebConstants.TAGS,
  98. parameters);
  99. /* Remove the RetrievalInfo object from model */
  100. if (model.containsKey("retrievalInfo")) {
  101. model.remove("retrievalInfo");
  102. }
  103. /* Put the ViewStatus object containing the tag data into the model */
  104. model.put(WebConstants.VIEW_STATUS, viewStatus);
  105. }
  106. /**
  107. * Displays tag cloud based on the retrieval parameters.
  108. *
  109. * @param retrievalInfo the {@link RetrievalInfo} object containing the
  110. * query parameters
  111. *
  112. * @param model the map object
  113. */
  114. @RequestMapping("/tagcloud")
  115. public void showTagCloud(@ModelAttribute RetrievalInfo retrievalInfo,
  116. Map<String, Object> model) {
  117. List<Tag> tags = tagService.getTagsForTagCloud(retrievalInfo);
  118. /*
  119. * The second argument is true so that a map containing max/min weight
  120. * is also added as data
  121. */
  122. Map<String, Object> parameters = new HashMap<String, Object>();
  123. if (tags != null && tags.size() > WebConstants.ZERO) {
  124. parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(tags));
  125. }
  126. ViewStatus viewStatus = ViewStatus.createTheViewStatus(tags, WebConstants.TAGS,
  127. parameters);
  128. /* Remove the RetrievalInfo object from model */
  129. if (model.containsKey("retrievalInfo")) {
  130. model.remove("retrievalInfo");
  131. }
  132. /* Put the ViewStatus object containing the tag data into the model */
  133. model.put(WebConstants.VIEW_STATUS, viewStatus);
  134. }
  135. /**
  136. * Displays the tag cloud for the specified user
  137. *
  138. * @param user the {@link User} whose cloud to be displayed
  139. * @param model the map object
  140. */
  141. @RequestMapping("/my.json")
  142. public void showMyTagCloud(HttpSession session, Map<String, Object> model) {
  143. User user = (User) session.getAttribute(WebConstants.USER);
  144. List<Tag> myTags = tagService.getMyTagCloud(user);
  145. Map<String, Object> parameters = null;
  146. if (myTags != null && myTags.size() > WebConstants.ZERO) {
  147. parameters = new HashMap<String, Object>();
  148. parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(myTags));
  149. }
  150. ViewStatus viewStatus = ViewStatus.createTheViewStatus(myTags, WebConstants.TAGS,
  151. parameters);
  152. model.put(WebConstants.VIEW_STATUS, viewStatus);
  153. if (model.containsKey(WebConstants.USER)) {
  154. model.remove(WebConstants.USER);
  155. }
  156. }
  157. @RequestMapping("/incrementweight")
  158. public String incrementWeights(@RequestParam String tagString) {
  159. if (tagString == null || tagString.equals("")) {
  160. logger.warn("No tag found in query string to increment the weight.");
  161. } else {
  162. List<Tag> tagList = tagService.incrementWeights(tagString);
  163. if (tagList == null || tagList.size() == 0) {
  164. logger.warn("No tag weight increment occured.");
  165. }
  166. }
  167. return "queue/queue";
  168. }
  169. /**
  170. * Decrement the weight for the tag.
  171. *
  172. * @param tagString for which weight to be decremented
  173. * @return
  174. */
  175. @RequestMapping("/decrementweight")
  176. public String decrementWeights(@RequestParam String tagString) {
  177. if (tagString == null || tagString.equals("")) {
  178. logger.warn("No tag found in query string to decrement the weight.");
  179. } else {
  180. List<Tag> tagList = tagService.decrementWeights(tagString);
  181. if (tagList == null || tagList.size() == 0) {
  182. logger.warn("No tag weight decrement occured.");
  183. }
  184. }
  185. return "queue/queue";
  186. }
  187. /**
  188. * Searches for a tag.
  189. *
  190. * @param offset of the search result
  191. * @param keyword for which tag to be searched
  192. * @param model the map object
  193. */
  194. @SuppressWarnings("unchecked")
  195. @RequestMapping("/search")
  196. public void searchTag(@RequestParam(required = false) String offset,
  197. @RequestParam(required = false) String keyword,
  198. Map<String, Object> model) {
  199. int offsetToSearch = 0;
  200. if (offset != null && offset.trim().length() > 0) {
  201. try {
  202. offsetToSearch = Integer.parseInt(offset);
  203. } catch (Exception e) {
  204. // no need to throw number format exception.
  205. }
  206. }
  207. keyword = WebConstants.ASTERISK + keyword + WebConstants.ASTERISK;
  208. ViewStatus viewStatus = new ViewStatus();
  209. SearchResult searchResult = SearchUtility.search(keyword, true, "title", offsetToSearch,
  210. IdeaExchangeConstants.DEFAULT_PAGE_SIZE, Tag.class.getSimpleName());
  211. List<Tag> tags = null;
  212. if (searchResult != null && searchResult.getTotalCount() > 0) {
  213. tags = (List<Tag>) searchResult.getData();
  214. tags = tagService.removeTagWithZeroWeight(tags);
  215. }
  216. if (tags != null && tags.size() > WebConstants.ZERO) {
  217. viewStatus.setStatus(WebConstants.SUCCESS);
  218. viewStatus.addData(WebConstants.TAGS, tags);
  219. } else {
  220. viewStatus.setStatus(WebConstants.ERROR);
  221. viewStatus.addMessage(WebConstants.ERROR, WebConstants.NO_TAGS_FOUND);
  222. }
  223. model.put(WebConstants.VIEW_STATUS, viewStatus);
  224. }
  225. public TagService getTagService() {
  226. return tagService;
  227. }
  228. public void setTagService(TagService tagService) {
  229. this.tagService = tagService;
  230. }
  231. /**
  232. * Return a map containing the maximum and minimum weights of the tags in
  233. * the tag cloud data received as list
  234. *
  235. * @param tags the list of tags to be searched for max and min weights
  236. * @return a map containing the maximum and minimum weights
  237. */
  238. private Map<String, Long> getTheMaxAndMinWeights(List<Tag> tags) {
  239. long min = Collections.min(tags).getWeightage();
  240. long max = Collections.max(tags).getWeightage();
  241. Map<String, Long> mapOfMinMax = new HashMap<String, Long>();
  242. mapOfMinMax.put(WebConstants.MAX_WEIGHT, max);
  243. mapOfMinMax.put(WebConstants.MIN_WEIGHT, min);
  244. return mapOfMinMax;
  245. }
  246. }