package com.vst.webapp.action;
import com.vst.model.*;
import com.vst.service.ObjectInspectionManager;
import com.vst.service.PhotoManager;
import com.vst.util.FileHelper;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by IntelliJ IDEA.
* User: And Lilia
* Date: 06.05.2009
* Time: 18:43:54
* To change this template use File | Settings | File Templates.
*/
public class InsertPhotoController extends BaseFormController {
PhotoManager photoManager;
ObjectInspectionManager objectInspectionManager;
public void setPhotoManager(PhotoManager photoManager) {
this.photoManager = photoManager;
}
public void setObjectInspectionManager(ObjectInspectionManager objectInspectionManager) {
this.objectInspectionManager = objectInspectionManager;
}
public InsertPhotoController() {
setCommandName("photo");
setCommandClass(Photo.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
request.getSession().setAttribute("type", request.getParameter("type"));
request.getSession().setAttribute("objectId", request.getParameter("objectId"));
if (request.getParameter("type").equals("objectInspectionList")) {
setCommandClass(PhotoObjectInspection.class);
if (request.getParameter("update") != null) {
request.setAttribute("update", request.getParameter("update"));
int id = Integer.parseInt(request.getParameter("update"));
return photoManager.getByIdPhotoObjectInspection((Integer) id);
} else {
return new PhotoObjectInspection();
}
} else if (request.getParameter("type").equals("oborudovanieList")) {
setCommandClass(PhotoOborudovanie.class);
if (request.getParameter("update") != null) {
request.setAttribute("update", request.getParameter("update"));
int id = Integer.parseInt(request.getParameter("update"));
return photoManager.getByIdPhotoOborudovanie((Integer) id);
} else {
return new PhotoOborudovanie();
}
}
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
ModelAndView mav = new ModelAndView(getSuccessView());
Photo photo = (Photo) command;
if (request.getSession().getAttribute("objectId") != null && request.getSession().getAttribute("type") != null) {
Integer id = (Integer) Integer.parseInt(request.getSession().getAttribute("objectId").toString());
ObjectInspection objectInspection = objectInspectionManager.getObjectInspectionBy(id);
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
if (photo.getPhotoId() != null) {
if (request.getSession().getAttribute("type").equals("oborudovanieList")) {
PhotoOborudovanie photoOborudovanie = photoManager.getByIdPhotoOborudovanie(photo.getPhotoId());
photoOborudovanie.setComment(photo.getComment());
photoManager.update(photoOborudovanie, file, FileHelper.getCurrentPath(request));
return new ModelAndView("redirect:/lookPhoto.html?objectId=" + request.getSession().getAttribute("objectId").toString() + "&type=oborudovanieList");
} else if (request.getSession().getAttribute("type").equals("objectInspectionList")) {
PhotoObjectInspection photoObjectInspection = photoManager.getByIdPhotoObjectInspection(photo.getPhotoId());
photoObjectInspection.setComment(photo.getComment());
photoManager.update(photoObjectInspection, file, FileHelper.getCurrentPath(request));
return new ModelAndView("redirect:/lookPhoto.html?objectId=" + request.getSession().getAttribute("objectId").toString() + "&type=objectInspectionList");
}
} else {
if (request.getSession().getAttribute("type").equals("oborudovanieList")) {
PhotoOborudovanie photoOb = photoManager.insertPhotoOborudovanie(file, FileHelper.getCurrentPath(request));
photoOb.setComment(photo.getComment());
objectInspection.getOborudovanieList().add(photoOb);
objectInspectionManager.updateObjectInspection(objectInspection);
mav.addObject("photo", new PhotoOborudovanie());
mav.addObject("result", new Integer(1));
return mav;
} else if (request.getSession().getAttribute("type").equals("objectInspectionList")) {
PhotoObjectInspection photoObjectInspection = photoManager.insertPhotoObjectInspection(file, FileHelper.getCurrentPath(request));
photoObjectInspection.setComment(photo.getComment());
objectInspection.getObjectInspectionList().add(photoObjectInspection);
objectInspectionManager.updateObjectInspection(objectInspection);
mav.addObject("photo", new PhotoObjectInspection());
mav.addObject("result", new Integer(1));
return mav;
}
}
}
mav.addObject("photo", new Photo());
return mav;
}
}