package it.hotel.controller.photo;
import it.hotel.controller.locale.ILocaleContainer;
import it.hotel.model.DirectoryUtils;
import it.hotel.model.gallery.Gallery;
import it.hotel.model.gallery.manager.IGalleryManager;
import it.hotel.model.photo.Photo;
import it.hotel.model.photo.manager.IPhotoManager;
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 PhotoControllerDelegate {
private IPhotoManager photoManager = null;
private ILocaleContainer localeContainer = null;
private IGalleryManager galleryManager = null;
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) {
String id = req.getParameter("id");
Photo photo = (Photo)photoManager.get(Integer.parseInt(id));
photoManager.remove(Integer.parseInt(id));
HttpSession session = null;
session = req.getSession();
ServletContext context = session.getServletContext();
String path = context.getRealPath("/");
DirectoryUtils.deleteFile(path + photo.getUrl() + "/small/" + photo.getName() + ".jpeg");
DirectoryUtils.deleteFile(path + photo.getUrl() + "/big/" + photo.getName() + ".jpeg");
return new ModelAndView("redirect:/photo/new.htm");
}
/**
*
* @param req
* @param resp
* @return
*/
public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp) {
String id = req.getParameter("id");
String locale = req.getParameter("locale");
String oldLocale = localeContainer.getLocale();
if (id != null){
if (locale != null){
localeContainer.setLocale(locale);
}
Photo photo = (Photo) photoManager.get(Integer.parseInt(id));
if (locale != null){
photo.setLocale(locale);
}
else{
photo.setLocale(oldLocale);
}
localeContainer.setLocale(oldLocale);
ModelAndView mav = new ModelAndView("hotel.photo.new");;
Gallery gallery = (Gallery)galleryManager.get(photo.getGallery().getId());
mav.addObject("photo", photo);
mav.addObject("photos", gallery.getPhotos());
return mav;
}
return new ModelAndView("redirect:/photo/new.htm");
}
public ModelAndView update (HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
String description = req.getParameter("description");
String oldLocale = localeContainer.getLocale();
String locale = req.getParameter("locale");
if (id != null){
if (locale != null){
localeContainer.setLocale(locale);
}
Photo photo = (Photo)photoManager.get(Integer.parseInt(id));
photo.setDescription(description);
photoManager.add(photo);
localeContainer.setLocale(oldLocale);
return new ModelAndView("redirect:/photo/new.htm");
}
return new ModelAndView("redirect:/photo/new.htm");
}
@Resource(name="photoManager")
public void setPhotoManager(IPhotoManager photoManager) {
this.photoManager = photoManager;
}
@Resource(name="localeContainer")
public void setLocaleContainer(ILocaleContainer localeContainer) {
this.localeContainer = localeContainer;
}
@Resource(name="galleryManager")
public void setGalleryManager(IGalleryManager galleryManager) {
this.galleryManager = galleryManager;
}
}