001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.tasks.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.SetUtil;
022    import com.liferay.portal.model.User;
023    import com.liferay.portlet.tasks.DuplicateReviewUserIdException;
024    import com.liferay.portlet.tasks.model.TasksProposal;
025    import com.liferay.portlet.tasks.model.TasksReview;
026    import com.liferay.portlet.tasks.service.base.TasksReviewLocalServiceBaseImpl;
027    import com.liferay.portlet.tasks.social.TasksActivityKeys;
028    
029    import java.util.Date;
030    import java.util.HashSet;
031    import java.util.List;
032    import java.util.Set;
033    
034    /**
035     * @author Raymond Augé
036     * @author Brian Wing Shun Chan
037     */
038    public class TasksReviewLocalServiceImpl
039            extends TasksReviewLocalServiceBaseImpl {
040    
041            public TasksReview addReview(
042                            long userId, long proposalId, long assignedByUserId, int stage)
043                    throws PortalException, SystemException {
044    
045                    // Review
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
049                            proposalId);
050                    User assignedByUser = userPersistence.findByPrimaryKey(
051                            assignedByUserId);
052                    Date now = new Date();
053    
054                    long reviewId = counterLocalService.increment();
055    
056                    TasksReview review = tasksReviewPersistence.create(reviewId);
057    
058                    review.setGroupId(proposal.getGroupId());
059                    review.setCompanyId(user.getCompanyId());
060                    review.setUserId(user.getUserId());
061                    review.setUserName(user.getFullName());
062                    review.setCreateDate(now);
063                    review.setModifiedDate(now);
064                    review.setProposalId(proposalId);
065                    review.setAssignedByUserId(assignedByUserId);
066                    review.setAssignedByUserName(assignedByUser.getFullName());
067                    review.setStage(stage);
068                    review.setCompleted(false);
069                    review.setRejected(false);
070    
071                    tasksReviewPersistence.update(review, false);
072    
073                    // Social
074    
075                    JSONObject extraData = JSONFactoryUtil.createJSONObject();
076    
077                    extraData.put("stage", review.getStage());
078                    extraData.put("completed", review.getCompleted());
079                    extraData.put("rejected", review.getRejected());
080    
081                    socialActivityLocalService.addActivity(
082                            assignedByUserId, proposal.getGroupId(),
083                            TasksProposal.class.getName(), proposalId,
084                            TasksActivityKeys.ASSIGN_PROPOSAL, extraData.toString(), userId);
085    
086                    return review;
087            }
088    
089            public TasksReview approveReview(
090                            long userId, long proposalId, int stage)
091                    throws PortalException, SystemException {
092    
093                    return updateReview(userId, proposalId, stage, false);
094            }
095    
096            public void deleteReview(long reviewId)
097                    throws PortalException, SystemException {
098    
099                    TasksReview review = tasksReviewPersistence.findByPrimaryKey(
100                            reviewId);
101    
102                    deleteReview(review);
103            }
104    
105            public void deleteReview(TasksReview review) throws SystemException {
106                    tasksReviewPersistence.remove(review);
107            }
108    
109            public void deleteReviews(long proposalId) throws SystemException {
110                    List<TasksReview> reviews = tasksReviewPersistence.findByProposalId(
111                            proposalId);
112    
113                    for (TasksReview review : reviews) {
114                            deleteReview(review);
115                    }
116            }
117    
118            public TasksReview getReview(long reviewId)
119                    throws PortalException, SystemException {
120    
121                    return tasksReviewPersistence.findByPrimaryKey(reviewId);
122            }
123    
124            public TasksReview getReview(long userId, long proposalId)
125                    throws PortalException, SystemException {
126    
127                    return tasksReviewPersistence.findByU_P(userId, proposalId);
128            }
129    
130            public List<TasksReview> getReviews(long proposalId)
131                    throws SystemException {
132    
133                    return tasksReviewPersistence.findByProposalId(proposalId);
134            }
135    
136            public List<TasksReview> getReviews(long proposalId, int stage)
137                    throws SystemException {
138    
139                    return tasksReviewPersistence.findByP_S(proposalId, stage);
140            }
141    
142            public List<TasksReview> getReviews(
143                            long proposalId, int stage, boolean completed)
144                    throws SystemException {
145    
146                    return tasksReviewPersistence.findByP_S_C(proposalId, stage, completed);
147            }
148    
149            public List<TasksReview> getReviews(
150                            long proposalId, int stage, boolean completed, boolean rejected)
151                    throws SystemException {
152    
153                    return tasksReviewPersistence.findByP_S_C_R(
154                            proposalId, stage, completed, rejected);
155            }
156    
157            public TasksReview rejectReview(
158                            long userId, long proposalId, int stage)
159                    throws PortalException, SystemException {
160    
161                    return updateReview(userId, proposalId, stage, true);
162            }
163    
164            public void updateReviews(
165                            long proposalId, long assignedByUserId, long[][] userIdsPerStage)
166                    throws PortalException, SystemException {
167    
168                    Set<Long> assignedUserIds = new HashSet<Long>();
169    
170                    for (int i = 0; i < userIdsPerStage.length; i++) {
171                            long[] userIds = userIdsPerStage[i];
172    
173                            for (long userId : userIds) {
174                                    if (assignedUserIds.contains(userId)) {
175                                            throw new DuplicateReviewUserIdException();
176                                    }
177                                    else {
178                                            assignedUserIds.add(userId);
179                                    }
180                            }
181                    }
182    
183                    for (int i = 0; i < userIdsPerStage.length; i++) {
184                            Set<Long> userIds = SetUtil.fromArray(userIdsPerStage[i]);
185    
186                            updateReviews(proposalId, assignedByUserId, i + 2, userIds);
187                    }
188            }
189    
190            protected TasksReview updateReview(
191                            long userId, long proposalId, int stage, boolean rejected)
192                    throws PortalException, SystemException {
193    
194                    TasksReview review = tasksReviewPersistence.findByU_P(
195                            userId, proposalId);
196    
197                    review.setModifiedDate(new Date());
198                    review.setCompleted(true);
199                    review.setRejected(rejected);
200    
201                    tasksReviewPersistence.update(review, false);
202    
203                    // Social
204    
205                    JSONObject extraData = JSONFactoryUtil.createJSONObject();
206    
207                    extraData.put("stage", review.getStage());
208                    extraData.put("completed", review.getCompleted());
209                    extraData.put("rejected", review.getRejected());
210    
211                    socialActivityLocalService.addActivity(
212                            userId, review.getGroupId(), TasksProposal.class.getName(),
213                            proposalId, TasksActivityKeys.REVIEW_PROPOSAL, extraData.toString(),
214                            review.getAssignedByUserId());
215    
216                    return review;
217            }
218    
219            protected void updateReviews(
220                            long proposalId, long assignedByUserId, int stage,
221                            Set<Long> userIds)
222                    throws PortalException, SystemException {
223    
224                    Set<Long> reviewUserIds = new HashSet<Long>();
225    
226                    List<TasksReview> reviews = tasksReviewPersistence.findByP_S(
227                            proposalId, stage);
228    
229                    for (TasksReview review : reviews) {
230                            if (!userIds.contains(review.getUserId())) {
231                                    deleteReview(review);
232                            }
233                            else {
234                                    reviewUserIds.add(review.getUserId());
235                            }
236                    }
237    
238                    for (long userId : userIds) {
239                            if (!reviewUserIds.contains(userId)) {
240                                    addReview(userId, proposalId, assignedByUserId, stage);
241                            }
242                    }
243            }
244    
245    }