package com.vst.webapp.action;
import com.vst.model.MaterialClassification;
import com.vst.service.MaterialClassificationManager;
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.Locale;
public class MaterialClassificationFormController extends BaseFormController {
private MaterialClassificationManager materialClassificationManager = null;
private MaterialManager materialManager = null;
public void setMaterialManager(MaterialManager materialManager) {
this.materialManager = materialManager;
}
public void setMaterialClassificationManager(MaterialClassificationManager materialClassificationManager) {
this.materialClassificationManager = materialClassificationManager;
}
public MaterialClassificationFormController() {
setCommandName("materialClassification");
setCommandClass(MaterialClassification.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("materialList", materialManager.getMaterials(null));
if (!isFormSubmission(request)) {
String materialClassificationId = request.getParameter("materialClassificationId");
MaterialClassification materialClassification = null;
if (!StringUtils.isEmpty(materialClassificationId)) {
materialClassification = materialClassificationManager.getMaterialClassification(materialClassificationId);
} else {
materialClassification = new MaterialClassification();
}
materialClassification.setDocLocation(request.getParameter("docLocation"));
return materialClassification;
}
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...");
}
MaterialClassification materialClassification = (MaterialClassification) command;
boolean isNew = (materialClassification.getMaterialClassificationId() == null);
Locale locale = request.getLocale();
if (!materialClassification.getMaterial().getMaterialId().equals(new Integer(-1))) {
materialClassification.setMaterial(materialManager.getMaterial(materialClassification.getMaterial().getMaterialId().toString()));
}
materialClassificationManager.saveMaterialClassification(materialClassification);
String key = (isNew) ? "materialClassification.added" : "materialClassification.updated";
saveMessage(request, getText(key, locale));
return new ModelAndView("redirect:" + materialClassification.getDocLocation());
}
}