001    /**
002     * Copyright (c) 2000-present 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.mail.Account;
019    import com.liferay.portal.kernel.mail.SMTPAccount;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.util.GroupSubscriptionCheckSubscriptionSender;
023    import com.liferay.portlet.messageboards.NoSuchMailingListException;
024    import com.liferay.portlet.messageboards.model.MBMailingList;
025    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Thiago Moreira
030     */
031    public class MBSubscriptionSender
032            extends GroupSubscriptionCheckSubscriptionSender {
033    
034            public MBSubscriptionSender(String resourceName) {
035                    super(resourceName);
036            }
037    
038            public void addMailingListSubscriber(long groupId, long categoryId)
039                    throws PortalException {
040    
041                    if (_calledAddMailingListSubscriber) {
042                            throw new IllegalArgumentException();
043                    }
044    
045                    _calledAddMailingListSubscriber = true;
046    
047                    MBMailingList mailingList = null;
048    
049                    try {
050                            mailingList = MBMailingListLocalServiceUtil.getCategoryMailingList(
051                                    groupId, categoryId);
052                    }
053                    catch (NoSuchMailingListException nsmle) {
054                            return;
055                    }
056    
057                    if (!mailingList.isActive()) {
058                            return;
059                    }
060    
061                    setFrom(mailingList.getOutEmailAddress(), null);
062                    setReplyToAddress(mailingList.getEmailAddress());
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                    subject = GetterUtil.getString(subject);
089                    mailId = GetterUtil.getString(mailId);
090    
091                    return subject.concat(StringPool.SPACE).concat(mailId);
092            }
093    
094            private boolean _calledAddMailingListSubscriber;
095    
096    }