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