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.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.promotego.api.geocoder.GeocodeException;
27  import org.promotego.api.geocoder.service.GeocodeResult;
28  import org.promotego.beans.Address;
29  import org.promotego.beans.Location;
30  import org.promotego.commands.LocationCommandBean;
31  import org.promotego.exceptions.NestedException;
32  import org.springframework.transaction.annotation.Transactional;
33  import org.springframework.web.servlet.ModelAndView;
34  
35  public class EditLocationController extends AddLocationController
36  {
37      @Transactional
38      @Override
39      public ModelAndView onSubmit(Object command)
40      {
41          Location location = ((LocationCommandBean)command).getLocation();
42          assert m_userHolder.getUser() != null : "User object may not be null";
43          assert location.getOwner().equals(m_userHolder.getUser()) : "Editing user must own the Location object";
44  
45          Address address = location.getAddress();
46          
47          if (address != null)
48          {
49              // TODO consider setting special system user, or just embedding the
50              // address object in the Hibernate sense. --Alf
51              // Make sure the user is null, so this address doesn't show up in any
52              // user's list of addresses.
53              address.setUser(null);
54      
55              // TODO Fix this mess somehow so that we can just pass the address directly to the geocoder.
56              org.promotego.api.geocoder.beans.Address geocoderAddress =
57                      new org.promotego.api.geocoder.beans.Address(address.getStreetAddress(), address.getCity(), address.getState(), address.getPostalCode(), address.getCountry());
58              
59              GeocodeResult result;
60              try
61              {
62                  result = m_geocoder.geocode(geocoderAddress);
63              }
64              catch(GeocodeException e)
65              {
66                  // TODO catch exception and render to user
67                  throw NestedException.wrap(e);
68              }
69              
70              // TODO Check geocode precision to see if it matches expected value
71              address.setGeolocation(result.getGeolocation());
72          }
73          
74          m_locationDao.update(location);
75          
76          Map<String, Object> model = new HashMap<String, Object>();
77          model.put("locationId", location.getId());
78          model.put("messageKey", "location.saved");
79          
80          return new ModelAndView(getSuccessView(), model);
81      }
82      
83      @Override
84      public Object formBackingObject(HttpServletRequest request) throws Exception
85      {
86      	LocationCommandBean retval;
87      	
88          String locationIdString = request.getParameter("locationId");
89          if (locationIdString != null)
90          {
91          	retval = (LocationCommandBean)createCommand();
92          	long locationId = Long.parseLong(locationIdString);
93          	retval.setLocation(m_locationDao.getById(locationId));
94          }
95          else
96          {
97          	retval = (LocationCommandBean)super.formBackingObject(request);
98          }
99          
100         return retval;
101     }
102 }