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.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  		// Search for expired game offers
52  		List<OfferedGame> expiredGames = m_offeredGameDao.findExpired(currentTime);
53  		
54  		// expire games and notify users
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          // TODO Make sender and subject configurable
84          msg.setFrom("noreply@promotego.org");
85          msg.setSubject("[PromoteGo] Expired game offer notification");
86          
87          // TODO Use a velocity template and Mime multipart/alternative to send this message.
88          // TODO add text reminding to search for games in your area
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 }