package com.vst.webapp.action;
import com.vst.model.EquipmentInspection;
import com.vst.service.EquipmentInspectionManager;
import com.vst.util.StringUtil;
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;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 07.07.2009
* Time: 14:52:45
* To change this template use File | Settings | File Templates.
*/
public class EquipmentInspectionFormController extends BaseFormController {
EquipmentInspectionManager equipmentInspectionManager;
public void setEquipmentInspectionManager(EquipmentInspectionManager equipmentInspectionManager) {
this.equipmentInspectionManager = equipmentInspectionManager;
}
public EquipmentInspectionFormController() {
setCommandClass(EquipmentInspection.class);
setCommandName("equipment");
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
EquipmentInspection equipmentInspection = new EquipmentInspection();
if (request.getParameter("equipment") != null) {
Integer id = Integer.valueOf(request.getParameter("equipment"));
equipmentInspection = equipmentInspectionManager.getById(id);
}
return equipmentInspection;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws IOException, SQLException {
ModelAndView mav = new ModelAndView(getSuccessView());
EquipmentInspection equipmentInspection = (EquipmentInspection) command;
if (equipmentInspection.getReviewPeriod() != null) {
Date d = equipmentInspection.getReviewPeriod();
String dateForLook = StringUtil.formatDate(d);
equipmentInspection.setDateForLook(dateForLook);
}
if (equipmentInspection.getEquipmentId() != null) {
equipmentInspectionManager.update(equipmentInspection);
return new ModelAndView("redirect:/equipmentInspectionList.html");
} else {
equipmentInspectionManager.saveEquipmentInspection(equipmentInspection);
mav.addObject("result", new Integer(1));
}
mav.addObject("equipment", new EquipmentInspection());
return mav;
}
}