package com.vst.webapp.action;
import com.vst.model.DefectRecomendation;
import com.vst.service.*;
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 DefectRecomendationFormController extends BaseFormController {
private DefectRecomendationManager defectRecomendationManager = null;
private DefectCategoryManager defectCategoryManager = null;
private DefectTypeManager defectTypeManager = null;
private DefectVarityManager defectVarityManager = null;
private ConstructionTypeManager constructionTypeManager = null;
private ReasonNameManager reasonNameManager = null;
private DefectZoneManager defectZoneManager = null;
public void setDefectZoneManager(DefectZoneManager defectZoneManager) {
this.defectZoneManager = defectZoneManager;
}
public void setReasonNameManager(ReasonNameManager reasonNameManager) {
this.reasonNameManager = reasonNameManager;
}
public void setDefectTypeManager(DefectTypeManager defectTypeManager) {
this.defectTypeManager = defectTypeManager;
}
public void setDefectVarityManager(DefectVarityManager defectVarityManager) {
this.defectVarityManager = defectVarityManager;
}
public void setDefectCategoryManager(DefectCategoryManager defectCategoryManager) {
this.defectCategoryManager = defectCategoryManager;
}
public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public void setDefectRecomendationManager(DefectRecomendationManager defectRecomendationManager) {
this.defectRecomendationManager = defectRecomendationManager;
}
public DefectRecomendationFormController() {
setCommandName("defectRecomendation");
setCommandClass(DefectRecomendation.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("constructionTypeList", constructionTypeManager.getConstructionTypes(null));
// request.setAttribute("defectTypeList", defectTypeManager.getDefectTypes(null));
if (request.getAttribute("defectTypeList") == null) {
request.setAttribute("defectTypeList", new ArrayList());
request.setAttribute("noDefectTypeList", "true");
}
if (request.getAttribute("defectZoneList") == null) {
request.setAttribute("defectZoneList", new ArrayList());
request.setAttribute("noDefectZoneList", "true");
}
if (request.getAttribute("defectVarityList") == null) {
request.setAttribute("defectVarityList", new ArrayList());
request.setAttribute("noVarityList", "true");
}
if (request.getAttribute("reasonNameList") == null) {
request.setAttribute("reasonNameList", reasonNameManager.getReasonNames(null));
}
if (request.getAttribute("defectCategoryList") == null) {
request.setAttribute("defectCategoryList", defectCategoryManager.getDefectCategorys(null));
}
if (!isFormSubmission(request)) {
String defectRecomendationId = request.getParameter("defectRecomendationId");
DefectRecomendation defectRecomendation = null;
if (!StringUtils.isEmpty(defectRecomendationId)) {
defectRecomendation = defectRecomendationManager.getDefectRecomendation(defectRecomendationId);
defectRecomendationManager.evict(defectRecomendation);
} else {
defectRecomendation = new DefectRecomendation();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
defectRecomendation.setEdited(true);
}
defectRecomendation.setDocLocation(request.getParameter("docLocation"));
return defectRecomendation;
}
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...");
}
DefectRecomendation defectRecomendation = (DefectRecomendation) command;
boolean isNew = (defectRecomendation.getDefectRecomendationId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
defectRecomendationManager.removeDefectRecomendation(defectRecomendation.getDefectRecomendationId().toString());
saveMessage(request, getText("defectRecomendation.deleted", locale));
} else {
Integer defectCategoryId = defectRecomendation.getDefectCategory().getDefectCategoryId();
if (!defectCategoryId.equals(new Integer(-1))) {
defectRecomendation.setDefectCategory(defectCategoryManager.getDefectCategory(defectCategoryId.toString()));
}
defectRecomendation.setConstructionType(constructionTypeManager.getConstructionType(defectRecomendation.getDefectCategory().getConstructionType().getConstructionTypeId().toString()));
// Integer constructionTypeId = defectRecomendation.getConstructionType().getConstructionTypeId();
// if (!constructionTypeId.equals(new Integer(-1))) {
// defectRecomendation.setConstructionType(constructionTypeManager.getConstructionType(constructionTypeId.toString()));
// }
defectRecomendationManager.saveDefectRecomendation(defectRecomendation);
String key = (isNew) ? "defectRecomendation.added" : "defectRecomendation.updated";
saveMessage(request, getText(key, locale));
if (defectRecomendation.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + defectRecomendation.getDefectRecomendationId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + defectRecomendation.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
DefectRecomendation defectRecomendation = (DefectRecomendation) command;
Integer constructionTypeId = defectRecomendation.getFormConstructionType().getConstructionTypeId();
if (!constructionTypeId.equals(new Integer(-1))) {
request.setAttribute("defectTypeList", defectTypeManager.getDefectTypesByConstructionTypeId(constructionTypeId));
request.setAttribute("defectZoneList", defectZoneManager.getDefectZonesByConstructionType(constructionTypeId.toString()));
}
if (!defectRecomendation.getFormDefectType().getDefectTypeId().equals(new Integer(-1))) {
request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(defectRecomendation.getFormDefectType().getDefectTypeId().toString()));
}
request.setAttribute("defectCategoryList", defectCategoryManager.getDefectCategoryByParameters(defectRecomendation));
request.setAttribute("reasonNameList", reasonNameManager.getReasonNamesByParameters(defectRecomendation));
if (request.getParameter("defectTypeSelect") != null || request.getParameter("attributeChoose") != null) {
request.setAttribute("defectRecomendation", defectRecomendation);
return new ModelAndView("defectRecomendationForm", "defectRecomendation", defectRecomendation);
}
return super.processFormSubmission(request, response, command, errors);
}
protected void onBind
(HttpServletRequest
request, Object
command)
throws Exception {
// if the user is being deleted, turn off validation
if (request.getParameter("delete") != null || request.getParameter("attributeChoose") != null || request.getParameter("defectTypeSelect") != null) {
super.setValidateOnBinding(false);
} else {
super.setValidateOnBinding(true);
}
}
}