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.model.Lock;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portlet.messageboards.LockedThreadException;
23  import com.liferay.portlet.messageboards.model.MBMessage;
24  import com.liferay.portlet.messageboards.model.MBThread;
25  import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
26  import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
27  import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
28  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
29  
30  import java.util.List;
31  
32  /**
33   * <a href="MBThreadServiceImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   * @author Deepak Gothe
37   * @author Mika Koivisto
38   */
39  public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
40  
41      public void deleteThread(long threadId)
42          throws PortalException, SystemException {
43  
44          if (lockLocalService.isLocked(
45                  MBThread.class.getName(), threadId)) {
46  
47              throw new LockedThreadException();
48          }
49  
50          List<MBMessage> messages = mbMessagePersistence.findByThreadId(
51              threadId);
52  
53          for (MBMessage message : messages) {
54              MBMessagePermission.check(
55                  getPermissionChecker(), message.getMessageId(),
56                  ActionKeys.DELETE);
57          }
58  
59          mbThreadLocalService.deleteThread(threadId);
60      }
61  
62      public Lock lockThread(long threadId)
63          throws PortalException, SystemException {
64  
65          MBThread thread = mbThreadLocalService.getThread(threadId);
66  
67          MBCategoryPermission.check(
68              getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
69              ActionKeys.LOCK_THREAD);
70  
71          return lockLocalService.lock(
72              getUserId(), MBThread.class.getName(), threadId,
73              String.valueOf(threadId), false,
74              MBThreadModelImpl.LOCK_EXPIRATION_TIME);
75      }
76  
77      public MBThread moveThread(long categoryId, long threadId)
78          throws PortalException, SystemException {
79  
80          MBThread thread = mbThreadLocalService.getThread(threadId);
81  
82          MBCategoryPermission.check(
83              getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
84              ActionKeys.MOVE_THREAD);
85  
86          MBCategoryPermission.check(
87              getPermissionChecker(), thread.getGroupId(), categoryId,
88              ActionKeys.MOVE_THREAD);
89  
90          return mbThreadLocalService.moveThread(
91              thread.getGroupId(), categoryId, threadId);
92      }
93  
94      public MBThread splitThread(long messageId, ServiceContext serviceContext)
95          throws PortalException, SystemException {
96  
97          MBMessage message = mbMessageLocalService.getMessage(messageId);
98  
99          MBCategoryPermission.check(
100             getPermissionChecker(), message.getGroupId(),
101             message.getCategoryId(), ActionKeys.MOVE_THREAD);
102 
103         return mbThreadLocalService.splitThread(messageId, serviceContext);
104     }
105 
106     public void unlockThread(long threadId)
107         throws PortalException, SystemException {
108 
109         MBThread thread = mbThreadLocalService.getThread(threadId);
110 
111         MBCategoryPermission.check(
112             getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
113             ActionKeys.LOCK_THREAD);
114 
115         lockLocalService.unlock(MBThread.class.getName(), threadId);
116     }
117 
118 }