1
22
23 package com.liferay.portlet.messageboards.service.jms;
24
25 import com.liferay.mail.service.MailServiceUtil;
26 import com.liferay.portal.NoSuchUserException;
27 import com.liferay.portal.kernel.mail.MailMessage;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.model.Subscription;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34 import com.liferay.portlet.messageboards.model.MBCategory;
35 import com.liferay.portlet.messageboards.model.MBThread;
36 import com.liferay.util.CollectionFactory;
37
38 import java.util.List;
39 import java.util.Set;
40
41 import javax.jms.Message;
42 import javax.jms.MessageListener;
43 import javax.jms.ObjectMessage;
44 import javax.jms.Queue;
45 import javax.jms.QueueConnection;
46 import javax.jms.QueueConnectionFactory;
47 import javax.jms.QueueReceiver;
48 import javax.jms.QueueSession;
49 import javax.jms.Session;
50
51 import javax.mail.internet.InternetAddress;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56
62 public class MBMessageConsumer implements MessageListener {
63
64 public void consume() {
65 try {
66 QueueConnectionFactory qcf = MBMessageQCFUtil.getQCF();
67 QueueConnection con = qcf.createQueueConnection();
68
69 QueueSession session = con.createQueueSession(
70 false, Session.AUTO_ACKNOWLEDGE);
71 Queue queue = (Queue)MBMessageQueueUtil.getQueue();
72
73 QueueReceiver subscriber = session.createReceiver(queue);
74
75 subscriber.setMessageListener(this);
76
77 con.start();
78 }
79 catch (Exception e) {
80 _log.error(e);
81 }
82 }
83
84 public void onMessage(Message msg) {
85 try {
86 ObjectMessage objMsg = (ObjectMessage)msg;
87
88 String[] array = (String[])objMsg.getObject();
89
90 _onMessage(array);
91 }
92 catch (Exception e) {
93 _log.error(e);
94 }
95 }
96
97 private void _onMessage(String[] array) throws Exception {
98 long companyId = GetterUtil.getLong(array[0]);
99 long userId = GetterUtil.getLong(array[1]);
100 String[] categoryIds = StringUtil.split(array[2]);
101 String threadId = array[3];
102 String fromName = array[4];
103 String fromAddress = array[5];
104 String subject = array[6];
105 String body = array[7];
106 String replyToAddress = array[8];
107 String messageId = array[9];
108 String inReplyTo = array[10];
109
110 Set sent = CollectionFactory.getHashSet();
111
112
114 List subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
115 companyId, MBThread.class.getName(), GetterUtil.getLong(threadId));
116
117 _sendEmail(
118 userId, fromName, fromAddress, subject, body, subscriptions, sent,
119 replyToAddress, messageId, inReplyTo);
120
121
123 for (int i = 0; i < categoryIds.length; i++) {
124 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
125 companyId, MBCategory.class.getName(),
126 GetterUtil.getLong(categoryIds[i]));
127
128 _sendEmail(
129 userId, fromName, fromAddress, subject, body, subscriptions,
130 sent, replyToAddress, messageId, inReplyTo);
131 }
132 }
133
134 private void _sendEmail(
135 long userId, String fromName, String fromAddress, String subject,
136 String body, List subscriptions, Set sent, String replyToAddress,
137 String messageId, String inReplyTo)
138 throws Exception {
139
140 for (int i = 0; i < subscriptions.size(); i++) {
141 Subscription subscription = (Subscription)subscriptions.get(i);
142
143 if (subscription.getUserId() == userId) {
144 continue;
145 }
146
147 Long subscribedUserId = new Long(subscription.getUserId());
148
149 if (sent.contains(subscribedUserId)) {
150 continue;
151 }
152 else {
153 sent.add(subscribedUserId);
154 }
155
156 User user = null;
157
158 try {
159 user = UserLocalServiceUtil.getUserById(
160 subscription.getUserId());
161 }
162 catch (NoSuchUserException nsue) {
163 SubscriptionLocalServiceUtil.deleteSubscription(
164 subscription.getSubscriptionId());
165
166 continue;
167 }
168
169 try {
170 InternetAddress from = new InternetAddress(
171 fromAddress, fromName);
172
173 InternetAddress to = new InternetAddress(
174 user.getEmailAddress(), user.getFullName());
175
176 String curSubject = StringUtil.replace(
177 subject,
178 new String[] {
179 "[$TO_ADDRESS$]",
180 "[$TO_NAME$]"
181 },
182 new String[] {
183 user.getFullName(),
184 user.getEmailAddress()
185 });
186
187 String curBody = StringUtil.replace(
188 body,
189 new String[] {
190 "[$TO_ADDRESS$]",
191 "[$TO_NAME$]"
192 },
193 new String[] {
194 user.getFullName(),
195 user.getEmailAddress()
196 });
197
198 InternetAddress replyTo = new InternetAddress(
199 replyToAddress, replyToAddress);
200
201 MailMessage message = new MailMessage(
202 from, to, curSubject, curBody, false);
203
204 message.setReplyTo(new InternetAddress[]{replyTo});
205 message.setMessageId(messageId);
206 message.setInReplyTo(inReplyTo);
207
208 MailServiceUtil.sendEmail(message);
209 }
210 catch (Exception e) {
211 _log.error(e);
212 }
213 }
214 }
215
216 private static Log _log = LogFactory.getLog(MBMessageConsumer.class);
217
218 }