View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
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.ScheduledGame;
26  import org.promotego.beans.User;
27  import org.promotego.dao.interfaces.ScheduledGameDao;
28  import org.springframework.beans.factory.annotation.Required;
29  import org.springframework.mail.MailException;
30  import org.springframework.mail.MailSender;
31  import org.springframework.mail.SimpleMailMessage;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  /***
35   * @author alf
36   *
37   */
38  public class ReminderService
39  {
40  	private static Logger m_logger = Logger.getLogger(ReminderService.class);
41  	
42  	private ScheduledGameDao m_scheduledGameDao;
43  	private long m_timeBetweenRuns;
44  	private MailSender m_mailSender;
45  	private String m_baseUrl;
46  	
47  	@Transactional
48  	public void remindScheduledGames()
49  	{
50  		Date currentTime = new Date();
51  		
52  		// Remind for games coming up in the next 3 hours
53  		List<ScheduledGame> threeHourList =
54  			m_scheduledGameDao.getGamesNeedingReminder(currentTime, 3600000*3+m_timeBetweenRuns);
55  		
56  		sendReminders(threeHourList, currentTime);
57  		
58  		// Remind for games coming up in the next 24 hours
59  		List<ScheduledGame> twentyFourHourList =
60  			m_scheduledGameDao.getGamesNeedingReminder(currentTime, 3600000*24+m_timeBetweenRuns);
61  		
62  		sendReminders(twentyFourHourList, currentTime);
63  	}
64  
65  	/***
66  	 * Send reminders to participants of the games in the list.
67  	 * 
68  	 * @param gameList The games for which to send reminders.
69  	 */
70  	private void sendReminders(List<ScheduledGame> gameList, Date currentTime)
71  	{
72  		for (ScheduledGame thisGame : gameList)
73  		{
74  			boolean messageError = false;
75  			try
76  			{
77  				sendReminder(thisGame.getOfferer(), thisGame);
78  			}
79  			catch (MailException me)
80  			{
81  				m_logger.error("Error sending message to " + thisGame.getOfferer(), me);
82  				messageError = true;
83  			}
84  			
85  			try
86  			{
87  				sendReminder(thisGame.getAccepter(), thisGame);
88  			}
89  			catch (MailException me)
90  			{
91  				m_logger.error("Error sending message to " + thisGame.getAccepter(), me);
92  				messageError = true;
93  			}
94  			
95  			if (!messageError)
96  			{
97  				thisGame.setLastReminder(currentTime);
98  				m_scheduledGameDao.update(thisGame);
99  			}
100 		}
101 	}
102 
103 	/***
104 	 * Send a reminder about an upcoming game to the specified user.
105 	 * 
106 	 * @param user The user to send the reminder to.
107 	 * @param scheduledGame The scheduled game to remind the user about.
108 	 */
109 	private void sendReminder(User user, ScheduledGame scheduledGame) throws MailException
110 	{
111 		SimpleMailMessage msg = new SimpleMailMessage();
112         msg.setTo(user.getEmailAddress());
113         // TODO Make sender and subject configurable
114         msg.setFrom("noreply@promotego.org");
115         msg.setSubject("[PromoteGo] Upcoming game reminder");
116         
117         // TODO Use a velocity template and Mime multipart/alternative to send this message.
118         msg.setText("This message is being sent to remind you about an upcoming game on " + scheduledGame.getStartTime() 
119         		+".  To see the details of your scheduled game, follow this link: " + m_baseUrl + "/showscheduledgame.do?scheduledGameId="
120         		+ scheduledGame.getId());
121 
122         m_mailSender.send(msg);
123 	}
124 
125 	@Required
126 	public void setScheduledGameDao(ScheduledGameDao scheduledGameDao)
127 	{
128 		m_scheduledGameDao = scheduledGameDao;
129 	}
130 
131 	@Required
132 	public void setTimeBetweenRuns(long timeBetweenRuns)
133 	{
134 		m_timeBetweenRuns = timeBetweenRuns;
135 	}
136 
137 	@Required
138 	public void setMailSender(MailSender mailSender)
139 	{
140 		m_mailSender = mailSender;
141 	}
142 
143 	@Required
144     public void setBaseUrl(String baseUrl)
145     {
146         m_baseUrl = baseUrl;
147     }
148 }