1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.impl;
24  
25  import com.liferay.documentlibrary.NoSuchDirectoryException;
26  import com.liferay.documentlibrary.service.DLServiceUtil;
27  import com.liferay.portal.PortalException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.impl.CompanyImpl;
31  import com.liferay.portal.model.impl.ResourceImpl;
32  import com.liferay.portal.service.ResourceLocalServiceUtil;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portlet.messageboards.model.MBMessage;
35  import com.liferay.portlet.messageboards.model.MBThread;
36  import com.liferay.portlet.messageboards.service.base.MBThreadLocalServiceBaseImpl;
37  import com.liferay.portlet.messageboards.service.persistence.MBMessageFlagFinder;
38  import com.liferay.portlet.messageboards.service.persistence.MBMessageFlagUtil;
39  import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
40  import com.liferay.portlet.messageboards.service.persistence.MBThreadFinder;
41  import com.liferay.portlet.messageboards.service.persistence.MBThreadUtil;
42  import com.liferay.portlet.messageboards.util.Indexer;
43  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
44  
45  import java.io.IOException;
46  
47  import java.rmi.RemoteException;
48  
49  import java.util.Iterator;
50  import java.util.List;
51  
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.apache.lucene.queryParser.ParseException;
55  
56  /**
57   * <a href="MBThreadLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class MBThreadLocalServiceImpl extends MBThreadLocalServiceBaseImpl {
63  
64      public void deleteThread(long threadId)
65          throws PortalException, SystemException {
66  
67          MBThread thread = MBThreadUtil.findByPrimaryKey(threadId);
68  
69          deleteThread(thread);
70      }
71  
72      public void deleteThread(MBThread thread)
73          throws PortalException, SystemException {
74  
75          MBMessage rootMessage = MBMessageUtil.findByPrimaryKey(
76              thread.getRootMessageId());
77  
78          // Lucene
79  
80          try {
81              Indexer.deleteMessages(
82                  rootMessage.getCompanyId(), thread.getThreadId());
83          }
84          catch (IOException ioe) {
85              _log.error("Deleting index " + thread.getThreadId(), ioe);
86          }
87          catch (ParseException pe) {
88              _log.error("Deleting index " + thread.getThreadId(), pe);
89          }
90  
91          // File attachments
92  
93          long companyId = rootMessage.getCompanyId();
94          String portletId = CompanyImpl.SYSTEM_STRING;
95          long repositoryId = CompanyImpl.SYSTEM;
96          String dirName = thread.getAttachmentsDir();
97  
98          try {
99              DLServiceUtil.deleteDirectory(
100                 companyId, portletId, repositoryId, dirName);
101         }
102         catch (NoSuchDirectoryException nsde) {
103         }
104         catch (RemoteException re) {
105             throw new SystemException(re);
106         }
107 
108         // Messages
109 
110         Iterator itr = MBMessageUtil.findByThreadId(
111             thread.getThreadId()).iterator();
112 
113         while (itr.hasNext()) {
114             MBMessage message = (MBMessage)itr.next();
115 
116             // Tags
117 
118             TagsAssetLocalServiceUtil.deleteAsset(
119                 MBMessage.class.getName(), message.getMessageId());
120 
121             // Message flags
122 
123             MBMessageFlagUtil.removeByMessageId(message.getMessageId());
124 
125             // Resources
126 
127             if (!message.isDiscussion()) {
128                 ResourceLocalServiceUtil.deleteResource(
129                     message.getCompanyId(), MBMessage.class.getName(),
130                     ResourceImpl.SCOPE_INDIVIDUAL, message.getMessageId());
131             }
132 
133             // Message
134 
135             MBMessageUtil.remove(message.getMessageId());
136         }
137 
138         // Thread
139 
140         MBThreadUtil.remove(thread.getThreadId());
141     }
142 
143     public void deleteThreads(long categoryId)
144         throws PortalException, SystemException {
145 
146         Iterator itr = MBThreadUtil.findByCategoryId(categoryId).iterator();
147 
148         while (itr.hasNext()) {
149             MBThread thread = (MBThread)itr.next();
150 
151             deleteThread(thread);
152         }
153     }
154 
155     public int getCategoriesThreadsCount(List categoryIds)
156         throws SystemException {
157 
158         return MBThreadFinder.countByCategoryIds(categoryIds);
159     }
160 
161     public List getGroupThreads(long groupId, int begin, int end)
162         throws SystemException {
163 
164         return MBThreadFinder.findByGroupId(groupId, begin, end);
165     }
166 
167     public List getGroupThreads(long groupId, long userId, int begin, int end)
168         throws SystemException {
169 
170         return getGroupThreads(groupId, userId, false, begin, end);
171     }
172 
173     public List getGroupThreads(
174             long groupId, long userId, boolean subscribed, int begin, int end)
175         throws SystemException {
176 
177         if (userId <= 0) {
178             return MBThreadFinder.findByGroupId(groupId, begin, end);
179         }
180         else {
181             if (subscribed) {
182                 return MBThreadFinder.findByS_G_U(groupId, userId, begin, end);
183             }
184             else {
185                 return MBThreadFinder.findByG_U(groupId, userId, begin, end);
186             }
187         }
188     }
189 
190     public int getGroupThreadsCount(long groupId) throws SystemException {
191         return MBThreadFinder.countByGroupId(groupId);
192     }
193 
194     public int getGroupThreadsCount(long groupId, long userId)
195         throws SystemException {
196 
197         return getGroupThreadsCount(groupId, userId, false);
198     }
199 
200     public int getGroupThreadsCount(
201             long groupId, long userId, boolean subscribed)
202         throws SystemException {
203 
204         if (userId <= 0) {
205             return MBThreadFinder.countByGroupId(groupId);
206         }
207         else {
208             if (subscribed) {
209                 return MBThreadFinder.countByS_G_U(groupId, userId);
210             }
211             else {
212                 return MBThreadFinder.countByG_U(groupId, userId);
213             }
214         }
215     }
216 
217     public MBThread getThread(long threadId)
218         throws PortalException, SystemException {
219 
220         return MBThreadUtil.findByPrimaryKey(threadId);
221     }
222 
223     public List getThreads(long categoryId, int begin, int end)
224         throws SystemException {
225 
226         return MBThreadUtil.findByCategoryId(categoryId, begin, end);
227     }
228 
229     public int getThreadsCount(long categoryId) throws SystemException {
230         return MBThreadUtil.countByCategoryId(categoryId);
231     }
232 
233     public boolean hasReadThread(long userId, long threadId)
234         throws PortalException, SystemException {
235 
236         User user = UserLocalServiceUtil.getUserById(userId);
237 
238         if (user.isDefaultUser()) {
239             return true;
240         }
241 
242         int total = MBMessageUtil.countByThreadId(threadId);
243         int read = MBMessageFlagFinder.countByU_T(userId, threadId);
244 
245         if (total != read) {
246             return false;
247         }
248         else {
249             return true;
250         }
251     }
252 
253     public MBThread updateThread(long threadId, int viewCount)
254         throws PortalException, SystemException {
255 
256         MBThread thread = MBThreadUtil.findByPrimaryKey(threadId);
257 
258         thread.setViewCount(viewCount);
259 
260         MBThreadUtil.update(thread);
261 
262         return thread;
263     }
264 
265     private static Log _log = LogFactory.getLog(MBThreadLocalServiceImpl.class);
266 
267 }