1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.services;
20
21 import java.util.Date;
22 import java.util.List;
23
24 import org.apache.log4j.Logger;
25 import org.promotego.beans.OfferedGame;
26 import org.promotego.dao.interfaces.OfferedGameDao;
27 import org.springframework.beans.factory.annotation.Required;
28 import org.springframework.mail.MailException;
29 import org.springframework.mail.MailSender;
30 import org.springframework.mail.SimpleMailMessage;
31 import org.springframework.transaction.annotation.Transactional;
32
33 /***
34 * @author alf
35 *
36 */
37 public class ExpirationService
38 {
39 private static Logger m_logger = Logger.getLogger(ExpirationService.class);
40
41 private OfferedGameDao m_offeredGameDao;
42 private MailSender m_mailSender;
43
44 private String m_baseUrl;
45
46 @Transactional
47 public void expireGameOffers()
48 {
49 Date currentTime = new Date();
50
51
52 List<OfferedGame> expiredGames = m_offeredGameDao.findExpired(currentTime);
53
54
55 for (OfferedGame offeredGame : expiredGames)
56 {
57 try
58 {
59 sendExpirationNotice(offeredGame);
60 m_offeredGameDao.delete(offeredGame);
61 }
62 catch (MailException me)
63 {
64 m_logger.error("Problem sending expiration notice for offered game: " + offeredGame, me);
65 }
66 catch (Exception e)
67 {
68 m_logger.error("Problem expiring offered game: " + offeredGame, e);
69 }
70 }
71 }
72
73 /***
74 * Send an expiration notice about an offered game.
75 *
76 * @param offeredGame The game offer that is being expired.
77 */
78 private void sendExpirationNotice(OfferedGame offeredGame) throws MailException
79 {
80 SimpleMailMessage msg = new SimpleMailMessage();
81 assert offeredGame.getOfferer() != null : "Offerer may not be null";
82 msg.setTo(offeredGame.getOfferer().getEmailAddress());
83
84 msg.setFrom("noreply@promotego.org");
85 msg.setSubject("[PromoteGo] Expired game offer notification");
86
87
88
89 msg.setText("Your game offer on " + offeredGame.getStartTime()
90 +" has expired. To offer a new game, follow this link: " + m_baseUrl + "/locationsearch.do");
91
92 m_mailSender.send(msg);
93 }
94
95 @Required
96 public void setMailSender(MailSender mailSender)
97 {
98 m_mailSender = mailSender;
99 }
100
101 @Required
102 public void setOfferedGameDao(OfferedGameDao offeredGameDao)
103 {
104 m_offeredGameDao = offeredGameDao;
105 }
106
107 @Required
108 public void setBaseUrl(String baseUrl)
109 {
110 m_baseUrl = baseUrl;
111 }
112 }