/src/main/java/com/google/ie/common/builder/IdeaBuilder.java

http://thoughtsite.googlecode.com/ · Java · 273 lines · 135 code · 20 blank · 118 comment · 27 complexity · 698d8a003cb31f6a995cedd98660e5e3 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.common.builder;
  16. import com.google.ie.business.domain.Idea;
  17. import com.google.ie.business.domain.Project;
  18. import com.google.ie.business.domain.Tag;
  19. import com.google.ie.business.domain.User;
  20. import com.google.ie.business.service.IdeaService;
  21. import com.google.ie.business.service.ProjectService;
  22. import com.google.ie.business.service.ShardedCounterService;
  23. import com.google.ie.business.service.TagService;
  24. import com.google.ie.business.service.UserService;
  25. import com.google.ie.common.exception.IdeasExchangeException;
  26. import com.google.ie.dto.IdeaDetail;
  27. import com.google.ie.dto.RetrievalInfo;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Component;
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import java.util.Iterator;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * This will be used for converting the complex idea dto
  38. * for different request flows like getIdeaForUser, getFeatured idea.
  39. *
  40. * @author Charanjeet singh
  41. */
  42. @Component
  43. public class IdeaBuilder {
  44. private static final int ONE = 1;
  45. private static final int ZERO = 0;
  46. @Autowired
  47. private IdeaService ideaService;
  48. @Autowired
  49. private ProjectService projectService;
  50. @Autowired
  51. private TagService tagService;
  52. @Autowired
  53. private UserService userService;
  54. @Autowired
  55. private ShardedCounterService shardedCounterService;
  56. /**
  57. * Retrieves the list of published ideas along with their tags and teams by
  58. * tag names.
  59. *
  60. * @param retrievalInfo the idea list retrieval information
  61. * @return Returns the list of IdeaDetail objects
  62. */
  63. public List<IdeaDetail> getIdeasByTagName(String tagName, RetrievalInfo retrievalInfo) {
  64. List<IdeaDetail> ideaDtoList = null;
  65. /* Fetch one more record than what is required */
  66. retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
  67. List<Idea> ideaList = ideaService.getIdeasByTagName(tagName, retrievalInfo);
  68. if (ideaList != null && ideaList.size() > ZERO) {
  69. ideaDtoList = convertToIdeaDetailList(ideaList, true, true, false, true);
  70. }
  71. return ideaDtoList;
  72. }
  73. /**
  74. * Retrieves the list of published ideas along with their tags and teams.
  75. *
  76. * @param retrievalInfo the idea list retrieval information
  77. * @return Returns the list of IdeaDetail objects
  78. */
  79. public List<IdeaDetail> getIdeasForListing(RetrievalInfo retrievalInfo) {
  80. List<IdeaDetail> ideaDtoList = null;
  81. /* Fetch one more record than what is required */
  82. retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
  83. List<Idea> ideaList = ideaService.getIdeas(retrievalInfo);
  84. if (ideaList != null && ideaList.size() > 0) {
  85. ideaDtoList = convertToIdeaDetailList(ideaList, true, true, true, true);
  86. }
  87. return ideaDtoList;
  88. }
  89. /**
  90. * Retrieves the list of Ideas published or saved by user.
  91. *
  92. * @param user the {@link User} object.
  93. * @param retrievalInfo the idea list retrieval information
  94. * @return Returns the list of IdeaDetail objects
  95. */
  96. public List<IdeaDetail> getIdeasForUser(User user, RetrievalInfo retrievalInfo) {
  97. List<IdeaDetail> ideaDtoList = null;
  98. /* Fetch one more record than what is required for paging purpose */
  99. retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
  100. List<Idea> ideaList = ideaService.getIdeasForUser(user, retrievalInfo);
  101. if (ideaList != null && ideaList.size() > ZERO) {
  102. ideaDtoList = convertToIdeaDetailList(ideaList, true, true, false, true);
  103. }
  104. return ideaDtoList;
  105. }
  106. /**
  107. * Gets idea details for the given idea key. For non admin request, only
  108. * published and duplicate ideas are fetched.
  109. * For admin requests, ideas with all statuses are fetched.
  110. *
  111. * @param ideaKey the key of the idea whose details are to be fetched
  112. * @param isAdmin boolean that indicates the request is for admin
  113. * @return {@link IdeaDetail} object containing the idea details
  114. * @throws IdeasExchangeException
  115. */
  116. public IdeaDetail getIdeaDetail(String ideaKey, boolean isAdmin) throws IdeasExchangeException {
  117. Idea idea = ideaService.getIdeaByKey(ideaKey);
  118. if (idea == null)
  119. return null;
  120. if (Idea.STATUS_PUBLISHED.equalsIgnoreCase(idea.getStatus())
  121. || Idea.STATUS_DUPLICATE.equalsIgnoreCase(idea.getStatus()) || isAdmin) {
  122. return convertToIdeaDetail(idea, true, true, true, true);
  123. }
  124. throw new IdeasExchangeException("", "Idea is marked as objectionable or deleted");
  125. }
  126. /**
  127. * @param shardedCounterService the shardedCounterService to set
  128. */
  129. public void setShardedCounterService(ShardedCounterService shardedCounterService) {
  130. this.shardedCounterService = shardedCounterService;
  131. }
  132. /**
  133. * @return the shardedCounterService
  134. */
  135. public ShardedCounterService getShardedCounterService() {
  136. return shardedCounterService;
  137. }
  138. /**
  139. * Converts the the list of Idea objects into list of IdeaDetail objects.
  140. *
  141. * @param ideaList The list of Idea objects to be transformed into
  142. * IdeaDetail.
  143. * @return Returns the list of IdeaDetail.
  144. */
  145. public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList) {
  146. return convertToIdeaDetailList(ideaList, true, true, true, true);
  147. }
  148. /**
  149. * Converts the the list of Idea objects into list of IdeaDetail objects.
  150. *
  151. * @param ideaList The list of Idea objects to be transformed into
  152. * IdeaDetail.
  153. * @return Returns the list of IdeaDetail.
  154. */
  155. public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList, boolean addUser) {
  156. return convertToIdeaDetailList(ideaList, true, true, addUser, true);
  157. }
  158. /**
  159. * Converts the the list of Idea objects into list of IdeaDetail objects.
  160. *
  161. * @param ideaList The list of Idea objects to be transformed into
  162. * IdeaDetail.
  163. * @param addTags Flag which means that also fetch Tag objects associated
  164. * with
  165. * the ideas, when set true.
  166. * @param addTeams Flag which means that also fetch Team objects associated
  167. * with the ideas, when set true.
  168. * @param trimLongFields
  169. * @return Returns the list of IdeaDetail.
  170. */
  171. public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList, boolean addTags,
  172. boolean addTeams, boolean addUser, boolean trimLongFields) {
  173. List<IdeaDetail> ideaDetailList = new ArrayList<IdeaDetail>();
  174. IdeaDetail ideaDetail = null;
  175. for (Idea idea : ideaList) {
  176. if (trimLongFields) {
  177. shortenFields(idea);
  178. }
  179. ideaDetail = convertToIdeaDetail(idea, addTags, addTeams, addUser, false);
  180. ideaDetailList.add(ideaDetail);
  181. }
  182. return ideaDetailList;
  183. }
  184. /**
  185. * Shortens the length of title and description fields
  186. *
  187. * @param idea
  188. */
  189. private void shortenFields(Idea idea) {
  190. if (null != idea) {
  191. /* 50 chars for title */
  192. idea.setTitle(StringUtils.abbreviate(idea.getTitle(), 50));
  193. /* 100 chars for description */
  194. idea.setDescription(StringUtils.abbreviate(idea.getDescription(), 150));
  195. }
  196. }
  197. /**
  198. * Converts the the Idea object to IdeaDetail object.
  199. *
  200. * @param idea The Idea object to be transformed into IdeaDetail.
  201. * @param addTag Flag which means that also fetch Tag objects associated
  202. * with
  203. * the idea, when set true.
  204. * @param addTeam Flag which means that also fetch Team objects associated
  205. * with the ideas, when set true.
  206. * @param addProjects Flag which means also fetch the Project objects
  207. * associated with the idea
  208. * @return Returns the list of IdeaDetail.
  209. */
  210. public IdeaDetail convertToIdeaDetail(Idea idea, boolean addTag, boolean addTeam,
  211. boolean addUser, boolean addProjects) {
  212. IdeaDetail ideaDetail = new IdeaDetail();
  213. ideaDetail.setIdea(idea);
  214. if (addTag) {
  215. /*
  216. * Get all tags using tag keys in idea and set them to the
  217. * corresponding property in ideaDto.
  218. */
  219. List<Tag> tagList = tagService.getTagsByKeys(idea.getTagKeys());
  220. ideaDetail.setTags(tagList);
  221. }
  222. if (addUser) {
  223. User user = userService.getUserByPrimaryKey(idea.getCreatorKey());
  224. if (user != null) {
  225. /* Get reputation points for user */
  226. long userPoints = shardedCounterService.getTotalPoint(idea.getCreatorKey());
  227. user.setReputationPoints(userPoints);
  228. }
  229. ideaDetail.setUser(user);
  230. }
  231. /* If true add the projects associated with the idea */
  232. if (addProjects) {
  233. List<Project> projects = projectService.getProjectsByIdeaKey(idea.getKey());
  234. /*
  235. * If the list is not empty,iterate it and create a map with key of
  236. * the project object as the key in the map and project name as the
  237. * value.Only these two attributes are required on the idea detail
  238. * page
  239. */
  240. if (projects != null && projects.size() > ZERO) {
  241. Iterator<Project> iterator = projects.iterator();
  242. Map<String, String> mapOfProjectIdAndName = new HashMap<String, String>();
  243. Project project;
  244. while (iterator.hasNext()) {
  245. project = iterator.next();
  246. mapOfProjectIdAndName.put(project.getKey(), project.getName());
  247. }
  248. ideaDetail.setMapOfProjectIdAndName(mapOfProjectIdAndName);
  249. }
  250. }
  251. return ideaDetail;
  252. }
  253. }