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

http://thoughtsite.googlecode.com/ · Java · 331 lines · 207 code · 36 blank · 88 comment · 43 complexity · c387ed536b1675e2ffdaecb45f3b4f0c 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.IdeaComment;
  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.business.service.ShardedCounterService;
  28. import com.google.ie.common.audit.AuditManager;
  29. import com.google.ie.common.constants.IdeaExchangeConstants;
  30. import com.google.ie.common.constants.IdeaExchangeErrorCodes;
  31. import com.google.ie.common.exception.SystemException;
  32. import com.google.ie.common.taskqueue.IndexQueueUpdater;
  33. import com.google.ie.dto.RetrievalInfo;
  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 org.springframework.transaction.annotation.Propagation;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import java.util.Date;
  41. import java.util.List;
  42. /**
  43. * A service implementation of the CommentService
  44. *
  45. * @author Sachneet
  46. *
  47. */
  48. @Service
  49. public class IdeaCommentServiceImpl implements CommentService {
  50. private static Logger logger = Logger.getLogger(IdeaCommentServiceImpl.class);
  51. private static final String IDEA_KEY = "ideaKey";
  52. @Autowired
  53. private CommentDao commentDao;
  54. @Autowired
  55. private AuditManager auditManager;
  56. @Autowired
  57. private EntityIndexService entityIndexService;
  58. @Autowired
  59. private ShardedCounterService shardedCounterService;
  60. @Autowired
  61. private AdminRequestDao adminRequestDao;
  62. public IdeaCommentServiceImpl() {
  63. }
  64. @Autowired
  65. private IndexQueueUpdater indexQueueUpdater;
  66. @Override
  67. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  68. public Comment addComment(Comment comment, User user) {
  69. if (comment != null && comment.getText() != null && user != null
  70. && user.getUserKey() != null) {
  71. comment.setCreatorKey(user.getUserKey());
  72. comment.setCreatedOn(new Date(System.currentTimeMillis()));
  73. comment.setStatus(Comment.STATUS_SAVED);
  74. comment = saveComment(comment);
  75. if (comment != null && comment.getKey() != null) {
  76. // add user points
  77. user = addPointsToUserOnPostComment(user, comment);
  78. getAuditManager().audit(user.getUserKey(), comment.getKey(),
  79. comment.getClass().getName(),
  80. ServiceConstants.AUDIT_ACTION_TYPE_SAVE_COMMENT);
  81. logger.info("Comment saved successfully.");
  82. }
  83. return comment;
  84. }
  85. return null;
  86. }
  87. /**
  88. * Saved a comment and mark it for indexing.
  89. *
  90. * @param comment Comment object to be saved.
  91. * @return Saved comment.
  92. */
  93. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  94. private Comment saveComment(Comment comment) {
  95. comment = getCommentDao().saveComment(comment);
  96. /*
  97. * Index the entity.Create an EntityIndex object for the entity to be
  98. * indexed and then queue the job to task queue
  99. */
  100. if (comment.getKey() != null) {
  101. EntityIndex entityIndex = entityIndexService.createEntityIndex(comment.getKey());
  102. getIndexQueueUpdater().indexEntity(entityIndex.getKey());
  103. }
  104. return comment;
  105. }
  106. @Override
  107. public String flagComment(String ideaCommentKey, User user) {
  108. String status = IdeaExchangeConstants.FAIL;
  109. /* Get description of the Comment */
  110. Comment comment = this.getCommentById(ideaCommentKey);
  111. if (comment != null && comment.getStatus().equals(IdeaComment.STATUS_FLAGGED)) {
  112. status = IdeaExchangeConstants.IDEA_COMMENT_ALLREADY_FLAGED;
  113. return status;
  114. }
  115. AdminRequest adminRequest = new AdminRequest();
  116. adminRequest.setEntityKey(ideaCommentKey);
  117. adminRequest.setEntityType(IdeaComment.class.getSimpleName());
  118. adminRequest.setRequesterkey(user.getUserKey());
  119. adminRequest.setRequestType(AdminRequest.REQUEST_OBJECTIONABLE);
  120. adminRequest.setCreatedOn(new Date());
  121. adminRequest.setStatus(AdminRequest.STATUS_PENDING);
  122. if (!StringUtils.isBlank(user.getEmailId())) {
  123. adminRequest.setRequesterEmail(user.getEmailId());
  124. }
  125. if (comment != null && !StringUtils.isBlank(comment.getKey())) {
  126. adminRequest.setEntityTitle(getTrimmedComment(comment.getText()));
  127. comment.setStatus(Comment.STATUS_FLAGGED);
  128. commentDao.saveComment(comment);
  129. } else {
  130. throw new SystemException(
  131. IdeaExchangeErrorCodes.COMMENT_NULL_EXCEPTION,
  132. "No Comment is associated with the given key");
  133. }
  134. if (adminRequestDao.saveRequest(adminRequest)) {
  135. status = IdeaExchangeConstants.SUCCESS;
  136. }
  137. return status;
  138. }
  139. /**
  140. * Trim the comment text upto 40 characters.
  141. *
  142. * @param commentText
  143. * @return the trimmed text.
  144. */
  145. protected String getTrimmedComment(String commentText) {
  146. StringBuilder trimcomment = new StringBuilder();
  147. if (StringUtils.length(commentText) > 40) {
  148. trimcomment.append(commentText.substring(0, 40));
  149. trimcomment.append("..");
  150. } else {
  151. trimcomment.append(commentText);
  152. }
  153. return trimcomment.toString();
  154. }
  155. @Override
  156. public List<Comment> getComments(String ideaKey, RetrievalInfo retrievalInfo) {
  157. List<Comment> commentList;
  158. if (!StringUtils.isBlank(ideaKey)) {
  159. retrievalInfo = prepareRetrievalInfo(retrievalInfo);
  160. commentList = getCommentDao().getComments(ideaKey, retrievalInfo, IDEA_KEY);
  161. if (commentList != null && commentList.size() > ServiceConstants.ZERO) {
  162. return commentList;
  163. }
  164. logger.info("Comment List is null : There is no comment on the given Idea.");
  165. }
  166. logger.warn("Idea key is null or has whitespace only");
  167. return null;
  168. }
  169. /**
  170. * Prepares the {@link RetrievalInfo} object with values to be used as query
  171. * parameters.
  172. * Checks the received RetrievalInfo object attributes for valid
  173. * data.Updates the attributes if they contain garbage values.If the
  174. * received {@link RetrievalInfo} object is null,sets it to a new instance
  175. * with its attributes set to default values.
  176. *
  177. * @param retrievalInfo the {@link RetrievalInfo} object containing the
  178. * values to be used as query parameters
  179. * @return the {@link RetrievalInfo} object containing the query parameters
  180. */
  181. private RetrievalInfo prepareRetrievalInfo(RetrievalInfo retrievalInfo) {
  182. if (retrievalInfo == null) {
  183. retrievalInfo = new RetrievalInfo();
  184. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  185. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  186. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  187. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  188. } else {
  189. // Handle garbage values if any.
  190. String orderOn = retrievalInfo.getOrderBy();
  191. String orderByParam = retrievalInfo.getOrderType();
  192. if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
  193. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  194. if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
  195. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  196. if (orderByParam == null || !((orderByParam.equals(DaoConstants.ORDERING_ASCENDING)
  197. || orderByParam.equals(DaoConstants.ORDERING_DESCENDING))))
  198. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  199. if (orderOn == null || !(orderOn.equals(ServiceConstants.
  200. IDEA_COMMENT_ORDERING_FIELD_CREATED_ON))) {
  201. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  202. }
  203. }
  204. return retrievalInfo;
  205. }
  206. /**
  207. * Update sharded counters for the users reputation point calculation.
  208. *
  209. * @param user the User who posted the comment on idea
  210. * @param idea The Idea object.
  211. * @return the User who posted the comment on idea.
  212. */
  213. private User addPointsToUserOnPostComment(User user, Comment comment) {
  214. int points = 0;
  215. if (comment.getKey() != null) {
  216. points = IdeaExchangeConstants.REPUTATION_POINTS_COMMENT_POST;
  217. }
  218. shardedCounterService.updateTotalPoints(user.getUserKey(), points);
  219. return user;
  220. }
  221. @Override
  222. public void updateComment(Comment comment) {
  223. commentDao.saveComment(comment);
  224. }
  225. /**
  226. * @param auditManager the auditManager to set
  227. */
  228. public void setAuditManager(AuditManager auditManager) {
  229. this.auditManager = auditManager;
  230. }
  231. /**
  232. * @return the auditManager
  233. */
  234. public AuditManager getAuditManager() {
  235. return auditManager;
  236. }
  237. /**
  238. * @param commentDao the commentDao to set
  239. */
  240. public void setCommentDao(CommentDao commentDao) {
  241. this.commentDao = commentDao;
  242. }
  243. /**
  244. * @return the commentDao
  245. */
  246. public CommentDao getCommentDao() {
  247. return commentDao;
  248. }
  249. @Override
  250. public Comment getCommentById(String entityKey) {
  251. return commentDao.findEntityByPrimaryKey(IdeaComment.class, entityKey);
  252. }
  253. /**
  254. * @return the entityIndexService
  255. */
  256. public EntityIndexService getEntityIndexService() {
  257. return entityIndexService;
  258. }
  259. /**
  260. * @param entityIndexService the entityIndexService to set
  261. */
  262. public void setEntityIndexService(EntityIndexService entityIndexService) {
  263. this.entityIndexService = entityIndexService;
  264. }
  265. /**
  266. * @return the shardedCounterService
  267. */
  268. public ShardedCounterService getShardedCounterService() {
  269. return shardedCounterService;
  270. }
  271. /**
  272. * @param shardedCounterService the shardedCounterService to set
  273. */
  274. public void setShardedCounterService(ShardedCounterService shardedCounterService) {
  275. this.shardedCounterService = shardedCounterService;
  276. }
  277. /**
  278. * @return the adminRequestDao
  279. */
  280. public AdminRequestDao getAdminRequestDao() {
  281. return adminRequestDao;
  282. }
  283. /**
  284. * @param adminRequestDao the adminRequestDao to set
  285. */
  286. public void setAdminRequestDao(AdminRequestDao adminRequestDao) {
  287. this.adminRequestDao = adminRequestDao;
  288. }
  289. public IndexQueueUpdater getIndexQueueUpdater() {
  290. return indexQueueUpdater;
  291. }
  292. public void setIndexQueueUpdater(IndexQueueUpdater indexQueueUpdater) {
  293. this.indexQueueUpdater = indexQueueUpdater;
  294. }
  295. }