001    /**
002     * Copyright (c) 2000-2013 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.mail.Account;
020    import com.liferay.portal.kernel.mail.SMTPAccount;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.util.SubscriptionSender;
024    import com.liferay.portlet.messageboards.NoSuchMailingListException;
025    import com.liferay.portlet.messageboards.model.MBMailingList;
026    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Thiago Moreira
031     */
032    public class MBSubscriptionSender extends SubscriptionSender {
033    
034            public void addMailingListSubscriber(long groupId, long categoryId)
035                    throws PortalException, SystemException {
036    
037                    if (_calledAddMailingListSubscriber) {
038                            throw new IllegalArgumentException();
039                    }
040    
041                    _calledAddMailingListSubscriber = true;
042    
043                    MBMailingList mailingList = null;
044    
045                    try {
046                            mailingList = MBMailingListLocalServiceUtil.getCategoryMailingList(
047                                    groupId, categoryId);
048                    }
049                    catch (NoSuchMailingListException nsmle) {
050                            return;
051                    }
052    
053                    if (!mailingList.isActive()) {
054                            return;
055                    }
056    
057                    setFrom(mailingList.getOutEmailAddress(), null);
058                    setReplyToAddress(mailingList.getEmailAddress());
059    
060                    if (mailingList.isOutCustom()) {
061                            String protocol = Account.PROTOCOL_SMTP;
062    
063                            if (mailingList.isOutUseSSL()) {
064                                    protocol = Account.PROTOCOL_SMTPS;
065                            }
066    
067                            SMTPAccount smtpAccount = (SMTPAccount)Account.getInstance(
068                                    protocol, mailingList.getOutServerPort());
069    
070                            smtpAccount.setHost(mailingList.getOutServerName());
071                            smtpAccount.setUser(mailingList.getOutUserName());
072                            smtpAccount.setPassword(mailingList.getOutPassword());
073    
074                            setSMTPAccount(smtpAccount);
075                    }
076    
077                    setSubject(getMailingListSubject(subject, mailId));
078    
079                    addRuntimeSubscribers(
080                            mailingList.getEmailAddress(), mailingList.getEmailAddress());
081            }
082    
083            protected String getMailingListSubject(String subject, String mailId) {
084                    subject = GetterUtil.getString(subject);
085                    mailId = GetterUtil.getString(mailId);
086    
087                    return subject.concat(StringPool.SPACE).concat(mailId);
088            }
089    
090            private boolean _calledAddMailingListSubscriber;
091    
092    }