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  
23  import org.promotego.api.geocoder.GeocodeException;
24  import org.promotego.api.geocoder.service.GeocodeResult;
25  import org.promotego.api.geocoder.service.Geocoder;
26  import org.promotego.beans.Address;
27  import org.promotego.beans.UserHolder;
28  import org.promotego.dao.interfaces.AddressDao;
29  import org.promotego.exceptions.NestedException;
30  import org.springframework.beans.factory.annotation.Required;
31  import org.springframework.transaction.annotation.Transactional;
32  import org.springframework.web.servlet.ModelAndView;
33  import org.springframework.web.servlet.mvc.SimpleFormController;
34  
35  public class EditAddressController extends SimpleFormController
36  {
37      private AddressDao m_addressDao;
38      private UserHolder m_userHolder;
39      private Geocoder m_geocoder;
40      
41      @Transactional
42      @Override
43      public ModelAndView onSubmit(Object command)
44      {
45          Address address = (Address)command;
46          assert m_userHolder.getUser() != null : "User object may not be null";
47          
48          boolean newAddress;
49  
50          if (address.getId() != null && address.getId() > -1)
51          {
52              // Enforce correct user
53              if (address.getUser().getId() != m_userHolder.getUser().getId())
54              {
55                  throw new IllegalArgumentException("You may only edit your own addresses");
56              }
57              
58              newAddress = false;
59          }
60          else
61          {
62              // New address: Set current user as owner
63              address.setUser(m_userHolder.getUser());
64              
65              newAddress = true;
66          }
67          
68          org.promotego.api.geocoder.beans.Address geocoderAddress =
69                  new org.promotego.api.geocoder.beans.Address(address.getStreetAddress(), address.getCity(), address.getState(), address.getPostalCode(), address.getCountry());
70          
71          GeocodeResult result;
72          try
73          {
74              result = m_geocoder.geocode(geocoderAddress);
75          }
76          catch(GeocodeException e)
77          {
78              // TODO catch exception and render to user
79              throw NestedException.wrap(e);
80          }
81          
82          // TODO Check geocode precision to see if it matches expected value
83          address.setGeolocation(result.getGeolocation());
84          
85          String messageKey;
86          if (newAddress)
87          {
88              m_addressDao.create(address);
89              messageKey = "address.created";
90          }
91          else
92          {
93              m_addressDao.update(address);
94              messageKey = "address.updated";
95          }
96          
97          return new ModelAndView(getSuccessView(), "messageKey", messageKey);
98      }
99      
100     @Override
101     protected Object formBackingObject(HttpServletRequest request)
102             throws Exception
103     {
104         String addressIdString = request.getParameter("addressId");
105         if (addressIdString != null)
106         {
107             Long addressId = Long.parseLong(addressIdString);
108             
109             Address theAddress = m_addressDao.getById(addressId);
110             
111             assert m_userHolder.getUser() != null : "User object may not be null";
112             
113             if (theAddress.getUser().getId() != m_userHolder.getUser().getId())
114             {
115                 throw new IllegalArgumentException("Users may only edit their own addresses");
116             }
117             
118             return theAddress;
119         }
120         else
121         {
122             return new Address();
123         }
124     }
125 
126     @Required
127     public void setAddressDao(AddressDao addressDao)
128     {
129         m_addressDao = addressDao;
130     }
131     
132     @Required
133     public void setUserHolder(UserHolder theUserHolder)
134     {
135         m_userHolder = theUserHolder;
136     }
137     
138     @Required
139     public void setGeocoder(Geocoder geocoder)
140     {
141         m_geocoder = geocoder;
142     }
143 }