/*
Copyright (C) European Community 2008 - Licensed under the EUPL V.1.0 (http://ec.europa.eu/idabc/en/document/6523)
*/
package it.hotel.controller.booking;
import it.hotel.controller.booking.DTO.BookingDTO;
import it.hotel.model.booking.Booking;
import it.hotel.model.booking.UnconfirmedBooking;
import it.hotel.model.booking.manager.IBookingManager;
import it.hotel.model.customer.Customer;
import it.hotel.model.customer.manager.ICustomerManager;
import it.hotel.model.mail.Mail;
import it.hotel.model.room.Room;
import it.hotel.model.room.manager.IRoomManager;
import it.hotel.model.structure.Structure;
import it.hotel.model.structure.manager.IStructureManager;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.mail.MailException;
import org.springframework.web.servlet.ModelAndView;
/**
*
*
*/
public class BookingControllerDelegate {
private IBookingManager bookingManager;
private IRoomManager roomManager;
private IStructureManager structureManager;
private ICustomerManager customerManager;
private Mail mail;
public ModelAndView paypal(){
return new ModelAndView("");
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView list(HttpServletRequest req, HttpServletResponse resp){
ArrayList<Booking> bookings =(ArrayList<Booking>) bookingManager.getAllBookingAndConfirmedBookings();
bookingManager.IdentifyCheckinableBookings(bookings);
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);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView listCompleted (HttpServletRequest req, HttpServletResponse resp){
ArrayList<Booking> bookings =(ArrayList<Booking>) bookingManager.getAllCompletedBookings();
ArrayList<BookingDTO> bookingsDTO = new ArrayList<BookingDTO>();
for (Booking booking : bookings) {
BookingDTO bookingDTO = new BookingDTO(booking);
bookingsDTO.add(bookingDTO);
}
return new ModelAndView("hotel.booking.listCompleted", "bookings", bookingsDTO);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView listConfirmed (HttpServletRequest req, HttpServletResponse resp){
ArrayList<Booking> bookings =(ArrayList<Booking>) bookingManager.getAllConfirmedBookings();
ArrayList<BookingDTO> bookingsDTO = new ArrayList<BookingDTO>();
for (Booking booking : bookings) {
BookingDTO bookingDTO = new BookingDTO(booking);
bookingsDTO.add(bookingDTO);
}
return new ModelAndView("hotel.booking.listConfirmed", "bookings", bookingsDTO);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView listUnconfirmed (HttpServletRequest req, HttpServletResponse resp){
ArrayList<Booking> bookings =(ArrayList<Booking>) bookingManager.getAllUnconfirmedBookings();
ArrayList<BookingDTO> bookingsDTO = new ArrayList<BookingDTO>();
for (Booking booking : bookings) {
BookingDTO bookingDTO = new BookingDTO(booking);
bookingsDTO.add(bookingDTO);
}
return new ModelAndView("hotel.booking.listUnConfirmed", "bookings", bookingsDTO);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp){
try {
int id = Integer.parseInt(req.getParameter("id"));
Booking booking=(Booking) bookingManager.get(id);
if(booking.getType().compareTo("UNCONFIRMED")==0)
{
bookingManager.remove(id);
return this.listUnconfirmed(req, resp);
}
bookingManager.remove(id);
return this.list(req, resp);
} catch(Exception e) {
return new ModelAndView("hotel.404");
}
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp){
int id = 0;
try{
id = Integer.parseInt(req.getParameter("id"));
} catch (Exception e){
return new ModelAndView("hotel.404");
}
Booking booking = (Booking) bookingManager.get(id);
if (booking == null)
{
return this.list(req, resp);
}
req.setAttribute("booking", booking);
return new ModelAndView("hotel.booking.new");
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView editCheckin(HttpServletRequest req, HttpServletResponse resp) {
String id= req.getParameter("id");
if(id != null){
Booking booking = (Booking)bookingManager.get(Integer.parseInt(id));
Customer customer = booking.getCustomer();
BookingDTO bookingDTO = new BookingDTO(booking);
req.setAttribute("booking", bookingDTO);
ArrayList<Structure> hotels = new ArrayList<Structure>();
hotels.add(booking.getStructure());
req.setAttribute("hotels", hotels);
return new ModelAndView("hotel.booking.BookingCheckinNew", "customer", customer);
}
return list(req, resp);
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView view(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
if(id != null){
req.setAttribute("booking", bookingManager.get(Integer.parseInt(id)));
return new ModelAndView("hotel.booking.view");
}
return this.list(req, resp);
}
/**
*
* @param req
* @param resp
* @return
*/
@SuppressWarnings("finally")
public ModelAndView checkIn(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
try {
bookingManager.checkIn(Integer.parseInt(id));
} catch (Exception e) {
} finally {
return this.list(req, resp);
}
}
/**
*
* @param req
* @param resp
* @return
* @throws Exception
*/
@SuppressWarnings("finally")
public ModelAndView enableBooking(HttpServletRequest req, HttpServletResponse resp) throws Exception{
int id = Integer.parseInt(req.getParameter("id"));
UnconfirmedBooking unConfirmed= (UnconfirmedBooking) bookingManager.get(id);
GregorianCalendar beginDate= unConfirmed.getBeginDate();
GregorianCalendar finishDate=unConfirmed.getFinishDate();
Room room=unConfirmed.getRoom();
Booking booking=new Booking(unConfirmed);
if(bookingManager.isRoomVacant(beginDate, finishDate, room)){
bookingManager.add(booking);
try {
mail.sendMailEnable(booking);
} catch (MailException e) {
System.out.println(e.getMessage());
bookingManager.remove(booking.getId());
throw new Exception ();
}
bookingManager.remove(id);
}
else{
id=unConfirmed.getId();
try {
mail.sendMailEnable(booking);
} catch (MailException e) {
System.out.println(e.getMessage());
throw new Exception ();
}
return new ModelAndView("redirect:/Booking/edit.htm","id", unConfirmed.getId());
}
return this.listUnconfirmed(req, resp);
}
@Resource(name = "roomManager")
public void setRoomManager(IRoomManager roomManager) {
this.roomManager = roomManager;
}
@Resource(name = "bookingManager")
public void setBookingManager(IBookingManager bookingManager) {
this.bookingManager = bookingManager;
}
@Resource(name = "customerManager")
public void setCustomerManager(ICustomerManager customerManager) {
this.customerManager = customerManager;
}
@Resource(name = "mail")
public void setMail(Mail mail) {
this.mail = mail;
}
public Mail getMail() {
return mail;
}
@Resource(name = "structureManager")
public void setStructureManager(IStructureManager structureManager) {
this.structureManager = structureManager;
}
}