0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.portlet.messageboards.service.impl;
0022:
0023: import com.liferay.documentlibrary.DuplicateFileException;
0024: import com.liferay.documentlibrary.NoSuchDirectoryException;
0025: import com.liferay.portal.PortalException;
0026: import com.liferay.portal.SystemException;
0027: import com.liferay.portal.kernel.mail.MailMessage;
0028: import com.liferay.portal.kernel.util.ContentTypes;
0029: import com.liferay.portal.kernel.util.ObjectValuePair;
0030: import com.liferay.portal.kernel.util.OrderByComparator;
0031: import com.liferay.portal.kernel.util.StringPool;
0032: import com.liferay.portal.kernel.util.StringUtil;
0033: import com.liferay.portal.kernel.util.Validator;
0034: import com.liferay.portal.model.Company;
0035: import com.liferay.portal.model.Group;
0036: import com.liferay.portal.model.ModelHintsUtil;
0037: import com.liferay.portal.model.User;
0038: import com.liferay.portal.model.impl.CompanyImpl;
0039: import com.liferay.portal.model.impl.GroupImpl;
0040: import com.liferay.portal.model.impl.ResourceImpl;
0041: import com.liferay.portal.security.auth.PrincipalException;
0042: import com.liferay.portal.theme.ThemeDisplay;
0043: import com.liferay.portal.util.PortalUtil;
0044: import com.liferay.portal.util.PortletKeys;
0045: import com.liferay.portal.util.PrefsPropsUtil;
0046: import com.liferay.portal.util.PropsUtil;
0047: import com.liferay.portal.util.PropsValues;
0048: import com.liferay.portlet.blogs.model.BlogsEntry;
0049: import com.liferay.portlet.messageboards.MessageBodyException;
0050: import com.liferay.portlet.messageboards.MessageSubjectException;
0051: import com.liferay.portlet.messageboards.NoSuchDiscussionException;
0052: import com.liferay.portlet.messageboards.NoSuchThreadException;
0053: import com.liferay.portlet.messageboards.RequiredMessageException;
0054: import com.liferay.portlet.messageboards.model.MBCategory;
0055: import com.liferay.portlet.messageboards.model.MBDiscussion;
0056: import com.liferay.portlet.messageboards.model.MBMessage;
0057: import com.liferay.portlet.messageboards.model.MBMessageDisplay;
0058: import com.liferay.portlet.messageboards.model.MBStatsUser;
0059: import com.liferay.portlet.messageboards.model.MBThread;
0060: import com.liferay.portlet.messageboards.model.MBTreeWalker;
0061: import com.liferay.portlet.messageboards.model.impl.MBMessageDisplayImpl;
0062: import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
0063: import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
0064: import com.liferay.portlet.messageboards.model.impl.MBTreeWalkerImpl;
0065: import com.liferay.portlet.messageboards.service.base.MBMessageLocalServiceBaseImpl;
0066: import com.liferay.portlet.messageboards.service.jms.MBMessageProducer;
0067: import com.liferay.portlet.messageboards.util.Indexer;
0068: import com.liferay.portlet.messageboards.util.MBUtil;
0069: import com.liferay.portlet.messageboards.util.comparator.MessageThreadComparator;
0070: import com.liferay.portlet.messageboards.util.comparator.ThreadLastPostDateComparator;
0071:
0072: import java.io.IOException;
0073:
0074: import java.rmi.RemoteException;
0075:
0076: import java.util.ArrayList;
0077: import java.util.Collections;
0078: import java.util.Comparator;
0079: import java.util.Date;
0080: import java.util.Iterator;
0081: import java.util.List;
0082:
0083: import javax.mail.internet.InternetAddress;
0084:
0085: import javax.portlet.PortletPreferences;
0086:
0087: import org.apache.commons.lang.time.StopWatch;
0088: import org.apache.commons.logging.Log;
0089: import org.apache.commons.logging.LogFactory;
0090:
0091: /**
0092: * <a href="MBMessageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
0093: *
0094: * @author Brian Wing Shun Chan
0095: *
0096: */
0097: public class MBMessageLocalServiceImpl extends
0098: MBMessageLocalServiceBaseImpl {
0099:
0100: public MBMessage addDiscussionMessage(long userId, String subject,
0101: String body) throws PortalException, SystemException {
0102:
0103: long groupId = 0;
0104: String className = StringPool.BLANK;
0105: long classPK = 0;
0106: long threadId = 0;
0107: long parentMessageId = 0;
0108: ThemeDisplay themeDisplay = null;
0109:
0110: return addDiscussionMessage(userId, groupId, className,
0111: classPK, threadId, parentMessageId, subject, body,
0112: themeDisplay);
0113: }
0114:
0115: public MBMessage addDiscussionMessage(long userId, long groupId,
0116: String className, long classPK, long threadId,
0117: long parentMessageId, String subject, String body)
0118: throws PortalException, SystemException {
0119:
0120: ThemeDisplay themeDisplay = null;
0121:
0122: MBMessage message = addDiscussionMessage(userId, groupId,
0123: className, classPK, threadId, parentMessageId, subject,
0124: body, themeDisplay);
0125:
0126: if (parentMessageId == MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) {
0127: long discussionId = counterLocalService.increment();
0128:
0129: MBDiscussion discussion = mbDiscussionPersistence
0130: .create(discussionId);
0131:
0132: discussion.setClassNameId(PortalUtil
0133: .getClassNameId(className));
0134: discussion.setClassPK(classPK);
0135: discussion.setThreadId(message.getThreadId());
0136:
0137: mbDiscussionPersistence.update(discussion);
0138: }
0139:
0140: return message;
0141: }
0142:
0143: public MBMessage addDiscussionMessage(long userId, long groupId,
0144: String className, long classPK, long threadId,
0145: long parentMessageId, String subject, String body,
0146: ThemeDisplay themeDisplay) throws PortalException,
0147: SystemException {
0148:
0149: long categoryId = CompanyImpl.SYSTEM;
0150:
0151: if (Validator.isNull(subject)) {
0152: subject = "N/A";
0153: }
0154:
0155: List files = new ArrayList();
0156: boolean anonymous = false;
0157: double priority = 0.0;
0158: String[] tagsEntries = null;
0159: PortletPreferences prefs = null;
0160: boolean addCommunityPermissions = true;
0161: boolean addGuestPermissions = true;
0162:
0163: mbCategoryLocalService.getSystemCategory();
0164:
0165: MBMessage message = addMessage(userId, categoryId, threadId,
0166: parentMessageId, subject, body, files, anonymous,
0167: priority, tagsEntries, prefs, addCommunityPermissions,
0168: addGuestPermissions, themeDisplay);
0169:
0170: if ((className.equals(BlogsEntry.class.getName()))
0171: && (themeDisplay != null)) {
0172:
0173: try {
0174: sendBlogsCommentsEmail(userId, classPK, message,
0175: themeDisplay);
0176: } catch (Exception e) {
0177: _log.error(e, e);
0178: }
0179: }
0180:
0181: return message;
0182: }
0183:
0184: public MBMessage addMessage(long userId, long categoryId,
0185: String subject, String body, List files, boolean anonymous,
0186: double priority, String[] tagsEntries,
0187: PortletPreferences prefs, boolean addCommunityPermissions,
0188: boolean addGuestPermissions, ThemeDisplay themeDisplay)
0189: throws PortalException, SystemException {
0190:
0191: return addMessage(userId, categoryId, subject, body, files,
0192: anonymous, priority, tagsEntries, prefs, Boolean
0193: .valueOf(addCommunityPermissions), Boolean
0194: .valueOf(addGuestPermissions), null, null,
0195: themeDisplay);
0196: }
0197:
0198: public MBMessage addMessage(long userId, long categoryId,
0199: String subject, String body, List files, boolean anonymous,
0200: double priority, String[] tagsEntries,
0201: PortletPreferences prefs, String[] communityPermissions,
0202: String[] guestPermissions, ThemeDisplay themeDisplay)
0203: throws PortalException, SystemException {
0204:
0205: return addMessage(userId, categoryId, subject, body, files,
0206: anonymous, priority, tagsEntries, prefs, null, null,
0207: communityPermissions, guestPermissions, themeDisplay);
0208: }
0209:
0210: public MBMessage addMessage(long userId, long categoryId,
0211: String subject, String body, List files, boolean anonymous,
0212: double priority, String[] tagsEntries,
0213: PortletPreferences prefs, Boolean addCommunityPermissions,
0214: Boolean addGuestPermissions, String[] communityPermissions,
0215: String[] guestPermissions, ThemeDisplay themeDisplay)
0216: throws PortalException, SystemException {
0217:
0218: long threadId = 0;
0219: long parentMessageId = 0;
0220:
0221: return addMessage(null, userId, categoryId, threadId,
0222: parentMessageId, subject, body, files, anonymous,
0223: priority, tagsEntries, prefs, addCommunityPermissions,
0224: addGuestPermissions, communityPermissions,
0225: guestPermissions, themeDisplay);
0226: }
0227:
0228: public MBMessage addMessage(long userId, long categoryId,
0229: long threadId, long parentMessageId, String subject,
0230: String body, List files, boolean anonymous,
0231: double priority, String[] tagsEntries,
0232: PortletPreferences prefs, boolean addCommunityPermissions,
0233: boolean addGuestPermissions, ThemeDisplay themeDisplay)
0234: throws PortalException, SystemException {
0235:
0236: return addMessage(null, userId, categoryId, threadId,
0237: parentMessageId, subject, body, files, anonymous,
0238: priority, tagsEntries, prefs, Boolean
0239: .valueOf(addCommunityPermissions), Boolean
0240: .valueOf(addGuestPermissions), null, null,
0241: themeDisplay);
0242: }
0243:
0244: public MBMessage addMessage(long userId, long categoryId,
0245: long threadId, long parentMessageId, String subject,
0246: String body, List files, boolean anonymous,
0247: double priority, String[] tagsEntries,
0248: PortletPreferences prefs, String[] communityPermissions,
0249: String[] guestPermissions, ThemeDisplay themeDisplay)
0250: throws PortalException, SystemException {
0251:
0252: return addMessage(null, userId, categoryId, threadId,
0253: parentMessageId, subject, body, files, anonymous,
0254: priority, tagsEntries, prefs, null, null,
0255: communityPermissions, guestPermissions, themeDisplay);
0256: }
0257:
0258: public MBMessage addMessage(String uuid, long userId,
0259: long categoryId, long threadId, long parentMessageId,
0260: String subject, String body, List files, boolean anonymous,
0261: double priority, String[] tagsEntries,
0262: PortletPreferences prefs, boolean addCommunityPermissions,
0263: boolean addGuestPermissions, ThemeDisplay themeDisplay)
0264: throws PortalException, SystemException {
0265:
0266: return addMessage(uuid, userId, categoryId, threadId,
0267: parentMessageId, subject, body, files, anonymous,
0268: priority, tagsEntries, prefs, Boolean
0269: .valueOf(addCommunityPermissions), Boolean
0270: .valueOf(addGuestPermissions), null, null,
0271: themeDisplay);
0272: }
0273:
0274: public MBMessage addMessage(String uuid, long userId,
0275: long categoryId, long threadId, long parentMessageId,
0276: String subject, String body, List files, boolean anonymous,
0277: double priority, String[] tagsEntries,
0278: PortletPreferences prefs, Boolean addCommunityPermissions,
0279: Boolean addGuestPermissions, String[] communityPermissions,
0280: String[] guestPermissions, ThemeDisplay themeDisplay)
0281: throws PortalException, SystemException {
0282:
0283: StopWatch stopWatch = null;
0284:
0285: if (_log.isDebugEnabled()) {
0286: stopWatch = new StopWatch();
0287:
0288: stopWatch.start();
0289: }
0290:
0291: // Message
0292:
0293: User user = userPersistence.findByPrimaryKey(userId);
0294: MBCategory category = mbCategoryPersistence
0295: .findByPrimaryKey(categoryId);
0296: subject = ModelHintsUtil.trimString(MBMessage.class.getName(),
0297: "subject", subject);
0298:
0299: if (prefs != null) {
0300: if (!MBUtil.isAllowAnonymousPosting(prefs)) {
0301: if (anonymous || user.isDefaultUser()) {
0302: throw new PrincipalException();
0303: }
0304: }
0305: }
0306:
0307: if (user.isDefaultUser()) {
0308: anonymous = true;
0309: }
0310:
0311: Date now = new Date();
0312:
0313: validate(subject, body);
0314:
0315: long messageId = counterLocalService.increment();
0316:
0317: logAddMessage(messageId, stopWatch, 1);
0318:
0319: MBMessage message = mbMessagePersistence.create(messageId);
0320:
0321: message.setUuid(uuid);
0322: message.setCompanyId(user.getCompanyId());
0323: message.setUserId(user.getUserId());
0324: message.setUserName(user.getFullName());
0325: message.setCreateDate(now);
0326: message.setModifiedDate(now);
0327:
0328: // Thread
0329:
0330: MBMessage parentMessage = mbMessagePersistence
0331: .fetchByPrimaryKey(parentMessageId);
0332:
0333: if (parentMessage == null) {
0334: parentMessageId = MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID;
0335: }
0336:
0337: MBThread thread = null;
0338:
0339: if (threadId > 0) {
0340: thread = mbThreadPersistence.fetchByPrimaryKey(threadId);
0341: }
0342:
0343: if ((thread == null)
0344: || (parentMessageId == MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID)) {
0345:
0346: threadId = counterLocalService.increment();
0347:
0348: thread = mbThreadPersistence.create(threadId);
0349:
0350: thread.setCategoryId(categoryId);
0351: thread.setRootMessageId(messageId);
0352: }
0353:
0354: thread.setMessageCount(thread.getMessageCount() + 1);
0355:
0356: if (anonymous) {
0357: thread.setLastPostByUserId(0);
0358: } else {
0359: thread.setLastPostByUserId(userId);
0360: }
0361:
0362: thread.setLastPostDate(now);
0363:
0364: if (priority != MBThreadImpl.PRIORITY_NOT_GIVEN) {
0365: thread.setPriority(priority);
0366: }
0367:
0368: logAddMessage(messageId, stopWatch, 2);
0369:
0370: // Message
0371:
0372: message.setCategoryId(categoryId);
0373: message.setThreadId(threadId);
0374: message.setParentMessageId(parentMessageId);
0375: message.setSubject(subject);
0376: message.setBody(body);
0377: message.setAttachments((files.size() > 0 ? true : false));
0378: message.setAnonymous(anonymous);
0379:
0380: // File attachments
0381:
0382: if (files.size() > 0) {
0383: long companyId = message.getCompanyId();
0384: String portletId = CompanyImpl.SYSTEM_STRING;
0385: long groupId = GroupImpl.DEFAULT_PARENT_GROUP_ID;
0386: long repositoryId = CompanyImpl.SYSTEM;
0387: String dirName = message.getAttachmentsDir();
0388:
0389: try {
0390: try {
0391: dlService.deleteDirectory(companyId, portletId,
0392: repositoryId, dirName);
0393: } catch (NoSuchDirectoryException nsde) {
0394: }
0395:
0396: dlService
0397: .addDirectory(companyId, repositoryId, dirName);
0398:
0399: for (int i = 0; i < files.size(); i++) {
0400: ObjectValuePair ovp = (ObjectValuePair) files
0401: .get(i);
0402:
0403: String fileName = (String) ovp.getKey();
0404: byte[] byteArray = (byte[]) ovp.getValue();
0405:
0406: try {
0407: dlService.addFile(companyId, portletId,
0408: groupId, repositoryId, dirName + "/"
0409: + fileName, StringPool.BLANK,
0410: new String[0], byteArray);
0411: } catch (DuplicateFileException dfe) {
0412: }
0413: }
0414: } catch (RemoteException re) {
0415: throw new SystemException(re);
0416: }
0417: }
0418:
0419: logAddMessage(messageId, stopWatch, 3);
0420:
0421: // Commit
0422:
0423: mbThreadPersistence.update(thread);
0424: mbMessagePersistence.update(message);
0425:
0426: logAddMessage(messageId, stopWatch, 4);
0427:
0428: // Resources
0429:
0430: if (!category.isDiscussion()) {
0431: if (user.isDefaultUser()) {
0432: addMessageResources(category, message, true, true);
0433: } else if ((addCommunityPermissions != null)
0434: && (addGuestPermissions != null)) {
0435:
0436: addMessageResources(category, message,
0437: addCommunityPermissions.booleanValue(),
0438: addGuestPermissions.booleanValue());
0439: } else {
0440: addMessageResources(category, message,
0441: communityPermissions, guestPermissions);
0442: }
0443: }
0444:
0445: logAddMessage(messageId, stopWatch, 5);
0446:
0447: // Statistics
0448:
0449: if (!category.isDiscussion()) {
0450: mbStatsUserLocalService.updateStatsUser(category
0451: .getGroupId(), userId);
0452: }
0453:
0454: logAddMessage(messageId, stopWatch, 6);
0455:
0456: // Subscriptions
0457:
0458: notifySubscribers(category, message, prefs, themeDisplay, false);
0459:
0460: logAddMessage(messageId, stopWatch, 7);
0461:
0462: // Category
0463:
0464: category.setLastPostDate(now);
0465:
0466: mbCategoryPersistence.update(category);
0467:
0468: logAddMessage(messageId, stopWatch, 8);
0469:
0470: // Tags
0471:
0472: updateTagsAsset(userId, message, tagsEntries);
0473:
0474: logAddMessage(messageId, stopWatch, 9);
0475:
0476: // Activity trackers
0477:
0478: if (!message.isDiscussion() && !user.isDefaultUser()) {
0479: /*long receiverUserId = 0;
0480:
0481: if (parentMessage != null) {
0482: receiverUserId = parentMessage.getUserId();
0483: }
0484:
0485: activityTrackerLocalService.addActivityTracker(
0486: userId, category.getGroupId(), MBMessage.class.getName(),
0487: messageId, MBActivityKeys.ADD, StringPool.BLANK,
0488: receiverUserId);*/
0489: }
0490:
0491: logAddMessage(messageId, stopWatch, 10);
0492:
0493: // Testing roll back
0494:
0495: /*if (true) {
0496: throw new SystemException("Testing roll back");
0497: }*/
0498:
0499: // Lucene
0500: try {
0501: if (!category.isDiscussion()) {
0502: Indexer.addMessage(message.getCompanyId(), category
0503: .getGroupId(), user.getFullName(), category
0504: .getCategoryId(), threadId, messageId, subject,
0505: body, tagsEntries);
0506: }
0507: } catch (IOException ioe) {
0508: _log.error("Indexing " + messageId, ioe);
0509: }
0510:
0511: logAddMessage(messageId, stopWatch, 11);
0512:
0513: return message;
0514: }
0515:
0516: public void addMessageResources(long categoryId, long messageId,
0517: boolean addCommunityPermissions, boolean addGuestPermissions)
0518: throws PortalException, SystemException {
0519:
0520: addMessageResources(categoryId, null, messageId,
0521: addCommunityPermissions, addGuestPermissions);
0522: }
0523:
0524: public void addMessageResources(long categoryId, String topicId,
0525: long messageId, boolean addCommunityPermissions,
0526: boolean addGuestPermissions) throws PortalException,
0527: SystemException {
0528:
0529: MBCategory category = mbCategoryPersistence
0530: .findByPrimaryKey(categoryId);
0531: MBMessage message = mbMessagePersistence
0532: .findByPrimaryKey(messageId);
0533:
0534: addMessageResources(category, message, addCommunityPermissions,
0535: addGuestPermissions);
0536: }
0537:
0538: public void addMessageResources(MBCategory category,
0539: MBMessage message, boolean addCommunityPermissions,
0540: boolean addGuestPermissions) throws PortalException,
0541: SystemException {
0542:
0543: resourceLocalService.addResources(message.getCompanyId(),
0544: category.getGroupId(), message.getUserId(),
0545: MBMessage.class.getName(), message.getMessageId(),
0546: false, addCommunityPermissions, addGuestPermissions);
0547: }
0548:
0549: public void addMessageResources(long categoryId, long messageId,
0550: String[] communityPermissions, String[] guestPermissions)
0551: throws PortalException, SystemException {
0552:
0553: addMessageResources(categoryId, null, messageId,
0554: communityPermissions, guestPermissions);
0555: }
0556:
0557: public void addMessageResources(long categoryId, String topicId,
0558: long messageId, String[] communityPermissions,
0559: String[] guestPermissions) throws PortalException,
0560: SystemException {
0561:
0562: MBCategory category = mbCategoryPersistence
0563: .findByPrimaryKey(categoryId);
0564: MBMessage message = mbMessagePersistence
0565: .findByPrimaryKey(messageId);
0566:
0567: addMessageResources(category, message, communityPermissions,
0568: guestPermissions);
0569: }
0570:
0571: public void addMessageResources(MBCategory category,
0572: MBMessage message, String[] communityPermissions,
0573: String[] guestPermissions) throws PortalException,
0574: SystemException {
0575:
0576: resourceLocalService.addModelResources(message.getCompanyId(),
0577: category.getGroupId(), message.getUserId(),
0578: MBMessage.class.getName(), message.getMessageId(),
0579: communityPermissions, guestPermissions);
0580: }
0581:
0582: public void deleteDiscussionMessage(long messageId)
0583: throws PortalException, SystemException {
0584:
0585: deleteMessage(messageId);
0586: }
0587:
0588: public void deleteDiscussionMessages(String className, long classPK)
0589: throws PortalException, SystemException {
0590:
0591: try {
0592: long classNameId = PortalUtil.getClassNameId(className);
0593:
0594: MBDiscussion discussion = mbDiscussionPersistence
0595: .findByC_C(classNameId, classPK);
0596:
0597: List messages = mbMessagePersistence.findByT_P(discussion
0598: .getThreadId(),
0599: MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID, 0, 1);
0600:
0601: if (messages.size() > 0) {
0602: MBMessage message = (MBMessage) messages.get(0);
0603:
0604: mbThreadLocalService
0605: .deleteThread(message.getThreadId());
0606: }
0607:
0608: mbDiscussionPersistence
0609: .remove(discussion.getDiscussionId());
0610: } catch (NoSuchDiscussionException nsde) {
0611: }
0612: }
0613:
0614: public void deleteMessage(long messageId) throws PortalException,
0615: SystemException {
0616:
0617: MBMessage message = mbMessagePersistence
0618: .findByPrimaryKey(messageId);
0619:
0620: deleteMessage(message);
0621: }
0622:
0623: public void deleteMessage(MBMessage message)
0624: throws PortalException, SystemException {
0625:
0626: // Lucene
0627:
0628: try {
0629: Indexer.deleteMessage(message.getCompanyId(), message
0630: .getMessageId());
0631: } catch (IOException ioe) {
0632: _log.error("Deleting index " + message.getMessageId(), ioe);
0633: }
0634:
0635: // File attachments
0636:
0637: if (message.isAttachments()) {
0638: long companyId = message.getCompanyId();
0639: String portletId = CompanyImpl.SYSTEM_STRING;
0640: long repositoryId = CompanyImpl.SYSTEM;
0641: String dirName = message.getAttachmentsDir();
0642:
0643: try {
0644: dlService.deleteDirectory(companyId, portletId,
0645: repositoryId, dirName);
0646: } catch (NoSuchDirectoryException nsde) {
0647: } catch (RemoteException re) {
0648: throw new SystemException(re);
0649: }
0650: }
0651:
0652: // Thread
0653:
0654: int count = mbMessagePersistence.countByThreadId(message
0655: .getThreadId());
0656:
0657: if (count == 1) {
0658:
0659: // File attachments
0660:
0661: long companyId = message.getCompanyId();
0662: String portletId = CompanyImpl.SYSTEM_STRING;
0663: long repositoryId = CompanyImpl.SYSTEM;
0664: String dirName = message.getThreadAttachmentsDir();
0665:
0666: try {
0667: dlService.deleteDirectory(companyId, portletId,
0668: repositoryId, dirName);
0669: } catch (NoSuchDirectoryException nsde) {
0670: } catch (RemoteException re) {
0671: throw new SystemException(re);
0672: }
0673:
0674: // Subscriptions
0675:
0676: subscriptionLocalService.deleteSubscriptions(message
0677: .getCompanyId(), MBThread.class.getName(), message
0678: .getThreadId());
0679:
0680: // Thread
0681:
0682: mbThreadPersistence.remove(message.getThreadId());
0683: } else if (count > 1) {
0684: MBThread thread = mbThreadPersistence
0685: .findByPrimaryKey(message.getThreadId());
0686:
0687: // Message is a root message
0688:
0689: if (thread.getRootMessageId() == message.getMessageId()) {
0690: List childrenMessages = mbMessagePersistence.findByT_P(
0691: message.getThreadId(), message.getMessageId());
0692:
0693: if (childrenMessages.size() > 1) {
0694: throw new RequiredMessageException(String
0695: .valueOf(message.getMessageId()));
0696: } else if (childrenMessages.size() == 1) {
0697: MBMessage childMessage = (MBMessage) childrenMessages
0698: .get(0);
0699:
0700: childMessage
0701: .setParentMessageId(MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID);
0702:
0703: mbMessagePersistence.update(childMessage);
0704:
0705: thread
0706: .setRootMessageId(childMessage
0707: .getMessageId());
0708:
0709: mbThreadPersistence.update(thread);
0710: }
0711: }
0712:
0713: // Message is a child message
0714:
0715: else {
0716: List childrenMessages = mbMessagePersistence.findByT_P(
0717: message.getThreadId(), message.getMessageId());
0718:
0719: // Message has children messages
0720:
0721: if (childrenMessages.size() > 0) {
0722: Iterator itr = childrenMessages.iterator();
0723:
0724: while (itr.hasNext()) {
0725: MBMessage childMessage = (MBMessage) itr.next();
0726:
0727: childMessage.setParentMessageId(message
0728: .getParentMessageId());
0729:
0730: mbMessagePersistence.update(childMessage);
0731: }
0732: }
0733: }
0734:
0735: // Thread
0736:
0737: thread.setMessageCount(count - 1);
0738:
0739: mbThreadPersistence.update(thread);
0740: }
0741:
0742: // Activity trackers
0743:
0744: activityTrackerLocalService.deleteActivityTrackers(
0745: MBMessage.class.getName(), message.getMessageId());
0746:
0747: // Tags
0748:
0749: tagsAssetLocalService.deleteAsset(MBMessage.class.getName(),
0750: message.getMessageId());
0751:
0752: // Message flags
0753:
0754: mbMessageFlagPersistence.removeByMessageId(message
0755: .getMessageId());
0756:
0757: // Resources
0758:
0759: if (!message.isDiscussion()) {
0760: resourceLocalService.deleteResource(message.getCompanyId(),
0761: MBMessage.class.getName(),
0762: ResourceImpl.SCOPE_INDIVIDUAL, message
0763: .getMessageId());
0764: }
0765:
0766: // Message
0767:
0768: mbMessagePersistence.remove(message.getPrimaryKey());
0769: }
0770:
0771: public List getCategoryMessages(long categoryId, int begin, int end)
0772: throws SystemException {
0773:
0774: return mbMessagePersistence.findByCategoryId(categoryId, begin,
0775: end);
0776: }
0777:
0778: public List getCategoryMessages(long categoryId, int begin,
0779: int end, OrderByComparator obc) throws SystemException {
0780:
0781: return mbMessagePersistence.findByCategoryId(categoryId, begin,
0782: end, obc);
0783: }
0784:
0785: public int getCategoryMessagesCount(long categoryId)
0786: throws SystemException {
0787:
0788: return mbMessagePersistence.countByCategoryId(categoryId);
0789: }
0790:
0791: public int getCategoriesMessagesCount(List categoryIds)
0792: throws SystemException {
0793:
0794: return mbMessageFinder.countByCategoryIds(categoryIds);
0795: }
0796:
0797: public List getCompanyMessages(long companyId, int begin, int end)
0798: throws SystemException {
0799:
0800: return mbMessagePersistence.findByCompanyId(companyId, begin,
0801: end);
0802: }
0803:
0804: public List getCompanyMessages(long companyId, int begin, int end,
0805: OrderByComparator obc) throws SystemException {
0806:
0807: return mbMessagePersistence.findByCompanyId(companyId, begin,
0808: end, obc);
0809: }
0810:
0811: public int getCompanyMessagesCount(long companyId)
0812: throws SystemException {
0813:
0814: return mbMessagePersistence.countByCompanyId(companyId);
0815: }
0816:
0817: public MBMessageDisplay getDiscussionMessageDisplay(long userId,
0818: String className, long classPK) throws PortalException,
0819: SystemException {
0820:
0821: long classNameId = PortalUtil.getClassNameId(className);
0822:
0823: MBMessage message = null;
0824:
0825: try {
0826: MBDiscussion discussion = mbDiscussionPersistence
0827: .findByC_C(classNameId, classPK);
0828:
0829: List messages = mbMessagePersistence.findByT_P(discussion
0830: .getThreadId(),
0831: MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID);
0832:
0833: message = (MBMessage) messages.get(0);
0834: } catch (NoSuchDiscussionException nsde) {
0835: String subject = String.valueOf(classPK);
0836: String body = subject;
0837:
0838: message = addDiscussionMessage(userId, subject, body);
0839:
0840: long discussionId = counterLocalService.increment();
0841:
0842: MBDiscussion discussion = mbDiscussionPersistence
0843: .create(discussionId);
0844:
0845: discussion.setClassNameId(classNameId);
0846: discussion.setClassPK(classPK);
0847: discussion.setThreadId(message.getThreadId());
0848:
0849: mbDiscussionPersistence.update(discussion);
0850: }
0851:
0852: return getMessageDisplay(message);
0853: }
0854:
0855: public int getDiscussionMessagesCount(long classNameId, long classPK)
0856: throws SystemException {
0857:
0858: MBDiscussion discussion = mbDiscussionPersistence.fetchByC_C(
0859: classNameId, classPK);
0860:
0861: if (discussion == null) {
0862: return 0;
0863: }
0864:
0865: int count = mbMessagePersistence.countByThreadId(discussion
0866: .getThreadId());
0867:
0868: if (count >= 1) {
0869: return count - 1;
0870: } else {
0871: return 0;
0872: }
0873: }
0874:
0875: public List getGroupMessages(long groupId, int begin, int end)
0876: throws SystemException {
0877:
0878: return mbMessageFinder.findByGroupId(groupId, begin, end);
0879: }
0880:
0881: public List getGroupMessages(long groupId, int begin, int end,
0882: OrderByComparator obc) throws SystemException {
0883:
0884: return mbMessageFinder.findByGroupId(groupId, begin, end, obc);
0885: }
0886:
0887: public List getGroupMessages(long groupId, long userId, int begin,
0888: int end) throws SystemException {
0889:
0890: return mbMessageFinder.findByG_U(groupId, userId, begin, end);
0891: }
0892:
0893: public List getGroupMessages(long groupId, long userId, int begin,
0894: int end, OrderByComparator obc) throws SystemException {
0895:
0896: return mbMessageFinder.findByG_U(groupId, userId, begin, end,
0897: obc);
0898: }
0899:
0900: public int getGroupMessagesCount(long groupId)
0901: throws SystemException {
0902: return mbMessageFinder.countByGroupId(groupId);
0903: }
0904:
0905: public int getGroupMessagesCount(long groupId, long userId)
0906: throws SystemException {
0907:
0908: return mbMessageFinder.countByG_U(groupId, userId);
0909: }
0910:
0911: public MBMessage getMessage(long messageId) throws PortalException,
0912: SystemException {
0913:
0914: return mbMessagePersistence.findByPrimaryKey(messageId);
0915: }
0916:
0917: public List getMessages(String className, long classPK)
0918: throws PortalException, SystemException {
0919:
0920: long classNameId = PortalUtil.getClassNameId(className);
0921:
0922: return mbMessageFinder.findByC_C(classNameId, classPK);
0923: }
0924:
0925: public MBMessageDisplay getMessageDisplay(long messageId)
0926: throws PortalException, SystemException {
0927:
0928: MBMessage message = getMessage(messageId);
0929:
0930: return getMessageDisplay(message);
0931: }
0932:
0933: public MBMessageDisplay getMessageDisplay(MBMessage message)
0934: throws PortalException, SystemException {
0935:
0936: MBCategory category = mbCategoryPersistence
0937: .findByPrimaryKey(message.getCategoryId());
0938:
0939: MBMessage parentMessage = null;
0940:
0941: if (message.isReply()) {
0942: parentMessage = mbMessagePersistence
0943: .findByPrimaryKey(message.getParentMessageId());
0944: }
0945:
0946: MBThread thread = mbThreadPersistence.findByPrimaryKey(message
0947: .getThreadId());
0948:
0949: thread.setViewCount(thread.getViewCount() + 1);
0950:
0951: mbThreadPersistence.update(thread);
0952:
0953: MBTreeWalker treeWalker = new MBTreeWalkerImpl(message);
0954:
0955: ThreadLastPostDateComparator comparator = new ThreadLastPostDateComparator(
0956: false);
0957:
0958: MBThread[] prevAndNextThreads = mbThreadPersistence
0959: .findByCategoryId_PrevAndNext(message.getThreadId(),
0960: message.getCategoryId(), comparator);
0961:
0962: MBThread previousThread = prevAndNextThreads[0];
0963: MBThread nextThread = prevAndNextThreads[2];
0964:
0965: MBThread firstThread = null;
0966:
0967: try {
0968: firstThread = mbThreadPersistence.findByCategoryId_First(
0969: message.getCategoryId(), comparator);
0970: } catch (NoSuchThreadException nste) {
0971: }
0972:
0973: MBThread lastThread = null;
0974:
0975: try {
0976: lastThread = mbThreadPersistence.findByCategoryId_Last(
0977: message.getCategoryId(), comparator);
0978: } catch (NoSuchThreadException nste) {
0979: }
0980:
0981: return new MBMessageDisplayImpl(message, parentMessage,
0982: category, thread, treeWalker, previousThread,
0983: nextThread, firstThread, lastThread);
0984: }
0985:
0986: public List getNoAssetMessages() throws SystemException {
0987: return mbMessageFinder.findByNoAssets();
0988: }
0989:
0990: public List getThreadMessages(long threadId) throws SystemException {
0991: return getThreadMessages(threadId,
0992: new MessageThreadComparator());
0993: }
0994:
0995: public List getThreadMessages(long threadId, Comparator comparator)
0996: throws SystemException {
0997:
0998: List messages = mbMessagePersistence.findByThreadId(threadId);
0999:
1000: Collections.sort(messages, comparator);
1001:
1002: return messages;
1003: }
1004:
1005: public int getThreadMessagesCount(long threadId)
1006: throws SystemException {
1007: return mbMessagePersistence.countByThreadId(threadId);
1008: }
1009:
1010: public void subscribeMessage(long userId, long messageId)
1011: throws PortalException, SystemException {
1012:
1013: MBMessage message = mbMessagePersistence
1014: .findByPrimaryKey(messageId);
1015:
1016: subscriptionLocalService.addSubscription(userId, MBThread.class
1017: .getName(), message.getThreadId());
1018: }
1019:
1020: public void unsubscribeMessage(long userId, long messageId)
1021: throws PortalException, SystemException {
1022:
1023: MBMessage message = mbMessagePersistence
1024: .findByPrimaryKey(messageId);
1025:
1026: subscriptionLocalService.deleteSubscription(userId,
1027: MBThread.class.getName(), message.getThreadId());
1028: }
1029:
1030: public MBMessage updateDiscussionMessage(long userId,
1031: long messageId, String subject, String body)
1032: throws PortalException, SystemException {
1033:
1034: if (Validator.isNull(subject)) {
1035: subject = "N/A";
1036: }
1037:
1038: List files = new ArrayList();
1039: double priority = 0.0;
1040: String[] tagsEntries = null;
1041: PortletPreferences prefs = null;
1042: ThemeDisplay themeDisplay = null;
1043:
1044: return updateMessage(userId, messageId, subject, body, files,
1045: priority, tagsEntries, prefs, themeDisplay);
1046: }
1047:
1048: public MBMessage updateMessage(long userId, long messageId,
1049: String subject, String body, List files, double priority,
1050: String[] tagsEntries, PortletPreferences prefs,
1051: ThemeDisplay themeDisplay) throws PortalException,
1052: SystemException {
1053:
1054: // Message
1055:
1056: MBMessage message = mbMessagePersistence
1057: .findByPrimaryKey(messageId);
1058:
1059: MBCategory category = message.getCategory();
1060: subject = ModelHintsUtil.trimString(MBMessage.class.getName(),
1061: "subject", subject);
1062: Date now = new Date();
1063:
1064: validate(subject, body);
1065:
1066: // File attachments
1067:
1068: if (files.size() > 0) {
1069: long companyId = message.getCompanyId();
1070: String portletId = CompanyImpl.SYSTEM_STRING;
1071: long groupId = GroupImpl.DEFAULT_PARENT_GROUP_ID;
1072: long repositoryId = CompanyImpl.SYSTEM;
1073: String dirName = message.getAttachmentsDir();
1074:
1075: try {
1076: try {
1077: dlService.deleteDirectory(companyId, portletId,
1078: repositoryId, dirName);
1079: } catch (NoSuchDirectoryException nsde) {
1080: }
1081:
1082: dlService
1083: .addDirectory(companyId, repositoryId, dirName);
1084:
1085: for (int i = 0; i < files.size(); i++) {
1086: ObjectValuePair ovp = (ObjectValuePair) files
1087: .get(i);
1088:
1089: String fileName = (String) ovp.getKey();
1090: byte[] byteArray = (byte[]) ovp.getValue();
1091:
1092: try {
1093: dlService.addFile(companyId, portletId,
1094: groupId, repositoryId, dirName + "/"
1095: + fileName, StringPool.BLANK,
1096: new String[0], byteArray);
1097: } catch (DuplicateFileException dfe) {
1098: }
1099: }
1100: } catch (RemoteException re) {
1101: throw new SystemException(re);
1102: }
1103: }
1104:
1105: // Message
1106:
1107: message.setModifiedDate(now);
1108: message.setSubject(subject);
1109: message.setBody(body);
1110: message.setAttachments((files.size() > 0 ? true : false));
1111:
1112: mbMessagePersistence.update(message);
1113:
1114: // Subscriptions
1115:
1116: notifySubscribers(category, message, prefs, themeDisplay, true);
1117:
1118: // Thread
1119:
1120: MBThread thread = mbThreadPersistence.findByPrimaryKey(message
1121: .getThreadId());
1122:
1123: if (priority != MBThreadImpl.PRIORITY_NOT_GIVEN) {
1124: thread.setPriority(priority);
1125: }
1126:
1127: mbThreadPersistence.update(thread);
1128:
1129: // Category
1130:
1131: category.setLastPostDate(now);
1132:
1133: mbCategoryPersistence.update(category);
1134:
1135: // Tags
1136:
1137: updateTagsAsset(userId, message, tagsEntries);
1138:
1139: // Lucene
1140:
1141: try {
1142: if (!category.isDiscussion()) {
1143: Indexer.updateMessage(message.getCompanyId(), category
1144: .getGroupId(), message.getUserName(), category
1145: .getCategoryId(), message.getThreadId(),
1146: messageId, subject, body, tagsEntries);
1147: }
1148: } catch (IOException ioe) {
1149: _log.error("Indexing " + messageId, ioe);
1150: }
1151:
1152: return message;
1153: }
1154:
1155: public MBMessage updateMessage(long messageId, Date createDate,
1156: Date modifiedDate) throws PortalException, SystemException {
1157:
1158: // Message
1159:
1160: MBMessage message = mbMessagePersistence
1161: .findByPrimaryKey(messageId);
1162:
1163: message.setCreateDate(createDate);
1164: message.setModifiedDate(modifiedDate);
1165:
1166: mbMessagePersistence.update(message);
1167:
1168: // Thread
1169:
1170: MBThread thread = mbThreadPersistence.findByPrimaryKey(message
1171: .getThreadId());
1172:
1173: if (message.isAnonymous()) {
1174: thread.setLastPostByUserId(0);
1175: } else {
1176: thread.setLastPostByUserId(message.getUserId());
1177: }
1178:
1179: thread.setLastPostDate(modifiedDate);
1180:
1181: mbThreadPersistence.update(thread);
1182:
1183: // Category
1184:
1185: MBCategory category = mbCategoryPersistence
1186: .findByPrimaryKey(message.getCategoryId());
1187:
1188: category.setLastPostDate(modifiedDate);
1189:
1190: mbCategoryPersistence.update(category);
1191:
1192: // Statistics
1193:
1194: MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
1195: category.getGroupId(), message.getUserId());
1196:
1197: if (statsUser != null) {
1198: statsUser.setLastPostDate(modifiedDate);
1199:
1200: mbStatsUserPersistence.update(statsUser);
1201: }
1202:
1203: return message;
1204: }
1205:
1206: public MBMessage updateMessage(long messageId, String body)
1207: throws PortalException, SystemException {
1208:
1209: MBMessage message = mbMessagePersistence
1210: .findByPrimaryKey(messageId);
1211:
1212: message.setBody(body);
1213:
1214: mbMessagePersistence.update(message);
1215:
1216: return message;
1217: }
1218:
1219: public void updateTagsAsset(long userId, MBMessage message,
1220: String[] tagsEntries) throws PortalException,
1221: SystemException {
1222:
1223: if (message.isDiscussion()) {
1224: return;
1225: }
1226:
1227: tagsAssetLocalService.updateAsset(userId, message.getCategory()
1228: .getGroupId(), MBMessage.class.getName(), message
1229: .getMessageId(), tagsEntries, null, null, null, null,
1230: ContentTypes.TEXT_HTML, message.getSubject(), null,
1231: null, null, 0, 0, null, false);
1232: }
1233:
1234: protected void logAddMessage(long messageId, StopWatch stopWatch,
1235: int block) {
1236:
1237: if (_log.isDebugEnabled()) {
1238: if ((messageId != 1) && ((messageId % 10) != 0)) {
1239: return;
1240: }
1241:
1242: _log.debug("Adding message block " + block + " for "
1243: + messageId + " takes " + stopWatch.getTime()
1244: + " ms");
1245: }
1246: }
1247:
1248: protected void notifySubscribers(MBCategory category,
1249: MBMessage message, PortletPreferences prefs,
1250: ThemeDisplay themeDisplay, boolean update)
1251: throws PortalException, SystemException {
1252:
1253: try {
1254: if (category.isDiscussion()) {
1255: return;
1256: }
1257:
1258: if (prefs == null) {
1259: long ownerId = category.getGroupId();
1260: int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
1261: long plid = PortletKeys.PREFS_PLID_SHARED;
1262: String portletId = PortletKeys.MESSAGE_BOARDS;
1263: String defaultPreferences = null;
1264:
1265: prefs = portletPreferencesLocalService.getPreferences(
1266: category.getCompanyId(), ownerId, ownerType,
1267: plid, portletId, defaultPreferences);
1268: }
1269:
1270: if (!update && MBUtil.getEmailMessageAddedEnabled(prefs)) {
1271: } else if (update
1272: && MBUtil.getEmailMessageUpdatedEnabled(prefs)) {
1273: } else {
1274: return;
1275: }
1276:
1277: Company company = companyPersistence
1278: .findByPrimaryKey(message.getCompanyId());
1279:
1280: Group group = groupPersistence.findByPrimaryKey(category
1281: .getGroupId());
1282:
1283: User user = userPersistence.findByPrimaryKey(message
1284: .getUserId());
1285:
1286: List categoryIds = new ArrayList();
1287:
1288: categoryIds.add(new Long(category.getCategoryId()));
1289: categoryIds.addAll(category.getAncestorCategoryIds());
1290:
1291: String messageURL = StringPool.BLANK;
1292:
1293: if (themeDisplay != null) {
1294: String portalURL = PortalUtil
1295: .getPortalURL(themeDisplay);
1296: String layoutURL = PortalUtil
1297: .getLayoutURL(themeDisplay);
1298:
1299: messageURL = portalURL + layoutURL
1300: + "/message_boards/message/"
1301: + message.getMessageId();
1302: }
1303:
1304: String portletName = PortalUtil.getPortletTitle(
1305: PortletKeys.MESSAGE_BOARDS, user);
1306:
1307: String fromName = MBUtil.getEmailFromName(prefs);
1308: String fromAddress = MBUtil.getEmailFromAddress(prefs);
1309:
1310: String mailingListAddress = StringPool.BLANK;
1311:
1312: if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
1313: mailingListAddress = MBUtil.getMailingListAddress(
1314: message.getCategoryId(),
1315: message.getMessageId(), company.getMx());
1316: }
1317:
1318: String replyToAddress = mailingListAddress;
1319: String messageId = MBUtil.getMailId(company.getMx(),
1320: message.getCategoryId(), message.getMessageId());
1321:
1322: fromName = StringUtil.replace(fromName, new String[] {
1323: "[$COMPANY_ID$]", "[$COMPANY_MX$]",
1324: "[$COMPANY_NAME$]", "[$COMMUNITY_NAME$]",
1325: "[$MAILING_LIST_ADDRESS$]",
1326: "[$MESSAGE_USER_ADDRESS$]",
1327: "[$MESSAGE_USER_NAME$]", "[$PORTLET_NAME$]" },
1328: new String[] {
1329: String.valueOf(company.getCompanyId()),
1330: company.getMx(), company.getName(),
1331: group.getName(), mailingListAddress,
1332: user.getEmailAddress(), user.getFullName(),
1333: portletName });
1334:
1335: fromAddress = StringUtil.replace(fromAddress, new String[] {
1336: "[$COMPANY_ID$]", "[$COMPANY_MX$]",
1337: "[$COMPANY_NAME$]", "[$COMMUNITY_NAME$]",
1338: "[$MAILING_LIST_ADDRESS$]",
1339: "[$MESSAGE_USER_ADDRESS$]",
1340: "[$MESSAGE_USER_NAME$]", "[$PORTLET_NAME$]" },
1341: new String[] {
1342: String.valueOf(company.getCompanyId()),
1343: company.getMx(), company.getName(),
1344: group.getName(), mailingListAddress,
1345: user.getEmailAddress(), user.getFullName(),
1346: portletName });
1347:
1348: String subjectPrefix = null;
1349: String body = null;
1350: String signature = null;
1351:
1352: if (update) {
1353: subjectPrefix = MBUtil
1354: .getEmailMessageUpdatedSubjectPrefix(prefs);
1355: body = MBUtil.getEmailMessageUpdatedBody(prefs);
1356: signature = MBUtil
1357: .getEmailMessageUpdatedSignature(prefs);
1358: } else {
1359: subjectPrefix = MBUtil
1360: .getEmailMessageAddedSubjectPrefix(prefs);
1361: body = MBUtil.getEmailMessageAddedBody(prefs);
1362: signature = MBUtil.getEmailMessageAddedSignature(prefs);
1363: }
1364:
1365: if (Validator.isNotNull(signature)) {
1366: body += "\n--\n" + signature;
1367: }
1368:
1369: subjectPrefix = StringUtil.replace(subjectPrefix,
1370: new String[] { "[$CATEGORY_NAME$]",
1371: "[$COMPANY_ID$]", "[$COMPANY_MX$]",
1372: "[$COMPANY_NAME$]", "[$COMMUNITY_NAME$]",
1373: "[$FROM_ADDRESS$]", "[$FROM_NAME$]",
1374: "[$MAILING_LIST_ADDRESS$]",
1375: "[$MESSAGE_BODY$]", "[$MESSAGE_ID$]",
1376: "[$MESSAGE_SUBJECT$]",
1377: "[$MESSAGE_USER_ADDRESS$]",
1378: "[$MESSAGE_USER_NAME$]", "[$PORTAL_URL$]",
1379: "[$PORTLET_NAME$]" }, new String[] {
1380: category.getName(),
1381: String.valueOf(company.getCompanyId()),
1382: company.getMx(), company.getName(),
1383: group.getName(), fromAddress, fromName,
1384: mailingListAddress, message.getBody(),
1385: String.valueOf(message.getMessageId()),
1386: message.getSubject(),
1387: user.getEmailAddress(), user.getFullName(),
1388: company.getVirtualHost(), portletName });
1389:
1390: body = StringUtil.replace(body, new String[] {
1391: "[$CATEGORY_NAME$]", "[$COMPANY_ID$]",
1392: "[$COMPANY_MX$]", "[$COMPANY_NAME$]",
1393: "[$COMMUNITY_NAME$]", "[$FROM_ADDRESS$]",
1394: "[$FROM_NAME$]", "[$MAILING_LIST_ADDRESS$]",
1395: "[$MESSAGE_BODY$]", "[$MESSAGE_ID$]",
1396: "[$MESSAGE_SUBJECT$]", "[$MESSAGE_URL$]",
1397: "[$MESSAGE_USER_ADDRESS$]",
1398: "[$MESSAGE_USER_NAME$]", "[$PORTAL_URL$]",
1399: "[$PORTLET_NAME$]" }, new String[] {
1400: category.getName(),
1401: String.valueOf(company.getCompanyId()),
1402: company.getMx(), company.getName(),
1403: group.getName(), fromAddress, fromName,
1404: mailingListAddress, message.getBody(),
1405: String.valueOf(message.getMessageId()),
1406: message.getSubject(), messageURL,
1407: user.getEmailAddress(), user.getFullName(),
1408: company.getVirtualHost(), portletName });
1409:
1410: String subject = message.getSubject();
1411:
1412: if (subject.indexOf(subjectPrefix) == -1) {
1413: subject = subjectPrefix + subject;
1414: }
1415:
1416: String inReplyTo = null;
1417:
1418: if (message.getParentMessageId() != MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) {
1419:
1420: inReplyTo = MBUtil.getMailId(company.getMx(), message
1421: .getCategoryId(), message.getParentMessageId());
1422: }
1423:
1424: MBMessageProducer.produce(new String[] {
1425: String.valueOf(message.getCompanyId()),
1426: String.valueOf(message.getUserId()),
1427: StringUtil.merge(categoryIds),
1428: String.valueOf(message.getThreadId()), fromName,
1429: fromAddress, subject, body, replyToAddress,
1430: messageId, inReplyTo });
1431: } catch (IOException ioe) {
1432: throw new SystemException(ioe);
1433: }
1434: }
1435:
1436: protected void sendBlogsCommentsEmail(long userId, long entryId,
1437: MBMessage message, ThemeDisplay themeDisplay)
1438: throws IOException, PortalException, SystemException {
1439:
1440: long companyId = message.getCompanyId();
1441:
1442: if (!PrefsPropsUtil.getBoolean(companyId,
1443: PropsUtil.BLOGS_EMAIL_COMMENTS_ADDED_ENABLED)) {
1444:
1445: return;
1446: }
1447:
1448: BlogsEntry entry = blogsEntryPersistence
1449: .findByPrimaryKey(entryId);
1450:
1451: String portalURL = PortalUtil.getPortalURL(themeDisplay);
1452: String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
1453:
1454: String blogsEntryURL = portalURL + layoutURL + "/blogs/"
1455: + entry.getUrlTitle();
1456:
1457: User blogsUser = userPersistence.findByPrimaryKey(entry
1458: .getUserId());
1459: User commentsUser = userPersistence.findByPrimaryKey(userId);
1460:
1461: String fromName = PrefsPropsUtil.getString(companyId,
1462: PropsUtil.ADMIN_EMAIL_FROM_NAME);
1463: String fromAddress = PrefsPropsUtil.getString(companyId,
1464: PropsUtil.ADMIN_EMAIL_FROM_ADDRESS);
1465:
1466: String toName = blogsUser.getFullName();
1467: String toAddress = blogsUser.getEmailAddress();
1468:
1469: String subject = PrefsPropsUtil.getContent(companyId,
1470: PropsUtil.BLOGS_EMAIL_COMMENTS_ADDED_SUBJECT);
1471: String body = PrefsPropsUtil.getContent(companyId,
1472: PropsUtil.BLOGS_EMAIL_COMMENTS_ADDED_BODY);
1473:
1474: subject = StringUtil.replace(subject, new String[] {
1475: "[$BLOGS_COMMENTS_USER_ADDRESS$]",
1476: "[$BLOGS_COMMENTS_USER_NAME$]", "[$BLOGS_ENTRY_URL$]",
1477: "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$TO_ADDRESS$]",
1478: "[$TO_NAME$]" }, new String[] {
1479: commentsUser.getEmailAddress(),
1480: commentsUser.getFullName(), blogsEntryURL, fromAddress,
1481: fromName, toAddress, toName });
1482:
1483: body = StringUtil.replace(body, new String[] {
1484: "[$BLOGS_COMMENTS_USER_ADDRESS$]",
1485: "[$BLOGS_COMMENTS_USER_NAME$]", "[$BLOGS_ENTRY_URL$]",
1486: "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$TO_ADDRESS$]",
1487: "[$TO_NAME$]" }, new String[] {
1488: commentsUser.getEmailAddress(),
1489: commentsUser.getFullName(), blogsEntryURL, fromAddress,
1490: fromName, toAddress, toName });
1491:
1492: InternetAddress from = new InternetAddress(fromAddress,
1493: fromName);
1494:
1495: InternetAddress to = new InternetAddress(toAddress, toName);
1496:
1497: MailMessage mailMessage = new MailMessage(from, to, subject,
1498: body, true);
1499:
1500: mailService.sendEmail(mailMessage);
1501: }
1502:
1503: protected void validate(String subject, String body)
1504: throws PortalException {
1505:
1506: if (Validator.isNull(subject)) {
1507: throw new MessageSubjectException();
1508: }
1509:
1510: if (Validator.isNull(body)) {
1511: throw new MessageBodyException();
1512: }
1513: }
1514:
1515: private static Log _log = LogFactory
1516: .getLog(MBMessageLocalServiceImpl.class);
1517:
1518: }
|