1 package booking.controller.services;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import net.sf.json.JSONObject;
7
8 import org.apache.log4j.Logger;
9 import org.hibernate.Session;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.ui.Model;
13 import org.springframework.web.bind.annotation.PathVariable;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestMethod;
16 import org.springframework.web.bind.annotation.RequestParam;
17 import org.springframework.web.bind.annotation.ResponseBody;
18
19 import booking.controller.common.DefaultController;
20 import booking.model.bo.ServicesBO;
21 import booking.model.entity.UserTO;
22 import booking.model.util.HibernateUtil;
23
24 @Controller
25 @RequestMapping("/services")
26 public class ServiceController extends DefaultController {
27
28 private static Logger logger = Logger.getLogger(ServiceController.class);
29
30 @Autowired
31 ServicesBO servicesBO;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @RequestMapping(value = "/reserve/{user}/{start}/{end}/{sport}", method = RequestMethod.POST)
52 public @ResponseBody
53 String reserve(HttpServletRequest request, HttpServletResponse response, @PathVariable("user") String user,
54 @PathVariable("start") String start, @PathVariable("end") String end, @PathVariable("sport") int sport,
55 Model model) {
56
57 logger.debug("reserve(" + user + ", " + start + ", " + end + ", " + sport + ")");
58
59 UserTO userTO = (UserTO) request.getSession().getAttribute("user");
60 if (!userTO.getEmail().equals(user)) {
61
62 } else {
63
64 }
65
66 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_OK)).toString();
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 @RequestMapping(value = "/create_user/{sex}/{alias}/{name}/{surname}/{phone}/{email}/{level}/{password}/{laboralTimesheet}/{nolaboralTimesheet}", method = RequestMethod.POST)
92 public @ResponseBody
93 String createUser(HttpServletRequest request, HttpServletResponse response, @PathVariable("sex") String sex,
94 @PathVariable("alias") String alias, @PathVariable("name") String name,
95 @PathVariable("surname") String surname, @PathVariable("phone") long phone,
96 @PathVariable("email") String email, @PathVariable("level") String level,
97 @PathVariable("password") String password, @PathVariable("laboralTimesheet") String laboralTimesheet,
98 @PathVariable("nolaboralTimesheet") String nolaboralTimesheet, Model model) {
99
100 logger.debug("createUSer(" + sex + ", " + alias + ", " + name + ", " + surname + ", " + phone + ", " + email
101 + ", " + password + ", " + level + ", [" + laboralTimesheet + "],[" + nolaboralTimesheet + "])");
102 ServiceControllerResponse serviceResponse = new ServiceControllerResponse();
103 try {
104
105 servicesBO.createUSer(sex, alias, name, surname, phone, email, password, level, laboralTimesheet,
106 nolaboralTimesheet);
107 serviceResponse.setAction(ServiceControllerResponse.STATUS_OK);
108 } catch (Exception e) {
109 serviceResponse.setAction(ServiceControllerResponse.STATUS_OK);
110 serviceResponse.setError(e.getMessage());
111 }
112
113 return JSONObject.fromObject(serviceResponse).toString();
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 @RequestMapping(value = "/login/{login}/{password}", method = RequestMethod.POST)
133 public @ResponseBody
134 String login(HttpServletRequest request, HttpServletResponse response, @PathVariable("login") String login,
135 @PathVariable("password") String password, Model model) {
136
137 logger.debug("login(" + login + ", " + password + ")");
138 ServiceControllerResponse serviceResponse = new ServiceControllerResponse();
139
140 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
141 UserTO userTO = servicesBO.login(login, password, session);
142 if (userTO != null) {
143 if (userTO.getLoginAttempts() < 0) {
144 serviceResponse.setAction(ServiceControllerResponse.STATUS_KO);
145 serviceResponse.setError("user.bloqued.need.to.reset.password");
146 return JSONObject.fromObject(serviceResponse).toString();
147 } else if (userTO.getLoginAttempts() < 3) {
148 serviceResponse.setAction(ServiceControllerResponse.STATUS_KO);
149 serviceResponse.setError("login.not.valid");
150 return JSONObject.fromObject(serviceResponse).toString();
151 } else {
152 request.getSession().setAttribute("user", userTO);
153 return JSONObject.fromObject(userTO).toString();
154 }
155
156 } else {
157 serviceResponse.setAction(ServiceControllerResponse.STATUS_KO);
158 serviceResponse.setError("login.not.valid");
159 return JSONObject.fromObject(serviceResponse).toString();
160 }
161
162 }
163
164 @RequestMapping(value = "/unlogin/{user}", method = RequestMethod.POST)
165 public @ResponseBody
166 String unlogin(HttpServletRequest request, HttpServletResponse response, @PathVariable("user") String user,
167 Model model) {
168 logger.debug("unlogin(" + user + ")");
169 request.getSession().removeAttribute("user");
170 request.getSession().invalidate();
171 ServiceControllerResponse serviceResponse = new ServiceControllerResponse();
172 serviceResponse.setAction(ServiceControllerResponse.STATUS_OK);
173 return JSONObject.fromObject(serviceResponse).toString();
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 @RequestMapping(value = "/remember_user/{login}/{date}", method = RequestMethod.POST)
192 public @ResponseBody
193 String rememberUser(HttpServletRequest request, HttpServletResponse response, @PathVariable("login") String login,
194 @PathVariable("date") String date, Model model) {
195
196 logger.debug("rememberUser(" + login + ")");
197
198 servicesBO.recoveryPassword(login);
199
200
201 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_OK)).toString();
202 }
203
204 @RequestMapping(value = "/change_password/{login}/{hash}/{password}", method = RequestMethod.POST)
205 public @ResponseBody
206 String changePassword(HttpServletRequest request, HttpServletResponse response,
207 @PathVariable("login") String login, @PathVariable("hash") String hash,
208 @PathVariable("password") String password, Model model) {
209
210 logger.debug("rememberUser(" + login + ")");
211
212 if (servicesBO.changePassword(login, hash, password)) {
213 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_OK)).toString();
214 } else {
215 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_KO)).toString();
216 }
217
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 @RequestMapping(value = "/create_match/{login}", method = RequestMethod.POST)
236 public @ResponseBody
237 String createMatch(HttpServletRequest request, HttpServletResponse response, @PathVariable("login") String login,
238 Model model) {
239
240 logger.debug("createMatch(" + login + ")");
241
242 if (request.getSession().getAttribute("user") == null) {
243
244 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_KO)).toString();
245 }
246
247 return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_KO)).toString();
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 @RequestMapping(value = "/getEvents/{sport}/{user}", method = RequestMethod.GET)
269 public @ResponseBody
270 String getEvents(HttpServletRequest request, HttpServletResponse response, @PathVariable("sport") int sport,
271 @PathVariable("user") String user, @RequestParam String start, @RequestParam String end,
272 @RequestParam String _, Model model) {
273
274 logger.debug("getEvents(" + sport + ", " + start + ", " + end);
275 return servicesBO.getEvents(sport, start, end);
276
277 }
278 }