package com.vst.webapp.action;
import com.vst.model.Photo;
import com.vst.model.PipeLineElement;
import com.vst.model.PipeLineElementDefect;
import com.vst.service.PhotoManager;
import com.vst.service.PipeLineElementDefectManager;
import com.vst.service.PipeLineElementManager;
import com.vst.util.FileHelper;
import com.vst.webapp.util.PageHelper;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 03.07.2009
* Time: 12:14:22
* To change this template use File | Settings | File Templates.
*/
public class PipeLineElementDefectController implements Controller {
PipeLineElementManager pipeLineElementManager;
PipeLineElementDefectManager pipeLineElementDefectManager;
PhotoManager photoManager;
public void setPhotoManager(PhotoManager photoManager) {
this.photoManager = photoManager;
}
public void setPipeLineElementManager(PipeLineElementManager pipeLineElementManager) {
this.pipeLineElementManager = pipeLineElementManager;
}
public void setPipeLineElementDefectManager(PipeLineElementDefectManager pipeLineElementDefectManager) {
this.pipeLineElementDefectManager = pipeLineElementDefectManager;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView("pipeLineElementDefectList");
Integer elementId = Integer.valueOf(httpServletRequest.getParameter("pipeElementId"));
if (httpServletRequest.getParameter("delete") != null) {
Integer deleteId = Integer.valueOf(httpServletRequest.getParameter("delete"));
PipeLineElementDefect pipeLineElementDefect = pipeLineElementDefectManager.getPipeLineElementDefectById(deleteId);
Integer fphotoId = pipeLineElementDefect.getFirstPhoto().getPhotoId();
Integer sphotoId = pipeLineElementDefect.getSecondPhoto().getPhotoId();
PipeLineElement pipeLineElement = pipeLineElementManager.getPipeLineElementById(elementId);
pipeLineElement.getPipeLineElementDefectList().remove(pipeLineElementDefect);
pipeLineElementDefectManager.delete(pipeLineElementDefect);
if (fphotoId != null) {
photoManager.delete(photoManager.getByIdPhoto(fphotoId));
}
if (sphotoId != null) {
photoManager.delete(photoManager.getByIdPhoto(sphotoId));
}
}
List list = pipeLineElementDefectManager.getPipeLineElementDefectByPipeLineElement(elementId, FileHelper.getCurrentPath(httpServletRequest));
for (int i = 0; i < list.size(); i++) {
PipeLineElementDefect pipe = (PipeLineElementDefect) list.get(i);
pipe = pipeLineElementDefectManager.getPipeLineElementDefectById(pipe.getDefectId());
Photo photo = photoManager.getByIdPhoto(pipe.getFirstPhoto().getPhotoId());
if (photo.getPhotoPlob()!= null && photo.getPhotoPlob().length()>0 && !photo.getWayToPhoto().trim().equals("")) {
photoManager.prepareForOpen(photo, FileHelper.getCurrentPath(httpServletRequest));
} else{
photo.setWayToPhoto("no photo");
}
pipe.setFirstPhoto(photo);
photo = photoManager.getByIdPhoto(pipe.getSecondPhoto().getPhotoId());
if (photo.getPhotoPlob()!= null && photo.getPhotoPlob().length()>0 && !photo.getWayToPhoto().trim().equals("")) {
photoManager.prepareForOpen(photo, FileHelper.getCurrentPath(httpServletRequest));
}else{
photo.setWayToPhoto("no photo");
}
pipe.setSecondPhoto(photo);
list.set(i, pipe);
}
modelAndView.addObject("objectId", httpServletRequest.getParameter("objectId"));
modelAndView.addObject("detailType", httpServletRequest.getParameter("detailType"));
modelAndView.addObject("pipeElementId", elementId);
Integer page = new Integer(0);
Integer pageCount;
if (httpServletRequest.getParameter("page") != null) {
page = (Integer) Integer.parseInt(httpServletRequest.getParameter("page"));
}
modelAndView.addObject("defectList", PageHelper.getListByPage(page, list));
pageCount = (Integer) list.size();
if ((pageCount.intValue() % 15) > 0) {
pageCount = (Integer) (1 + (pageCount.intValue() / 15));
} else {
pageCount = (Integer) (pageCount.intValue() / 15);
}
if (pageCount.intValue() == 0) {
pageCount = new Integer(1);
}
modelAndView.addObject("page", page);
modelAndView.addObject("pageCount", pageCount);
return modelAndView;
}
}