/*
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.manager.IBookingManager;
import it.hotel.model.customer.Customer;
import it.hotel.model.customer.manager.ICustomerManager;
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 it.hotel.model.typology.Typology;
import it.hotel.model.typology.manager.ITypologyManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
/**
*
*
*/
public class BookingWizardEditController extends AbstractWizardFormController {
private IBookingManager bookingManager;
private IRoomManager roomManager;
private ICustomerManager customerManager;
private IStructureManager structureManager;
private ITypologyManager typologyManager;
private String failurePage;
private Room roomTemp;
/**
* @throws
* @return
*/
protected Map referenceData(HttpServletRequest req,
Object command, Errors errors, int page) throws Exception {
Map map = new HashMap();
String id = req.getParameter("id");
Booking booking = (Booking) bookingManager.get(Integer.parseInt(id));
roomTemp = booking.getRoom();
ArrayList<Structure> hotels = new ArrayList<Structure>();
hotels.add(booking.getStructure());
if(page==0){
ArrayList<Room> rooms = new ArrayList<Room>();
ArrayList<Typology> typologies = new ArrayList<Typology>();
rooms = (ArrayList) roomManager.getRoomsFromStructure(booking.getStructure().getId());
typologies = (ArrayList) typologyManager.getTypologiesFromStructure(booking.getStructure().getId());
map.put("rooms", rooms);
map.put("hotels", hotels);
map.put("typologies", typologies);
}
if(page==1){
map.put("hotels", hotels);
}
if(id != null && !"0".equals(id)){
BookingDTO bookingDTO = new BookingDTO(booking);
map.put("booking", bookingDTO);
Collection vacantRooms = bookingManager.getVacantRoomsForStructure(bookingDTO.getStructure(), bookingDTO.getBeginDate(), bookingDTO.getFinishDate());
vacantRooms.add(roomTemp);
map.put("vacantRooms", vacantRooms );
}
this.setProperties(command);
map.put("errors", errors.getAllErrors());
return map;
}
/**
*
* @param command
*/
private void setProperties(Object command){
BookingDTO booking = (BookingDTO) command;
if(booking.getRoomId() != 0){
Room room = (Room)roomManager.get(booking.getRoomId());
booking.setRoom(room);
}
if(booking.getCustomerId() != 0){
booking.setCustomer((Customer) customerManager.get(booking.getCustomerId()));
}
if(booking.getStructure().getId() != 0){
booking.setStructure((Structure) structureManager.get(booking.getStructure().getId()));
}
}
/**
* @throws
* @return
*/
@Override
protected ModelAndView processFinish(HttpServletRequest arg0,
HttpServletResponse arg1, Object command, BindException arg3)
throws Exception {
this.setProperties(command);
BookingDTO booking = (BookingDTO) command;
Booking tempBooking = (Booking) bookingManager.get(booking.getId());
bookingManager.remove(tempBooking);
GregorianCalendar begindateCalendar = booking.getBeginDate();
GregorianCalendar finishdateCalendar = booking.getFinishDate();
if(!bookingManager.isVacant(booking.getRoom(), begindateCalendar, finishdateCalendar)){
tempBooking.setId(0);
bookingManager.add(tempBooking);
return new ModelAndView(getFailurePage(), "roomNumber", booking.getRoom().getNumber());
}else{
booking.setId(0);
Booking newBooking = createBookingFromBookingDTO(booking);
bookingManager.add(newBooking);
}
return new ModelAndView(getSuccessView());
}
private Booking createBookingFromBookingDTO(BookingDTO bookingDTO){
Booking booking = new Booking();
booking.setType(Booking.TYPE_BOOKING);
booking.setAccomodation(bookingDTO.getAccomodation());
booking.setBeginDate(bookingDTO.getBeginDate());
booking.setFinishDate(bookingDTO.getFinishDate());
booking.setCustomer(bookingDTO.getCustomer());
booking.setCustomerId(bookingDTO.getCustomerId());
booking.setStructure(bookingDTO.getStructure());
booking.setRoom(bookingDTO.getRoom());
booking.setRoomId(bookingDTO.getRoomId());
booking.setId(0);
booking.setCode(bookingDTO.getCode());
return booking;
}
protected void validatePage(Object command, Errors errors, int page, boolean finish) {
getValidator().validate(command, errors);
}
public String getSuccessView() {
return getPages()[getPages().length-1];
}
public String getFailurePage() {
return failurePage;
}
public void setFailurePage(String failurePage) {
this.failurePage = failurePage;
}
@Resource(name = "customerManager")
public void setCustomerManager(ICustomerManager customerManager) {
this.customerManager = customerManager;
}
@Resource(name = "bookingManager")
public void setBookingManager(IBookingManager bookingManager) {
this.bookingManager = bookingManager;
}
@Resource(name = "roomManager")
public void setRoomManager(IRoomManager roomManager) {
this.roomManager = roomManager;
}
@Resource(name = "typologyManager")
public void setTypologyManager(ITypologyManager typologyManager) {
this.typologyManager = typologyManager;
}
@Resource(name = "structureManager")
public void setStructureManager(IStructureManager structureManager) {
this.structureManager = structureManager;
}
}