package com.vst.webapp.action;
import com.vst.model.*;
import com.vst.service.*;
import com.vst.webapp.util.ParentsForClassCopy;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Lilia
* Date: 21.08.2009
* Time: 6:14:14
* To change this template use File | Settings | File Templates.
*/
public class ObjectCopyController implements Controller {
ConstructionExampleManager constructionExampleManager=null;
BuildingObjectManager buildingObjectManager = null;
ObjectConstructionManager objectConstructionManager = null;
ElementCopyManager elementCopyManager = null;
ConstructionDefectManager constructionDefectManager=null;
public void setConstructionDefectManager(ConstructionDefectManager constructionDefectManager) {
this.constructionDefectManager = constructionDefectManager;
}
public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setElementCopyManager(ElementCopyManager elementCopyManager) {
this.elementCopyManager = elementCopyManager;
}
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView("objectCopy");
if (httpServletRequest.getParameter("id") != null && httpServletRequest.getParameter("class") != null) {
String classname = httpServletRequest.getParameter("class");
String objectId = httpServletRequest.getParameter("id");
List parentsList = new ArrayList();
if (classname.equals("objectConstruction")) {
List buildingObjectList = buildingObjectManager.getBuildingObjects(null);
for (int i = 0; i < buildingObjectList.size(); i++) {
ParentsForClassCopy parentsForClassCopy = new ParentsForClassCopy();
BuildingObject buildingObject = (BuildingObject) buildingObjectList.get(i);
parentsForClassCopy.setId((long)buildingObject.getObjectId());
parentsForClassCopy.setName(buildingObject.toString());
parentsList.add(parentsForClassCopy);
}
if (httpServletRequest.getParameter("copy") != null && httpServletRequest.getParameter("parentId") != null && httpServletRequest.getParameter("count") != null) {
Integer count = new Integer(httpServletRequest.getParameter("count"));
BuildingObject buildingObject = buildingObjectManager.getBuildingObject(httpServletRequest.getParameter("parentId"));
ObjectConstruction oldObjectConstruction = objectConstructionManager.getObjectConstruction(objectId);
for (int j = 0; j < count.intValue(); j++) {
ObjectConstruction newObjectConstruction = (ObjectConstruction) elementCopyManager.getNewObject(oldObjectConstruction);
List elementList = newObjectConstruction.getConstructionExamples();
for (int f = 0; f < elementList.size(); f++) {
ConstructionExample constructionExample = (ConstructionExample) elementList.get(f);
constructionExample.setBuildObjectId(new Integer(httpServletRequest.getParameter("parentId")));
}
newObjectConstruction.setConstructionExamples(elementList);
buildingObject.getConstructionTypes().add(newObjectConstruction);
objectConstructionManager.saveObjectConstruction(newObjectConstruction);
elementCopyManager.setNumZerro();
}
buildingObjectManager.saveBuildingObject(buildingObject);
modelAndView.addObject("sucessfull", "ok");
}
}
if (classname.equals("constructionExample")) {
System.out.println("constructionExample");
ConstructionExample oldConstructionExample = constructionExampleManager.getConstructionExample(objectId);
List objectConstructionsList = objectConstructionManager.getObjectConstructionByObjectIdAndConstructionType(oldConstructionExample.getBuildObjectId(),oldConstructionExample.getConstructionType().getConstructionTypeId());
for (int i = 0; i < objectConstructionsList.size(); i++) {
ParentsForClassCopy parentsForClassCopy = new ParentsForClassCopy();
ObjectConstruction objectConstruction = (ObjectConstruction) objectConstructionsList.get(i);
parentsForClassCopy.setId((long)objectConstruction.getTypeId());
parentsForClassCopy.setName(objectConstruction.toString()+" "+objectConstruction.getDescription());
parentsList.add(parentsForClassCopy);
}
if (httpServletRequest.getParameter("copy") != null && httpServletRequest.getParameter("parentId") != null && httpServletRequest.getParameter("count") != null) {
Integer count = new Integer(httpServletRequest.getParameter("count"));
ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(httpServletRequest.getParameter("parentId"));
for (int j = 0; j < count.intValue(); j++) {
System.out.println(objectId);
System.out.println(oldConstructionExample);
System.out.println(oldConstructionExample.getExampleId());
ConstructionExample newConstructionExample = (ConstructionExample) elementCopyManager.getNewObject(oldConstructionExample);
newConstructionExample.setObjectConstructionId(new Integer(httpServletRequest.getParameter("parentId")));
objectConstruction.getConstructionExamples().add(newConstructionExample);
constructionExampleManager.saveConstructionExample(newConstructionExample);
elementCopyManager.setNumZerro();
}
objectConstructionManager.saveObjectConstruction(objectConstruction);
modelAndView.addObject("sucessfull", "ok");
}
}
if (classname.equals("constructionDefectZone") || classname.equals("constructionDefect")) {
Integer exampleId=new Integer(0);
if (classname.equals("constructionDefectZone")){
ConstructionDefectZone constructionDefectZone=constructionDefectManager.getConstructionDefectZone(objectId);
exampleId=constructionDefectZone.getExampleId();
}else if (classname.equals("constructionDefect")){
ConstructionDefect constructionDefect=constructionDefectManager.getConstructionDefect(objectId);
exampleId=constructionDefect.getExampleId();
}
ConstructionExample constEx=constructionExampleManager.getConstructionExample(exampleId.toString());
List constructionExampleList=constructionExampleManager.getConstructionExamples(constEx.getObjectConstructionId().toString());
for (int i = 0; i < constructionExampleList.size(); i++) {
ParentsForClassCopy parentsForClassCopy = new ParentsForClassCopy();
ConstructionExample constructionExample = (ConstructionExample) constructionExampleList.get(i);
parentsForClassCopy.setId((long)constructionExample.getExampleId());
parentsForClassCopy.setName(constructionExample.toString());
parentsList.add(parentsForClassCopy);
}
if (classname.equals("constructionDefectZone") && httpServletRequest.getParameter("copy") != null && httpServletRequest.getParameter("parentId") != null && httpServletRequest.getParameter("count") != null) {
Integer count = new Integer(httpServletRequest.getParameter("count"));
ConstructionDefectZone oldConstructionDefectZone = constructionDefectManager.getConstructionDefectZone(objectId);
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(httpServletRequest.getParameter("parentId"));
for (int j = 0; j < count.intValue(); j++) {
ConstructionDefectZone newConstructionDefectZone = (ConstructionDefectZone) elementCopyManager.getNewObject(oldConstructionDefectZone);
newConstructionDefectZone.setExampleId(new Integer(httpServletRequest.getParameter("parentId")));
constructionExample.getExampleDefectsZone().add(newConstructionDefectZone);
constructionDefectManager.saveConstructionDefectZone(newConstructionDefectZone);
elementCopyManager.setNumZerro();
}
constructionExampleManager.saveConstructionExample(constructionExample);
modelAndView.addObject("sucessfull", "ok");
}
if (classname.equals("constructionDefect") && httpServletRequest.getParameter("copy") != null && httpServletRequest.getParameter("parentId") != null && httpServletRequest.getParameter("count") != null) {
Integer count = new Integer(httpServletRequest.getParameter("count"));
ConstructionDefect oldConstructionDefect = constructionDefectManager.getConstructionDefect(objectId);
ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(httpServletRequest.getParameter("parentId"));
for (int j = 0; j < count.intValue(); j++) {
ConstructionDefect newConstructionDefect = (ConstructionDefect) elementCopyManager.getNewObject(oldConstructionDefect);
newConstructionDefect.setExampleId(new Integer(httpServletRequest.getParameter("parentId")));
constructionExample.getExampleDefects().add(newConstructionDefect);
constructionDefectManager.saveConstructionDefect(newConstructionDefect);
elementCopyManager.setNumZerro();
}
constructionExampleManager.saveConstructionExample(constructionExample);
modelAndView.addObject("sucessfull", "ok");
}
}
modelAndView.addObject("id", objectId);
modelAndView.addObject("class", classname);
modelAndView.addObject("parentsList", parentsList);
}
return modelAndView;
}
}