001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 splitThread(
296                            long messageId, String subject, ServiceContext serviceContext)
297                    throws PortalException, SystemException {
298    
299                    MBMessage message = mbMessageLocalService.getMessage(messageId);
300    
301                    MBCategoryPermission.check(
302                            getPermissionChecker(), message.getGroupId(),
303                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
304    
305                    return mbThreadLocalService.splitThread(
306                            messageId, subject, serviceContext);
307            }
308    
309            public void unlockThread(long threadId)
310                    throws PortalException, SystemException {
311    
312                    MBThread thread = mbThreadLocalService.getThread(threadId);
313    
314                    MBCategoryPermission.check(
315                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
316                            ActionKeys.LOCK_THREAD);
317    
318                    lockLocalService.unlock(MBThread.class.getName(), threadId);
319            }
320    
321            protected List<MBThread> doGetGroupThreads(
322                            long groupId, long userId, int status, boolean subscribed,
323                            boolean includeAnonymous, int start, int end)
324                    throws SystemException {
325    
326                    if (userId <= 0) {
327                            if (status == WorkflowConstants.STATUS_ANY) {
328                                    return mbThreadPersistence.findByGroupId(groupId, start, end);
329                            }
330                            else {
331                                    return mbThreadPersistence.findByG_S(
332                                            groupId, status, start, end);
333                            }
334                    }
335                    else if (subscribed) {
336                            return mbThreadFinder.findByS_G_U_S(
337                                    groupId, userId, status, start, end);
338                    }
339                    else if (includeAnonymous) {
340                            return mbThreadFinder.findByG_U_S(
341                                    groupId, userId, status, start, end);
342                    }
343                    else {
344                            return mbThreadFinder.findByG_U_A_S(
345                                    groupId, userId, false, status, start, end);
346                    }
347            }
348    
349            protected int doGetGroupThreadsCount(
350                            long groupId, long userId, int status, boolean subscribed,
351                            boolean includeAnonymous)
352                    throws SystemException {
353    
354                    if (userId <= 0) {
355                            if (status == WorkflowConstants.STATUS_ANY) {
356                                    return mbThreadPersistence.countByGroupId(groupId);
357                            }
358                            else {
359                                    return mbThreadPersistence.countByG_S(groupId, status);
360                            }
361                    }
362                    else if (subscribed) {
363                            return mbThreadFinder.countByS_G_U_S(groupId, userId, status);
364                    }
365                    else if (includeAnonymous) {
366                            return mbThreadFinder.countByG_U_S(groupId, userId, status);
367                    }
368                    else {
369                            return mbThreadFinder.countByG_U_A_S(
370                                    groupId, userId, false, status);
371                    }
372            }
373    
374    }