String aDate = request.getParameter("adate");
String rDate = request.getParameter("rdate");
String updateReservation = request.getParameter("updateReservation");
/* instance of database */
DAO dao = DAO.getInstance();
/*current date*/
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
/*useful to extract the current user*/
HttpSession session = request.getSession(false);
String username = (String) session.getAttribute("username");
/* if user press the Update Reservation Button */
if (updateReservation != null) {
/* all fields must be filled && and reservation id must match with the one from DB*/
if (reservationId.isEmpty() || aDate.isEmpty() || rDate.isEmpty()
|| dao.ofy().find(HotelReservation.class, reservationId) == null) {
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"red\">"
+ "Invalid Reservation id! <br/>"
+ "</font>");
RequestDispatcher rd = request.getRequestDispatcher("reservations.jsp");
rd.forward(request, response);
/* The date must be in a logical order*/
} else if ((aDate.compareTo(rDate) > 0)
|| (aDate.compareTo(dateFormat.format(date).toString()) <= 0)) {
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"red\">"
+ "Wrong Date! <br/>"
+ "</font>");
RequestDispatcher rd = request.getRequestDispatcher("reservations.jsp");
rd.forward(request, response);
} else {
/*
* PUT method
*/
/* extract from the DB the reservation that the user want to update*/
HotelReservation HR = dao.ofy().find(HotelReservation.class, reservationId);
/*create the query link*/
String link = "http://mdwhotel.appspot.com/api/v1/reservation/?res_id=";
link += reservationId + "&start=" + aDate + "&end=" + rDate + "&email=" + HR.getEmail();
Client client = Client.create();
WebResource webResource = client.resource(link);
webResource.accept("application/xml");
/*invoke the call*/
String resp = webResource.put(String.class);
/* if the application sends me back an XML who contains this message
* means that they don't have available rooms between thhose dates
* and I'm not deleting the reservation that he made before
*/
if (!resp.contains("Sorry - the room is not free in these days")) {
/*delete the last reservation because they will send to me anotherone*/
dao.ofy().delete(HR);
/* parse the XML file that I receive from them*/
StringToXML(resp);
/* set the new information of the reservation*/
HR.setStart(Rstart);
HR.setEnd(Rend);
HR.setDesc(Rdesc);
HR.setEmail(Remail);
HR.setId(Rid);
HR.setName(Rname);
HR.setRoomId(RroomId);
HR.setNrOfPers(RnrOfPers);
dao.put(HR);
} else {
/* if they don't have available rooms between those date
* a message retrun
*/
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"red\">"
+ "No aviable rooms!<br/>"
+ "</font>");
}
/* successful */
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"green\">"
+ "Reservation successfully updated!<br/>"
+ "</font>");
RequestDispatcher rd = request.getRequestDispatcher("reservations.jsp");
rd.forward(request, response);
response.sendRedirect("reservations.jsp");
}
/* if delete reservation button is pressed*/
/* verify if the id is correct */
} else if (reservationId.isEmpty()
|| dao.ofy().find(HotelReservation.class, reservationId) == null) {
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"red\">"
+ "Invalid Reservation id! <br/>"
+ "</font>");
RequestDispatcher rd = request.getRequestDispatcher("reservations.jsp");
rd.forward(request, response);
} else {
/*
* DELETE method
*/
/* extract the desired reservation from DB*/
HotelReservation HR = dao.ofy().find(HotelReservation.class, reservationId);
/* create the query link */
String link = "http://mdwhotel.appspot.com/api/v1/reservation/?res_id=";
link += reservationId + "&email=" + HR.getEmail();
Client client = Client.create();
WebResource webResource = client.resource(link);
webResource.accept("application/xml");
/*invoke method */
String resp = webResource.delete(String.class);
/*delete the reservation from the DB*/
dao.ofy().delete(HotelReservation.class, reservationId);
/* successful */
request.setAttribute("errorMessage", "<font size=\"2\" "
+ "face=\"arial\" color=\"green\">"
+ "Reservation successfully deleted!<br/>"
+ "</font>");