package com.vst.webapp.action;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectAnswer;
import com.vst.model.ObjectPhoto;
import com.vst.service.*;
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.HashMap;
import java.util.Locale;
import java.util.Map;
public class BuildingObjectFormController extends BaseFormController {
private BuildingObjectManager buildingObjectManager;
private ObjectTypeManager objectTypeManager;
private DangerCategoryManager dangerCategoryManager;
private RegionManager regionManager;
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public void setRegionManager(RegionManager regionManager) {
this.regionManager = regionManager;
}
public void setDangerCategoryManager(DangerCategoryManager dangerCategoryManager) {
this.dangerCategoryManager = dangerCategoryManager;
}
public void setObjectTypeManager(ObjectTypeManager objectTypeManager) {
this.objectTypeManager = objectTypeManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public BuildingObjectFormController() {
setCommandName("buildingObject");
setCommandClass(BuildingObject.class);
}
protected Map referenceData(HttpServletRequest request) throws Exception {
HashMap refData = new HashMap();
refData.put("objectTypeList", objectTypeManager.getObjectTypes(null));
refData.put("regionList", regionManager.getRegions(null));
System.out.println(regionManager.getRegions(null));
refData.put("dangerCategoryList", dangerCategoryManager.getDangerCategorys(null));
return refData;
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
if (!isFormSubmission(request)) {
//getting the list of research users if current user is admin
if (request.isUserInRole("admin")) {
// request.setAttribute("userList", userManager.getResearchUsers());
request.setAttribute("userList", userManager.getUsers(null));
}
String objectId = request.getParameter("objectId");
BuildingObject buildingObject = null;
if (!StringUtils.isEmpty(objectId)) {
buildingObject = buildingObjectManager.getBuildingObjectForList(new Integer(objectId));
buildingObjectManager.evict(buildingObject);
} else {
buildingObject = new BuildingObject();
}
buildingObjectManager.makeNotNull(buildingObject);
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
buildingObject.setEdited(true);
}
buildingObject.setDocLocation(request.getParameter("docLocation"));
return buildingObject;
}
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...");
}
BuildingObject buildingObject = (BuildingObject) command;
if (buildingObject.getUser().getId() != null && !buildingObject.getUser().getId().equals(new Integer(-1))) {
buildingObject.setUser(userManager.getUser(buildingObject.getUser().getId().toString()));
} else {
buildingObject.setUser(userManager.getUserByUsername(request.getRemoteUser()));
}
boolean isNew = (buildingObject.getObjectId() == null);
Locale locale = request.getLocale();
Integer dangerCategoryId = buildingObject.getDangerCategory().getDangerCategoryId();
if (!dangerCategoryId.equals(new Integer(-1))) {
buildingObject.setDangerCategory(dangerCategoryManager.getDangerCategory(dangerCategoryId.toString()));
} else {
buildingObject.setDangerCategory(null);
}
//retrieving object type from base
if (!buildingObject.getObjectType().getObjectTypeId().equals(new Integer(-1))) {
buildingObject.setObjectType(objectTypeManager.getObjectType(buildingObject.getObjectType().getObjectTypeId().toString()));
} else {
buildingObject.setObjectType(null);
}
if (request.getParameter("delete") != null) {
buildingObjectManager.removeBuildingObject(buildingObject.getObjectId().toString());
saveMessage(request, getText("buildingObject.deleted", locale));
} else {
if (!isNew) {
//update only need properties from form
buildingObjectManager
.updateBuildingObjectByMainForm(
buildingObject.getObjectId(),
buildingObject);
} else {
buildingObjectManager.saveBuildingObject(buildingObject);
}
String key = (isNew) ? "buildingObject.added" : "buildingObject.updated";
saveMessage(request, getText(key, locale));
}
request.getSession().setAttribute("madeChanges", new Boolean(true));
request.getSession().setAttribute("pType", "buildingObject" );
request.getSession().setAttribute("pId", buildingObject.getObjectId() );
//request.getSession().setAttribute("isNew", true);
System.out.println("REDIRECT" + buildingObject.getDocLocation()+buildingObject.getObjectId());
return new ModelAndView("redirect:" + buildingObject.getDocLocation()+"?objectId="+buildingObject.getObjectId());
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
if (request.getParameter("cancel") != null) {
return new ModelAndView(getCancelView());
}
BuildingObject buildingObject = (BuildingObject) command;
//добавляем роазличные критерии для специалиста
if (request.getParameter("addPhoto") != null || request.getParameter("deletePhoto") != null) {
if (request.getParameter("addPhoto") != null) {
buildingObject.getPhotoes().add(new ObjectPhoto());
}
if (request.getParameter("deletePhoto") != null) {
buildingObject.getPhotoes().remove(buildingObject.getPhotoNum());
}
if (buildingObject.isEdited()) {
return new ModelAndView("redirect:updating.html?id=" + buildingObject.getObjectId() + "&fieldId=" + request.getParameter("fieldId"));
}
return new ModelAndView("buildingObjectForm", "buildingObject", buildingObject);
}
return super.processFormSubmission(request, response, command, errors);
}
}