001    /**
002     * Copyright (c) 2000-2011 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.List;
036    
037    /**
038     * @author Jorge Ferrer
039     * @author Deepak Gothe
040     * @author Mika Koivisto
041     * @author Shuyang Zhou
042     */
043    public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
044    
045            public void deleteThread(long threadId)
046                    throws PortalException, SystemException {
047    
048                    if (lockLocalService.isLocked(
049                                    MBThread.class.getName(), threadId)) {
050    
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, int status, boolean subscribed,
068                            boolean includeAnonymous, int start, int end)
069                    throws PortalException, SystemException {
070    
071                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
072                            return doGetGroupThreads(
073                                    groupId, userId, status, subscribed, includeAnonymous, start,
074                                    end);
075                    }
076    
077                    long[] categoryIds = mbCategoryService.getCategoryIds(
078                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
079    
080                    if (categoryIds.length == 0) {
081                            return Collections.emptyList();
082                    }
083    
084                    if (userId <= 0) {
085                            if (status == WorkflowConstants.STATUS_ANY) {
086                                    return mbThreadPersistence.findByG_C(
087                                            groupId, categoryIds, start, end);
088                            }
089                            else {
090                                    return mbThreadPersistence.findByG_C_S(
091                                            groupId, categoryIds, status, start, end);
092                            }
093                    }
094                    else {
095                            if (subscribed) {
096                                    return mbThreadFinder.filterFindByS_G_U_C_S(
097                                            groupId, userId, categoryIds, status, start, end);
098                            }
099                            else {
100                                    List<Long> threadIds = null;
101    
102                                    if (includeAnonymous) {
103                                            threadIds = mbMessageFinder.filterFindByG_U_C_S(
104                                                    groupId, userId, categoryIds, status, start, end);
105                                    }
106                                    else {
107                                            threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
108                                                    groupId, userId, categoryIds, false, status, start,
109                                                    end);
110                                    }
111    
112                                    List<MBThread> threads = new ArrayList<MBThread>(
113                                            threadIds.size());
114    
115                                    for (long threadId : threadIds) {
116                                            MBThread thread = mbThreadPersistence.findByPrimaryKey(
117                                                    threadId);
118    
119                                            threads.add(thread);
120                                    }
121    
122                                    return threads;
123                            }
124                    }
125            }
126    
127            public List<MBThread> getGroupThreads(
128                            long groupId, long userId, int status, boolean subscribed,
129                            int start, int end)
130                    throws PortalException, SystemException {
131    
132                    return getGroupThreads(
133                            groupId, userId, status, subscribed, true, start, end);
134            }
135    
136            public List<MBThread> getGroupThreads(
137                            long groupId, long userId, int status, int start, int end)
138                    throws PortalException, SystemException {
139    
140                    return getGroupThreads(groupId, userId, status, false, start, end);
141            }
142    
143            public int getGroupThreadsCount(long groupId, long userId, int status)
144                    throws SystemException {
145    
146                    return getGroupThreadsCount(groupId, userId, status, false);
147            }
148    
149            public int getGroupThreadsCount(
150                            long groupId, long userId, int status, boolean subscribed)
151                    throws SystemException {
152    
153                    return getGroupThreadsCount(groupId, userId, status, subscribed, true);
154            }
155    
156            public int getGroupThreadsCount(
157                            long groupId, long userId, int status, boolean subscribed,
158                            boolean includeAnonymous)
159                    throws SystemException {
160    
161                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
162                            return doGetGroupThreadsCount(
163                                    groupId, userId, status, subscribed, includeAnonymous);
164                    }
165    
166                    long[] categoryIds = mbCategoryService.getCategoryIds(
167                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
168    
169                    if (categoryIds.length == 0) {
170                            return 0;
171                    }
172    
173                    if (userId <= 0) {
174                            if (status == WorkflowConstants.STATUS_ANY) {
175                                    return mbThreadPersistence.countByG_C(
176                                            groupId, categoryIds);
177                            }
178                            else {
179                                    return mbThreadPersistence.countByG_C_S(
180                                            groupId, categoryIds, status);
181                            }
182                    }
183                    else {
184                            if (subscribed) {
185                                    return mbThreadFinder.filterCountByS_G_U_C_S(
186                                            groupId, userId, categoryIds, status);
187                            }
188                            else {
189                                    if (includeAnonymous) {
190                                            return mbMessageFinder.filterCountByG_U_C_S(
191                                                    groupId, userId, categoryIds, status);
192                                    }
193                                    else {
194                                            return mbMessageFinder.filterCountByG_U_C_A_S(
195                                                    groupId, userId, categoryIds, false, status);
196                                    }
197                            }
198                    }
199            }
200    
201            public List<MBThread> getThreads(
202                            long groupId, long categoryId, int status, int start, int end)
203                    throws SystemException {
204    
205                    if (status == WorkflowConstants.STATUS_ANY) {
206                            return mbThreadFinder.filterFindByG_C(
207                                    groupId, categoryId, start, end);
208                    }
209                    else {
210                            return mbThreadFinder.filterFindByG_C_S(
211                                    groupId, categoryId, status, start, end);
212                    }
213            }
214    
215            public int getThreadsCount(long groupId, long categoryId, int status)
216                    throws SystemException {
217    
218                    if (status == WorkflowConstants.STATUS_ANY) {
219                            return mbThreadFinder.filterCountByG_C(groupId, categoryId);
220                    }
221                    else {
222                            return mbThreadFinder.filterCountByG_C_S(
223                                    groupId, categoryId, status);
224                    }
225            }
226    
227            public Lock lockThread(long threadId)
228                    throws PortalException, SystemException {
229    
230                    MBThread thread = mbThreadLocalService.getThread(threadId);
231    
232                    MBCategoryPermission.check(
233                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
234                            ActionKeys.LOCK_THREAD);
235    
236                    return lockLocalService.lock(
237                            getUserId(), MBThread.class.getName(), threadId,
238                            String.valueOf(threadId), false,
239                            MBThreadModelImpl.LOCK_EXPIRATION_TIME);
240            }
241    
242            public MBThread moveThread(long categoryId, long threadId)
243                    throws PortalException, SystemException {
244    
245                    MBThread thread = mbThreadLocalService.getThread(threadId);
246    
247                    MBCategoryPermission.check(
248                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
249                            ActionKeys.MOVE_THREAD);
250    
251                    MBCategoryPermission.check(
252                            getPermissionChecker(), thread.getGroupId(), categoryId,
253                            ActionKeys.MOVE_THREAD);
254    
255                    return mbThreadLocalService.moveThread(
256                            thread.getGroupId(), categoryId, threadId);
257            }
258    
259            public MBThread splitThread(
260                            long messageId, String subject, ServiceContext serviceContext)
261                    throws PortalException, SystemException {
262    
263                    MBMessage message = mbMessageLocalService.getMessage(messageId);
264    
265                    MBCategoryPermission.check(
266                            getPermissionChecker(), message.getGroupId(),
267                            message.getCategoryId(), ActionKeys.MOVE_THREAD);
268    
269                    return mbThreadLocalService.splitThread(
270                            messageId, subject, serviceContext);
271            }
272    
273            public void unlockThread(long threadId)
274                    throws PortalException, SystemException {
275    
276                    MBThread thread = mbThreadLocalService.getThread(threadId);
277    
278                    MBCategoryPermission.check(
279                            getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
280                            ActionKeys.LOCK_THREAD);
281    
282                    lockLocalService.unlock(MBThread.class.getName(), threadId);
283            }
284    
285            protected List<MBThread> doGetGroupThreads(
286                            long groupId, long userId, int status, boolean subscribed,
287                            boolean includeAnonymous, int start, int end)
288                    throws SystemException {
289    
290                    if (userId <= 0) {
291                            if (status == WorkflowConstants.STATUS_ANY) {
292                                    return mbThreadPersistence.findByGroupId(groupId, start, end);
293                            }
294                            else {
295                                    return mbThreadPersistence.findByG_S(
296                                            groupId, status, start, end);
297                            }
298                    }
299                    else if (subscribed) {
300                            return mbThreadFinder.findByS_G_U_S(
301                                    groupId, userId, status, start, end);
302                    }
303                    else if (includeAnonymous) {
304                            return mbThreadFinder.findByG_U_S(
305                                    groupId, userId, status, start, end);
306                    }
307                    else {
308                            return mbThreadFinder.findByG_U_A_S(
309                                    groupId, userId, false, status, start, end);
310                    }
311            }
312    
313            protected int doGetGroupThreadsCount(
314                            long groupId, long userId, int status, boolean subscribed,
315                            boolean includeAnonymous)
316                    throws SystemException {
317    
318                    if (userId <= 0) {
319                            if (status == WorkflowConstants.STATUS_ANY) {
320                                    return mbThreadPersistence.countByGroupId(groupId);
321                            }
322                            else {
323                                    return mbThreadPersistence.countByG_S(groupId, status);
324                            }
325                    }
326                    else if (subscribed) {
327                            return mbThreadFinder.countByS_G_U_S(groupId, userId, status);
328                    }
329                    else if (includeAnonymous) {
330                            return mbThreadFinder.countByG_U_S(groupId, userId, status);
331                    }
332                    else {
333                            return mbThreadFinder.countByG_U_A_S(
334                                    groupId, userId, false, status);
335                    }
336            }
337    
338    }