1 package org.sourceforge.vlibrary.user.workers;
2
3 import java.util.Locale;
4
5 import org.apache.log4j.Logger;
6 import org.sourceforge.vlibrary.user.domain.Book;
7 import org.sourceforge.vlibrary.user.domain.Reader;
8 import org.springframework.context.support.ResourceBundleMessageSource;
9 import org.springframework.mail.SimpleMailMessage;
10
11
12
13
14
15 public class MailWorker {
16
17 private ResourceBundleMessageSource resourceBundleMessageSource;
18
19
20
21 private static Logger logger =
22 Logger.getLogger(MailWorker.class.getName());
23
24 private MailSender mailSender;
25
26 public void setMailSender( MailSender mailSender ) {
27 this.mailSender = mailSender;
28 }
29
30
31
32 public MailWorker() {
33 super();
34 }
35
36
37
38
39 public void setResourceBundleMessageSource(ResourceBundleMessageSource resourceBundleMessageSource) {
40 this.resourceBundleMessageSource = resourceBundleMessageSource;
41 }
42
43
44
45
46
47
48
49
50
51 public void sendRequestConfirmation(Reader possessor, Reader requestor,
52 Book bk, String locationDescription)
53 throws Exception {
54 SimpleMailMessage message = new SimpleMailMessage();
55 String subject = resourceBundleMessageSource
56 .getMessage("email.request.confirmation.subject", new Object[] {
57 bk.getTitle()
58 }, Locale.US);
59 message.setSubject(subject);
60 String adminAddress = resourceBundleMessageSource
61 .getMessage("email.admin.address", null, Locale.US);
62 message.setFrom(adminAddress);
63 StringBuffer buf2 = new StringBuffer();
64 buf2.append(resourceBundleMessageSource
65 .getMessage("email.request.confirmation.messageLine1",
66 new Object[] {
67 bk.getTitle(),
68 possessor.getFirstName() + " " +
69 possessor.getLastName()
70 }, Locale.US));
71 buf2.append(resourceBundleMessageSource
72 .getMessage("email.request.confirmation.messageLine2", null,
73 Locale.US));
74 buf2.append(resourceBundleMessageSource
75 .getMessage("email.request.messageLine3", null, Locale.US));
76 buf2.append(resourceBundleMessageSource
77 .getMessage("email.request.messageLine4", new Object[] {
78 adminAddress
79 }, Locale.US));
80 message.setText(buf2.toString());
81 message.setTo(requestor.getEmail());
82 mailSender.send(message);
83
84 if (logger.isInfoEnabled()) {
85 logger.info("Request acknowledgement sent to Requestor: " +
86 requestor + " for book: " + bk.getId() + "(" +
87 bk.getTitle() + ")");
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public void sendAcquisitionRequestConfirmation(Reader requestor,
103 Book bk, String locationDescription)
104 throws Exception {
105 SimpleMailMessage message = new SimpleMailMessage();
106 String subject = resourceBundleMessageSource
107 .getMessage("email.request.confirmation.subject", new Object[] {
108 bk.getTitle()
109 }, Locale.US);
110 message.setSubject(subject);
111 String adminAddress = resourceBundleMessageSource
112 .getMessage("email.admin.address", null, Locale.US);
113 message.setFrom(adminAddress);
114 StringBuffer buf2 = new StringBuffer();
115 buf2.append(resourceBundleMessageSource
116 .getMessage("email.request.confirmation.ordered.messageLine1",
117 new Object[] {
118 bk.getTitle(),
119 locationDescription
120 }, Locale.US));
121 buf2.append(resourceBundleMessageSource
122 .getMessage("email.request.confirmation.ordered.messageLine2", null,
123 Locale.US));
124 buf2.append(resourceBundleMessageSource
125 .getMessage("email.request.confirmation.ordered.messageLine3",
126 new Object[] {locationDescription}, Locale.US));
127 buf2.append(resourceBundleMessageSource
128 .getMessage("email.request.messageLine4", new Object[] {
129 adminAddress
130 }, Locale.US));
131 message.setText(buf2.toString());
132 message.setTo(requestor.getEmail());
133 mailSender.send(message);
134
135 if (logger.isInfoEnabled()) {
136 logger.info("Acquisition request acknowledgement sent to Requestor: " +
137 requestor + " for book: " + bk.getId() + "(" +
138 bk.getTitle() + ")" + " at " + locationDescription);
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151 public void sendRequestNotification(Reader possessor,
152 Reader requestor, Book bk, String location)
153 throws Exception {
154 SimpleMailMessage message = new SimpleMailMessage();
155
156 String subject = resourceBundleMessageSource.getMessage( "email.request.subject",
157 new Object[] { bk.getTitle() },
158 Locale.US);
159
160 message.setSubject( subject );
161
162 String adminAddress = resourceBundleMessageSource.getMessage( "email.admin.address",
163 null,
164 Locale.US);
165
166 message.setFrom( adminAddress );
167
168 message.setFrom(adminAddress);
169
170 StringBuffer buf = new StringBuffer();
171
172 buf.append( resourceBundleMessageSource.getMessage( "email.request.messageLine1",
173 new Object[] { requestor.getFirstName()+" "+requestor.getLastName(),
174 bk.getTitle() },
175 Locale.US));
176 buf.append( resourceBundleMessageSource.getMessage( "email.request.messageLine2",
177 null,
178 Locale.US));
179
180 buf.append( resourceBundleMessageSource.getMessage( "email.request.messageLine3",
181 null,
182 Locale.US));
183
184 buf.append( resourceBundleMessageSource.getMessage( "email.request.messageLine4",
185 new Object[] { adminAddress },
186 Locale.US));
187
188 message.setText( buf.toString() );
189
190 message.setTo( possessor.getEmail() );
191
192 mailSender.send(message);
193
194
195 if (logger.isInfoEnabled()) {
196 logger.info("Request message sent to Possessor: " + requestor +
197 " for book: " + bk.getId() + "(" + bk.getTitle() + ") at location "
198 + location);
199 }
200 }
201
202
203
204
205
206
207
208
209
210
211
212 public void sendAcquisitionNotification(Reader requestor, Book bk,
213 String location)
214 throws Exception {
215 SimpleMailMessage message = new SimpleMailMessage();
216 String subject = resourceBundleMessageSource
217 .getMessage("email.request.subject", new Object[] {
218 bk.getTitle()
219 }, Locale.US);
220 message.setSubject(subject);
221 String adminAddress = resourceBundleMessageSource
222 .getMessage("email.admin.address", null, Locale.US);
223 message.setFrom(adminAddress);
224 message.setTo(adminAddress);
225 StringBuffer buf = new StringBuffer();
226 buf.append(resourceBundleMessageSource
227 .getMessage("email.request.acquisition.messageLine1", new Object[] {
228 requestor.getFirstName() + " " + requestor.getLastName(),
229 bk.getTitle() + " (ISBN: " + bk.getIsbn() + ")",
230 location
231 }, Locale.US));
232 message.setText(buf.toString());
233 mailSender.send(message);
234
235
236 if (logger.isInfoEnabled()) {
237 logger.info("Acquisition message sent to admin " +
238 " for book: " + bk.getId() + "(" + bk.getTitle() + ")");
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251 public void sendReturnNotification(Reader rd, Book bk, String locationDescription)
252 throws Exception {
253 SimpleMailMessage message = new SimpleMailMessage();
254
255 String subject = resourceBundleMessageSource.getMessage( "email.return.notification.subject",
256 new Object[] { bk.getTitle(), locationDescription },
257 Locale.US);
258
259 message.setSubject( subject );
260
261 String adminAddress = resourceBundleMessageSource.getMessage( "email.admin.address",
262 null,
263 Locale.US);
264
265 message.setFrom( adminAddress );
266
267 message.setTo( rd.getEmail());
268
269 message.setText("");
270
271 mailSender.send(message);
272
273
274 if (logger.isInfoEnabled()) {
275 logger.info( resourceBundleMessageSource.getMessage( "email.return.notification.sent",
276 new Object[] { rd, message.getSubject() },
277 Locale.US));
278 }
279 }
280
281
282
283
284
285
286
287
288 public void sendDeleteConfirmation(Reader rd, Book bk)
289 throws Exception {
290 SimpleMailMessage message = new SimpleMailMessage();
291
292 String subject = resourceBundleMessageSource.getMessage( "email.delete.confirmation.subject",
293 new Object[] { bk.getTitle() },
294 Locale.US);
295
296 message.setSubject( subject );
297
298 String adminAddress = resourceBundleMessageSource.getMessage( "email.admin.address",
299 null,
300 Locale.US);
301
302 message.setFrom( adminAddress );
303
304 message.setTo( rd.getEmail() );
305
306 message.setText("");
307
308 mailSender.send(message);
309
310
311 if (logger.isInfoEnabled()) {
312 logger.info( resourceBundleMessageSource.getMessage( "email.delete.notification.sent",
313 new Object[] { rd, message.getSubject() },
314 Locale.US));
315 }
316 }
317
318
319
320
321
322
323
324
325
326
327 public void sendCheckoutNotification(Reader requestor,
328 Reader newPossessor, Book bk, String locationDescription)
329 throws Exception {
330 SimpleMailMessage message = new SimpleMailMessage();
331
332 String subject = resourceBundleMessageSource.getMessage( "email.checkout.notification.subject",
333 new Object[] { newPossessor.getFirstName() + " " + newPossessor.getLastName(),
334 bk.getTitle() },
335 Locale.US);
336
337 message.setSubject( subject );
338
339 String adminAddress = resourceBundleMessageSource.getMessage( "email.admin.address",
340 null,
341 Locale.US);
342
343 message.setFrom( adminAddress );
344
345 message.setTo( requestor.getEmail() );
346
347 message.setText( resourceBundleMessageSource.getMessage( "email.checkout.notification.message",
348 null,
349 Locale.US));
350
351 mailSender.send(message);
352
353
354 if (logger.isInfoEnabled()) {
355 logger.info( resourceBundleMessageSource.getMessage( "email.checkout.notification.sent",
356 new Object[] { requestor, message.getSubject() },
357 Locale.US));
358 }
359 }
360
361
362
363
364
365
366
367
368 public void sendNewPasswordConfirmation(Reader rd, String newPassword)
369 throws Exception {
370 SimpleMailMessage message = new SimpleMailMessage();
371
372 String subject = resourceBundleMessageSource.getMessage( "email.forgotPassword.subject",
373 null,
374 Locale.US);
375
376 message.setSubject( subject );
377
378 String adminAddress = resourceBundleMessageSource.getMessage( "email.admin.address",
379 null,
380 Locale.US);
381
382 message.setFrom( adminAddress );
383
384 message.setTo( rd.getEmail() );
385
386 message.setText( resourceBundleMessageSource.getMessage( "email.forgotPassword.messageLine1",
387 new Object[] { rd.getFirstName(), rd.getLastName() },
388 Locale.US) +
389 resourceBundleMessageSource.getMessage( "email.forgotPassword.messageLine2",
390 null,
391 Locale.US) +
392 resourceBundleMessageSource.getMessage( "email.forgotPassword.messageLine3",
393 new Object[] { newPassword },
394 Locale.US) +
395 resourceBundleMessageSource.getMessage( "email.forgotPassword.messageLine4",
396 null,
397 Locale.US) +
398 resourceBundleMessageSource.getMessage( "email.forgotPassword.messageLine5",
399 null,
400 Locale.US)
401 );
402
403 mailSender.send(message);
404
405
406 if (logger.isInfoEnabled()) {
407 logger.info( resourceBundleMessageSource.getMessage( "email.checkout.notification.sent",
408 new Object[] { rd, message.getSubject() },
409 Locale.US));
410 }
411 }
412
413 }