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