1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53 address.setUser(null);
54
55
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
67 throw NestedException.wrap(e);
68 }
69
70
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 }