View Javadoc

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   * @version $Revision$ $Date$
13   *
14   */
15  public class MailWorker {
16      /** Message Resources */
17      private ResourceBundleMessageSource resourceBundleMessageSource;
18  
19  
20      /** log4j category */
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       * Used for Spring Dependency Injection
38       *****************************************************************************/
39      public void setResourceBundleMessageSource(ResourceBundleMessageSource resourceBundleMessageSource) {
40          this.resourceBundleMessageSource = resourceBundleMessageSource;
41      }
42  
43      /**
44       * Sends request confirmation to requestor
45       *
46       * @param possessor = the current possessor of the book
47       * @param requestor = the requestor of the book
48       * @param bk = the book being requested
49       * @exception Exception
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          // log send
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       * Sends request confirmation to requestor for a book that is not available
93       * in the location of the order.  Tells the requestor that the admin has
94       * been notified and the requestor will be notified if / when the book has
95       * been provisioned and is available for checkout at the location.
96       *
97       * @param requestor = the requestor of the book
98       * @param bk the book being requested
99       * @param locationDescription description of the location
100      * @exception Exception
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         // log send
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      * Sends request notification to current possessor
144      *
145      * @param possessor the current possessor of the book
146      * @param requestor the requester of the book
147      * @param bk the book being requested
148      * @param location location of the request
149      * @exception Exception
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         // log send
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      * Sends notification to the admin email that a request has been made for a book
204      * that is not available (checked into or checked out from) the location where the
205      * request has been made.
206      *
207      * @param requestor the requester of the book
208      * @param bk the book being requested
209      * @param location location of the request
210      * @exception Exception
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         // log send
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      * Sends return notification
245      *
246      * @param bk id of the book that has been returned
247      * @param rd id of the reader to receive notification
248      * @param locationDescription human-readable description of the dropoff location where the book was returned
249      * @exception Exception
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         // log send
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      * Sends deleted request notification
283      *
284      * @param bk = the id of the book
285      * @param rd = the id of the reader who has deleted the request
286      * @exception Exception
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         // log send
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      * Sends checkout notification
320      *
321      * @param newPossessor the new possessor of the book
322      * @param requestor the requester of the book
323      * @param bk the book being requested
324      * @param locationDescription the location the book was checked out from
325      * @exception Exception
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         // log send
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      * Sends new password generated as a result of Forgot Your Password action
363      *
364      * @param rd = the id of the reader to receive notification
365      * @param newPassword = the new password that was generated
366      * @exception Exception
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         // log send
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 }