package it.hotel.controller.facility;
import it.hotel.controller.locale.ILocaleContainer;
import it.hotel.controller.user.IUserContainer;
import it.hotel.model.facility.Facility;
import it.hotel.model.facility.manager.IFacilityManager;
import it.hotel.model.structure.Structure;
import it.hotel.model.structure.manager.IStructureManager;
import it.hotel.model.user.IUser;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
public class FacilityControllerDelegate {
protected IStructureManager structureManager = null;
protected IUserContainer userContainer = null;
protected IFacilityManager facilityManager = null;
protected ILocaleContainer localeContainer = null;
public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp){
IUser user = userContainer.getUser();
String sid = req.getParameter("id");
if (sid != null && user.getRole().hasPermission("SUPERUSER")){
String locale = req.getParameter("locale");
String oldLocale = localeContainer.getLocale();
if (locale != null){
localeContainer.setLocale(locale);
}
int id = Integer.parseInt(req.getParameter("id"));
Facility facility = (Facility) facilityManager.get(id);
if (locale != null){
facility.setLocale(locale);
}
else{
facility.setLocale(oldLocale);
}
localeContainer.setLocale(oldLocale);
ModelAndView mav = new ModelAndView("hotel.facility.new","facility",facility);
return mav;
}
int hotelId = user.getStructureId();
Collection<Facility> facilities = (Collection<Facility>) facilityManager.getAll();
Collection<Facility> hotelFacilities = ((Structure) structureManager.get(hotelId)).getFacilities();
ModelAndView mvc = new ModelAndView("hotel.facility.edit");
mvc.addObject("allFacilities",facilities);
mvc.addObject("hotelFacilities",hotelFacilities);
return mvc;
}
public ModelAndView update(HttpServletRequest req, HttpServletResponse resp){
IUser user = userContainer.getUser();
int hotelId = user.getStructureId();
String[] codes = req.getParameterValues("facility");
Structure structure = (Structure) structureManager.get(hotelId);
Set<Facility> hotelFacilities = new HashSet<Facility>();
Facility fac = new Facility();
if (codes != null){
for (String code : codes){
fac.setCode(code);
Facility facility = (Facility) facilityManager.searchByExample(fac).iterator().next();
if (facility != null){
hotelFacilities.add(facility);
}
}
}
structure.setFacilities(hotelFacilities);
structureManager.add(structure);
ModelAndView mvc = new ModelAndView("redirect:/Facility/edit.htm");
return mvc;
}
@Resource(name = "userContainer")
public void setUserContainer(IUserContainer userContainer) {
this.userContainer = userContainer;
}
@Resource(name = "localizedFacilityManager")
public void setFacilityManager(IFacilityManager facilityManager) {
this.facilityManager = facilityManager;
}
@Resource(name="localeContainer")
public void setLocaleContainer(ILocaleContainer localeContainer) {
this.localeContainer = localeContainer;
}
@Resource(name="structureRawManager")
public void setStructureManager(IStructureManager structureManager) {
this.structureManager = structureManager;
}
}