package com.vst.webapp.action;
import com.vst.model.Diameter;
import com.vst.model.ObjectInspection;
import com.vst.model.Thickness;
import com.vst.service.DiameterManager;
import com.vst.service.GostManager;
import com.vst.service.ThicknessManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: And Lilia
* Date: 13.05.2009
* Time: 18:20:23
* To change this template use File | Settings | File Templates.
*/
public class InsertDiameterController extends BaseFormController {
GostManager gostManager;
DiameterManager diameterManager;
public void setGostManager(GostManager gostManager) {
this.gostManager = gostManager;
}
public void setDiameterManager(DiameterManager diameterManager) {
this.diameterManager = diameterManager;
}
ThicknessManager thicknessManager;
public void setThicknessManager(ThicknessManager thicknessManager) {
this.thicknessManager = thicknessManager;
}
public InsertDiameterController() {
setCommandName("diameter");
setCommandClass(Diameter.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
request.setAttribute("thicknessSelect", thicknessManager.getListThickness());
request.setAttribute("gostSelect", gostManager.getGostList());
if (!isFormSubmission(request)) {
if (request.getParameter("diameterId") != null) {
int id = Integer.parseInt(request.getParameter("diameterId"));
return diameterManager.getDiameterById((Integer) id);
} else {
return new Diameter();
}
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
ModelAndView mav = new ModelAndView(getSuccessView());
if (request.getParameter("cansel") != null) {
return new ModelAndView("redirect:/lookDiameter.html");
}
Diameter diameter = (Diameter) command;
List list = diameter.getThicknessList();
for (int i = 0; i < list.size(); i++) {
list.set(i, thicknessManager.getThicknessById(((Thickness) list.get(i)).getThicknessId()));
}
diameter.setThicknessList(list);
diameter.setGost(gostManager.getGostById(diameter.getGost().getGostId()));
if (diameter.getDiametrId() != null) {
diameterManager.update(diameter);
} else {
diameterManager.insert(diameter);
mav.addObject("diameter", new Diameter());
mav.addObject("result", new Integer(1));
return mav;
}
return new ModelAndView("redirect:/lookDiameter.html");
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
Diameter diameter = (Diameter) command;
ModelAndView mav = new ModelAndView("insertDiameter");
if (request.getParameter("addThickness") != null) {
diameter.getThicknessList().add(new Thickness());
mav.addObject("diameter", diameter);
return mav;
}
if (request.getParameter("deleteThickness") != null) {
diameter.getThicknessList().remove(diameter.getThicknessNumber());
mav.addObject("diameter", diameter);
return mav;
}
return super.processFormSubmission(request,
response,
command,
errors);
}
}