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.mail.kernel.model.Account;
018    import com.liferay.mail.kernel.model.SMTPAccount;
019    import com.liferay.message.boards.kernel.model.MBMailingList;
020    import com.liferay.message.boards.kernel.service.MBMailingListLocalServiceUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.model.User;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.GroupSubscriptionCheckSubscriptionSender;
027    import com.liferay.portal.kernel.util.StringPool;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Thiago Moreira
032     * @author Roberto D??az
033     */
034    public class MBSubscriptionSender
035            extends GroupSubscriptionCheckSubscriptionSender {
036    
037            public MBSubscriptionSender(String resourceName) {
038                    super(resourceName);
039            }
040    
041            public void addMailingListSubscriber(long groupId, long categoryId) {
042                    if (_calledAddMailingListSubscriber) {
043                            throw new IllegalStateException("Method may only be called once");
044                    }
045    
046                    _calledAddMailingListSubscriber = true;
047    
048                    MBMailingList mailingList =
049                            MBMailingListLocalServiceUtil.fetchCategoryMailingList(
050                                    groupId, categoryId);
051    
052                    if ((mailingList == null) || !mailingList.isActive()) {
053                            return;
054                    }
055    
056                    setFrom(mailingList.getOutEmailAddress(), null);
057                    setReplyToAddress(mailingList.getEmailAddress());
058    
059                    if (mailingList.isOutCustom()) {
060                            String protocol = Account.PROTOCOL_SMTP;
061    
062                            if (mailingList.isOutUseSSL()) {
063                                    protocol = Account.PROTOCOL_SMTPS;
064                            }
065    
066                            SMTPAccount smtpAccount = (SMTPAccount)Account.getInstance(
067                                    protocol, mailingList.getOutServerPort());
068    
069                            smtpAccount.setHost(mailingList.getOutServerName());
070                            smtpAccount.setUser(mailingList.getOutUserName());
071                            smtpAccount.setPassword(mailingList.getOutPassword());
072    
073                            setSMTPAccount(smtpAccount);
074                    }
075    
076                    setSubject(getMailingListSubject(subject, mailId));
077    
078                    addRuntimeSubscribers(
079                            mailingList.getEmailAddress(), mailingList.getEmailAddress());
080            }
081    
082            public void setAnonymous(boolean anonymous) {
083                    _anonymous = anonymous;
084            }
085    
086            public void setFullName(String fullName) {
087                    _fullName = fullName;
088            }
089    
090            protected String getMailingListSubject(String subject, String mailId) {
091                    subject = GetterUtil.getString(subject);
092                    mailId = GetterUtil.getString(mailId);
093    
094                    return subject.concat(StringPool.SPACE).concat(mailId);
095            }
096    
097            @Override
098            protected void populateNotificationEventJSONObject(
099                    JSONObject notificationEventJSONObject) {
100    
101                    notificationEventJSONObject.put("anonymous", _anonymous);
102                    notificationEventJSONObject.put("fullName", _fullName);
103    
104                    super.populateNotificationEventJSONObject(notificationEventJSONObject);
105            }
106    
107            @Override
108            protected void sendNotification(User user) throws Exception {
109                    sendEmailNotification(user);
110    
111                    if (currentUserId == user.getUserId()) {
112                            if (_log.isDebugEnabled()) {
113                                    _log.debug("Skip notification for user " + currentUserId);
114                            }
115    
116                            return;
117                    }
118    
119                    sendUserNotification(user);
120            }
121    
122            private static final Log _log = LogFactoryUtil.getLog(
123                    MBSubscriptionSender.class);
124    
125            private boolean _anonymous;
126            private boolean _calledAddMailingListSubscriber;
127            private String _fullName;
128    
129    }