001    /**
002     * Copyright (c) 2000-2012 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.security.permission.InlineSQLHelperUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.messageboards.LockedThreadException;
025    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
026    import com.liferay.portlet.messageboards.model.MBMessage;
027    import com.liferay.portlet.messageboards.model.MBThread;
028    import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
029    import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
030    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
031    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
032    
033    import java.util.ArrayList;
034    import java.util.Collections;
035    import java.util.List;
036    
037    /**
038     * @author Jorge Ferrer
039     * @author Deepak Gothe
040     * @author Mika Koivisto
041     * @author Shuyang Zhou
042     */
043    public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
044    
045            public void deleteThread(long threadId)
046                    throws PortalException, SystemException {
047    
048                    if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
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                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
070                            return doGetGroupThreads(
071                                    groupId, userId, status, subscribed, includeAnonymous, start,
072                                    end);
073                    }
074    
075                    long[] categoryIds = mbCategoryService.getCategoryIds(
076                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
077    
078                    if (categoryIds.length == 0) {
079                            return Collections.emptyList();
080                    }
081    
082                    if (userId <= 0) {
083                            if (status == WorkflowConstants.STATUS_ANY) {
084                                    return mbThreadPersistence.findByG_C(
085                                            groupId, categoryIds, start, end);
086                            }
087                            else {
088                                    return mbThreadPersistence.findByG_C_S(
089                                            groupId, categoryIds, status, start, end);
090                            }
091                    }
092                    else {
093                            if (subscribed) {
094                                    return mbThreadFinder.filterFindByS_G_U_C_S(
095                                            groupId, userId, categoryIds, status, start, end);
096                            }
097                            else {
098                                    List<Long> threadIds = null;
099    
100                                    if (includeAnonymous) {
101                                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
102                                                    groupId, userId, categoryIds, status, start, end);
103                                    }
104                                    else {
105                                            threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
106                                                    groupId, userId, categoryIds, false, status, start,
107                                                    end);
108                                    }
109    
110                                    List<MBThread> threads = new ArrayList<MBThread>(
111                                            threadIds.size());
112    
113                                    for (long threadId : threadIds) {
114                                            MBThread thread = mbThreadPersistence.findByPrimaryKey(
115                                                    threadId);
116    
117                                            threads.add(thread);
118                                    }
119    
120                                    return threads;
121                            }
122                    }
123            }
124    
125            public List<MBThread> getGroupThreads(
126                            long groupId, long userId, int status, boolean subscribed,
127                            int start, int end)
128                    throws PortalException, SystemException {
129    
130                    return getGroupThreads(
131                            groupId, userId, status, subscribed, true, start, end);
132            }
133    
134            public List<MBThread> getGroupThreads(
135                            long groupId, long userId, int status, int start, int end)
136                    throws PortalException, SystemException {
137    
138                    return getGroupThreads(groupId, userId, status, false, start, end);
139            }
140    
141            public int getGroupThreadsCount(long groupId, long userId, int status)
142                    throws SystemException {
143    
144                    return getGroupThreadsCount(groupId, userId, status, false);
145            }
146    
147            public int getGroupThreadsCount(
148                            long groupId, long userId, int status, boolean subscribed)
149                    throws SystemException {
150    
151                    return getGroupThreadsCount(groupId, userId, status, subscribed, true);
152            }
153    
154            public int getGroupThreadsCount(
155                            long groupId, long userId, int status, boolean subscribed,
156                            boolean includeAnonymous)
157                    throws SystemException {
158    
159                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
160                            return doGetGroupThreadsCount(
161                                    groupId, userId, status, subscribed, includeAnonymous);
162                    }
163    
164                    long[] categoryIds = mbCategoryService.getCategoryIds(
165                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
166    
167                    if (categoryIds.length == 0) {
168                            return 0;
169                    }
170    
171                    if (userId <= 0) {
172                            if (status == WorkflowConstants.STATUS_ANY) {
173                                    return mbThreadPersistence.countByG_C(groupId, categoryIds);
174                            }
175                            else {
176                                    return mbThreadPersistence.countByG_C_S(
177                                            groupId, categoryIds, status);
178                            }
179                    }
180                    else {
181                            if (subscribed) {
182                                    return mbThreadFinder.filterCountByS_G_U_C_S(
183                                            groupId, userId, categoryIds, status);
184                            }
185                            else {
186                                    if (includeAnonymous) {
187                                            return mbMessageFinder.filterCountByG_U_C_S(
188                                                    groupId, userId, categoryIds, status);
189                                    }
190                                    else {
191                                            return mbMessageFinder.filterCountByG_U_C_A_S(
192                                                    groupId, userId, categoryIds, false, status);
193                                    }
194                            }
195                    }
196            }
197    
198            public List<MBThread> getThreads(
199                            long groupId, long categoryId, int status, int start, int end)
200                    throws SystemException {
201    
202                    if (status == WorkflowConstants.STATUS_ANY) {
203                            return mbThreadFinder.filterFindByG_C(
204                                    groupId, categoryId, start, end);
205                    }
206                    else {
207                            return mbThreadFinder.filterFindByG_C_S(
208                                    groupId, categoryId, status, start, end);
209                    }
210            }
211    
212            public int getThreadsCount(long groupId, long categoryId, int status)
213                    throws SystemException {
214    
215                    if (status == WorkflowConstants.STATUS_ANY) {
216                            return mbThreadFinder.filterCountByG_C(groupId, categoryId);
217                    }
218                    else {
219                            return mbThreadFinder.filterCountByG_C_S(
220                                    groupId, categoryId, status);
221                    }
222            }
223    
224            public Lock lockThread(long threadId)
225                    throws PortalException, SystemException {
226    
227                    MBThread thread = mbThreadLocalService.getThread(threadId);
228    
229                    MBCategoryPermission.check(
230                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
231                            ActionKeys.LOCK_THREAD);
232    
233                    return lockLocalService.lock(
234                            getUserId(), MBThread.class.getName(), threadId,
235                            String.valueOf(threadId), false,
236                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
237            }
238    
239            public MBThread moveThread(long categoryId, long threadId)
240                    throws PortalException, SystemException {
241    
242                    MBThread thread = mbThreadLocalService.getThread(threadId);
243    
244                    MBCategoryPermission.check(
245                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
246                            ActionKeys.MOVE_THREAD);
247    
248                    MBCategoryPermission.check(
249                            getPermissionChecker(), thread.getGroupId(), categoryId,
250                            ActionKeys.MOVE_THREAD);
251    
252                    return mbThreadLocalService.moveThread(
253                            thread.getGroupId(), categoryId, threadId);
254            }
255    
256            public MBThread splitThread(
257                            long messageId, String subject, ServiceContext serviceContext)
258                    throws PortalException, SystemException {
259    
260                    MBMessage message = mbMessageLocalService.getMessage(messageId);
261    
262                    MBCategoryPermission.check(
263                            getPermissionChecker(), message.getGroupId(),
264                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
265    
266                    return mbThreadLocalService.splitThread(
267                            messageId, subject, serviceContext);
268            }
269    
270            public void unlockThread(long threadId)
271                    throws PortalException, SystemException {
272    
273                    MBThread thread = mbThreadLocalService.getThread(threadId);
274    
275                    MBCategoryPermission.check(
276                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
277                            ActionKeys.LOCK_THREAD);
278    
279                    lockLocalService.unlock(MBThread.class.getName(), threadId);
280            }
281    
282            protected List<MBThread> doGetGroupThreads(
283                            long groupId, long userId, int status, boolean subscribed,
284                            boolean includeAnonymous, int start, int end)
285                    throws SystemException {
286    
287                    if (userId <= 0) {
288                            if (status == WorkflowConstants.STATUS_ANY) {
289                                    return mbThreadPersistence.findByGroupId(groupId, start, end);
290                            }
291                            else {
292                                    return mbThreadPersistence.findByG_S(
293                                            groupId, status, start, end);
294                            }
295                    }
296                    else if (subscribed) {
297                            return mbThreadFinder.findByS_G_U_S(
298                                    groupId, userId, status, start, end);
299                    }
300                    else if (includeAnonymous) {
301                            return mbThreadFinder.findByG_U_S(
302                                    groupId, userId, status, start, end);
303                    }
304                    else {
305                            return mbThreadFinder.findByG_U_A_S(
306                                    groupId, userId, false, status, start, end);
307                    }
308            }
309    
310            protected int doGetGroupThreadsCount(
311                            long groupId, long userId, int status, boolean subscribed,
312                            boolean includeAnonymous)
313                    throws SystemException {
314    
315                    if (userId <= 0) {
316                            if (status == WorkflowConstants.STATUS_ANY) {
317                                    return mbThreadPersistence.countByGroupId(groupId);
318                            }
319                            else {
320                                    return mbThreadPersistence.countByG_S(groupId, status);
321                            }
322                    }
323                    else if (subscribed) {
324                            return mbThreadFinder.countByS_G_U_S(groupId, userId, status);
325                    }
326                    else if (includeAnonymous) {
327                            return mbThreadFinder.countByG_U_S(groupId, userId, status);
328                    }
329                    else {
330                            return mbThreadFinder.countByG_U_A_S(
331                                    groupId, userId, false, status);
332                    }
333            }
334    
335    }