package com.vst.webapp.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.vst.Constants;
import com.vst.model.BuildingObject;
import com.vst.model.ConstructionExample;
import com.vst.model.ConstructionType;
import com.vst.model.DangerCategory;
import com.vst.model.ObjectConstruction;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.ConstructionTypeManager;
import com.vst.service.ElementCopyManager;
import com.vst.service.ObjectConstructionManager;
import org.apache.commons.beanutils.BeanUtils;
import com.vst.util.FileHelper;
public class ObjectConstructionController implements Controller {
private final Log log = LogFactory
.getLog(ObjectConstructionController.class);
private ObjectConstructionManager objectConstructionManager = null;
private ConstructionTypeManager constructionTypeManager = null;
private BuildingObjectManager buildingObjectManager = null;
private ConstructionExampleManager constructionExampleManager = null;
private ElementCopyManager elementCopyManager = null;
public void setElementCopyManager(ElementCopyManager elementCopyManager) {
this.elementCopyManager = elementCopyManager;
}
public void setConstructionExampleManager(
ConstructionExampleManager constructionExampleManager) {
this.constructionExampleManager = constructionExampleManager;
}
public void setBuildingObjectManager(
BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setObjectConstructionManager(
ObjectConstructionManager objectConstructionManager) {
this.objectConstructionManager = objectConstructionManager;
}
public void setConstructionTypeManager(
ConstructionTypeManager constructionTypeManager) {
this.constructionTypeManager = constructionTypeManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView commonMav = new ModelAndView("objectConstructionList");
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
String pageNumber = request.getParameter("d-2834692-p");
if (pageNumber != null) {
request.setAttribute("pageNumber", "d-2834692-p=" + pageNumber);
}
// generating constructionType to the session
ObjectConstruction objectConstruction = new ObjectConstruction();
if (request.getParameter("objectId") != null) {
if (log.isDebugEnabled()) {
log.debug("setting the object to the context");
}
// setting the object to the context
BuildingObject buildingObject = buildingObjectManager
.getBuildingObjectForBreadCrump(request
.getParameter("objectId"));
request.getSession().setAttribute("buildingObjectName",
buildingObject.getName());
request.getSession().setAttribute(
"buildingObjectNameRef",
"buildingObjects.html?objectId="
+ buildingObject.getObjectId());
}
if (request.getParameter("typeId") != null
&& !request.getParameter("typeId").trim().equals("")) {
if (log.isDebugEnabled()) {
log.debug("setting the object construction to the context");
}
objectConstruction = objectConstructionManager
.getObjectConstructionForBreadCrump(request
.getParameter("typeId"));
request.getSession().setAttribute("constructionTypeName",objectConstruction.getConstructionType().getName());
request.getSession().setAttribute("constructionTypeNameRef","objectConstructions.html?typeId="+ objectConstruction.getTypeId() + "&objectId="
+ objectConstruction.getObjectId());
} else {
// removing objectconstruction type from breadcrump
request.getSession().setAttribute("constructionTypeName", "");
request.getSession().setAttribute("constructionTypeNameRef", "");
}
// destroying examples and defect refferences
request.getSession().setAttribute("constructionExampleName", "");
request.getSession().setAttribute("constructionDefectName", "");
ObjectConstruction newObjectConstruction = new ObjectConstruction();
if (request.getParameter("addConstructionType") != null) {
if (log.isDebugEnabled()) {
log.debug("Adding new object construction of specified construction type.");
}
String buildObjectId = request.getParameter("buildObjectId");
// ModelAndView mav2 = new ModelAndView(
// "redirect:objectConstructions.html?objectId="
// + buildObjectId);
// ObjectConstruction newObjectConstruction = new ObjectConstruction();
newObjectConstruction.setObjectId(new Integer(buildObjectId));
Integer constructionTypeId = new Integer(request.getParameter("constructionTypeId"));
if (!constructionTypeId.equals(new Integer(-1))) {
newObjectConstruction.setConstructionType(constructionTypeManager
.getConstructionType(constructionTypeId.toString()));
newObjectConstruction.setMaterial(constructionTypeManager.getConstructionType(constructionTypeId.toString()).getMaterial());
}
objectConstructionManager.saveObjectConstruction(newObjectConstruction);
System.out.println("Generated Id"+newObjectConstruction.getTypeId());
//return mav2;
}
String typeId = "0";
if (request.getParameter("addExamples") != null) {
if (log.isDebugEnabled()) {
log.debug("Adding new examples ... ");
}
ModelAndView mav2 = new ModelAndView(
"redirect:constructionExamples.html");
String buildObjectId = request.getParameter("buildObjectId");
if(request.getParameter("addConstructionType")!=null){
typeId = newObjectConstruction.getTypeId().toString();
}else{
typeId = request.getParameter("typeId");
}
objectConstruction = objectConstructionManager.getObjectConstructionForBreadCrump(typeId);
// adding new examples to the construction varity
int exampleNumber = 0;
try {
exampleNumber = Integer.parseInt(request.getParameter("examplesNumber"));
} catch (NumberFormatException e) {
e.printStackTrace();
return new ModelAndView(
"redirect:objectConstructions.html?objectId="
+ buildObjectId + "&typeId=" + typeId);
}
if (exampleNumber > 0) {
ConstructionExample constructionExample = new ConstructionExample();
// generating the unique number of construction
constructionExample.setExampleName("конструкция 0");
System.out.println(objectConstruction.getTypeId().intValue());
Integer number = constructionExampleManager.getExampleNumberByConstructionObjectId(new Integer(objectConstruction.getTypeId()));
constructionExample.setExampleRelativeNumber(number);
DangerCategory dc = new DangerCategory();
// 0 -default danger category means N/A
dc.setDangerCategoryId(new Integer(0));
constructionExample.setDangerCategory(dc);
constructionExample.setObjectId(new Integer(buildObjectId));
constructionExample.setBuildObjectId(new Integer(buildObjectId));
constructionExample.setObjectConstructionId(objectConstruction.getTypeId());
constructionExample.setConstructionType(objectConstruction.getConstructionType());
constructionExample.setRecommendedDangerCategory(null);
constructionExample.setParent(objectConstruction);
// setting the ways to esciz
// checking esciz
if (constructionExample.getWayToDefectEsckiz() == null
|| constructionExample.getWayToDefectEsckiz().equals("")) {
// retrieving esciz from object construction
Blob eskiz = objectConstructionManager
.getObjectConstructionImage(new Integer(typeId),"eskiz");
// esciz for defects
if (eskiz != null) {
String wayToDefectEskiz = "eskiz"
+ (System.currentTimeMillis() & 0xffffffffL)
+ ".jpg";
constructionExample.setDefectEsckizBlob(eskiz);
constructionExample
.setWayToDefectEsckiz(wayToDefectEskiz);
constructionExample.setDefectEsckizWayImage("image"
+ wayToDefectEskiz);
// esciz for power points
String wayToPowerEskiz = "powereskiz"
+ (System.currentTimeMillis() & 0xffffffffL)
+ ".jpg";
constructionExample
.setWayToPowerEsckiz(wayToPowerEskiz);
constructionExample.setPowerEsckizBlob(eskiz);
constructionExample.setPowerEsckizWayImage("image"
+ wayToPowerEskiz);
}
}
constructionExampleManager
.saveConstructionExample(constructionExample);
constructionExampleManager.copyExample(
constructionExample.getExampleId(), exampleNumber - 1);
Cookie cookie = new Cookie("nodeEnsureVisible", ""
+ (new java.util.Date().getTime()));
cookie.setValue(String.valueOf(number));
cookie.setMaxAge(30 * 24 * 60 * 60);
response.addCookie(cookie);
}
if (request.getParameter("typeId") != null ) {
if(request.getParameter("addExamples") == null){
objectConstruction = objectConstructionManager.getObjectConstructionForBreadCrump(request.getParameter("typeId"));
// objectConstructionManager.evict(objectConstruction);
request.getSession().setAttribute("constructionTypeName", objectConstruction.getConstructionType().getName());
request.getSession().setAttribute("constructionTypeNameRef", "objectConstructions.html?typeId=" + objectConstruction.getTypeId() + "&objectId=" + objectConstruction.getObjectId());
mav2.addObject("typeId", request.getParameter("typeId"));
mav2.addObject("treeParams", "FFFFFF");
return mav2;
}}
}
/*
if (request.getParameter("addConstructionType") != null) {
if (log.isDebugEnabled()) {
log.debug("Adding new object construction of specified construction type.");
}
String buildObjectId = request.getParameter("buildObjectId");
ModelAndView mav2 = new ModelAndView(
"redirect:objectConstructions.html?objectId="
+ buildObjectId);
ObjectConstruction newObjectConstruction = new ObjectConstruction();
newObjectConstruction.setObjectId(new Integer(buildObjectId));
Integer constructionTypeId = new Integer(request.getParameter("constructionTypeId"));
if (!constructionTypeId.equals(new Integer(-1))) {
newObjectConstruction.setConstructionType(constructionTypeManager
.getConstructionType(constructionTypeId.toString()));
newObjectConstruction.setMaterial(constructionTypeManager.getConstructionType(constructionTypeId.toString()).getMaterial());
}
objectConstructionManager.saveObjectConstruction(newObjectConstruction);
return mav2;
}
*/
if (request.getParameter("delete") != null) {
ResourceBundle bundle = ResourceBundle
.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
String objectConstructionId = request.getParameter("id");
objectConstruction = objectConstructionManager
.getObjectConstruction(objectConstructionId);
BuildingObject buildingObject = buildingObjectManager
.getBuildingObject(objectConstruction.getObjectId()
.toString());
// TODO find out the way of updating collection indexes without
// extra sql query
Integer constructionPosition = objectConstruction
.getTypePosition();
buildingObject.getConstructionTypes()
.remove(objectConstruction);
objectConstructionManager
.removeObjectConstruction(objectConstructionId);
buildingObjectManager.saveBuildingObject(buildingObject);
request.getSession().setAttribute("madeChanges",
new Boolean(true));
request.setAttribute("message",
bundle.getString("objectConstruction.deleted"));
return new ModelAndView(
"redirect:objectConstructions.html?objectId="
+ request.getParameter("objectId"));
} catch (Exception e) {
e.printStackTrace();
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage",
bundle.getString("commonMistake"));
}
}
if (request.getParameter("elementCopy") != null) {
BuildingObject buildingObject = buildingObjectManager
.getBuildingObject(request.getParameter("objectId"));
ObjectConstruction oldObjectConstruction = objectConstructionManager
.getObjectConstruction(request.getParameter("id"));
newObjectConstruction = (ObjectConstruction) elementCopyManager
.getNewObject(oldObjectConstruction);
buildingObject.getConstructionTypes().add(newObjectConstruction);
objectConstructionManager
.saveObjectConstruction(newObjectConstruction);
buildingObjectManager.saveBuildingObject(buildingObject);
elementCopyManager.setNumZerro();
}
String objectId = request.getParameter("objectId");
if(request.getParameter("buildObjectId")!=null)
objectId = request.getParameter("buildObjectId");
List objectConstructions = new ArrayList();
if (objectId != null) {
objectConstructions = objectConstructionManager
.getListForObjectConstructionPage(new Integer(objectId));
}
// retrieving eskiz from each construction
for (int i = 0; i < objectConstructions.size(); i++) {
ObjectConstruction construction = (ObjectConstruction) objectConstructions
.get(i);
if (construction.getEsckizBlob() != null) {
File file = new File(FileHelper.getCurrentPath(request)
+ construction.getWayToEsckiz());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(
FileHelper.getCurrentPath(request)
+ construction.getWayToEsckiz());
Blob blob = construction.getEsckizBlob();
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
}
}
objectConstructions = objectConstructionManager
.fillExampleNumbers(objectConstructions);
request.getSession().setAttribute(Constants.OBJECTCONSTRUCTION_LIST,
objectConstructions);
commonMav.addObject(Constants.OBJECTCONSTRUCTION_LIST,
objectConstructions);
ConstructionType constructionType = new ConstructionType();
// populate object with request parameters
BeanUtils.populate(constructionType, request.getParameterMap());
// BeanUtils.populate(constructionType, request.getParameterMap());
List constructionTypes = new ArrayList();
constructionTypes = constructionTypeManager
.getConstructionTypes(constructionType);
System.out.println(objectConstructions.size());
System.out.println(constructionTypes.size());
//for (Object o : constructionTypes) {
// ConstructionType ct = new ConstructionType();
// ct = (ConstructionType) o;
// System.out.println(ct.getConstructionTypeId());
//}
request.getSession().setAttribute(Constants.CONSTRUCTIONTYPE_LIST,
constructionTypes);
commonMav.addObject(Constants.CONSTRUCTIONTYPE_LIST, constructionTypes);
request.getSession().setAttribute("pType", "constructionType");
if(request.getParameter("addConstructionType") != null){
request.getSession().setAttribute("pId", typeId);
if(request.getParameter("typeId")==null)request.setAttribute("typeId",typeId);
} else {
request.getSession().setAttribute("pId", request.getParameter("typeId"));
}
if (request.getParameter("form") != null) {
return new ModelAndView("objectConstructionFormNew");
}
return commonMav;
}
}