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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.promotego.api.geocoder.GeocodeException;
31  import org.promotego.api.geocoder.service.GeocodeResult;
32  import org.promotego.api.geocoder.service.Geocoder;
33  import org.promotego.beans.Address;
34  import org.promotego.beans.Location;
35  import org.promotego.beans.LocationType;
36  import org.promotego.beans.UserHolder;
37  import org.promotego.commands.LocationCommandBean;
38  import org.promotego.dao.interfaces.LocationDao;
39  import org.promotego.dao.interfaces.LocationTypeDao;
40  import org.promotego.editors.BeanIdEditor;
41  import org.promotego.exceptions.NestedException;
42  import org.promotego.viewbeans.NamedValueBean;
43  import org.springframework.beans.factory.annotation.Required;
44  import org.springframework.transaction.annotation.Transactional;
45  import org.springframework.validation.BindException;
46  import org.springframework.web.bind.ServletRequestDataBinder;
47  import org.springframework.web.servlet.ModelAndView;
48  import org.springframework.web.servlet.mvc.SimpleFormController;
49  
50  public class AddLocationController extends SimpleFormController
51  {
52      protected LocationDao m_locationDao;
53      protected LocationTypeDao m_locationTypeDao;
54      protected UserHolder m_userHolder;
55      protected Geocoder m_geocoder;
56      private List<NamedValueBean> m_hourOptions;
57      private List<NamedValueBean> m_minuteOptions;
58      private NamedValueBean [] m_ampmOptions = { new NamedValueBean("AM", 0), new NamedValueBean("PM", 12*3600) };
59      private String [] m_dayNames = { "Sunday", "Monday", "Tuesday",
60  		"Wednesday", "Thursday", "Friday", "Saturday"
61      };
62      
63      private static final int MINUTE_INCREMENT = 5;
64      
65      public AddLocationController()
66      {
67      	m_hourOptions = new ArrayList<NamedValueBean>(12);
68      	m_hourOptions.add(new NamedValueBean("12", 0));
69      	for (int i=1; i<=11; i++)
70      	{
71      		m_hourOptions.add(new NamedValueBean(Integer.toString(i), i*3600));
72      	}
73      	
74      	m_minuteOptions = new ArrayList<NamedValueBean>(60/MINUTE_INCREMENT);
75      	for (int i=0; i<60; i+=MINUTE_INCREMENT)
76      	{
77      		String minuteString = Integer.toString(i);
78      		if (minuteString.length() == 1)
79      		{
80      			minuteString = "0" + minuteString;
81      		}
82      		m_minuteOptions.add(new NamedValueBean(minuteString, i*60));
83      	}
84      }
85      
86      @Transactional
87      @Override
88      public ModelAndView onSubmit(Object command)
89      {
90          Location location = ((LocationCommandBean)command).getLocation();
91          assert m_userHolder.getUser() != null : "User object may not be null";
92  
93          location.setOwner(m_userHolder.getUser());
94          
95          Address address = location.getAddress();
96          
97          if (address != null)
98          {
99              // TODO consider setting special system user, or just embedding the
100             // address object in the Hibernate sense. --Alf
101             // Make sure the user is null, so this address doesn't show up in any
102             // user's list of addresses.
103             address.setUser(null);
104     
105             org.promotego.api.geocoder.beans.Address geocoderAddress =
106                     new org.promotego.api.geocoder.beans.Address(address.getStreetAddress(), address.getCity(), address.getState(), address.getPostalCode(), address.getCountry());
107             
108             GeocodeResult result;
109             try
110             {
111                 result = m_geocoder.geocode(geocoderAddress);
112             }
113             catch(GeocodeException e)
114             {
115                 // TODO catch exception and render to user
116                 throw NestedException.wrap(e);
117             }
118             
119             // TODO Check geocode precision to see if it matches expected value
120             address.setGeolocation(result.getGeolocation());
121         }
122         
123         m_locationDao.create(location);
124         
125         Map<String, Object> model = new HashMap<String, Object>();
126         model.put("locationId", location.getId());
127         model.put("messageKey", "location.saved");
128         
129         return new ModelAndView(getSuccessView(), model);
130     }
131 
132     @SuppressWarnings("unchecked")
133     @Transactional(readOnly=true)
134     @Override
135     protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception
136     {
137         Map <String,Object>myControlModel;
138         
139         if (controlModel == null)
140         {
141             myControlModel = new HashMap<String,Object>();
142         }
143         else
144         {
145             myControlModel = new HashMap<String,Object>(controlModel);
146         }
147 
148         List<LocationType> locationTypes = m_locationTypeDao.findAll();
149         
150         Collections.sort(locationTypes);
151         
152         myControlModel.put("locationTypes", locationTypes);
153         
154         myControlModel.put("hourOptions", m_hourOptions);
155         myControlModel.put("minuteOptions", m_minuteOptions);
156         myControlModel.put("dayNames", m_dayNames);
157         myControlModel.put("ampmOptions", m_ampmOptions);
158         
159         return super.showForm(request, response, errors, myControlModel);
160     }
161     
162     @Override
163     protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
164     {
165         binder.registerCustomEditor(LocationType.class, new BeanIdEditor<LocationType>(m_locationTypeDao));
166         
167         super.initBinder(request, binder);
168     }
169     
170     @Override
171     public Object formBackingObject(HttpServletRequest request) throws Exception
172     {
173         LocationCommandBean theObject = (LocationCommandBean)createCommand();
174         theObject.setLocation(new Location());
175         theObject.setAddress(new Address());
176         return theObject;
177     }
178 
179     @Required
180     public void setUserHolder(UserHolder theUserHolder)
181     {
182         m_userHolder = theUserHolder;
183     }
184     
185     @Required
186     public void setGeocoder(Geocoder geocoder)
187     {
188         m_geocoder = geocoder;
189     }
190 
191     @Required
192     public void setLocationDao(LocationDao locationDao)
193     {
194         m_locationDao = locationDao;
195     }
196 
197     @Required
198     public void setLocationTypeDao(LocationTypeDao locationTypeDao)
199     {
200         m_locationTypeDao = locationTypeDao;
201     }
202 }