View Javadoc
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  	 * Method that receive a request for make a reservation
36  	 * 
37  	 * @author dpuigdomenec
38  	 * 
39  	 * @param request
40  	 * @param response
41  	 * @param login
42  	 * @param start
43  	 * @param end
44  	 * @param sport
45  	 * @param model
46  	 * @return
47  	 * @preconditions
48  	 * @postconditions
49  	 * @see
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  			// User not authorized
62  		} else {
63  			// User authorized
64  		}
65  
66  		return JSONObject.fromObject(new ServiceControllerResponse(ServiceControllerResponse.STATUS_OK)).toString();
67  	}
68  
69  	/**
70  	 * 
71  	 * Method that receive a request for create a new user
72  	 * 
73  	 * @author dpuigdomenec
74  	 * 
75  	 * @param request
76  	 * @param response
77  	 * @param sex
78  	 * @param alias
79  	 * @param name
80  	 * @param surname
81  	 * @param phone
82  	 * @param email
83  	 * @param password
84  	 * @param level
85  	 * @param model
86  	 * @return
87  	 * @preconditions
88  	 * @postconditions
89  	 * @see
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 	 * Method to authenticate to the system
119 	 * 
120 	 * @author dpuigdomenec
121 	 * 
122 	 * @param request
123 	 * @param response
124 	 * @param user
125 	 * @param password
126 	 * @param model
127 	 * @return
128 	 * @preconditions
129 	 * @postconditions
130 	 * @see
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 	 * Method that receive a request to remember the password for the user
179 	 * 
180 	 * @author dpuigdomenec
181 	 * 
182 	 * @param request
183 	 * @param response
184 	 * @param login
185 	 * @param model
186 	 * @return
187 	 * @preconditions
188 	 * @postconditions
189 	 * @see
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 		// Always return OK
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 	 * Method that receive a request to create a match
223 	 * 
224 	 * @author dpuigdomenec
225 	 * 
226 	 * @param request
227 	 * @param response
228 	 * @param login
229 	 * @param model
230 	 * @return
231 	 * @preconditions
232 	 * @postconditions
233 	 * @see
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 	 * Service to get the events to render in the calendar
253 	 * 
254 	 * @author dpuigdomenec
255 	 * 
256 	 * @param request
257 	 * @param response
258 	 * @param sport
259 	 * @param start
260 	 * @param end
261 	 * @param _
262 	 * @param model
263 	 * @return
264 	 * @preconditions
265 	 * @postconditions
266 	 * @see
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 }