001    /**
002     * Copyright (c) 2000-2013 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.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    }