001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.StringPool;
026    import com.liferay.portal.util.SubscriptionSender;
027    import com.liferay.portlet.messageboards.NoSuchMailingListException;
028    import com.liferay.portlet.messageboards.model.MBMailingList;
029    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
030    
031    import java.util.Locale;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Thiago Moreira
036     */
037    public class MBSubscriptionSender extends SubscriptionSender {
038    
039            public void addMailingListSubscriber(long groupId, long categoryId)
040                    throws PortalException, SystemException {
041    
042                    if (_calledAddMailingListSubscriber) {
043                            throw new IllegalArgumentException();
044                    }
045    
046                    _calledAddMailingListSubscriber = true;
047    
048                    MBMailingList mailingList = null;
049    
050                    try {
051                            mailingList = MBMailingListLocalServiceUtil.getCategoryMailingList(
052                                    groupId, categoryId);
053                    }
054                    catch (NoSuchMailingListException nsmle) {
055                            return;
056                    }
057    
058                    if (!mailingList.isActive()) {
059                            return;
060                    }
061    
062                    setFrom(mailingList.getOutEmailAddress(), null);
063    
064                    if (mailingList.isOutCustom()) {
065                            String protocol = Account.PROTOCOL_SMTP;
066    
067                            if (mailingList.isOutUseSSL()) {
068                                    protocol = Account.PROTOCOL_SMTPS;
069                            }
070    
071                            SMTPAccount smtpAccount = (SMTPAccount)Account.getInstance(
072                                    protocol, mailingList.getOutServerPort());
073    
074                            smtpAccount.setHost(mailingList.getOutServerName());
075                            smtpAccount.setUser(mailingList.getOutUserName());
076                            smtpAccount.setPassword(mailingList.getOutPassword());
077    
078                            setSMTPAccount(smtpAccount);
079                    }
080    
081                    setSubject(getMailingListSubject(subject, mailId));
082    
083                    addRuntimeSubscribers(
084                            mailingList.getEmailAddress(), mailingList.getEmailAddress());
085            }
086    
087            protected String getMailingListSubject(String subject, String mailId) {
088                    return subject.concat(StringPool.SPACE).concat(mailId);
089            }
090    
091            @Override
092            protected void processMailMessage(MailMessage mailMessage, Locale locale)
093                    throws Exception {
094    
095                    super.processMailMessage(mailMessage, locale);
096    
097                    if (htmlFormat) {
098                            try {
099                                    String processedBody = BBCodeTranslatorUtil.getHTML(
100                                            mailMessage.getBody());
101    
102                                    mailMessage.setBody(processedBody);
103                            }
104                            catch (Exception e) {
105                                    _log.error(
106                                            "Could not parse message " + mailId + " " + e.getMessage());
107                            }
108                    }
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(MBSubscriptionSender.class);
112    
113            private boolean _calledAddMailingListSubscriber;
114    
115    }