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.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.Lock;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.messageboards.LockedThreadException;
026    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
027    import com.liferay.portlet.messageboards.model.MBMessage;
028    import com.liferay.portlet.messageboards.model.MBThread;
029    import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
030    import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
031    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
032    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
033    
034    import java.util.ArrayList;
035    import java.util.Collections;
036    import java.util.Date;
037    import java.util.List;
038    
039    /**
040     * @author Jorge Ferrer
041     * @author Deepak Gothe
042     * @author Mika Koivisto
043     * @author Shuyang Zhou
044     */
045    public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
046    
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            public List<MBThread> getGroupThreads(
067                            long groupId, long userId, Date modifiedDate, int status, int start,
068                            int end)
069                    throws PortalException, SystemException {
070    
071                    long[] categoryIds = mbCategoryService.getCategoryIds(
072                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
073    
074                    if (categoryIds.length == 0) {
075                            return Collections.emptyList();
076                    }
077    
078                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
079                            QueryDefinition queryDefinition = new QueryDefinition(
080                                    status, start, end, null);
081    
082                            return mbThreadFinder.findByG_U_LPD(
083                                    groupId, userId, categoryIds, modifiedDate, queryDefinition);
084                    }
085    
086                    List<Long> threadIds = mbMessageFinder.filterFindByG_U_MD_C_S(
087                            groupId, userId, modifiedDate, categoryIds, status, start, end);
088    
089                    List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
090    
091                    for (long threadId : threadIds) {
092                            MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
093    
094                            threads.add(thread);
095                    }
096    
097                    return threads;
098            }
099    
100            public List<MBThread> getGroupThreads(
101                            long groupId, long userId, int status, boolean subscribed,
102                            boolean includeAnonymous, int start, int end)
103                    throws PortalException, SystemException {
104    
105                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
106                            return doGetGroupThreads(
107                                    groupId, userId, status, subscribed, includeAnonymous, start,
108                                    end);
109                    }
110    
111                    long[] categoryIds = mbCategoryService.getCategoryIds(
112                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
113    
114                    if (categoryIds.length == 0) {
115                            return Collections.emptyList();
116                    }
117    
118                    List<Long> threadIds = null;
119    
120                    if (userId <= 0) {
121                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
122                                    groupId, 0, categoryIds, status, start, end);
123                    }
124                    else {
125                            if (subscribed) {
126                                    QueryDefinition queryDefinition = new QueryDefinition(
127                                            status, start, end, null);
128    
129                                    return mbThreadFinder.filterFindByS_G_U_C(
130                                            groupId, userId, categoryIds, queryDefinition);
131                            }
132                            else {
133                                    if (includeAnonymous) {
134                                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
135                                                    groupId, userId, categoryIds, status, start, end);
136                                    }
137                                    else {
138                                            threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
139                                                    groupId, userId, categoryIds, false, status, start,
140                                                    end);
141                                    }
142                            }
143                    }
144    
145                    List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
146    
147                    for (long threadId : threadIds) {
148                            MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
149    
150                            threads.add(thread);
151                    }
152    
153                    return threads;
154            }
155    
156            public List<MBThread> getGroupThreads(
157                            long groupId, long userId, int status, boolean subscribed,
158                            int start, int end)
159                    throws PortalException, SystemException {
160    
161                    return getGroupThreads(
162                            groupId, userId, status, subscribed, true, start, end);
163            }
164    
165            public List<MBThread> getGroupThreads(
166                            long groupId, long userId, int status, int start, int end)
167                    throws PortalException, SystemException {
168    
169                    return getGroupThreads(groupId, userId, status, false, start, end);
170            }
171    
172            public int getGroupThreadsCount(
173                            long groupId, long userId, Date modifiedDate, int status)
174                    throws SystemException {
175    
176                    long[] categoryIds = mbCategoryService.getCategoryIds(
177                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
178    
179                    if (categoryIds.length == 0) {
180                            return 0;
181                    }
182    
183                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
184                            QueryDefinition queryDefinition = new QueryDefinition(status);
185    
186                            return mbThreadFinder.countByG_U_LPD(
187                                    groupId, userId, categoryIds, modifiedDate, queryDefinition);
188                    }
189    
190                    return mbMessageFinder.filterCountByG_U_MD_C_S(
191                            groupId, userId, modifiedDate, categoryIds, status);
192            }
193    
194            public int getGroupThreadsCount(long groupId, long userId, int status)
195                    throws SystemException {
196    
197                    return getGroupThreadsCount(groupId, userId, status, false);
198            }
199    
200            public int getGroupThreadsCount(
201                            long groupId, long userId, int status, boolean subscribed)
202                    throws SystemException {
203    
204                    return getGroupThreadsCount(groupId, userId, status, subscribed, true);
205            }
206    
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                                    QueryDefinition queryDefinition = new QueryDefinition(status);
231    
232                                    return mbThreadFinder.filterCountByS_G_U_C(
233                                            groupId, userId, categoryIds, queryDefinition);
234                            }
235                            else {
236                                    if (includeAnonymous) {
237                                            return mbMessageFinder.filterCountByG_U_C_S(
238                                                    groupId, userId, categoryIds, status);
239                                    }
240                                    else {
241                                            return mbMessageFinder.filterCountByG_U_C_A_S(
242                                                    groupId, userId, categoryIds, false, status);
243                                    }
244                            }
245                    }
246            }
247    
248            public List<MBThread> getThreads(
249                            long groupId, long categoryId, int status, int start, int end)
250                    throws SystemException {
251    
252                    if (status == WorkflowConstants.STATUS_ANY) {
253                            return mbThreadFinder.filterFindByG_C(
254                                    groupId, categoryId, start, end);
255                    }
256                    else {
257                            QueryDefinition queryDefinition = new QueryDefinition(
258                                    status, start, end, null);
259    
260                            return mbThreadFinder.filterFindByG_C(
261                                    groupId, new long[] {categoryId}, queryDefinition);
262                    }
263            }
264    
265            public int getThreadsCount(long groupId, long categoryId, int status)
266                    throws SystemException {
267    
268                    QueryDefinition queryDefinition = new QueryDefinition(status);
269    
270                    return mbThreadFinder.filterCountByG_C(
271                            groupId, new long[] {categoryId}, queryDefinition);
272            }
273    
274            public Lock lockThread(long threadId)
275                    throws PortalException, SystemException {
276    
277                    MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
278    
279                    MBCategoryPermission.check(
280                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
281                            ActionKeys.LOCK_THREAD);
282    
283                    return lockLocalService.lock(
284                            getUserId(), MBThread.class.getName(), threadId,
285                            String.valueOf(threadId), false,
286                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
287            }
288    
289            public MBThread moveThread(long categoryId, long threadId)
290                    throws PortalException, SystemException {
291    
292                    MBThread thread = mbThreadLocalService.getThread(threadId);
293    
294                    MBCategoryPermission.check(
295                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
296                            ActionKeys.MOVE_THREAD);
297    
298                    MBCategoryPermission.check(
299                            getPermissionChecker(), thread.getGroupId(), categoryId,
300                            ActionKeys.MOVE_THREAD);
301    
302                    return mbThreadLocalService.moveThread(
303                            thread.getGroupId(), categoryId, threadId);
304            }
305    
306            public MBThread moveThreadFromTrash(long categoryId, long threadId)
307                    throws PortalException, SystemException {
308    
309                    MBThread thread = mbThreadLocalService.getThread(threadId);
310    
311                    MBCategoryPermission.check(
312                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
313                            ActionKeys.UPDATE);
314    
315                    return mbThreadLocalService.moveThreadFromTrash(
316                            getUserId(), categoryId, threadId);
317            }
318    
319            public MBThread moveThreadToTrash(long threadId)
320                    throws PortalException, SystemException {
321    
322                    if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
323                            throw new LockedThreadException();
324                    }
325    
326                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
327                            threadId);
328    
329                    for (MBMessage message : messages) {
330                            MBMessagePermission.check(
331                                    getPermissionChecker(), message.getMessageId(),
332                                    ActionKeys.DELETE);
333                    }
334    
335                    return mbThreadLocalService.moveThreadToTrash(getUserId(), threadId);
336            }
337    
338            public void restoreThreadFromTrash(long threadId)
339                    throws PortalException, SystemException {
340    
341                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
342                            threadId);
343    
344                    for (MBMessage message : messages) {
345                            MBMessagePermission.check(
346                                    getPermissionChecker(), message.getMessageId(),
347                                    ActionKeys.DELETE);
348                    }
349    
350                    mbThreadLocalService.restoreThreadFromTrash(getUserId(), threadId);
351            }
352    
353            public MBThread splitThread(
354                            long messageId, String subject, ServiceContext serviceContext)
355                    throws PortalException, SystemException {
356    
357                    MBMessage message = mbMessageLocalService.getMessage(messageId);
358    
359                    MBCategoryPermission.check(
360                            getPermissionChecker(), message.getGroupId(),
361                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
362    
363                    return mbThreadLocalService.splitThread(
364                            messageId, subject, serviceContext);
365            }
366    
367            public void unlockThread(long threadId)
368                    throws PortalException, SystemException {
369    
370                    MBThread thread = mbThreadLocalService.getThread(threadId);
371    
372                    MBCategoryPermission.check(
373                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
374                            ActionKeys.LOCK_THREAD);
375    
376                    lockLocalService.unlock(MBThread.class.getName(), threadId);
377            }
378    
379            protected List<MBThread> doGetGroupThreads(
380                            long groupId, long userId, int status, boolean subscribed,
381                            boolean includeAnonymous, int start, int end)
382                    throws SystemException {
383    
384                    long[] categoryIds = mbCategoryService.getCategoryIds(
385                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
386    
387                    if (categoryIds.length == 0) {
388                            return Collections.emptyList();
389                    }
390    
391                    QueryDefinition queryDefinition = new QueryDefinition(
392                            status, start, end, null);
393    
394                    if (userId <= 0) {
395                            return mbThreadFinder.findByG_C(
396                                    groupId, categoryIds, queryDefinition);
397                    }
398                    else if (subscribed) {
399                            return mbThreadFinder.findByS_G_U_C(
400                                    groupId, userId, categoryIds, queryDefinition);
401                    }
402                    else if (includeAnonymous) {
403                            return mbThreadFinder.findByG_U_C(
404                                    groupId, userId, categoryIds, queryDefinition);
405                    }
406                    else {
407                            return mbThreadFinder.findByG_U_C_A(
408                                    groupId, userId, categoryIds, false, queryDefinition);
409                    }
410            }
411    
412            protected int doGetGroupThreadsCount(
413                            long groupId, long userId, int status, boolean subscribed,
414                            boolean includeAnonymous)
415                    throws SystemException {
416    
417                    long[] categoryIds = mbCategoryService.getCategoryIds(
418                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
419    
420                    if (categoryIds.length == 0) {
421                            return 0;
422                    }
423    
424                    QueryDefinition queryDefinition = new QueryDefinition(status);
425    
426                    if (userId <= 0) {
427                            return mbThreadFinder.countByG_C(
428                                    groupId, categoryIds, queryDefinition);
429                    }
430                    else if (subscribed) {
431                            return mbThreadFinder.countByS_G_U_C(
432                                    groupId, userId, categoryIds, queryDefinition);
433                    }
434                    else if (includeAnonymous) {
435                            return mbThreadFinder.countByG_U_C(
436                                    groupId, userId, categoryIds, queryDefinition);
437                    }
438                    else {
439                            return mbThreadFinder.countByG_U_C_A(
440                                    groupId, userId, categoryIds, false, queryDefinition);
441                    }
442            }
443    
444    }