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.Reason;
import com.vst.service.ReasonManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ReasonController implements Controller {
private final Log log = LogFactory.getLog(ReasonController.class);
private ReasonManager reasonManager = null;
public void setReasonManager(ReasonManager reasonManager) {
this.reasonManager = reasonManager;
}
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");
}
String pageNumber = request.getParameter("d-2636748-p");
if (pageNumber != null) {
request.setAttribute("pageNumber", "d-2636748-p=" + pageNumber);
}
if (request.getParameter("delete") != null) {
String reasonId = request.getParameter("id");
ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
if (reasonManager.removeReason(reasonId)) {
request.setAttribute("message", bundle.getString("reason.deleted"));
} else {
request.setAttribute("errormessage", bundle.getString("reason.isUsed"));
request.setAttribute("refferenceList", reasonManager.getConnectedRefferences(reasonId));
}
} catch (Exception e) {
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage", bundle.getString("commonMistake"));
}
}
Reason reason = new Reason();
// populate object with request parameters
BeanUtils.populate(reason, request.getParameterMap());
List reasons = reasonManager.getReasons(reason);
return new ModelAndView("reasonList", Constants.REASON_LIST, reasons);
}
}