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.Parameter;
import com.vst.service.ParameterManager;
import com.vst.service.MeasureManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
public class ParameterFormController extends BaseFormController {
private ParameterManager parameterManager = null;
private MeasureManager measureManager = null;
public void setMeasureManager(MeasureManager measureManager) {
this.measureManager = measureManager;
}
public void setParameterManager(ParameterManager parameterManager) {
this.parameterManager = parameterManager;
}
public ParameterFormController() {
setCommandName("parameter");
setCommandClass(Parameter.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("measureList", measureManager.getMeasures(null));
if (!isFormSubmission(request)) {
String parameterId = request.getParameter("parameterId");
Parameter parameter = null;
if (!StringUtils.isEmpty(parameterId)) {
parameter = parameterManager.getParameter(parameterId);
parameterManager.evict(parameter);
} else {
parameter = new Parameter();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
parameter.setEdited(true);
}
parameter.setDocLocation(request.getParameter("docLocation"));
return parameter;
}
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...");
}
Parameter parameter = (Parameter) command;
boolean isNew = (parameter.getParameterId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
parameterManager.removeParameter(parameter.getParameterId().toString());
saveMessage(request, getText("parameter.deleted", locale));
} else {
Integer measureId = parameter.getMeasure().getMeasureId();
if (!measureId.equals(new Integer(-1))) {
parameter.setMeasure(measureManager.getMeasure(measureId.toString()));
}
parameterManager.saveParameter(parameter);
String key = (isNew) ? "parameter.added" : "parameter.updated";
saveMessage(request, getText(key, locale));
if (parameter.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + parameter.getParameterId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + parameter.getDocLocation());
}
}