1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.workflow.WorkflowConstants;
20  import com.liferay.portal.model.Lock;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portlet.messageboards.LockedThreadException;
24  import com.liferay.portlet.messageboards.model.MBMessage;
25  import com.liferay.portlet.messageboards.model.MBThread;
26  import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
27  import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
28  import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
29  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
30  
31  import java.util.List;
32  
33  /**
34   * <a href="MBThreadServiceImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Jorge Ferrer
37   * @author Deepak Gothe
38   * @author Mika Koivisto
39   */
40  public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
41  
42      public void deleteThread(long threadId)
43          throws PortalException, SystemException {
44  
45          if (lockLocalService.isLocked(
46                  MBThread.class.getName(), threadId)) {
47  
48              throw new LockedThreadException();
49          }
50  
51          List<MBMessage> messages = mbMessagePersistence.findByThreadId(
52              threadId);
53  
54          for (MBMessage message : messages) {
55              MBMessagePermission.check(
56                  getPermissionChecker(), message.getMessageId(),
57                  ActionKeys.DELETE);
58          }
59  
60          mbThreadLocalService.deleteThread(threadId);
61      }
62  
63      public List<MBThread> getThreads(
64              long groupId, long categoryId, int status, int start, int end)
65          throws SystemException {
66  
67          if (status == WorkflowConstants.STATUS_ANY) {
68              return mbThreadFinder.filterFindByG_C(
69                  groupId, categoryId, start, end);
70          }
71          else {
72              return mbThreadFinder.filterFindByG_C_S(
73                  groupId, categoryId, status, start, end);
74          }
75      }
76  
77      public int getThreadsCount(long groupId, long categoryId, int status)
78          throws SystemException {
79  
80          if (status == WorkflowConstants.STATUS_ANY) {
81              return mbThreadFinder.filterCountByG_C(groupId, categoryId);
82          }
83          else {
84              return mbThreadFinder.filterCountByG_C_S(
85                  groupId, categoryId, status);
86          }
87      }
88  
89      public Lock lockThread(long threadId)
90          throws PortalException, SystemException {
91  
92          MBThread thread = mbThreadLocalService.getThread(threadId);
93  
94          MBCategoryPermission.check(
95              getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
96              ActionKeys.LOCK_THREAD);
97  
98          return lockLocalService.lock(
99              getUserId(), MBThread.class.getName(), threadId,
100             String.valueOf(threadId), false,
101             MBThreadModelImpl.LOCK_EXPIRATION_TIME);
102     }
103 
104     public MBThread moveThread(long categoryId, long threadId)
105         throws PortalException, SystemException {
106 
107         MBThread thread = mbThreadLocalService.getThread(threadId);
108 
109         MBCategoryPermission.check(
110             getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
111             ActionKeys.MOVE_THREAD);
112 
113         MBCategoryPermission.check(
114             getPermissionChecker(), thread.getGroupId(), categoryId,
115             ActionKeys.MOVE_THREAD);
116 
117         return mbThreadLocalService.moveThread(
118             thread.getGroupId(), categoryId, threadId);
119     }
120 
121     public MBThread splitThread(long messageId, ServiceContext serviceContext)
122         throws PortalException, SystemException {
123 
124         MBMessage message = mbMessageLocalService.getMessage(messageId);
125 
126         MBCategoryPermission.check(
127             getPermissionChecker(), message.getGroupId(),
128             message.getCategoryId(), ActionKeys.MOVE_THREAD);
129 
130         return mbThreadLocalService.splitThread(messageId, serviceContext);
131     }
132 
133     public void unlockThread(long threadId)
134         throws PortalException, SystemException {
135 
136         MBThread thread = mbThreadLocalService.getThread(threadId);
137 
138         MBCategoryPermission.check(
139             getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
140             ActionKeys.LOCK_THREAD);
141 
142         lockLocalService.unlock(MBThread.class.getName(), threadId);
143     }
144 
145 }