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 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
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
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
79 throw NestedException.wrap(e);
80 }
81
82
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 }