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 java.util.Date;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.promotego.beans.OfferedGame;
27  import org.promotego.beans.OfferedGameHolder;
28  import org.promotego.beans.ScheduledGame;
29  import org.promotego.beans.UserHolder;
30  import org.promotego.dao.interfaces.OfferedGameDao;
31  import org.promotego.dao.interfaces.ScheduledGameDao;
32  import org.promotego.editors.BeanIdEditor;
33  import org.springframework.beans.factory.annotation.Required;
34  import org.springframework.mail.MailException;
35  import org.springframework.mail.MailSender;
36  import org.springframework.mail.SimpleMailMessage;
37  import org.springframework.transaction.annotation.Transactional;
38  import org.springframework.validation.BindException;
39  import org.springframework.web.bind.ServletRequestDataBinder;
40  import org.springframework.web.servlet.ModelAndView;
41  import org.springframework.web.servlet.mvc.AbstractCommandController;
42  
43  public class AcceptGameOfferController extends AbstractCommandController
44  {
45      private OfferedGameDao m_offeredGameDao;
46      private ScheduledGameDao m_scheduledGameDao;
47      private UserHolder m_userHolder;
48      private String m_baseUrl;
49      private MailSender m_mailSender;
50  
51      @Transactional
52      @Override
53      protected ModelAndView handle(HttpServletRequest request,
54              HttpServletResponse response, Object command, BindException errors)
55              throws Exception
56      {
57          OfferedGame offeredGame = ((OfferedGameHolder)command).getOfferedGame();
58          
59          assert m_userHolder.getUser() != null : "User may not be null";
60          assert offeredGame != null : "Offered game may not be null";
61          
62          // Return if the offered game is invalid.
63          if (offeredGame == null || offeredGame.getStartTime().before(new Date()))
64          {
65          	return new ModelAndView("redirect:home.do", "errorKey", "gameOffer.expired");
66          }
67          
68          if (m_userHolder.getUser().getId().equals(offeredGame.getOfferer().getId()))
69          {
70              throw new IllegalArgumentException("You may not accept your own game offers");
71          }
72          
73          ScheduledGame scheduledGame = new ScheduledGame(offeredGame);
74          
75          scheduledGame.setAccepter(m_userHolder.getUser());
76          
77          m_offeredGameDao.delete(offeredGame);
78          m_scheduledGameDao.create(scheduledGame);
79          
80          // Send acceptance notice, and set the last reminder time on the scheduledGame.
81          sendAcceptedNotice(scheduledGame);
82          scheduledGame.setLastReminder(new Date());
83          m_scheduledGameDao.update(scheduledGame);
84          
85          return new ModelAndView("showScheduledGame", "scheduledGame", scheduledGame);
86      }
87  
88      /***
89       * Send an acceptance notice to game's offerer.
90       * 
91       * @param scheduledGame The new scheduled game we're notifying about.
92       */
93      private void sendAcceptedNotice(ScheduledGame scheduledGame) throws MailException
94      {
95          SimpleMailMessage msg = new SimpleMailMessage();
96          msg.setTo(scheduledGame.getOfferer().getEmailAddress());
97          // TODO Make sender and subject configurable
98          msg.setFrom("noreply@promotego.org");
99          msg.setSubject("[PromoteGo] Game acceptance notice");
100         
101         // TODO Use a velocity template and Mime multipart/alternative to send this message.
102         msg.setText("This message is being sent to notify you that your game offer on " + scheduledGame.getStartTime() 
103                 + " has been accepted by " + scheduledGame.getAccepter().getUsername() + ".  To see the details of your"
104                 + " game, follow this link: " + m_baseUrl + "/showscheduledgame.do?scheduledGameId="
105                 + scheduledGame.getId());
106 
107         m_mailSender.send(msg);
108     }
109 
110     @Override
111     protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
112     {
113         binder.registerCustomEditor(OfferedGame.class, new BeanIdEditor<OfferedGame>(m_offeredGameDao));
114         
115         super.initBinder(request, binder);
116     }
117 
118     @Required
119     public void setOfferedGameDao(OfferedGameDao offeredGameDao)
120     {
121         m_offeredGameDao = offeredGameDao;
122     }
123 
124     @Required
125     public void setUserHolder(UserHolder userHolder)
126     {
127         m_userHolder = userHolder;
128     }
129 
130     @Required
131     public void setScheduledGameDao(ScheduledGameDao scheduledGameDao)
132     {
133         m_scheduledGameDao = scheduledGameDao;
134     }
135 
136     @Required
137     public void setMailSender(MailSender mailSender)
138     {
139         m_mailSender = mailSender;
140     }
141 
142     @Required
143     public void setBaseUrl(String baseUrl)
144     {
145         m_baseUrl = baseUrl;
146     }
147 }