/*
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.controller.hotel.HotelSimpleFormController;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
*
*
*/
public class BookingSearchController extends HotelSimpleFormController {
private IBookingManager bookingManager;
private ICustomerManager customerManager;
/**
* @throws
* @return
*/
protected Object formBackingObject(HttpServletRequest request) throws Exception {
BookingSearchDTO searchDTO = new BookingSearchDTO();
Object object = super.formBackingObject(request);
List customers = this.searchCustomers(request);
List found = new ArrayList();
Map toSearch = new HashMap();
String type = request.getParameter("type");
if(!"ALL".equals(type)) toSearch.put("type", type);
for (Object customerObject : customers) {
toSearch.put("customer", customerObject);
List<Booking> bookings = (List<Booking>)bookingManager.searchAll(toSearch);
bookingManager.IdentifyCheckinableBookings(bookings);
found.addAll(bookings);
}
String name = request.getParameter("name");
String surname = request.getParameter("surname");
if(!"".equals(name)) searchDTO.setName(name);
if(!"".equals(surname)) searchDTO.setSurname(surname);
if(!"".equals(type)) searchDTO.setType(type);
ArrayList<BookingDTO> foundDTO = new ArrayList<BookingDTO>();
for (Object booking : found) {
BookingDTO bookingDTO = new BookingDTO((Booking) booking);
foundDTO.add(bookingDTO);
}
request.setAttribute("bookings", foundDTO);
return searchDTO;
// return new ModelAndView("redirect:/Booking/list.htm","bookings", foundDTO);
}
/**
*
* @param request
* @return
*/
private List<Customer> searchCustomers(HttpServletRequest request){
Customer customer = new Customer();
String name = request.getParameter("name");
String surname = request.getParameter("surname");
if(!"".equals(name)) customer.setName(name);
if(!"".equals(surname)) customer.setSurname(surname);
return (List) customerManager.searchCustomer(customer);
}
@Resource(name = "bookingManager")
public void setBookingManager(IBookingManager bookingManager){
this.bookingManager = bookingManager;
}
@Resource(name = "customerManager")
public void setCustomerManager(ICustomerManager customerManager) {
this.customerManager = customerManager;
}
}