package com.vst.webapp.action;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.vst.webapp.action.BaseFormController;
import com.vst.model.Material;
import com.vst.service.MaterialManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
public class MaterialFormController extends BaseFormController {
private MaterialManager materialManager = null;
public void setMaterialManager(MaterialManager materialManager) {
this.materialManager = materialManager;
}
public MaterialFormController() {
setCommandName("material");
setCommandClass(Material.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
if (!isFormSubmission(request)) {
String materialId = request.getParameter("materialId");
Material material = null;
if (!StringUtils.isEmpty(materialId)) {
material = materialManager.getMaterial(materialId);
materialManager.evict(material);
} else {
material = new Material();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
material.setEdited(true);
}
material.setDocLocation(request.getParameter("docLocation"));
return material;
}
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...");
}
Material material = (Material) command;
boolean isNew = (material.getMaterialId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
materialManager.removeMaterial(material.getMaterialId().toString());
saveMessage(request, getText("material.deleted", locale));
} else {
materialManager.saveMaterial(material);
String key = (isNew) ? "material.added" : "material.updated";
saveMessage(request, getText(key, locale));
if (material.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + material.getMaterialId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + material.getDocLocation());
}
}