package com.vst.webapp.action;
import com.vst.model.Photo;
import com.vst.model.PipeLineElement;
import com.vst.model.PipeLineElementDefect;
import com.vst.service.*;
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;
import java.sql.SQLException;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 03.07.2009
* Time: 12:13:57
* To change this template use File | Settings | File Templates.
*/
public class PipeLineElementDefectFormController extends BaseFormController {
DangerCategoryManager dangerCategoryManager;
DefectVarityManager defectVarityManager;
DefectTypeManager defectTypeManager;
PipeLineElementDefectManager pipeLineElementDefectManager;
PipeLineElementManager pipeLineElementManager;
PhotoManager photoManager;
public void setPhotoManager(PhotoManager photoManager) {
this.photoManager = photoManager;
}
public void setDangerCategoryManager(DangerCategoryManager dangerCategoryManager) {
this.dangerCategoryManager = dangerCategoryManager;
}
public void setDefectVarityManager(DefectVarityManager defectVarityManager) {
this.defectVarityManager = defectVarityManager;
}
public void setDefectTypeManager(DefectTypeManager defectTypeManager) {
this.defectTypeManager = defectTypeManager;
}
public void setPipeLineElementDefectManager(PipeLineElementDefectManager pipeLineElementDefectManager) {
this.pipeLineElementDefectManager = pipeLineElementDefectManager;
}
public void setPipeLineElementManager(PipeLineElementManager pipeLineElementManager) {
this.pipeLineElementManager = pipeLineElementManager;
}
public PipeLineElementDefectFormController() {
setCommandName("pipeLineElementDefect");
setCommandClass(PipeLineElementDefect.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
request.setAttribute("dangerCategoryList", dangerCategoryManager.getDangerCategorys(null));
request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritys(null));
request.setAttribute("defectTypeList", defectTypeManager.getDefectTypes(null));
if (!isFormSubmission(request)) {
request.getSession().setAttribute("objectId", request.getParameter("objectId"));
request.getSession().setAttribute("detailType", request.getParameter("detailType"));
request.getSession().setAttribute("pipeElementId", request.getParameter("pipeElementId"));
PipeLineElementDefect pipeLineElementDefect = new PipeLineElementDefect();
if (request.getParameter("update") != null) {
pipeLineElementDefect = pipeLineElementDefectManager.getPipeLineElementDefectById(Integer.valueOf(request.getParameter("update")));
}
return pipeLineElementDefect;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception, SQLException {
ModelAndView mav = new ModelAndView(getSuccessView());
PipeLineElementDefect pipeLineElementDefect = (PipeLineElementDefect) command;
Integer elementId = Integer.valueOf(request.getSession().getAttribute("pipeElementId").toString());
PipeLineElement pipeLineElement = pipeLineElementManager.getPipeLineElementById(elementId);
pipeLineElementDefect.setDangerCategory(dangerCategoryManager.getDangerCategory(String.valueOf(pipeLineElementDefect.getDangerCategory().getDangerCategoryId())));
pipeLineElementDefect.setDefectType(defectTypeManager.getDefectType(String.valueOf(pipeLineElementDefect.getDefectType().getDefectTypeId())));
if (pipeLineElementDefect.getDefectVarity().getVarityId()!=null && pipeLineElementDefect.getDefectVarity().getVarityId().longValue()!=-1){
pipeLineElementDefect.setDefectVarity(defectVarityManager.getDefectVarity(String.valueOf(pipeLineElementDefect.getDefectVarity().getVarityId())));
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile ffile = (CommonsMultipartFile) multipartRequest.getFile("firstPhoto.file");
CommonsMultipartFile sfile = (CommonsMultipartFile) multipartRequest.getFile("secondPhoto.file");
if (pipeLineElementDefect.getDefectId() != null) {
pipeLineElementDefect = pipeLineElementDefectManager.getPipeLineElementDefectById(pipeLineElementDefect.getDefectId());
Photo photo = photoManager.getByIdPhoto(pipeLineElementDefect.getFirstPhoto().getPhotoId());
photoManager.update(photo, ffile, FileHelper.getCurrentPath(request));
photo = photoManager.getByIdPhoto(pipeLineElementDefect.getSecondPhoto().getPhotoId());
photoManager.update(photo, sfile, FileHelper.getCurrentPath(request));
pipeLineElementDefectManager.update(pipeLineElementDefect);
return new ModelAndView("redirect:/pipeLineElementDefectList.html?objectId=" + request.getParameter("objectId") + "&detailType=" + request.getParameter("detailType") + "&pipeElementId=" + elementId);
} else {
if (ffile != null && ffile.getSize() > 0) {
Photo photo = photoManager.insertPhoto(ffile, FileHelper.getCurrentPath(request));
pipeLineElementDefect.setFirstPhoto(photo);
} else {
Photo photo = new Photo();
photo.setWayToPhoto("no photo");
photoManager.insert(photo);
pipeLineElementDefect.setFirstPhoto(photo);
}
if (sfile != null && sfile.getSize() > 0) {
Photo photo = photoManager.insertPhoto(sfile, FileHelper.getCurrentPath(request));
pipeLineElementDefect.setSecondPhoto(photo);
} else {
Photo photo = new Photo();
photo.setWayToPhoto("no photo");
photoManager.insert(photo);
pipeLineElementDefect.setSecondPhoto(photo);
}
pipeLineElementDefectManager.insert(pipeLineElementDefect);
pipeLineElement.getPipeLineElementDefectList().add(pipeLineElementDefect);
pipeLineElementManager.update(pipeLineElement);
mav.addObject("result", new Integer(1));
}
mav.addObject("objectId", request.getParameter("objectId"));
mav.addObject("detailType", request.getParameter("detailType"));
mav.addObject("pipeElementId", elementId);
mav.addObject("pipeLineElementDefect", new PipeLineElementDefect());
return mav;
}
}