/*
Copyright (C) European Community 2008 - Licensed under the EUPL V.1.0 (http://ec.europa.eu/idabc/en/document/6523)
*/
package it.hotel.controller.confirmedBooking;
import it.hotel.controller.booking.DTO.BookingDTO;
import it.hotel.controller.confirmedBooking.DTO.ConfirmedBookingDTO;
import it.hotel.model.CalendarUtils;
import it.hotel.model.booking.Booking;
import it.hotel.model.booking.ConfirmedBooking;
import it.hotel.model.booking.manager.IBookingManager;
import it.hotel.model.report.ConfirmedBookingReportMaker;
import it.hotel.model.service.Service;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
/**
*
*
*/
public class ConfirmedBookingDelegate {
IBookingManager bookingManager;
@Resource(name="bookingManager")
public void setManager(IBookingManager manager) {
this.bookingManager = manager;
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView view(HttpServletRequest req, HttpServletResponse resp){
int id = Integer.parseInt(req.getParameter("id"));
ConfirmedBooking confirmed = bookingManager.getConfirmedBooking(id);
bookingManager.updateConfirmedBookingWithTodaysDateIfNecessary(confirmed);
ConfirmedBookingDTO confirmedBookingDTO = new ConfirmedBookingDTO(confirmed);
return new ModelAndView("hotel.confirmedBooking.view", "confirmedBooking", confirmedBookingDTO);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView print(HttpServletRequest req, HttpServletResponse resp){
HttpSession session = null;
session = req.getSession();
ServletContext context = session.getServletContext();
String path = context.getRealPath("/");
int id = Integer.parseInt(req.getParameter("id"));
ConfirmedBooking confirmedBooking = bookingManager.getConfirmedBooking(id);
bookingManager.updateConfirmedBookingWithTodaysDateIfNecessary(confirmedBooking);
ConfirmedBookingDTO confirmedBookingDTO = new ConfirmedBookingDTO(confirmedBooking);
ConfirmedBookingReportMaker report = new ConfirmedBookingReportMaker(confirmedBooking);
OutputStream out = null;
try {
out = resp.getOutputStream();
report.createReport(path, out);
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView("hotel.confirmedBooking.view", "confirmedBooking", confirmedBookingDTO );
}
/**
* This method allows the check-out of the active booking
* at the end of the vacancy from the back-office.
* @param req
* @param resp
* @return a new ModelAndView whit the list of the bookings.
*/
@SuppressWarnings("finally")
public ModelAndView checkOut(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
try {
bookingManager.checkOut(Integer.parseInt(id));
} catch (Exception e) {
} finally {
ArrayList<Booking> bookings =(ArrayList<Booking>) bookingManager.getAllBookingAndConfirmedBookings();
ArrayList<BookingDTO> bookingsDTO = new ArrayList<BookingDTO>();
for (Booking booking : bookings) {
BookingDTO bookingDTO = new BookingDTO(booking);
bookingsDTO.add(bookingDTO);
}
return new ModelAndView("hotel.booking.list", "bookings", bookingsDTO );
}
}
/**
* This method allows the owner to add services at the customer's bill.
* @param req
* @param resp
* @return
*/
public ModelAndView addService(HttpServletRequest req, HttpServletResponse resp){
int id = Integer.parseInt(req.getParameter("id"));
ConfirmedBooking confirmedBooking = bookingManager.getConfirmedBooking(id);
try{
double cost_double = Double.parseDouble(req.getParameter("cost"));
BigDecimal cost = BigDecimal.valueOf(cost_double);
String description = req.getParameter("description");
Service service = new Service();
service.setCost(cost);
service.setDescription(description);
service.setDateadded(CalendarUtils.GetToday());
confirmedBooking.addServices(service);
bookingManager.updateConfirmedBookingWithTodaysDateIfNecessary(confirmedBooking);
bookingManager.add(confirmedBooking);
}
catch (Exception e){
saveError(req, "Amount must be a double");
}
ConfirmedBookingDTO confirmedBookingDTO = new ConfirmedBookingDTO(confirmedBooking);
return new ModelAndView("hotel.confirmedBooking.view", "confirmedBooking", confirmedBookingDTO );
}
public void saveError(HttpServletRequest request, String msg) {
List errors = (List) request.getAttribute("errors");
if (errors == null) {
errors = new ArrayList();
}
errors.add(msg);
request.setAttribute("errors", errors);
}
}