package com.vst.webapp.action;
import com.vst.model.ConstructionType;
import com.vst.model.DefectZone;
import com.vst.service.ConstructionTypeManager;
import com.vst.service.DefectZoneManager;
import com.vst.service.MaterialManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ConstructionTypeFormController extends BaseFormController {
private ConstructionTypeManager constructionTypeManager = null;
private MaterialManager materialManager = null;
private DefectZoneManager defectZoneManager = null;
public void setDefectZoneManager(DefectZoneManager defectZoneManager) {
this.defectZoneManager = defectZoneManager;
}
public void setMaterialManager(MaterialManager materialManager) {
this.materialManager = materialManager;
}
public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public ConstructionTypeFormController() {
setCommandName("constructionType");
setCommandClass(ConstructionType.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("materialList", materialManager.getMaterials(null));
request.setAttribute("defectZoneList", defectZoneManager.getDefectZones(null));
if (!isFormSubmission(request)) {
String constructionTypeId = request.getParameter("constructionTypeId");
ConstructionType constructionType = null;
if (!StringUtils.isEmpty(constructionTypeId)) {
constructionType = constructionTypeManager.getConstructionType(constructionTypeId);
constructionTypeManager.evict(constructionType);
} else {
constructionType = new ConstructionType();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
constructionType.setEdited(true);
}
constructionType.setDocLocation(request.getParameter("docLocation"));
return constructionType;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'onSubmit' method...");
}
ConstructionType constructionType = (ConstructionType) command;
boolean isNew = (constructionType.getConstructionTypeId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
constructionTypeManager.removeConstructionType(constructionType.getConstructionTypeId().toString());
saveMessage(request, getText("constructionType.deleted", locale));
} else {
Integer materialId = constructionType.getMaterial().getMaterialId();
if (!materialId.equals(new Integer(-1))) {
constructionType.setMaterial(materialManager.getMaterial(materialId.toString()));
}
List defectZoneListFromForm = constructionType.getDefectZones();
List defectZoneListToBase = new ArrayList();
for (int i = 0; i < defectZoneListFromForm.size(); i++) {
DefectZone defectZone = (DefectZone) defectZoneListFromForm.get(i);
Integer defectZoneId = defectZone.getDefectZoneId();
if (!defectZoneId.equals(new Integer(-1))) {
defectZoneListToBase.add(defectZoneManager.getDefectZone(defectZoneId.toString()));
}
}
constructionType.setDefectZones(defectZoneListToBase);
constructionTypeManager.saveConstructionType(constructionType);
String key = (isNew) ? "constructionType.added" : "constructionType.updated";
saveMessage(request, getText(key, locale));
if (constructionType.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + constructionType.getConstructionTypeId() + "&fieldId=" + request.getParameter("fieldId"));
}
return new ModelAndView("redirect:" + constructionType.getDocLocation());
}
return new ModelAndView("redirect:" + constructionType.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
ConstructionType constructionType = (ConstructionType) command;
if (request.getParameter("addDefectZone") != null) {
constructionType.getDefectZones().add(new DefectZone());
return new ModelAndView("constructionTypeForm", "constructionType", constructionType);
}
if (request.getParameter("deleteDefectZone") != null) {
constructionType.getDefectZones().remove(constructionType.getDefectZoneNumber());
return new ModelAndView("constructionTypeForm", "constructionType", constructionType);
}
return super.processFormSubmission(request,
response,
command,
errors);
}
}