001    /**
002     * Copyright (c) 2000-2013 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.Date;
036    import java.util.List;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Deepak Gothe
041     * @author Mika Koivisto
042     * @author Shuyang Zhou
043     */
044    public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
045    
046            @Override
047            public void deleteThread(long threadId)
048                    throws PortalException, SystemException {
049    
050                    if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
051                            throw new LockedThreadException();
052                    }
053    
054                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
055                            threadId);
056    
057                    for (MBMessage message : messages) {
058                            MBMessagePermission.check(
059                                    getPermissionChecker(), message.getMessageId(),
060                                    ActionKeys.DELETE);
061                    }
062    
063                    mbThreadLocalService.deleteThread(threadId);
064            }
065    
066            @Override
067            public List<MBThread> getGroupThreads(
068                            long groupId, long userId, Date modifiedDate, int status, int start,
069                            int end)
070                    throws PortalException, SystemException {
071    
072                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
073                            return mbThreadFinder.findByG_U_MD_S(
074                                    groupId, userId, modifiedDate, status, start, end);
075                    }
076    
077                    long[] categoryIds = mbCategoryService.getCategoryIds(
078                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
079    
080                    if (categoryIds.length == 0) {
081                            return Collections.emptyList();
082                    }
083    
084                    List<Long> threadIds = mbMessageFinder.filterFindByG_U_MD_C_S(
085                            groupId, userId, modifiedDate, categoryIds, status, start, end);
086    
087                    List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
088    
089                    for (long threadId : threadIds) {
090                            MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
091    
092                            threads.add(thread);
093                    }
094    
095                    return threads;
096            }
097    
098            @Override
099            public List<MBThread> getGroupThreads(
100                            long groupId, long userId, int status, boolean subscribed,
101                            boolean includeAnonymous, int start, int end)
102                    throws PortalException, SystemException {
103    
104                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
105                            return doGetGroupThreads(
106                                    groupId, userId, status, subscribed, includeAnonymous, start,
107                                    end);
108                    }
109    
110                    long[] categoryIds = mbCategoryService.getCategoryIds(
111                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
112    
113                    if (categoryIds.length == 0) {
114                            return Collections.emptyList();
115                    }
116    
117                    List<Long> threadIds = null;
118    
119                    if (userId <= 0) {
120                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
121                                    groupId, 0, categoryIds, status, start, end);
122                    }
123                    else {
124                            if (subscribed) {
125                                    return mbThreadFinder.filterFindByS_G_U_C_S(
126                                            groupId, userId, categoryIds, status, start, end);
127                            }
128                            else {
129                                    if (includeAnonymous) {
130                                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
131                                                    groupId, userId, categoryIds, status, start, end);
132                                    }
133                                    else {
134                                            threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
135                                                    groupId, userId, categoryIds, false, status, start,
136                                                    end);
137                                    }
138                            }
139                    }
140    
141                    List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
142    
143                    for (long threadId : threadIds) {
144                            MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
145    
146                            threads.add(thread);
147                    }
148    
149                    return threads;
150            }
151    
152            @Override
153            public List<MBThread> getGroupThreads(
154                            long groupId, long userId, int status, boolean subscribed,
155                            int start, int end)
156                    throws PortalException, SystemException {
157    
158                    return getGroupThreads(
159                            groupId, userId, status, subscribed, true, start, end);
160            }
161    
162            @Override
163            public List<MBThread> getGroupThreads(
164                            long groupId, long userId, int status, int start, int end)
165                    throws PortalException, SystemException {
166    
167                    return getGroupThreads(groupId, userId, status, false, start, end);
168            }
169    
170            @Override
171            public int getGroupThreadsCount(
172                            long groupId, long userId, Date modifiedDate, int status)
173                    throws SystemException {
174    
175                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
176                            return mbThreadFinder.countByG_U_MD_S(
177                                    groupId, userId, modifiedDate, status);
178                    }
179    
180                    long[] categoryIds = mbCategoryService.getCategoryIds(
181                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
182    
183                    if (categoryIds.length == 0) {
184                            return 0;
185                    }
186    
187                    return mbMessageFinder.filterCountByG_U_MD_C_S(
188                            groupId, userId, modifiedDate, categoryIds, status);
189            }
190    
191            @Override
192            public int getGroupThreadsCount(long groupId, long userId, int status)
193                    throws SystemException {
194    
195                    return getGroupThreadsCount(groupId, userId, status, false);
196            }
197    
198            @Override
199            public int getGroupThreadsCount(
200                            long groupId, long userId, int status, boolean subscribed)
201                    throws SystemException {
202    
203                    return getGroupThreadsCount(groupId, userId, status, subscribed, true);
204            }
205    
206            @Override
207            public int getGroupThreadsCount(
208                            long groupId, long userId, int status, boolean subscribed,
209                            boolean includeAnonymous)
210                    throws SystemException {
211    
212                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
213                            return doGetGroupThreadsCount(
214                                    groupId, userId, status, subscribed, includeAnonymous);
215                    }
216    
217                    long[] categoryIds = mbCategoryService.getCategoryIds(
218                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
219    
220                    if (categoryIds.length == 0) {
221                            return 0;
222                    }
223    
224                    if (userId <= 0) {
225                            return mbMessageFinder.filterCountByG_U_C_S(
226                                    groupId, 0, categoryIds, status);
227                    }
228                    else {
229                            if (subscribed) {
230                                    return mbThreadFinder.filterCountByS_G_U_C_S(
231                                            groupId, userId, categoryIds, status);
232                            }
233                            else {
234                                    if (includeAnonymous) {
235                                            return mbMessageFinder.filterCountByG_U_C_S(
236                                                    groupId, userId, categoryIds, status);
237                                    }
238                                    else {
239                                            return mbMessageFinder.filterCountByG_U_C_A_S(
240                                                    groupId, userId, categoryIds, false, status);
241                                    }
242                            }
243                    }
244            }
245    
246            @Override
247            public List<MBThread> getThreads(
248                            long groupId, long categoryId, int status, int start, int end)
249                    throws SystemException {
250    
251                    if (status == WorkflowConstants.STATUS_ANY) {
252                            return mbThreadFinder.filterFindByG_C(
253                                    groupId, categoryId, start, end);
254                    }
255                    else {
256                            return mbThreadFinder.filterFindByG_C_S(
257                                    groupId, categoryId, status, start, end);
258                    }
259            }
260    
261            @Override
262            public int getThreadsCount(long groupId, long categoryId, int status)
263                    throws SystemException {
264    
265                    if (status == WorkflowConstants.STATUS_ANY) {
266                            return mbThreadFinder.filterCountByG_C(groupId, categoryId);
267                    }
268                    else {
269                            return mbThreadFinder.filterCountByG_C_S(
270                                    groupId, categoryId, status);
271                    }
272            }
273    
274            @Override
275            public Lock lockThread(long threadId)
276                    throws PortalException, SystemException {
277    
278                    MBThread thread = mbThreadLocalService.getThread(threadId);
279    
280                    MBCategoryPermission.check(
281                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
282                            ActionKeys.LOCK_THREAD);
283    
284                    return lockLocalService.lock(
285                            getUserId(), MBThread.class.getName(), threadId,
286                            String.valueOf(threadId), false,
287                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
288            }
289    
290            @Override
291            public MBThread moveThread(long categoryId, long threadId)
292                    throws PortalException, SystemException {
293    
294                    MBThread thread = mbThreadLocalService.getThread(threadId);
295    
296                    MBCategoryPermission.check(
297                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
298                            ActionKeys.MOVE_THREAD);
299    
300                    MBCategoryPermission.check(
301                            getPermissionChecker(), thread.getGroupId(), categoryId,
302                            ActionKeys.MOVE_THREAD);
303    
304                    return mbThreadLocalService.moveThread(
305                            thread.getGroupId(), categoryId, threadId);
306            }
307    
308            @Override
309            public MBThread splitThread(
310                            long messageId, String subject, ServiceContext serviceContext)
311                    throws PortalException, SystemException {
312    
313                    MBMessage message = mbMessageLocalService.getMessage(messageId);
314    
315                    MBCategoryPermission.check(
316                            getPermissionChecker(), message.getGroupId(),
317                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
318    
319                    return mbThreadLocalService.splitThread(
320                            messageId, subject, serviceContext);
321            }
322    
323            @Override
324            public void unlockThread(long threadId)
325                    throws PortalException, SystemException {
326    
327                    MBThread thread = mbThreadLocalService.getThread(threadId);
328    
329                    MBCategoryPermission.check(
330                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
331                            ActionKeys.LOCK_THREAD);
332    
333                    lockLocalService.unlock(MBThread.class.getName(), threadId);
334            }
335    
336            protected List<MBThread> doGetGroupThreads(
337                            long groupId, long userId, int status, boolean subscribed,
338                            boolean includeAnonymous, int start, int end)
339                    throws SystemException {
340    
341                    if (userId <= 0) {
342                            if (status == WorkflowConstants.STATUS_ANY) {
343                                    return mbThreadPersistence.findByGroupId(groupId, start, end);
344                            }
345                            else {
346                                    return mbThreadPersistence.findByG_S(
347                                            groupId, status, start, end);
348                            }
349                    }
350                    else if (subscribed) {
351                            return mbThreadFinder.findByS_G_U_S(
352                                    groupId, userId, status, start, end);
353                    }
354                    else if (includeAnonymous) {
355                            return mbThreadFinder.findByG_U_S(
356                                    groupId, userId, status, start, end);
357                    }
358                    else {
359                            return mbThreadFinder.findByG_U_A_S(
360                                    groupId, userId, false, status, start, end);
361                    }
362            }
363    
364            protected int doGetGroupThreadsCount(
365                            long groupId, long userId, int status, boolean subscribed,
366                            boolean includeAnonymous)
367                    throws SystemException {
368    
369                    if (userId <= 0) {
370                            if (status == WorkflowConstants.STATUS_ANY) {
371                                    return mbThreadPersistence.countByGroupId(groupId);
372                            }
373                            else {
374                                    return mbThreadPersistence.countByG_S(groupId, status);
375                            }
376                    }
377                    else if (subscribed) {
378                            return mbThreadFinder.countByS_G_U_S(groupId, userId, status);
379                    }
380                    else if (includeAnonymous) {
381                            return mbThreadFinder.countByG_U_S(groupId, userId, status);
382                    }
383                    else {
384                            return mbThreadFinder.countByG_U_A_S(
385                                    groupId, userId, false, status);
386                    }
387            }
388    
389    }