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