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.OfferedGameHolder;
26  import org.promotego.beans.UserHolder;
27  import org.promotego.dao.interfaces.OfferedGameDao;
28  import org.promotego.editors.BeanIdEditor;
29  import org.springframework.beans.factory.annotation.Required;
30  import org.springframework.transaction.annotation.Transactional;
31  import org.springframework.validation.BindException;
32  import org.springframework.web.bind.ServletRequestDataBinder;
33  import org.springframework.web.servlet.ModelAndView;
34  import org.springframework.web.servlet.mvc.AbstractCommandController;
35  
36  public class CancelGameOfferController extends AbstractCommandController
37  {
38      private OfferedGameDao m_offeredGameDao;
39      private UserHolder m_userHolder;
40  
41      @Transactional
42      @Override
43      protected ModelAndView handle(HttpServletRequest request,
44              HttpServletResponse response, Object command, BindException errors)
45              throws Exception
46      {
47          OfferedGame offeredGame = ((OfferedGameHolder)command).getOfferedGame();
48          
49          assert m_userHolder.getUser() != null : "User object may not be null";
50          
51          if (!m_userHolder.getUser().getId().equals(offeredGame.getOfferer().getId()))
52          {
53              throw new IllegalArgumentException("You may only cancel your own game offers");
54          }
55          
56          m_offeredGameDao.delete(offeredGame);
57          
58          return new ModelAndView("redirect:showofferedgames.do", "messageKey", "gameOffer.cancelled");
59      }
60  
61      @Override
62      protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
63      {
64          binder.registerCustomEditor(OfferedGame.class, new BeanIdEditor<OfferedGame>(m_offeredGameDao));
65          
66          super.initBinder(request, binder);
67      }
68  
69      @Required
70      public void setOfferedGameDao(OfferedGameDao offeredGameDao)
71      {
72          m_offeredGameDao = offeredGameDao;
73      }
74  
75      @Required
76      public void setUserHolder(UserHolder userHolder)
77      {
78          m_userHolder = userHolder;
79      }
80  }