package com.vst.webapp.action;
import com.vst.model.DetailSymbol;
import com.vst.service.ConstructionTypeManager;
import com.vst.service.DefectTypeManager;
import com.vst.service.DefectVarityManager;
import com.vst.service.DetailSymbolManager;
import com.vst.util.FileHelper;
import com.vst.util.ImageUtil;
import com.vst.webapp.util.ObjectPicture;
import org.hibernate.Hibernate;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: Lilia
* Date: 26.11.2009
* Time: 12:01:32
* To change this template use File | Settings | File Templates.
*/
public class DetailSymbolFormController extends BaseFormController {
DetailSymbolManager detailSymbolManager;
DefectTypeManager defectTypeManager;
DefectVarityManager defectVarityManager;
ConstructionTypeManager constructionTypeManager;
public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public void setDefectVarityManager(DefectVarityManager defectVarityManager) {
this.defectVarityManager = defectVarityManager;
}
public void setDefectTypeManager(DefectTypeManager defectTypeManager) {
this.defectTypeManager = defectTypeManager;
}
public void setDetailSymbolManager(DetailSymbolManager detailSymbolManager) {
this.detailSymbolManager = detailSymbolManager;
}
public DetailSymbolFormController() {
setCommandName("detailSymbol");
setCommandClass(DetailSymbol.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
request.setAttribute("defectTypeList", defectTypeManager.getDefectTypes(null));
request.setAttribute("constructionTypeList", constructionTypeManager.getConstructionTypes(null));
if (request.getSession().getAttribute("defectTypeId")!=null){
String id=request.getSession().getAttribute("defectTypeId").toString();
request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(id));
} else{
request.setAttribute("defectVarityList", new ArrayList());
}
if (!isFormSubmission(request)) {
DetailSymbol detailSymbol = null;
if (request.getParameter("symbolId")!=null) {
detailSymbol = detailSymbolManager.getByIdPDetailSymbol(Integer.valueOf(request.getParameter("symbolId")));
} else {
detailSymbol = new DetailSymbol();
}
if (request.getParameter("type")!=null){
detailSymbol.setType(request.getParameter("type"));
request.getSession().setAttribute("type",request.getParameter("type"));
}
if (detailSymbol.getDefectType()!=null && detailSymbol.getDefectType().getDefectTypeId()!=null){
request.getSession().setAttribute("defectTypeId",detailSymbol.getDefectType().getDefectTypeId());
request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(detailSymbol.getDefectType().getDefectTypeId().toString()));
}else{
request.setAttribute("defectVarityList", new ArrayList());
}
if (detailSymbol.getWayToSymbol() == null || detailSymbol.getWayToSymbolImage().equals("")) {
String saveWay = ImageUtil.getUniqueJPEGFile(request);
request.setAttribute("saveWay", saveWay);
detailSymbol.setWayToSymbol(saveWay);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FileHelper.getCurrentPath(request)+detailSymbol.getWayToSymbol()));
out.writeObject(new ObjectPicture());
out.close();
String saveWayImage = "image" + saveWay;
detailSymbol.setWayToSymbolImage(saveWayImage);
request.setAttribute("saveWayImage", saveWayImage);
request.getSession().setAttribute("saveWayImage", saveWayImage);
request.getSession().setAttribute("saveWay", saveWay);
} else {
request.getSession().setAttribute("saveWayImage", detailSymbol.getWayToSymbolImage());
request.getSession().setAttribute("saveWay", detailSymbol.getWayToSymbol());
request.getSession().setAttribute("loadWay", detailSymbol.getWayToSymbol());
}
return detailSymbol;
}
return super.formBackingObject(request); //To change body of overridden methods use File | Settings | File Templates.
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors) throws Exception {
DetailSymbol detailSymbol = (DetailSymbol) command;
File eskizFile = new File(FileHelper.getCurrentPath(request) + detailSymbol.getWayToSymbol());
if (detailSymbol.getWayToSymbol() != null && !detailSymbol.getWayToSymbol().equals("") && eskizFile.exists()) {
detailSymbol.setSymbolBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + detailSymbol.getWayToSymbol())));
}
if (detailSymbol.getType().equals("defect")){
detailSymbol.setConstructionType(null);
}
if (detailSymbol.getType().equals("defect") && detailSymbol.getDefectVarity().getVarityId().longValue()==-1){
detailSymbol.setDefectVarity(null);
}
if (detailSymbol.getType().equals("detail")){
detailSymbol.setDefectVarity(null);
detailSymbol.setDefectType(null);
}
if (detailSymbol.getSymbolId() != null) {
detailSymbolManager.update(detailSymbol);
} else {
detailSymbolManager.insert(detailSymbol);
}
ModelAndView mav = new ModelAndView(getSuccessView());
DetailSymbol detSymbol = new DetailSymbol();
detSymbol.setType(detailSymbol.getType());
String saveWay = ImageUtil.getUniqueJPEGFile(request);
mav.addObject("saveWay", saveWay);
detSymbol.setWayToSymbol(saveWay);
String saveWayImage = "image" + saveWay;
detSymbol.setWayToSymbolImage(saveWayImage);
mav.addObject("saveWayImage", saveWayImage);
mav.addObject("detailSymbol", detSymbol);
mav.addObject("result", new Integer(1));
return mav;
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
errors.printStackTrace();
if (request.getParameter("cancel") != null) {
return new ModelAndView("redirect:detailSymbolList.html?type="+request.getSession().getAttribute("type").toString());
}
return super.processFormSubmission(request, response, command, errors);
}
}