001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.mail.Account;
022    import com.liferay.portal.kernel.mail.MailMessage;
023    import com.liferay.portal.kernel.mail.SMTPAccount;
024    import com.liferay.portal.kernel.parsers.bbcode.BBCodeTranslatorUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.util.SubscriptionSender;
028    import com.liferay.portlet.messageboards.NoSuchMailingListException;
029    import com.liferay.portlet.messageboards.model.MBMailingList;
030    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
031    
032    import java.util.Locale;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Thiago Moreira
037     */
038    public class MBSubscriptionSender extends SubscriptionSender {
039    
040            public void addMailingListSubscriber(long groupId, long categoryId)
041                    throws PortalException, SystemException {
042    
043                    if (_calledAddMailingListSubscriber) {
044                            throw new IllegalArgumentException();
045                    }
046    
047                    _calledAddMailingListSubscriber = true;
048    
049                    MBMailingList mailingList = null;
050    
051                    try {
052                            mailingList = MBMailingListLocalServiceUtil.getCategoryMailingList(
053                                    groupId, categoryId);
054                    }
055                    catch (NoSuchMailingListException nsmle) {
056                            return;
057                    }
058    
059                    if (!mailingList.isActive()) {
060                            return;
061                    }
062    
063                    setFrom(mailingList.getOutEmailAddress(), null);
064    
065                    if (mailingList.isOutCustom()) {
066                            String protocol = Account.PROTOCOL_SMTP;
067    
068                            if (mailingList.isOutUseSSL()) {
069                                    protocol = Account.PROTOCOL_SMTPS;
070                            }
071    
072                            SMTPAccount smtpAccount = (SMTPAccount)Account.getInstance(
073                                    protocol, mailingList.getOutServerPort());
074    
075                            smtpAccount.setHost(mailingList.getOutServerName());
076                            smtpAccount.setUser(mailingList.getOutUserName());
077                            smtpAccount.setPassword(mailingList.getOutPassword());
078    
079                            setSMTPAccount(smtpAccount);
080                    }
081    
082                    setSubject(getMailingListSubject(subject, mailId));
083    
084                    addRuntimeSubscribers(
085                            mailingList.getEmailAddress(), mailingList.getEmailAddress());
086            }
087    
088            protected String getMailingListSubject(String subject, String mailId) {
089                    subject = GetterUtil.getString(subject);
090                    mailId = GetterUtil.getString(mailId);
091    
092                    return subject.concat(StringPool.SPACE).concat(mailId);
093            }
094    
095            @Override
096            protected void processMailMessage(MailMessage mailMessage, Locale locale)
097                    throws Exception {
098    
099                    super.processMailMessage(mailMessage, locale);
100    
101                    if (htmlFormat) {
102                            try {
103                                    String processedBody = BBCodeTranslatorUtil.getHTML(
104                                            mailMessage.getBody());
105    
106                                    mailMessage.setBody(processedBody);
107                            }
108                            catch (Exception e) {
109                                    _log.error(
110                                            "Could not parse message " + mailId + " " + e.getMessage());
111                            }
112                    }
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(MBSubscriptionSender.class);
116    
117            private boolean _calledAddMailingListSubscriber;
118    
119    }