package com.vst.webapp.action;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.beanutils.BeanUtils;
import com.vst.Constants;
import com.vst.model.Material;
import com.vst.service.MaterialManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MaterialController implements Controller {
private final Log log = LogFactory.getLog(MaterialController.class);
private MaterialManager materialManager = null;
public void setMaterialManager(MaterialManager materialManager) {
this.materialManager = materialManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
}
if (request.getParameter("delete") != null) {
String materialId = request.getParameter("id");
ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
if (materialManager.removeMaterial(materialId)) {
request.setAttribute("message", bundle.getString("material.deleted"));
} else {
request.setAttribute("errormessage", bundle.getString("material.isUsed"));
request.setAttribute("refferenceList", materialManager.getConnectedRefferences(materialId));
}
} catch (Exception e) {
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage", bundle.getString("commonMistake"));
}
}
Material material = new Material();
// populate object with request parameters
BeanUtils.populate(material, request.getParameterMap());
List materials = materialManager.getMaterials(material);
return new ModelAndView("materialList", Constants.MATERIAL_LIST, materials);
}
}