001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.service.impl;
022:
023: import com.liferay.documentlibrary.NoSuchDirectoryException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.model.impl.CompanyImpl;
028: import com.liferay.portal.model.impl.ResourceImpl;
029: import com.liferay.portal.theme.ThemeDisplay;
030: import com.liferay.portlet.messageboards.model.MBCategory;
031: import com.liferay.portlet.messageboards.model.MBMessage;
032: import com.liferay.portlet.messageboards.model.MBThread;
033: import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
034: import com.liferay.portlet.messageboards.service.base.MBThreadLocalServiceBaseImpl;
035: import com.liferay.portlet.messageboards.util.Indexer;
036:
037: import java.io.IOException;
038:
039: import java.rmi.RemoteException;
040:
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import javax.portlet.PortletPreferences;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import org.apache.lucene.queryParser.ParseException;
049:
050: /**
051: * <a href="MBThreadLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class MBThreadLocalServiceImpl extends
057: MBThreadLocalServiceBaseImpl {
058:
059: public void deleteThread(long threadId) throws PortalException,
060: SystemException {
061:
062: MBThread thread = mbThreadPersistence
063: .findByPrimaryKey(threadId);
064:
065: deleteThread(thread);
066: }
067:
068: public void deleteThread(MBThread thread) throws PortalException,
069: SystemException {
070:
071: MBMessage rootMessage = mbMessagePersistence
072: .findByPrimaryKey(thread.getRootMessageId());
073:
074: // Lucene
075:
076: try {
077: Indexer.deleteMessages(rootMessage.getCompanyId(), thread
078: .getThreadId());
079: } catch (IOException ioe) {
080: _log.error("Deleting index " + thread.getThreadId(), ioe);
081: } catch (ParseException pe) {
082: _log.error("Deleting index " + thread.getThreadId(), pe);
083: }
084:
085: // File attachments
086:
087: long companyId = rootMessage.getCompanyId();
088: String portletId = CompanyImpl.SYSTEM_STRING;
089: long repositoryId = CompanyImpl.SYSTEM;
090: String dirName = thread.getAttachmentsDir();
091:
092: try {
093: dlService.deleteDirectory(companyId, portletId,
094: repositoryId, dirName);
095: } catch (NoSuchDirectoryException nsde) {
096: } catch (RemoteException re) {
097: throw new SystemException(re);
098: }
099:
100: // Messages
101:
102: Iterator itr = mbMessagePersistence.findByThreadId(
103: thread.getThreadId()).iterator();
104:
105: while (itr.hasNext()) {
106: MBMessage message = (MBMessage) itr.next();
107:
108: // Activity trackers
109:
110: activityTrackerLocalService.deleteActivityTrackers(
111: MBMessage.class.getName(), message.getMessageId());
112:
113: // Tags
114:
115: tagsAssetLocalService.deleteAsset(
116: MBMessage.class.getName(), message.getMessageId());
117:
118: // Message flags
119:
120: mbMessageFlagPersistence.removeByMessageId(message
121: .getMessageId());
122:
123: // Resources
124:
125: if (!message.isDiscussion()) {
126: resourceLocalService.deleteResource(message
127: .getCompanyId(), MBMessage.class.getName(),
128: ResourceImpl.SCOPE_INDIVIDUAL, message
129: .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) throws PortalException,
143: SystemException {
144:
145: Iterator itr = mbThreadPersistence.findByCategoryId(categoryId)
146: .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,
168: int end) throws SystemException {
169:
170: return getGroupThreads(groupId, userId, false, begin, end);
171: }
172:
173: public List getGroupThreads(long groupId, long userId,
174: boolean subscribed, int begin, int end)
175: throws SystemException {
176:
177: if (userId <= 0) {
178: return mbThreadFinder.findByGroupId(groupId, begin, end);
179: } else {
180: if (subscribed) {
181: return mbThreadFinder.findByS_G_U(groupId, userId,
182: begin, end);
183: } else {
184: return mbThreadFinder.findByG_U(groupId, userId, begin,
185: end);
186: }
187: }
188: }
189:
190: public int getGroupThreadsCount(long groupId)
191: throws SystemException {
192: return mbThreadFinder.countByGroupId(groupId);
193: }
194:
195: public int getGroupThreadsCount(long groupId, long userId)
196: throws SystemException {
197:
198: return getGroupThreadsCount(groupId, userId, false);
199: }
200:
201: public int getGroupThreadsCount(long groupId, long userId,
202: boolean subscribed) throws SystemException {
203:
204: if (userId <= 0) {
205: return mbThreadFinder.countByGroupId(groupId);
206: } else {
207: if (subscribed) {
208: return mbThreadFinder.countByS_G_U(groupId, userId);
209: } else {
210: return mbThreadFinder.countByG_U(groupId, userId);
211: }
212: }
213: }
214:
215: public MBThread getThread(long threadId) throws PortalException,
216: SystemException {
217:
218: return mbThreadPersistence.findByPrimaryKey(threadId);
219: }
220:
221: public List getThreads(long categoryId, int begin, int end)
222: throws SystemException {
223:
224: return mbThreadPersistence.findByCategoryId(categoryId, begin,
225: 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: } else {
247: return true;
248: }
249: }
250:
251: public MBThread moveThread(long categoryId, long threadId)
252: throws PortalException, SystemException {
253:
254: MBThread thread = mbThreadPersistence
255: .findByPrimaryKey(threadId);
256:
257: long oldCategoryId = thread.getCategoryId();
258:
259: MBCategory category = mbCategoryPersistence
260: .findByPrimaryKey(categoryId);
261:
262: // Messages
263:
264: Iterator itr = mbMessagePersistence.findByC_T(oldCategoryId,
265: thread.getThreadId()).iterator();
266:
267: while (itr.hasNext()) {
268: MBMessage message = (MBMessage) itr.next();
269:
270: message.setCategoryId(category.getCategoryId());
271:
272: mbMessagePersistence.update(message);
273:
274: // Lucene
275:
276: try {
277: if (!category.isDiscussion()) {
278: Indexer.updateMessage(message.getCompanyId(),
279: category.getGroupId(), message
280: .getUserName(), category
281: .getCategoryId(), message
282: .getThreadId(), message
283: .getMessageId(), message
284: .getSubject(), message.getBody(),
285: message.getTagsEntries());
286: }
287: } catch (IOException ioe) {
288: _log.error("Indexing " + message.getMessageId(), ioe);
289: }
290: }
291:
292: // Thread
293:
294: thread.setCategoryId(category.getCategoryId());
295:
296: mbThreadPersistence.update(thread);
297:
298: return thread;
299: }
300:
301: public MBThread splitThread(long messageId,
302: PortletPreferences prefs, ThemeDisplay themeDisplay)
303: throws PortalException, SystemException {
304:
305: MBMessage message = mbMessagePersistence
306: .findByPrimaryKey(messageId);
307:
308: long oldThreadId = message.getThreadId();
309: MBCategory category = mbCategoryPersistence
310: .fetchByPrimaryKey(message.getCategoryId());
311:
312: // Create new thread
313:
314: MBThread thread = addThread(message.getCategoryId(), message);
315:
316: // Update message
317:
318: message.setThreadId(thread.getThreadId());
319: message.setParentMessageId(0);
320:
321: mbMessagePersistence.update(message);
322:
323: try {
324: if (!category.isDiscussion()) {
325: Indexer.updateMessage(message.getCompanyId(), category
326: .getGroupId(), message.getUserName(), category
327: .getCategoryId(), message.getThreadId(),
328: message.getMessageId(), message.getSubject(),
329: message.getBody(), message.getTagsEntries());
330: }
331: } catch (IOException ioe) {
332: _log.error("Indexing " + message.getMessageId(), ioe);
333: }
334:
335: // Update children
336:
337: Iterator itr = mbMessagePersistence.findByT_P(oldThreadId,
338: message.getMessageId()).iterator();
339:
340: int messagesMoved = 1;
341:
342: while (itr.hasNext()) {
343: MBMessage curMessage = (MBMessage) itr.next();
344:
345: curMessage.setCategoryId(message.getCategoryId());
346: curMessage.setThreadId(message.getThreadId());
347:
348: mbMessagePersistence.update(curMessage);
349:
350: messagesMoved++;
351:
352: try {
353: if (!category.isDiscussion()) {
354: Indexer.updateMessage(curMessage.getCompanyId(),
355: category.getGroupId(), curMessage
356: .getUserName(), category
357: .getCategoryId(), curMessage
358: .getThreadId(), curMessage
359: .getMessageId(), curMessage
360: .getSubject(),
361: curMessage.getBody(), curMessage
362: .getTagsEntries());
363: }
364: } catch (IOException ioe) {
365: _log
366: .error("Indexing " + curMessage.getMessageId(),
367: ioe);
368: }
369: }
370:
371: // Update old thread
372:
373: MBThread oldThread = mbThreadPersistence
374: .findByPrimaryKey(oldThreadId);
375:
376: oldThread.setMessageCount(oldThread.getMessageCount()
377: - messagesMoved);
378:
379: mbThreadPersistence.update(oldThread);
380:
381: return thread;
382:
383: }
384:
385: public MBThread updateThread(long threadId, int viewCount)
386: throws PortalException, SystemException {
387:
388: MBThread thread = mbThreadPersistence
389: .findByPrimaryKey(threadId);
390:
391: thread.setViewCount(viewCount);
392:
393: mbThreadPersistence.update(thread);
394:
395: return thread;
396: }
397:
398: protected MBThread addThread(long categoryId, MBMessage message)
399: throws SystemException, PortalException {
400:
401: long threadId = counterLocalService.increment();
402:
403: MBThread thread = mbThreadPersistence.create(threadId);
404:
405: thread.setCategoryId(categoryId);
406: thread.setRootMessageId(message.getMessageId());
407:
408: thread.setMessageCount(thread.getMessageCount() + 1);
409:
410: if (message.isAnonymous()) {
411: thread.setLastPostByUserId(0);
412: } else {
413: thread.setLastPostByUserId(message.getUserId());
414: }
415:
416: thread.setLastPostDate(message.getCreateDate());
417:
418: if (message.getPriority() != MBThreadImpl.PRIORITY_NOT_GIVEN) {
419: thread.setPriority(message.getPriority());
420: }
421:
422: mbThreadPersistence.update(thread);
423:
424: return thread;
425: }
426:
427: private static Log _log = LogFactory
428: .getLog(MBThreadLocalServiceImpl.class);
429:
430: }
|