package com.vst.webapp.action;
import com.vst.model.ObjectInspection;
import com.vst.model.PhotoObjectInspection;
import com.vst.model.PhotoOborudovanie;
import com.vst.service.ObjectInspectionManager;
import com.vst.service.PhotoManager;
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.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: And Lilia
* Date: 06.05.2009
* Time: 18:44:13
* To change this template use File | Settings | File Templates.
*/
public class LookPhotoController implements Controller {
ObjectInspectionManager objectInspectionManager;
PhotoManager photoManager;
public void setPhotoManager(PhotoManager photoManager) {
this.photoManager = photoManager;
}
public void setObjectInspectionManager(ObjectInspectionManager objectInspectionManager) {
this.objectInspectionManager = objectInspectionManager;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mav = new ModelAndView("lookPhoto");
/*Integer page=new Integer(0);
Integer pageCount=new Integer(1);
if (httpServletRequest.getParameter("page")!=null){
page=(Integer)Integer.parseInt(httpServletRequest.getParameter("page"));
}
*/
if (httpServletRequest.getParameter("type") != null && httpServletRequest.getParameter("objectId") != null) {
Integer id = (Integer) Integer.parseInt(httpServletRequest.getParameter("objectId"));
List list = new ArrayList();
ObjectInspection objectInspection = objectInspectionManager.getObjectInspectionBy((Integer) Integer.parseInt(httpServletRequest.getParameter("objectId")));
if (httpServletRequest.getParameter("delete") != null) {
Integer photoId = (Integer) Integer.parseInt(httpServletRequest.getParameter("delete"));
if (httpServletRequest.getParameter("type").equals("oborudovanieList")) {
PhotoOborudovanie p = photoManager.getByIdPhotoOborudovanie(photoId);
objectInspection.getOborudovanieList().remove(p);
objectInspectionManager.updateObjectInspection(objectInspection);
photoManager.delete(p);
} else if (httpServletRequest.getParameter("type").equals("objectInspectionList")) {
PhotoObjectInspection p = photoManager.getByIdPhotoObjectInspection(photoId);
objectInspection.getObjectInspectionList().remove(p);
objectInspectionManager.updateObjectInspection(objectInspection);
photoManager.delete(p);
}
}
Integer page = new Integer(0);
Integer pageCount=new Integer(1);
if (httpServletRequest.getParameter("page") != null) {
page = (Integer) Integer.parseInt(httpServletRequest.getParameter("page"));
}
if (httpServletRequest.getParameter("type").equals("oborudovanieList")) {
//pageCount=(Integer)objectInspectionManager.getObjectInspectionBy(id).getOborudovanieList().size();
// System.out.println("COUNT == "+pageCount +" id== "+id);
list = PageHelper.getListByPage(page,objectInspection.getOborudovanieList()) ;
System.out.println("SIZE = " + list.size());
for (int i = 0; i < list.size(); i++) {
PhotoOborudovanie photo = (PhotoOborudovanie) list.get(i);
photoManager.prepareForOpen(photo, FileHelper.getCurrentPath(httpServletRequest));
}
pageCount = (Integer) objectInspection.getOborudovanieList().size();
} else if (httpServletRequest.getParameter("type").equals("objectInspectionList")) {
// pageCount=(Integer)objectInspectionManager.getObjectInspectionBy(id).getObjectInspectionList().size();
list = PageHelper.getListByPage(page,objectInspection.getObjectInspectionList());
for (int i = 0; i < list.size(); i++) {
PhotoObjectInspection photo = (PhotoObjectInspection) list.get(i);
photoManager.prepareForOpen(photo, FileHelper.getCurrentPath(httpServletRequest));
}
pageCount = (Integer) objectInspection.getObjectInspectionList().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);
}
mav.addObject("page", page);
mav.addObject("pageCount", pageCount);
mav.addObject("type", httpServletRequest.getParameter("type"));
mav.addObject("object", objectInspectionManager.getObjectInspectionBy(id));
}
return mav; //To change body of implemented methods use File | Settings | File Templates.
}
}