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