PageRenderTime 337ms CodeModel.GetById 131ms app.highlight 96ms RepoModel.GetById 105ms app.codeStats 0ms

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