1   /**
2    * Copyright (c) 2000-2008 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.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.CompanyConstants;
29  import com.liferay.portal.model.ResourceConstants;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portlet.messageboards.model.MBCategory;
33  import com.liferay.portlet.messageboards.model.MBMessage;
34  import com.liferay.portlet.messageboards.model.MBThread;
35  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
36  import com.liferay.portlet.messageboards.service.base.MBThreadLocalServiceBaseImpl;
37  import com.liferay.portlet.messageboards.util.Indexer;
38  
39  import java.io.IOException;
40  
41  import java.rmi.RemoteException;
42  
43  import java.util.List;
44  
45  import javax.portlet.PortletPreferences;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  import org.apache.lucene.queryParser.ParseException;
50  
51  /**
52   * <a href="MBThreadLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class MBThreadLocalServiceImpl extends MBThreadLocalServiceBaseImpl {
58  
59      public void deleteThread(long threadId)
60          throws PortalException, SystemException {
61  
62          MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
63  
64          deleteThread(thread);
65      }
66  
67      public void deleteThread(MBThread thread)
68          throws PortalException, SystemException {
69  
70          MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
71              thread.getRootMessageId());
72  
73          // Lucene
74  
75          try {
76              Indexer.deleteMessages(
77                  rootMessage.getCompanyId(), thread.getThreadId());
78          }
79          catch (IOException ioe) {
80              _log.error("Deleting index " + thread.getThreadId(), ioe);
81          }
82          catch (ParseException pe) {
83              _log.error("Deleting index " + thread.getThreadId(), pe);
84          }
85  
86          // File attachments
87  
88          long companyId = rootMessage.getCompanyId();
89          String portletId = CompanyConstants.SYSTEM_STRING;
90          long repositoryId = CompanyConstants.SYSTEM;
91          String dirName = thread.getAttachmentsDir();
92  
93          try {
94              dlService.deleteDirectory(
95                  companyId, portletId, repositoryId, dirName);
96          }
97          catch (NoSuchDirectoryException nsde) {
98          }
99          catch (RemoteException re) {
100             throw new SystemException(re);
101         }
102 
103         // Messages
104 
105         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
106             thread.getThreadId());
107 
108         for (MBMessage message : messages) {
109 
110             // Tags
111 
112             tagsAssetLocalService.deleteAsset(
113                 MBMessage.class.getName(), message.getMessageId());
114 
115             // Social
116 
117             socialActivityLocalService.deleteActivities(
118                 MBMessage.class.getName(), message.getMessageId());
119 
120             // Message flags
121 
122             mbMessageFlagPersistence.removeByMessageId(message.getMessageId());
123 
124             // Resources
125 
126             if (!message.isDiscussion()) {
127                 resourceLocalService.deleteResource(
128                     message.getCompanyId(), MBMessage.class.getName(),
129                     ResourceConstants.SCOPE_INDIVIDUAL, message.getMessageId());
130             }
131 
132             // Message
133 
134             mbMessagePersistence.remove(message.getMessageId());
135         }
136 
137         // Thread
138 
139         mbThreadPersistence.remove(thread.getThreadId());
140     }
141 
142     public void deleteThreads(long categoryId)
143         throws PortalException, SystemException {
144 
145         List<MBThread> threads = mbThreadPersistence.findByCategoryId(
146             categoryId);
147 
148         for (MBThread thread : threads) {
149             deleteThread(thread);
150         }
151     }
152 
153     public int getCategoriesThreadsCount(List<Long> categoryIds)
154         throws SystemException {
155 
156         return mbThreadFinder.countByCategoryIds(categoryIds);
157     }
158 
159     public List<MBThread> getGroupThreads(long groupId, int begin, int end)
160         throws SystemException {
161 
162         return mbThreadFinder.findByGroupId(groupId, begin, end);
163     }
164 
165     public List<MBThread> getGroupThreads(
166             long groupId, long userId, int begin, int end)
167         throws SystemException {
168 
169         return getGroupThreads(groupId, userId, false, begin, end);
170     }
171 
172     public List<MBThread> getGroupThreads(
173             long groupId, long userId, boolean subscribed, int begin, int end)
174         throws SystemException {
175 
176         if (userId <= 0) {
177             return mbThreadFinder.findByGroupId(groupId, begin, end);
178         }
179         else {
180             if (subscribed) {
181                 return mbThreadFinder.findByS_G_U(groupId, userId, begin, end);
182             }
183             else {
184                 return mbThreadFinder.findByG_U(groupId, userId, begin, end);
185             }
186         }
187     }
188 
189     public int getGroupThreadsCount(long groupId) throws SystemException {
190         return mbThreadFinder.countByGroupId(groupId);
191     }
192 
193     public int getGroupThreadsCount(long groupId, long userId)
194         throws SystemException {
195 
196         return getGroupThreadsCount(groupId, userId, false);
197     }
198 
199     public int getGroupThreadsCount(
200             long groupId, long userId, boolean subscribed)
201         throws SystemException {
202 
203         if (userId <= 0) {
204             return mbThreadFinder.countByGroupId(groupId);
205         }
206         else {
207             if (subscribed) {
208                 return mbThreadFinder.countByS_G_U(groupId, userId);
209             }
210             else {
211                 return mbThreadFinder.countByG_U(groupId, userId);
212             }
213         }
214     }
215 
216     public MBThread getThread(long threadId)
217         throws PortalException, SystemException {
218 
219         return mbThreadPersistence.findByPrimaryKey(threadId);
220     }
221 
222     public List<MBThread> getThreads(long categoryId, int begin, int end)
223         throws SystemException {
224 
225         return mbThreadPersistence.findByCategoryId(categoryId, begin, end);
226     }
227 
228     public int getThreadsCount(long categoryId) throws SystemException {
229         return mbThreadPersistence.countByCategoryId(categoryId);
230     }
231 
232     public boolean hasReadThread(long userId, long threadId)
233         throws PortalException, SystemException {
234 
235         User user = userPersistence.findByPrimaryKey(userId);
236 
237         if (user.isDefaultUser()) {
238             return true;
239         }
240 
241         int total = mbMessagePersistence.countByThreadId(threadId);
242         int read = mbMessageFlagFinder.countByU_T(userId, threadId);
243 
244         if (total != read) {
245             return false;
246         }
247         else {
248             return true;
249         }
250     }
251 
252     public MBThread moveThread(long categoryId, long threadId)
253         throws PortalException, SystemException {
254 
255         MBThread thread = mbThreadPersistence.findByPrimaryKey(
256             threadId);
257 
258         long oldCategoryId = thread.getCategoryId();
259 
260         MBCategory category = mbCategoryPersistence.findByPrimaryKey(
261             categoryId);
262 
263         // Messages
264 
265         List<MBMessage> messages = mbMessagePersistence.findByC_T(
266             oldCategoryId, thread.getThreadId());
267 
268         for (MBMessage message : messages) {
269             message.setCategoryId(category.getCategoryId());
270 
271             mbMessagePersistence.update(message, false);
272 
273             // Lucene
274 
275             try {
276                 if (!category.isDiscussion()) {
277                     Indexer.updateMessage(
278                         message.getCompanyId(), category.getGroupId(),
279                         message.getUserName(), category.getCategoryId(),
280                         message.getThreadId(), message.getMessageId(),
281                         message.getSubject(), message.getBody(),
282                         message.getTagsEntries());
283                 }
284             }
285             catch (IOException ioe) {
286                 _log.error("Indexing " + message.getMessageId(), ioe);
287             }
288         }
289 
290         // Thread
291 
292         thread.setCategoryId(category.getCategoryId());
293 
294         mbThreadPersistence.update(thread, false);
295 
296         return thread;
297     }
298 
299     public MBThread splitThread(
300             long messageId, PortletPreferences prefs, ThemeDisplay themeDisplay)
301         throws PortalException, SystemException {
302 
303         MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
304 
305         long oldThreadId = message.getThreadId();
306         MBCategory category = mbCategoryPersistence.fetchByPrimaryKey(
307             message.getCategoryId());
308 
309         // Create new thread
310 
311         MBThread thread = addThread(message.getCategoryId(), message);
312 
313         // Update message
314 
315         message.setThreadId(thread.getThreadId());
316         message.setParentMessageId(0);
317 
318         mbMessagePersistence.update(message, false);
319 
320         try {
321             if (!category.isDiscussion()) {
322                 Indexer.updateMessage(
323                     message.getCompanyId(), category.getGroupId(),
324                     message.getUserName(), category.getCategoryId(),
325                     message.getThreadId(), message.getMessageId(),
326                     message.getSubject(), message.getBody(),
327                     message.getTagsEntries());
328             }
329         }
330         catch (IOException ioe) {
331             _log.error("Indexing " + message.getMessageId(), ioe);
332         }
333 
334         // Update children
335 
336         List<MBMessage> messages = mbMessagePersistence.findByT_P(
337             oldThreadId, message.getMessageId());
338 
339         int messagesMoved = 1;
340 
341         for (MBMessage curMessage : messages) {
342             curMessage.setCategoryId(message.getCategoryId());
343             curMessage.setThreadId(message.getThreadId());
344 
345             mbMessagePersistence.update(curMessage, false);
346 
347             messagesMoved++;
348 
349             try {
350                 if (!category.isDiscussion()) {
351                     Indexer.updateMessage(
352                         curMessage.getCompanyId(), category.getGroupId(),
353                         curMessage.getUserName(), category.getCategoryId(),
354                         curMessage.getThreadId(), curMessage.getMessageId(),
355                         curMessage.getSubject(), curMessage.getBody(),
356                         curMessage.getTagsEntries());
357                 }
358             }
359             catch (IOException ioe) {
360                 _log.error("Indexing " + curMessage.getMessageId(), ioe);
361             }
362         }
363 
364         // Update old thread
365 
366         MBThread oldThread = mbThreadPersistence.findByPrimaryKey(oldThreadId);
367 
368         oldThread.setMessageCount(oldThread.getMessageCount() - messagesMoved);
369 
370         mbThreadPersistence.update(oldThread, false);
371 
372         return thread;
373 
374     }
375 
376     public MBThread updateThread(long threadId, int viewCount)
377         throws PortalException, SystemException {
378 
379         MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
380 
381         thread.setViewCount(viewCount);
382 
383         mbThreadPersistence.update(thread, false);
384 
385         return thread;
386     }
387 
388     protected MBThread addThread(long categoryId, MBMessage message)
389         throws SystemException, PortalException {
390 
391         long threadId = counterLocalService.increment();
392 
393         MBThread thread = mbThreadPersistence.create(threadId);
394 
395         thread.setCategoryId(categoryId);
396         thread.setRootMessageId(message.getMessageId());
397 
398         thread.setMessageCount(thread.getMessageCount() + 1);
399 
400         if (message.isAnonymous()) {
401             thread.setLastPostByUserId(0);
402         }
403         else {
404             thread.setLastPostByUserId(message.getUserId());
405         }
406 
407         thread.setLastPostDate(message.getCreateDate());
408 
409         if (message.getPriority() != MBThreadImpl.PRIORITY_NOT_GIVEN) {
410             thread.setPriority(message.getPriority());
411         }
412 
413         mbThreadPersistence.update(thread, false);
414 
415         return thread;
416     }
417 
418     private static Log _log = LogFactory.getLog(MBThreadLocalServiceImpl.class);
419 
420 }