package com.vst.webapp.action;
import com.vst.model.AuthentificationElement;
import com.vst.model.DefectZone;
import com.vst.model.Parameter;
import com.vst.service.AuthentificationElementManager;
import com.vst.service.DefectZoneManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;
import java.util.ArrayList;
public class DefectZoneFormController extends BaseFormController {
private DefectZoneManager defectZoneManager = null;
private AuthentificationElementManager authentificationElementManager = null;
public void setAuthentificationElementManager(AuthentificationElementManager authentificationElementManager) {
this.authentificationElementManager = authentificationElementManager;
}
public void setDefectZoneManager(DefectZoneManager defectZoneManager) {
this.defectZoneManager = defectZoneManager;
}
public DefectZoneFormController() {
setCommandName("defectZone");
setCommandClass(DefectZone.class);
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
request.setAttribute("authentificationElementList", authentificationElementManager.getAuthentificationElements(null));
if (!isFormSubmission(request)) {
String defectZoneId = request.getParameter("defectZoneId");
DefectZone defectZone = null;
if (!StringUtils.isEmpty(defectZoneId)) {
defectZone = defectZoneManager.getDefectZone(defectZoneId);
defectZoneManager.evict(defectZone);
} else {
defectZone = new DefectZone();
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
defectZone.setEdited(true);
}
defectZone.setDocLocation(request.getParameter("docLocation"));
return defectZone;
}
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...");
}
DefectZone defectZone = (DefectZone) command;
boolean isNew = (defectZone.getDefectZoneId() == null);
Locale locale = request.getLocale();
if (request.getParameter("delete") != null) {
defectZoneManager.removeDefectZone(defectZone.getDefectZoneId().toString());
saveMessage(request, getText("defectZone.deleted", locale));
} else {
List defectZoneAutentifications = new ArrayList();
List defectZoneAutentificationsFromForm = defectZone.getAuthentificationElements();
for (int i = 0; i < defectZoneAutentificationsFromForm.size(); i++) {
AuthentificationElement authentificationElement = (AuthentificationElement) defectZoneAutentificationsFromForm.get(i);
defectZoneAutentifications.add(authentificationElementManager.getAuthentificationElement(authentificationElement.getAuthentificationId().toString()));
}
defectZone.setAuthentificationElements(defectZoneAutentifications);
defectZoneManager.saveDefectZone(defectZone);
String key = (isNew) ? "defectZone.added" : "defectZone.updated";
saveMessage(request, getText(key, locale));
if (defectZone.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + defectZone.getDefectZoneId() + "&fieldId=" + request.getParameter("fieldId"));
}
}
return new ModelAndView("redirect:" + defectZone.getDocLocation());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
DefectZone defectZone = (DefectZone) command;
if (request.getParameter("addElement") != null) {
defectZone.getAuthentificationElements().add(new AuthentificationElement());
return new ModelAndView("defectZoneForm", "defectZone", defectZone);
}
if (request.getParameter("deleteElement") != null) {
defectZone.getAuthentificationElements().remove(defectZone.getAuthentificationNumber());
return new ModelAndView("defectZoneForm", "defectZone", defectZone);
}
return super.processFormSubmission(request,
response,
command,
errors);
}
}