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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.model.User;
024    import com.liferay.portal.util.GroupSubscriptionCheckSubscriptionSender;
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
033            extends GroupSubscriptionCheckSubscriptionSender {
034    
035            public MBSubscriptionSender(String resourceName) {
036                    super(resourceName);
037            }
038    
039            public void addMailingListSubscriber(long groupId, long categoryId) {
040                    if (_calledAddMailingListSubscriber) {
041                            throw new IllegalStateException("Method may only be called once");
042                    }
043    
044                    _calledAddMailingListSubscriber = true;
045    
046                    MBMailingList mailingList =
047                            MBMailingListLocalServiceUtil.fetchCategoryMailingList(
048                                    groupId, categoryId);
049    
050                    if ((mailingList == null) || !mailingList.isActive()) {
051                            return;
052                    }
053    
054                    setFrom(mailingList.getOutEmailAddress(), null);
055                    setReplyToAddress(mailingList.getEmailAddress());
056    
057                    if (mailingList.isOutCustom()) {
058                            String protocol = Account.PROTOCOL_SMTP;
059    
060                            if (mailingList.isOutUseSSL()) {
061                                    protocol = Account.PROTOCOL_SMTPS;
062                            }
063    
064                            SMTPAccount smtpAccount = (SMTPAccount)Account.getInstance(
065                                    protocol, mailingList.getOutServerPort());
066    
067                            smtpAccount.setHost(mailingList.getOutServerName());
068                            smtpAccount.setUser(mailingList.getOutUserName());
069                            smtpAccount.setPassword(mailingList.getOutPassword());
070    
071                            setSMTPAccount(smtpAccount);
072                    }
073    
074                    setSubject(getMailingListSubject(subject, mailId));
075    
076                    addRuntimeSubscribers(
077                            mailingList.getEmailAddress(), mailingList.getEmailAddress());
078            }
079    
080            protected String getMailingListSubject(String subject, String mailId) {
081                    subject = GetterUtil.getString(subject);
082                    mailId = GetterUtil.getString(mailId);
083    
084                    return subject.concat(StringPool.SPACE).concat(mailId);
085            }
086    
087            @Override
088            protected void sendNotification(User user) throws Exception {
089                    sendEmailNotification(user);
090    
091                    if (currentUserId == user.getUserId() ) {
092                            if (_log.isDebugEnabled()) {
093                                    _log.debug("Skip notification for user " + currentUserId);
094                            }
095    
096                            return;
097                    }
098    
099                    sendUserNotification(user);
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    MBSubscriptionSender.class);
104    
105            private boolean _calledAddMailingListSubscriber;
106    
107    }