package com.vst.webapp.action;
import com.vst.model.DetailDurability;
import com.vst.model.PipeLineElement;
import com.vst.service.DetailDurabilityManager;
import com.vst.service.PipeLineElementManager;
import com.vst.util.FileHelper;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 30.06.2009
* Time: 18:38:23
* To change this template use File | Settings | File Templates.
*/
public class DetailDurabilityFormController extends BaseFormController {
PipeLineElementManager pipeLineElementManager;
DetailDurabilityManager detailDurabilityManager;
public PipeLineElementManager getPipeLineElementManager() {
return pipeLineElementManager;
}
public void setPipeLineElementManager(PipeLineElementManager pipeLineElementManager) {
this.pipeLineElementManager = pipeLineElementManager;
}
public void setDetailDurabilityManager(DetailDurabilityManager detailDurabilityManager) {
this.detailDurabilityManager = detailDurabilityManager;
}
public DetailDurabilityFormController() {
setCommandName("detailDurability");
setCommandClass(DetailDurability.class);
}
/* detailDurabilityForm.html?objectId=${objectId}&detailType=${detailType}&pipeElementId=${idElement}&update=${det.durabilityId}*/
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
request.getSession().setAttribute("objectId", request.getParameter("objectId"));
request.getSession().setAttribute("detailType", request.getParameter("detailType"));
request.getSession().setAttribute("pipeElementId", request.getParameter("pipeElementId"));
DetailDurability detailDurability = new DetailDurability();
if (request.getParameter("update") != null) {
Integer id = Integer.valueOf(request.getParameter("update"));
detailDurability = detailDurabilityManager.getDetailDurabilityById(id);
} else {
detailDurability.setDurabilityUniqueNumber(detailDurabilityManager.getNumber(Integer.valueOf(request.getParameter("pipeElementId")), FileHelper.getCurrentPath(request)));
}
return detailDurability;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws IOException, SQLException {
ModelAndView mav = new ModelAndView(getSuccessView());
Integer objectId = Integer.valueOf(String.valueOf(request.getSession().getAttribute("objectId")));
Integer detailType = Integer.valueOf(String.valueOf(request.getSession().getAttribute("detailType")));
Integer pipeElementId = Integer.valueOf(String.valueOf(request.getSession().getAttribute("pipeElementId")));
mav.addObject("objectId", objectId);
mav.addObject("detailType", detailType);
mav.addObject("pipeElementId", pipeElementId);
DetailDurability detailDurability = (DetailDurability) command;
if (detailDurability.getDurabilityId() != null) {
detailDurabilityManager.update(detailDurability);
return new ModelAndView("redirect:/detailDurabilityList.html?objectId=" + objectId + "&detailType=" + detailType + "&pipeElementId=" + pipeElementId);
} else {
detailDurabilityManager.insert(detailDurability);
PipeLineElement pipeLineElement = pipeLineElementManager.getPipeLineElementById(pipeElementId);
pipeLineElement.getDetailDurabilityList().add(detailDurability);
pipeLineElementManager.update(pipeLineElement);
mav.addObject("result", new Integer(1));
}
DetailDurability detail = new DetailDurability();
detail.setDurabilityUniqueNumber(detailDurabilityManager.getNumber(pipeElementId, FileHelper.getCurrentPath(request)));
mav.addObject("detailDurability", detail);
return mav;
}
}