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.Calendar;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.TimeZone;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.promotego.beans.Location;
30  import org.promotego.beans.OfferedGame;
31  import org.promotego.beans.UserHolder;
32  import org.promotego.dao.interfaces.LocationDao;
33  import org.promotego.dao.interfaces.OfferedGameDao;
34  import org.promotego.editors.BeanIdEditor;
35  import org.promotego.formbackingbeans.GameOfferBean;
36  import org.promotego.helpers.DateHelper;
37  import org.promotego.logic.storehours.NullStoreHours;
38  import org.springframework.beans.factory.annotation.Required;
39  import org.springframework.transaction.annotation.Transactional;
40  import org.springframework.validation.BindException;
41  import org.springframework.web.bind.ServletRequestDataBinder;
42  import org.springframework.web.servlet.ModelAndView;
43  import org.springframework.web.servlet.mvc.SimpleFormController;
44  
45  public class OfferGameController extends SimpleFormController
46  {
47      private OfferedGameDao m_offeredGameDao;
48      private LocationDao m_locationDao;
49      private UserHolder m_userHolder;
50      
51      @Transactional
52      @Override
53      public ModelAndView onSubmit(Object command)
54      {
55          GameOfferBean gameOfferBean = (GameOfferBean)command;
56          
57          assert m_userHolder.getUser() != null : "User object may not be null";
58          
59          OfferedGame offeredGame = new OfferedGame();
60          
61          offeredGame.setDuration(gameOfferBean.getDuration());
62          offeredGame.setLocation(gameOfferBean.getLocation());
63          offeredGame.setOfferer(m_userHolder.getUser());
64          
65          offeredGame.setStartTime(gameOfferBean.getStartTime());
66          
67          m_offeredGameDao.create(offeredGame);
68          
69          Map<String,Object> model = new HashMap<String, Object>();
70          
71          model.put("offeredGame", offeredGame);
72          
73          return new ModelAndView(getSuccessView(), model);
74      }
75  
76  	@SuppressWarnings("unchecked")
77      @Override
78      protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception
79      {
80          Map<String,Object> myControlModel = new HashMap<String, Object>();
81          
82          if (controlModel != null)
83          {
84              myControlModel.putAll(controlModel);
85          }
86          
87          myControlModel.put("durations", DateHelper.getDurations());
88          myControlModel.put("years", DateHelper.getYears());
89          myControlModel.put("months", DateHelper.getMonths());
90          myControlModel.put("days", DateHelper.getDays());
91          myControlModel.put("hours", DateHelper.getHours());
92          myControlModel.put("minutes", DateHelper.getMinutes());
93          myControlModel.put("ampm", DateHelper.getAmpm());
94          
95          return super.showForm(request, response, errors, myControlModel);
96      }
97  
98      /***
99       * Generate the GameOfferBean and populate it with the location, if
100      * locationId is present.
101      * 
102      * TODO determine how to use binding and custom property editor to set the location.
103      */
104     @SuppressWarnings("unchecked")
105     @Override
106     protected Object formBackingObject(HttpServletRequest request) throws Exception
107     {
108         GameOfferBean gameOfferBean = new GameOfferBean();
109         
110         String locationIdString = request.getParameter("locationId");
111         
112         if (locationIdString != null)
113         {
114             // Populate the offeredGame's location, if locationId was provided.
115             Location location = m_locationDao.getById(Long.parseLong(locationIdString));
116             gameOfferBean.setLocation(location);
117             if (location.getStoreHours() instanceof NullStoreHours)
118             {
119                 gameOfferBean.setHoursValid(false);
120             }
121             else
122             {
123                 gameOfferBean.setHoursValid(true);
124             }
125         }
126         
127         // TODO use the location's time zone
128         Calendar theCalendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
129         
130         // Calculate how many minutes to roll to the next 15-minute increment
131         int minutesUp = theCalendar.get(Calendar.MINUTE)%15;
132         minutesUp = minutesUp==0?0:15-minutesUp;
133         
134         // Move the minutes forward to the next 15-minute interval
135         theCalendar.add(Calendar.MINUTE, minutesUp);
136         
137         // Move forward one day
138         theCalendar.add(Calendar.DAY_OF_MONTH, 1);
139         
140         // Initialize start time on the form bean
141         gameOfferBean.setStartTime(theCalendar);
142         
143         return gameOfferBean;
144     }
145 
146     @Override
147     protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
148     {
149         binder.registerCustomEditor(Location.class, new BeanIdEditor<Location>(m_locationDao));
150 
151         super.initBinder(request, binder);
152     }
153 
154     @Required
155     public void setUserHolder(UserHolder theUserHolder)
156     {
157         m_userHolder = theUserHolder;
158     }
159 
160     @Required
161     public void setOfferedGameDao(OfferedGameDao offeredGameDao)
162     {
163         m_offeredGameDao = offeredGameDao;
164     }
165 
166     @Required
167     public void setLocationDao(LocationDao locationDao)
168     {
169         m_locationDao = locationDao;
170     }
171 }