package com.vst.webapp.action;
import com.vst.model.BuildingObject;
import com.vst.model.LevelPlan;
import com.vst.service.BuildingObjectManager;
import com.vst.service.DetailSymbolManager;
import com.vst.util.FileHelper;
import com.vst.util.ImageUtil;
import org.hibernate.Hibernate;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
/**
* Created by IntelliJ IDEA.
* User: Lilia
* Date: 09.12.2009
* Time: 12:50:33
* To change this template use File | Settings | File Templates.
*/
public class LevelPlanFormController extends BaseFormController {
DetailSymbolManager detailSymbolManager;
BuildingObjectManager buildingObjectManager;
public BuildingObjectManager getBuildingObjectManager() {
return buildingObjectManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setDetailSymbolManager(DetailSymbolManager detailSymbolManager) {
this.detailSymbolManager = detailSymbolManager;
}
public LevelPlanFormController() {
setCommandName("levelPlan");
setCommandClass(LevelPlan.class);
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (!isFormSubmission(request)) {
LevelPlan levelPlan = null;
if (request.getParameter("levelPlanId")!=null) {
levelPlan = detailSymbolManager.getByIdLevelPlan(Integer.valueOf(request.getParameter("levelPlanId")));
} else {
levelPlan = new LevelPlan();
levelPlan.setObjectId(Integer.valueOf(request.getParameter("objectId")));
}
if (levelPlan.getWayToLevelPlan() == null || levelPlan.getWayToLevelPlanImage().equals("")) {
String saveWay = ImageUtil.getUniqueJPEGFile(request);
request.setAttribute("saveWay", saveWay);
levelPlan.setWayToLevelPlan(saveWay);
String saveWayImage = "image" + saveWay;
levelPlan.setWayToLevelPlanImage(saveWayImage);
request.setAttribute("saveWayImage", saveWayImage);
request.getSession().setAttribute("saveWayImage", saveWayImage);
request.getSession().setAttribute("saveWay", saveWay);
} else {
request.getSession().setAttribute("saveWayImage", levelPlan.getWayToLevelPlanImage());
request.getSession().setAttribute("saveWay", levelPlan.getWayToLevelPlan());
request.getSession().setAttribute("loadWay", levelPlan.getWayToLevelPlan());
}
return levelPlan;
}
return super.formBackingObject(request);
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors) throws Exception {
LevelPlan levelPlan = (LevelPlan) command;
if (request.getParameter("cancel") != null) {
return new ModelAndView("redirect:levelPlanList.html?objectId="+levelPlan.getObjectId());
}
File file = new File(FileHelper.getCurrentPath(request) + levelPlan.getWayToLevelPlan());
if (levelPlan.getWayToLevelPlan() != null && !levelPlan.getWayToLevelPlan().equals("") && file.exists()) {
levelPlan.setLevelBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + levelPlan.getWayToLevelPlan())));
}
if (levelPlan.getLevelId() != null) {
detailSymbolManager.update(levelPlan);
return new ModelAndView("redirect:levelPlanList.html?objectId="+levelPlan.getObjectId());
} else {
BuildingObject buildingObject=buildingObjectManager.getBuildingObject(levelPlan.getObjectId().toString());
buildingObject.getLevelPlanList().add(levelPlan);
detailSymbolManager.insert(levelPlan);
buildingObjectManager.saveBuildingObject(buildingObject);
}
ModelAndView mav = new ModelAndView(getSuccessView());
LevelPlan levelPlan1 = new LevelPlan();
String saveWay = ImageUtil.getUniqueJPEGFile(request);
mav.addObject("saveWay", saveWay);
levelPlan1.setWayToLevelPlan(saveWay);
String saveWayImage = "image" + saveWay;
levelPlan1.setWayToLevelPlanImage(saveWayImage);
mav.addObject("saveWayImage", saveWayImage);
levelPlan1.setObjectId(levelPlan.getObjectId());
mav.addObject("levelPlan", levelPlan1);
mav.addObject("result", new Integer(1));
return mav;
}
public ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
LevelPlan levelPlan=(LevelPlan)command;
if (request.getParameter("cancel") != null) {
return new ModelAndView("redirect:levelPlanList.html?objectId="+levelPlan.getObjectId());
}
return super.processFormSubmission(request, response, command, errors);
}
}