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.messageboards.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.workflow.WorkflowConstants;
020    import com.liferay.portal.model.Lock;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.messageboards.LockedThreadException;
024    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
025    import com.liferay.portlet.messageboards.model.MBMessage;
026    import com.liferay.portlet.messageboards.model.MBThread;
027    import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
028    import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
029    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
030    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
031    
032    import java.util.ArrayList;
033    import java.util.Collections;
034    import java.util.List;
035    
036    /**
037     * @author Jorge Ferrer
038     * @author Deepak Gothe
039     * @author Mika Koivisto
040     */
041    public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
042    
043            public void deleteThread(long threadId)
044                    throws PortalException, SystemException {
045    
046                    if (lockLocalService.isLocked(
047                                    MBThread.class.getName(), threadId)) {
048    
049                            throw new LockedThreadException();
050                    }
051    
052                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
053                            threadId);
054    
055                    for (MBMessage message : messages) {
056                            MBMessagePermission.check(
057                                    getPermissionChecker(), message.getMessageId(),
058                                    ActionKeys.DELETE);
059                    }
060    
061                    mbThreadLocalService.deleteThread(threadId);
062            }
063    
064            public List<MBThread> getGroupThreads(
065                            long groupId, long userId, int status, boolean subscribed,
066                            boolean includeAnonymous, int start, int end)
067                    throws PortalException, SystemException {
068    
069                    long[] categoryIds = mbCategoryService.getCategoryIds(
070                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
071    
072                    if (categoryIds.length == 0) {
073                            return Collections.EMPTY_LIST;
074                    }
075    
076                    if (userId <= 0) {
077                            if (status == WorkflowConstants.STATUS_ANY) {
078                                    return mbThreadPersistence.findByG_C(
079                                            groupId, categoryIds, start, end);
080                            }
081                            else {
082                                    return mbThreadPersistence.findByG_C_S(
083                                            groupId, categoryIds, status, start, end);
084                            }
085                    }
086                    else {
087                            if (subscribed) {
088                                    return mbThreadFinder.filterFindByS_G_U_C_S(
089                                            groupId, userId, categoryIds, status, start, end);
090                            }
091                            else {
092                                    List<Long> threadIds = null;
093    
094                                    if (includeAnonymous) {
095                                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
096                                                    groupId, userId, categoryIds, status, start, end);
097                                    }
098                                    else {
099                                            threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
100                                                    groupId, userId, categoryIds, false, status, start,
101                                                    end);
102                                    }
103    
104                                    List<MBThread> threads = new ArrayList<MBThread>(
105                                            threadIds.size());
106    
107                                    for (long threadId : threadIds) {
108                                            MBThread thread = mbThreadPersistence.findByPrimaryKey(
109                                                    threadId);
110    
111                                            threads.add(thread);
112                                    }
113    
114                                    return threads;
115                            }
116                    }
117            }
118    
119            public List<MBThread> getGroupThreads(
120                            long groupId, long userId, int status, boolean subscribed,
121                            int start, int end)
122                    throws PortalException, SystemException {
123    
124                    return getGroupThreads(
125                            groupId, userId, status, subscribed, true, start, end);
126            }
127    
128            public List<MBThread> getGroupThreads(
129                            long groupId, long userId, int status, int start, int end)
130                    throws PortalException, SystemException {
131    
132                    return getGroupThreads(groupId, userId, status, false, start, end);
133            }
134    
135            public int getGroupThreadsCount(long groupId, long userId, int status)
136                    throws SystemException {
137    
138                    return getGroupThreadsCount(groupId, userId, status, false);
139            }
140    
141            public int getGroupThreadsCount(
142                            long groupId, long userId, int status, boolean subscribed)
143                    throws SystemException {
144    
145                    return getGroupThreadsCount(groupId, userId, status, subscribed, true);
146            }
147    
148            public int getGroupThreadsCount(
149                            long groupId, long userId, int status, boolean subscribed,
150                            boolean includeAnonymous)
151                    throws SystemException {
152    
153                    long[] categoryIds = mbCategoryService.getCategoryIds(
154                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
155    
156                    if (categoryIds.length == 0) {
157                            return 0;
158                    }
159    
160                    if (userId <= 0) {
161                            if (status == WorkflowConstants.STATUS_ANY) {
162                                    return mbThreadPersistence.countByG_C(
163                                            groupId, categoryIds);
164                            }
165                            else {
166                                    return mbThreadPersistence.countByG_C_S(
167                                            groupId, categoryIds, status);
168                            }
169                    }
170                    else {
171                            if (subscribed) {
172                                    return mbThreadFinder.filterCountByS_G_U_C_S(
173                                            groupId, userId, categoryIds, status);
174                            }
175                            else {
176                                    if (includeAnonymous) {
177                                            return mbMessageFinder.filterCountByG_U_C_S(
178                                                    groupId, userId, categoryIds, status);
179                                    }
180                                    else {
181                                            return mbMessageFinder.filterCountByG_U_C_A_S(
182                                                    groupId, userId, categoryIds, false, status);
183                                    }
184                            }
185                    }
186            }
187    
188            public List<MBThread> getThreads(
189                            long groupId, long categoryId, int status, int start, int end)
190                    throws SystemException {
191    
192                    if (status == WorkflowConstants.STATUS_ANY) {
193                            return mbThreadFinder.filterFindByG_C(
194                                    groupId, categoryId, start, end);
195                    }
196                    else {
197                            return mbThreadFinder.filterFindByG_C_S(
198                                    groupId, categoryId, status, start, end);
199                    }
200            }
201    
202            public int getThreadsCount(long groupId, long categoryId, int status)
203                    throws SystemException {
204    
205                    if (status == WorkflowConstants.STATUS_ANY) {
206                            return mbThreadFinder.filterCountByG_C(groupId, categoryId);
207                    }
208                    else {
209                            return mbThreadFinder.filterCountByG_C_S(
210                                    groupId, categoryId, status);
211                    }
212            }
213    
214            public Lock lockThread(long threadId)
215                    throws PortalException, SystemException {
216    
217                    MBThread thread = mbThreadLocalService.getThread(threadId);
218    
219                    MBCategoryPermission.check(
220                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
221                            ActionKeys.LOCK_THREAD);
222    
223                    return lockLocalService.lock(
224                            getUserId(), MBThread.class.getName(), threadId,
225                            String.valueOf(threadId), false,
226                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
227            }
228    
229            public MBThread moveThread(long categoryId, long threadId)
230                    throws PortalException, SystemException {
231    
232                    MBThread thread = mbThreadLocalService.getThread(threadId);
233    
234                    MBCategoryPermission.check(
235                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
236                            ActionKeys.MOVE_THREAD);
237    
238                    MBCategoryPermission.check(
239                            getPermissionChecker(), thread.getGroupId(), categoryId,
240                            ActionKeys.MOVE_THREAD);
241    
242                    return mbThreadLocalService.moveThread(
243                            thread.getGroupId(), categoryId, threadId);
244            }
245    
246            public MBThread splitThread(long messageId, ServiceContext serviceContext)
247                    throws PortalException, SystemException {
248    
249                    MBMessage message = mbMessageLocalService.getMessage(messageId);
250    
251                    MBCategoryPermission.check(
252                            getPermissionChecker(), message.getGroupId(),
253                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
254    
255                    return mbThreadLocalService.splitThread(messageId, serviceContext);
256            }
257    
258            public void unlockThread(long threadId)
259                    throws PortalException, SystemException {
260    
261                    MBThread thread = mbThreadLocalService.getThread(threadId);
262    
263                    MBCategoryPermission.check(
264                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
265                            ActionKeys.LOCK_THREAD);
266    
267                    lockLocalService.unlock(MBThread.class.getName(), threadId);
268            }
269    
270    }