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.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                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
072                            QueryDefinition queryDefinition = new QueryDefinition(
073                                    status, start, end, null);
074    
075                            return mbThreadFinder.findByG_U_LPD(
076                                    groupId, userId, modifiedDate, queryDefinition);
077                    }
078    
079                    long[] categoryIds = mbCategoryService.getCategoryIds(
080                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
081    
082                    if (categoryIds.length == 0) {
083                            return Collections.emptyList();
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                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
177                            QueryDefinition queryDefinition = new QueryDefinition(status);
178    
179                            return mbThreadFinder.countByG_U_LPD(
180                                    groupId, userId, modifiedDate, queryDefinition);
181                    }
182    
183                    long[] categoryIds = mbCategoryService.getCategoryIds(
184                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
185    
186                    if (categoryIds.length == 0) {
187                            return 0;
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, categoryId, queryDefinition);
262                    }
263            }
264    
265            public int getThreadsCount(long groupId, long categoryId, int status)
266                    throws SystemException {
267    
268                    if (status == WorkflowConstants.STATUS_ANY) {
269                            return mbThreadFinder.filterCountByG_C(groupId, categoryId);
270                    }
271                    else {
272                            QueryDefinition queryDefinition = new QueryDefinition(status);
273    
274                            return mbThreadFinder.filterCountByG_C(
275                                    groupId, categoryId, queryDefinition);
276                    }
277            }
278    
279            public Lock lockThread(long threadId)
280                    throws PortalException, SystemException {
281    
282                    MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
283    
284                    MBCategoryPermission.check(
285                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
286                            ActionKeys.LOCK_THREAD);
287    
288                    return lockLocalService.lock(
289                            getUserId(), MBThread.class.getName(), threadId,
290                            String.valueOf(threadId), false,
291                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
292            }
293    
294            public MBThread moveThread(long categoryId, long threadId)
295                    throws PortalException, SystemException {
296    
297                    MBThread thread = mbThreadLocalService.getThread(threadId);
298    
299                    MBCategoryPermission.check(
300                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
301                            ActionKeys.MOVE_THREAD);
302    
303                    MBCategoryPermission.check(
304                            getPermissionChecker(), thread.getGroupId(), categoryId,
305                            ActionKeys.MOVE_THREAD);
306    
307                    return mbThreadLocalService.moveThread(
308                            thread.getGroupId(), categoryId, threadId);
309            }
310    
311            public MBThread moveThreadFromTrash(long categoryId, long threadId)
312                    throws PortalException, SystemException {
313    
314                    MBThread thread = mbThreadLocalService.getThread(threadId);
315    
316                    MBCategoryPermission.check(
317                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
318                            ActionKeys.UPDATE);
319    
320                    return mbThreadLocalService.moveThreadFromTrash(
321                            getUserId(), categoryId, threadId);
322            }
323    
324            public MBThread moveThreadToTrash(long threadId)
325                    throws PortalException, SystemException {
326    
327                    if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
328                            throw new LockedThreadException();
329                    }
330    
331                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
332                            threadId);
333    
334                    for (MBMessage message : messages) {
335                            MBMessagePermission.check(
336                                    getPermissionChecker(), message.getMessageId(),
337                                    ActionKeys.DELETE);
338                    }
339    
340                    return mbThreadLocalService.moveThreadToTrash(getUserId(), threadId);
341            }
342    
343            public void restoreThreadFromTrash(long threadId)
344                    throws PortalException, SystemException {
345    
346                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
347                            threadId);
348    
349                    for (MBMessage message : messages) {
350                            MBMessagePermission.check(
351                                    getPermissionChecker(), message.getMessageId(),
352                                    ActionKeys.DELETE);
353                    }
354    
355                    mbThreadLocalService.restoreThreadFromTrash(getUserId(), threadId);
356            }
357    
358            public MBThread splitThread(
359                            long messageId, String subject, ServiceContext serviceContext)
360                    throws PortalException, SystemException {
361    
362                    MBMessage message = mbMessageLocalService.getMessage(messageId);
363    
364                    MBCategoryPermission.check(
365                            getPermissionChecker(), message.getGroupId(),
366                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
367    
368                    return mbThreadLocalService.splitThread(
369                            messageId, subject, serviceContext);
370            }
371    
372            public void unlockThread(long threadId)
373                    throws PortalException, SystemException {
374    
375                    MBThread thread = mbThreadLocalService.getThread(threadId);
376    
377                    MBCategoryPermission.check(
378                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
379                            ActionKeys.LOCK_THREAD);
380    
381                    lockLocalService.unlock(MBThread.class.getName(), threadId);
382            }
383    
384            protected List<MBThread> doGetGroupThreads(
385                            long groupId, long userId, int status, boolean subscribed,
386                            boolean includeAnonymous, int start, int end)
387                    throws SystemException {
388    
389                    if (userId <= 0) {
390                            if (status == WorkflowConstants.STATUS_ANY) {
391                                    return mbThreadPersistence.findByGroupId(groupId, start, end);
392                            }
393                            else {
394                                    return mbThreadPersistence.findByG_S(
395                                            groupId, status, start, end);
396                            }
397                    }
398                    else if (subscribed) {
399                            QueryDefinition queryDefinition = new QueryDefinition(
400                                    status, start, end, null);
401    
402                            return mbThreadFinder.findByS_G_U(groupId, userId, queryDefinition);
403                    }
404                    else if (includeAnonymous) {
405                            QueryDefinition queryDefinition = new QueryDefinition(
406                                    status, start, end, null);
407    
408                            return mbThreadFinder.findByG_U(groupId, userId, queryDefinition);
409                    }
410                    else {
411                            QueryDefinition queryDefinition = new QueryDefinition(
412                                    status, start, end, null);
413    
414                            return mbThreadFinder.findByG_U_A(
415                                    groupId, userId, false, queryDefinition);
416                    }
417            }
418    
419            protected int doGetGroupThreadsCount(
420                            long groupId, long userId, int status, boolean subscribed,
421                            boolean includeAnonymous)
422                    throws SystemException {
423    
424                    if (userId <= 0) {
425                            if (status == WorkflowConstants.STATUS_ANY) {
426                                    return mbThreadPersistence.countByGroupId(groupId);
427                            }
428                            else {
429                                    return mbThreadPersistence.countByG_S(groupId, status);
430                            }
431                    }
432                    else if (subscribed) {
433                            QueryDefinition queryDefinition = new QueryDefinition(status);
434    
435                            return mbThreadFinder.countByS_G_U(
436                                    groupId, userId, queryDefinition);
437                    }
438                    else if (includeAnonymous) {
439                            QueryDefinition queryDefinition = new QueryDefinition(status);
440    
441                            return mbThreadFinder.countByG_U(groupId, userId, queryDefinition);
442                    }
443                    else {
444                            QueryDefinition queryDefinition = new QueryDefinition(status);
445    
446                            return mbThreadFinder.countByG_U_A(
447                                    groupId, userId, false, queryDefinition);
448                    }
449            }
450    
451    }