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