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.ArrayList;
22  import java.util.Collections;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.promotego.api.geocoder.util.DistanceUtil;
33  import org.promotego.beans.Address;
34  import org.promotego.beans.OfferedGame;
35  import org.promotego.beans.UserHolder;
36  import org.promotego.dao.interfaces.AddressDao;
37  import org.promotego.dao.interfaces.OfferedGameDao;
38  import org.promotego.editors.BeanIdEditor;
39  import org.promotego.formbackingbeans.RadiusSearchBean;
40  import org.promotego.helpers.MetadataHelper;
41  import org.promotego.viewbeans.OfferedGameViewBean;
42  import org.springframework.beans.factory.annotation.Required;
43  import org.springframework.validation.BindException;
44  import org.springframework.web.bind.ServletRequestDataBinder;
45  import org.springframework.web.servlet.ModelAndView;
46  import org.springframework.web.servlet.mvc.SimpleFormController;
47  
48  public class FindOfferedGameController extends SimpleFormController
49  {
50      private OfferedGameDao m_offeredGameDao;
51      private UserHolder m_userHolder;
52      private MetadataHelper m_metadataHelper;
53      private AddressDao m_addressDao;
54      
55      @Override
56      public ModelAndView onSubmit(Object command)
57      {
58          RadiusSearchBean radiusSearchBean = (RadiusSearchBean)command;
59          
60          assert m_userHolder.getUser() != null : "User object may not be null";
61          
62          Address address = radiusSearchBean.getAddress();
63          List<OfferedGame> offeredGames = m_offeredGameDao.findNear(
64                  address.getGeolocation(),
65                  radiusSearchBean.getMiles());
66          
67          List<OfferedGameViewBean> offeredGameViewBeans
68                      = new ArrayList<OfferedGameViewBean>(offeredGames.size());
69          
70          Date currentTime = new Date();
71          Iterator<OfferedGame> i = offeredGames.iterator();
72          while (i.hasNext())
73          {
74          	OfferedGame thisOfferedGame = i.next();
75          	if (thisOfferedGame.getOfferer().equals(m_userHolder.getUser())
76          			|| thisOfferedGame.getStartTime().before(currentTime))
77          	{
78          		// Don't display this offered game.  It's either expired or
79          		// it was offered by the current user.
80          		i.remove();
81          		continue;
82          	}
83          	
84              OfferedGameViewBean thisViewBean = new OfferedGameViewBean();
85              thisViewBean.setOfferedGame(thisOfferedGame);
86              double distance = DistanceUtil.calculateDistance(thisOfferedGame.getLocation().getAddress().getGeolocation(), address.getGeolocation());
87              distance = Math.floor(distance*10+0.5)/10.0;
88              thisViewBean.setDistance(distance);
89  
90              offeredGameViewBeans.add(thisViewBean);
91          }
92          
93          // Sort locations by distance
94          Collections.sort(offeredGameViewBeans);
95          
96          Map<String,Object> model = new HashMap<String, Object>();
97          
98          model.put("offeredGames", offeredGameViewBeans);
99          model.put("search", true);
100         
101         return new ModelAndView(getSuccessView(), model);
102     }
103 
104     @SuppressWarnings("unchecked")
105     @Override
106     protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception
107     {
108         // User will provide: Address and search radius, and eventually start
109         // and end dates
110         Map<String,Object> myControlModel = new HashMap<String, Object>();
111         
112         if (controlModel != null)
113         {
114             myControlModel.putAll(controlModel);
115         }
116         
117         assert m_userHolder.getUser() != null : "User object may not be null";
118         
119         myControlModel.put("addresses", m_userHolder.getUser().getAddresses());
120         myControlModel.put("searchRadii", m_metadataHelper.getSearchRadii());
121         
122         return super.showForm(request, response, errors, myControlModel);
123     }
124     
125     @Override
126     protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
127     {
128         binder.registerCustomEditor(Address.class, new BeanIdEditor<Address>(m_addressDao));
129         
130         super.initBinder(request, binder);
131     }
132     
133     @Required
134     public void setUserHolder(UserHolder userHolder)
135     {
136         m_userHolder = userHolder;
137     }
138 
139     @Required
140     public void setOfferedGameDao(OfferedGameDao offeredGameDao)
141     {
142         m_offeredGameDao = offeredGameDao;
143     }
144 
145     @Required
146     public void setMetadataHelper(MetadataHelper metadataHelper)
147     {
148         m_metadataHelper = metadataHelper;
149     }
150 
151     @Required
152     public void setAddressDao(AddressDao addressDao)
153     {
154         m_addressDao = addressDao;
155     }
156 }