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.controllers;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.promotego.beans.OfferedGame;
25  import org.promotego.beans.ScheduledGame;
26  import org.promotego.beans.User;
27  import org.promotego.beans.UserHolder;
28  import org.promotego.dao.interfaces.OfferedGameDao;
29  import org.promotego.dao.interfaces.ScheduledGameDao;
30  import org.springframework.beans.factory.annotation.Required;
31  import org.springframework.mail.MailException;
32  import org.springframework.mail.MailSender;
33  import org.springframework.mail.SimpleMailMessage;
34  import org.springframework.transaction.annotation.Transactional;
35  import org.springframework.web.servlet.ModelAndView;
36  import org.springframework.web.servlet.mvc.AbstractController;
37  
38  public class CancelScheduledGameController extends AbstractController
39  {
40      private ScheduledGameDao m_scheduledGameDao;
41      private OfferedGameDao m_offeredGameDao;
42      private UserHolder m_userHolder;
43      private MailSender m_mailSender;
44      private String m_baseUrl;
45  
46      @Transactional
47      @Override
48      protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
49      {
50          User user = m_userHolder.getUser();
51          assert user != null : "User object may not be null";
52          
53      	Long scheduledGameId = Long.parseLong(request.getParameter("scheduledGameId"));
54          ScheduledGame scheduledGame = m_scheduledGameDao.getById(scheduledGameId);
55          
56          OfferedGame offeredGame = new OfferedGame();
57          
58          if (scheduledGame.getOfferer().getId().equals(user.getId()))
59          {
60              offeredGame.setOfferer(scheduledGame.getAccepter());
61          }
62          else if (scheduledGame.getAccepter().getId().equals(user.getId()))
63          {
64              offeredGame.setOfferer(scheduledGame.getOfferer());
65          }
66          else
67          {
68              throw new IllegalArgumentException("Only offerer or accepter may cancel a scheduled game");
69          }
70          
71          offeredGame.setDuration(scheduledGame.getDuration());
72          offeredGame.setLocation(scheduledGame.getLocation());
73          offeredGame.setStartTime(scheduledGame.getStartTime());
74          
75          m_offeredGameDao.create(offeredGame);
76          m_scheduledGameDao.delete(scheduledGame);
77          
78          sendCancellationNotice(offeredGame);
79  
80          return new ModelAndView("home", "messageKey", "scheduledGame.cancelled");
81      }
82      
83      /***
84       * Send a cancellation notice to the specified user.
85       * 
86       * @param user The user to send the notice to.
87       * @param offeredGame The offered game the user has on the system.
88       */
89      private void sendCancellationNotice(OfferedGame offeredGame) throws MailException
90      {
91          SimpleMailMessage msg = new SimpleMailMessage();
92          msg.setTo(offeredGame.getOfferer().getEmailAddress());
93          // TODO Make sender and subject configurable
94          msg.setFrom("noreply@promotego.org");
95          msg.setSubject("[PromoteGo] Game cancellation notice");
96          
97          // TODO Use a velocity template and Mime multipart/alternative to send this message.
98          msg.setText("This message is being sent to notify you that your game game on " + offeredGame.getStartTime() 
99                  + " has been cancelled by your opponent.  Your game has been automatically converted to a game"
100                 + " offer.  To see the details of your new game offer, follow this link: " + m_baseUrl
101                 + "/showofferedgame.do?offeredGameId="
102                 + offeredGame.getId());
103 
104         m_mailSender.send(msg);
105     }
106     
107     @Required
108     public void setScheduledGameDao(ScheduledGameDao scheduledGameDao)
109     {
110         m_scheduledGameDao = scheduledGameDao;
111     }
112     
113     @Required
114     public void setOfferedGameDao(OfferedGameDao offeredGameDao)
115     {
116         m_offeredGameDao = offeredGameDao;
117     }
118 
119     @Required
120     public void setUserHolder(UserHolder userHolder)
121     {
122         m_userHolder = userHolder;
123     }
124 
125     @Required
126     public void setMailSender(MailSender mailSender)
127     {
128         m_mailSender = mailSender;
129     }
130 
131     @Required
132     public void setBaseUrl(String baseUrl)
133     {
134         m_baseUrl = baseUrl;
135     }
136 }