001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.service.jms;
022:
023: import com.liferay.mail.service.MailServiceUtil;
024: import com.liferay.portal.NoSuchUserException;
025: import com.liferay.portal.kernel.mail.MailMessage;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.model.Subscription;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.service.SubscriptionLocalServiceUtil;
031: import com.liferay.portal.service.UserLocalServiceUtil;
032: import com.liferay.portlet.messageboards.model.MBCategory;
033: import com.liferay.portlet.messageboards.model.MBThread;
034: import com.liferay.util.CollectionFactory;
035:
036: import java.util.List;
037: import java.util.Set;
038:
039: import javax.jms.Message;
040: import javax.jms.MessageListener;
041: import javax.jms.ObjectMessage;
042: import javax.jms.Queue;
043: import javax.jms.QueueConnection;
044: import javax.jms.QueueConnectionFactory;
045: import javax.jms.QueueReceiver;
046: import javax.jms.QueueSession;
047: import javax.jms.Session;
048:
049: import javax.mail.internet.InternetAddress;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: /**
055: * <a href="MBMessageConsumer.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class MBMessageConsumer implements MessageListener {
061:
062: public void consume() {
063: try {
064: QueueConnectionFactory qcf = MBMessageQCFUtil.getQCF();
065: QueueConnection con = qcf.createQueueConnection();
066:
067: QueueSession session = con.createQueueSession(false,
068: Session.AUTO_ACKNOWLEDGE);
069: Queue queue = (Queue) MBMessageQueueUtil.getQueue();
070:
071: QueueReceiver subscriber = session.createReceiver(queue);
072:
073: subscriber.setMessageListener(this );
074:
075: con.start();
076: } catch (Exception e) {
077: _log.error(e);
078: }
079: }
080:
081: public void onMessage(Message msg) {
082: try {
083: ObjectMessage objMsg = (ObjectMessage) msg;
084:
085: String[] array = (String[]) objMsg.getObject();
086:
087: _onMessage(array);
088: } catch (Exception e) {
089: _log.error("Error sending message board notifications", e);
090: }
091: }
092:
093: private void _onMessage(String[] array) throws Exception {
094: long companyId = GetterUtil.getLong(array[0]);
095: long userId = GetterUtil.getLong(array[1]);
096: String[] categoryIds = StringUtil.split(array[2]);
097: String threadId = array[3];
098: String fromName = array[4];
099: String fromAddress = array[5];
100: String subject = array[6];
101: String body = array[7];
102: String replyToAddress = array[8];
103: String messageId = array[9];
104: String inReplyTo = array[10];
105:
106: Set sent = CollectionFactory.getHashSet();
107:
108: if (_log.isInfoEnabled()) {
109: _log.info("Sending notifications for {messageId="
110: + messageId + ", threadId=" + threadId
111: + ", categoryIds=" + array[2] + "}");
112: }
113:
114: // Threads
115:
116: List subscriptions = SubscriptionLocalServiceUtil
117: .getSubscriptions(companyId, MBThread.class.getName(),
118: GetterUtil.getLong(threadId));
119:
120: _sendEmail(userId, fromName, fromAddress, subject, body,
121: subscriptions, sent, replyToAddress, messageId,
122: inReplyTo);
123:
124: // Categories
125:
126: for (int i = 0; i < categoryIds.length; i++) {
127: subscriptions = SubscriptionLocalServiceUtil
128: .getSubscriptions(companyId, MBCategory.class
129: .getName(), GetterUtil
130: .getLong(categoryIds[i]));
131:
132: _sendEmail(userId, fromName, fromAddress, subject, body,
133: subscriptions, sent, replyToAddress, messageId,
134: inReplyTo);
135: }
136:
137: if (_log.isInfoEnabled()) {
138: _log.info("Finished sending notifications");
139: }
140: }
141:
142: private void _sendEmail(long userId, String fromName,
143: String fromAddress, String subject, String body,
144: List subscriptions, Set sent, String replyToAddress,
145: String messageId, String inReplyTo) throws Exception {
146:
147: for (int i = 0; i < subscriptions.size(); i++) {
148: Subscription subscription = (Subscription) subscriptions
149: .get(i);
150:
151: Long subscribedUserId = new Long(subscription.getUserId());
152:
153: if (sent.contains(subscribedUserId)) {
154: if (_log.isDebugEnabled()) {
155: _log.debug("Do not send a duplicate email to user "
156: + subscribedUserId);
157: }
158:
159: continue;
160: } else {
161: if (_log.isDebugEnabled()) {
162: _log
163: .debug("Add user "
164: + subscribedUserId
165: + " to the list of users who have received an email");
166: }
167:
168: sent.add(subscribedUserId);
169: }
170:
171: User user = null;
172:
173: try {
174: user = UserLocalServiceUtil.getUserById(subscription
175: .getUserId());
176: } catch (NoSuchUserException nsue) {
177: if (_log.isInfoEnabled()) {
178: _log.info("Subscription "
179: + subscription.getSubscriptionId()
180: + " is stale and will be deleted");
181: }
182:
183: SubscriptionLocalServiceUtil
184: .deleteSubscription(subscription
185: .getSubscriptionId());
186:
187: continue;
188: }
189:
190: try {
191: InternetAddress from = new InternetAddress(fromAddress,
192: fromName);
193:
194: InternetAddress to = new InternetAddress(user
195: .getEmailAddress(), user.getFullName());
196:
197: String curSubject = StringUtil
198: .replace(subject, new String[] {
199: "[$TO_ADDRESS$]", "[$TO_NAME$]" },
200: new String[] { user.getFullName(),
201: user.getEmailAddress() });
202:
203: String curBody = StringUtil.replace(body, new String[] {
204: "[$TO_ADDRESS$]", "[$TO_NAME$]" },
205: new String[] { user.getFullName(),
206: user.getEmailAddress() });
207:
208: InternetAddress replyTo = new InternetAddress(
209: replyToAddress, replyToAddress);
210:
211: MailMessage message = new MailMessage(from, to,
212: curSubject, curBody, false);
213:
214: message.setReplyTo(new InternetAddress[] { replyTo });
215: message.setMessageId(messageId);
216: message.setInReplyTo(inReplyTo);
217:
218: MailServiceUtil.sendEmail(message);
219: } catch (Exception e) {
220: _log.error(e);
221: }
222: }
223: }
224:
225: private static Log _log = LogFactory
226: .getLog(MBMessageConsumer.class);
227:
228: }
|