/src/main/java/com/google/ie/business/service/impl/ProjectCommentServiceImpl.java

http://thoughtsite.googlecode.com/ · Java · 287 lines · 186 code · 29 blank · 72 comment · 40 complexity · e78f9841f124e102979e4564203b211d 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.business.service.impl;
  16. import com.google.ie.business.dao.AdminRequestDao;
  17. import com.google.ie.business.dao.CommentDao;
  18. import com.google.ie.business.dao.impl.DaoConstants;
  19. import com.google.ie.business.domain.AdminRequest;
  20. import com.google.ie.business.domain.Comment;
  21. import com.google.ie.business.domain.EntityIndex;
  22. import com.google.ie.business.domain.ProjectComment;
  23. import com.google.ie.business.domain.User;
  24. import com.google.ie.business.service.CommentService;
  25. import com.google.ie.business.service.EntityIndexService;
  26. import com.google.ie.business.service.ServiceConstants;
  27. import com.google.ie.common.audit.AuditManager;
  28. import com.google.ie.common.constants.IdeaExchangeConstants;
  29. import com.google.ie.common.constants.IdeaExchangeErrorCodes;
  30. import com.google.ie.common.exception.SystemException;
  31. import com.google.ie.common.taskqueue.IndexQueueUpdater;
  32. import com.google.ie.dto.RetrievalInfo;
  33. import com.google.ie.web.controller.WebConstants;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.apache.log4j.Logger;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.stereotype.Service;
  38. import java.util.Date;
  39. import java.util.List;
  40. /**
  41. * A service implementation of CommentService. Class is used to provide
  42. * operations on comments of projects.
  43. *
  44. * @author Anuj Sirohi
  45. *
  46. */
  47. @Service
  48. public class ProjectCommentServiceImpl implements CommentService {
  49. private static final Logger LOGGER = Logger.getLogger(ProjectCommentServiceImpl.class);
  50. @Autowired
  51. private CommentDao commentDao;
  52. @Autowired
  53. private AuditManager auditManager;
  54. @Autowired
  55. private EntityIndexService entityIndexService;
  56. /**
  57. * @return the entityIndexService
  58. */
  59. public EntityIndexService getEntityIndexService() {
  60. return entityIndexService;
  61. }
  62. /**
  63. * @param entityIndexService the entityIndexService to set
  64. */
  65. public void setEntityIndexService(EntityIndexService entityIndexService) {
  66. this.entityIndexService = entityIndexService;
  67. }
  68. @Autowired
  69. private AdminRequestDao adminRequestDao;
  70. @Autowired
  71. private IndexQueueUpdater indexQueueUpdater;
  72. public IndexQueueUpdater getIndexQueueUpdater() {
  73. return indexQueueUpdater;
  74. }
  75. public void setIndexQueueUpdater(IndexQueueUpdater indexQueueUpdater) {
  76. this.indexQueueUpdater = indexQueueUpdater;
  77. }
  78. private static final String PROJECT_KEY = "projectKey";
  79. @Override
  80. public Comment addComment(Comment comment, User user) {
  81. if (comment != null && !StringUtils.isBlank(comment.getText())
  82. && user != null && !StringUtils.isBlank(user.getUserKey())) {
  83. comment.setCreatorKey(user.getUserKey());
  84. comment.setCreatedOn(new Date());
  85. comment.setStatus(Comment.STATUS_SAVED);
  86. comment = commentDao.saveComment(comment);
  87. if (comment != null && comment.getKey() != null) {
  88. /*
  89. * Index the entity.Create an EntityIndex object for the entity
  90. * to be indexed and then queue the job to task queue
  91. */
  92. EntityIndex entityIndex = entityIndexService.createEntityIndex(comment.getKey());
  93. getIndexQueueUpdater().indexEntity(entityIndex.getKey());
  94. }
  95. if (comment != null && !StringUtils.isBlank(comment.getKey())) {
  96. auditManager.audit(user.getUserKey(), comment.getKey(),
  97. ProjectComment.class.getSimpleName(),
  98. ServiceConstants.AUDIT_ACTION_TYPE_SAVE_COMMENT);
  99. LOGGER.info("Project comment is saved");
  100. return comment;
  101. }
  102. LOGGER.error("Comment is not saved : Detached Comment object is null or without key");
  103. throw new SystemException("save.failed.exception",
  104. "Comment is not saved : Detached Comment object is null or without key");
  105. }
  106. LOGGER.error("Parameter passed to the method are illegal");
  107. throw new SystemException("illegal.argument.exception",
  108. "Either Comment is null or is without text / User is null or without key");
  109. }
  110. @Override
  111. public String flagComment(String projectCommentKey, User user) {
  112. /* Get description of the Comment */
  113. String status = IdeaExchangeConstants.FAIL;
  114. Comment comment = this.getCommentById(projectCommentKey);
  115. /* check if comment is already flagged */
  116. if (comment != null && comment.getStatus().equals(ProjectComment.STATUS_FLAGGED)) {
  117. status = IdeaExchangeConstants.PROJETC_COMMENT_ALLREADY_FLAGED;
  118. return status;
  119. }
  120. /* Create admin request to flag a project comment */
  121. AdminRequest adminRequest = new AdminRequest();
  122. adminRequest.setEntityKey(projectCommentKey);
  123. adminRequest.setEntityType(ProjectComment.class.getSimpleName());
  124. adminRequest.setRequesterkey(user.getUserKey());
  125. adminRequest.setRequestType(AdminRequest.REQUEST_OBJECTIONABLE);
  126. adminRequest.setCreatedOn(new Date());
  127. adminRequest.setStatus(AdminRequest.STATUS_PENDING);
  128. if (!StringUtils.isBlank(user.getEmailId())) {
  129. adminRequest.setRequesterEmail(user.getEmailId());
  130. }
  131. if (comment != null && !StringUtils.isBlank(comment.getKey())) {
  132. adminRequest.setEntityTitle(getTrimmedComment(comment.getText()));
  133. comment.setStatus(Comment.STATUS_FLAGGED);
  134. commentDao.saveComment(comment);
  135. } else {
  136. throw new SystemException(
  137. IdeaExchangeErrorCodes.COMMENT_NULL_EXCEPTION,
  138. "No Comment is associated with the given key");
  139. }
  140. if (adminRequestDao.saveRequest(adminRequest)) {
  141. status = IdeaExchangeConstants.SUCCESS;
  142. }
  143. return status;
  144. }
  145. /**
  146. * Trim the comment text upto 40 characters.
  147. *
  148. * @param commentText text of the comment posted on idea.
  149. * @return trimmed comment text
  150. */
  151. protected String getTrimmedComment(String commentText) {
  152. StringBuilder trimcomment = new StringBuilder();
  153. if (StringUtils.length(commentText) > 40) {
  154. trimcomment.append(commentText.substring(0, 40));
  155. trimcomment.append("..");
  156. } else {
  157. trimcomment.append(commentText);
  158. }
  159. return trimcomment.toString();
  160. }
  161. @Override
  162. public Comment getCommentById(String entityKey) {
  163. return getCommentDao().findEntityByPrimaryKey(ProjectComment.class, entityKey);
  164. }
  165. @Override
  166. public List<Comment> getComments(String key, RetrievalInfo retrievalInfo) {
  167. List<Comment> commentList;
  168. /* Fetch one more record than what is required */
  169. retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + WebConstants.ONE);
  170. if (!StringUtils.isBlank(key)) {
  171. retrievalInfo = prepareRetrievalInfo(retrievalInfo);
  172. commentList = getCommentDao().getComments(key, retrievalInfo, PROJECT_KEY);
  173. if (commentList != null && commentList.size() > ServiceConstants.ZERO) {
  174. return commentList;
  175. }
  176. LOGGER.info("Comment List is null : There is no comment on the given Idea.");
  177. }
  178. LOGGER.warn("Project key is null or has whitespace only or no comment found on project");
  179. return null;
  180. }
  181. /**
  182. * Prepares the {@link RetrievalInfo} object with values to be used as query
  183. * parameters.
  184. * Checks the received RetrievalInfo object attributes for valid
  185. * data.Updates the attributes if they contain garbage values.If the
  186. * received {@link RetrievalInfo} object is null,sets it to a new instance
  187. * with its attributes set to default values.
  188. *
  189. * @param retrievalInfo the {@link RetrievalInfo} object containing the
  190. * values to be used as query parameters
  191. * @return the {@link RetrievalInfo} object containing the query parameters
  192. */
  193. private RetrievalInfo prepareRetrievalInfo(RetrievalInfo retrievalInfo) {
  194. if (retrievalInfo == null) {
  195. retrievalInfo = new RetrievalInfo();
  196. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  197. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  198. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  199. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  200. } else {
  201. // Handle garbage values if any.
  202. String orderOn = retrievalInfo.getOrderBy();
  203. String orderByParam = retrievalInfo.getOrderBy();
  204. if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
  205. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  206. if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
  207. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  208. if (orderByParam == null || !((orderByParam.equals(DaoConstants.ORDERING_ASCENDING)
  209. || orderByParam.equals(DaoConstants.ORDERING_DESCENDING))))
  210. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  211. if (orderOn == null || !(orderOn.equals(ServiceConstants.
  212. IDEA_COMMENT_ORDERING_FIELD_CREATED_ON))) {
  213. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  214. }
  215. }
  216. return retrievalInfo;
  217. }
  218. @Override
  219. public void updateComment(Comment comment) {
  220. commentDao.saveComment(comment);
  221. }
  222. /**
  223. * @return the commentDao
  224. */
  225. public CommentDao getCommentDao() {
  226. return commentDao;
  227. }
  228. /**
  229. * @param commentDao the commentDao to set
  230. */
  231. public void setCommentDao(CommentDao commentDao) {
  232. this.commentDao = commentDao;
  233. }
  234. /**
  235. * @return the auditManager
  236. */
  237. public AuditManager getAuditManager() {
  238. return auditManager;
  239. }
  240. /**
  241. * @param auditManager the auditManager to set
  242. */
  243. public void setAuditManager(AuditManager auditManager) {
  244. this.auditManager = auditManager;
  245. }
  246. /**
  247. * @return the adminRequestDao
  248. */
  249. public AdminRequestDao getAdminRequestDao() {
  250. return adminRequestDao;
  251. }
  252. /**
  253. * @param adminRequestDao the adminRequestDao to set
  254. */
  255. public void setAdminRequestDao(AdminRequestDao adminRequestDao) {
  256. this.adminRequestDao = adminRequestDao;
  257. }
  258. }