Package com.vst.webapp.action

Source Code of com.vst.webapp.action.ConstructionDefectController

package com.vst.webapp.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

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 com.vst.Constants;
import com.vst.model.BuildingObject;
import com.vst.model.ConstructionDefect;
import com.vst.model.ConstructionDefectZone;
import com.vst.model.ConstructionExample;
import com.vst.model.ObjectConstruction;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ConstructionDefectManager;
import com.vst.service.ConstructionExampleManager;
import com.vst.service.ElementCopyManager;
import com.vst.service.ObjectConstructionManager;
import com.vst.service.ParameterManager;
import com.vst.util.FileHelper;
import com.vst.webapp.util.ObjectPicture;
import com.vst.webapp.util.Symbols;

public class ConstructionDefectController extends AbstractVstController {
  protected static final Log log = LogFactory.getLog(ConstructionDefectController.class);
    private ConstructionDefectManager constructionDefectManager = null;
    private ConstructionExampleManager constructionExampleManager = null;
    private ObjectConstructionManager objectConstructionManager = null;
    private BuildingObjectManager buildingObjectManager = null;
    private ParameterManager parameterManager = null;
    private ElementCopyManager elementCopyManager = null;


    public void setElementCopyManager(ElementCopyManager elementCopyManager) {
        this.elementCopyManager = elementCopyManager;
    }


    public void setParameterManager(ParameterManager parameterManager) {
        this.parameterManager = parameterManager;
    }


    public void setObjectConstructionManager(ObjectConstructionManager objectConstructionManager) {
        this.objectConstructionManager = objectConstructionManager;
    }

    public void setConstructionExampleManager(ConstructionExampleManager constructionExampleManager) {
        this.constructionExampleManager = constructionExampleManager;
    }

    public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
        this.buildingObjectManager = buildingObjectManager;
    }

    public void setConstructionDefectManager(ConstructionDefectManager constructionDefectManager) {
        this.constructionDefectManager = constructionDefectManager;
    }

    private void processParamPageNumber(HttpServletRequest request,
      HttpServletResponse response) {
      String pageNumber = request.getParameter("d-3341154-p");
        if (pageNumber != null) {
            request.setAttribute("pageNumber", "d-3341154-p=" + pageNumber);
        }
  }

  private boolean isCopyParamPresent(HttpServletRequest request) {
    return request.getParameter("copy") != null;
  }

  private void doCopyDefect(HttpServletRequest request,
            HttpServletResponse response) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
    String constructionDefectId = request.getParameter("copy");
        String exId = request.getParameter("exampleId");
        ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(exId);

        if (request.getParameter("type").equals("simple")) {

            ConstructionDefect constructionDefect = constructionDefectManager.getConstructionDefect(constructionDefectId);
            ConstructionDefect newConstructionDefect = (ConstructionDefect) elementCopyManager.getNewObject(constructionDefect);

            constructionExample.getExampleDefects().add(newConstructionDefect);
            constructionDefectManager.saveConstructionDefect(newConstructionDefect);
            constructionExampleManager.saveConstructionExample(constructionExample);

        } else if (request.getParameter("type").equals("zone")) {


            ConstructionDefectZone constructionDefect = constructionDefectManager.getConstructionDefectZone(constructionDefectId);
            ConstructionDefectZone newConstructionDefect = (ConstructionDefectZone) elementCopyManager.getNewObject(constructionDefect);

            constructionExample.getExampleDefectsZone().add(newConstructionDefect);
            constructionDefectManager.saveConstructionDefectZone(newConstructionDefect);
            constructionExampleManager.saveConstructionExample(constructionExample);
        }
  }

  private void doDeleteDefectSymbols(HttpServletRequest request, String pathToSymbols, ConstructionDefect constructionDefect) throws FileNotFoundException, IOException{
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(FileHelper.getCurrentPath(request)+pathToSymbols));
        ObjectPicture objectPicture=new ObjectPicture();
        try {
            objectPicture = (ObjectPicture) in.readObject();
            if (objectPicture != null) {
               List list=objectPicture.getObjectList();
                for (int l=0; l<list.size(); l++){
                       if (list.get(l) instanceof Symbols){
                            Symbols symbols=(Symbols)list.get(l);
                               if (symbols.getType().equals("defect") && symbols.getDefect().getConstructionDefectId()!=null && symbols.getDefect().getConstructionDefectId().equals(constructionDefect.getConstructionDefectId())){
                                   objectPicture.deleteObject(symbols);
                               }
                       }
                }
            }
        } catch (ClassNotFoundException e) {

        }
        in.close();


        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FileHelper.getCurrentPath(request)+pathToSymbols));
         out.writeObject(objectPicture);
         out.close();
  }

  private void doRemoveDefect(HttpServletRequest request,
            HttpServletResponse response){

        String constructionDefectId = request.getParameter("id");
        ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
        if (extractDefectTypeParam(request).equals("simple")) {


            boolean hasFatalError = false;
            try {
                ConstructionDefect constructionDefect = constructionDefectManager.getConstructionDefect(constructionDefectId);
                ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
                constructionExample.getExampleDefects().remove(constructionDefect);
                constructionExampleManager.saveConstructionExample(constructionExample);
                request.getSession().setAttribute("madeChanges", new Boolean(true));
                request.setAttribute("message", bundle.getString("constructionDefect.deleted"));

                ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
        try {
          doDeleteDefectSymbols(request, objectConstruction.getWayToDefectMap(),
              constructionDefect);
        } catch (IOException e) {

        }

            } catch (Exception e) {
                hasFatalError = true;
            }
            if (hasFatalError) {
                request.setAttribute("errormessage", bundle.getString("commonMistake"));
            }

        } else if (extractDefectTypeParam(request).equals("zone")) {

            ConstructionDefectZone constructionDefect = constructionDefectManager.getConstructionDefectZone(constructionDefectId);
            ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
            constructionExample.getExampleDefectsZone().remove(constructionDefect);
            constructionExampleManager.saveConstructionExample(constructionExample);
            constructionDefectManager.deleteConstructionDefectZone(constructionDefect);
            request.getSession().setAttribute("madeChanges", new Boolean(true));
            request.setAttribute("message", bundle.getString("constructionDefect.deleted"));

        }

  }

  private String extractDefectTypeParam(HttpServletRequest request){
    return  request.getParameter("type");
  }

  private String extractConstructionIdParam(HttpServletRequest request){
    return  request.getParameter("objectConstructionId");
  }

  private String extractExampleIdParam(HttpServletRequest request){
    return  request.getParameter("exampleId");
  }

  private List<?> prepareDefectListForView(HttpServletRequest request,
            HttpServletResponse response){
        List constructionDefects = new ArrayList();
        String exampleId = extractExampleIdParam(request);

        //creating refference for construction breadcrump
        if (extractExampleIdParam(request) != null) {
            ConstructionExample constructionExample = constructionExampleManager.getConstructionExampleForBreadCrump(request.getParameter("exampleId"));
            if (constructionExample != null) {
             request.getSession().setAttribute("constructionExampleName", (constructionExample.getExampleName() == null || constructionExample.getExampleName().trim().equals("")) ? String.valueOf(constructionExample.getExampleNumber()) : constructionExample.getExampleName());
             System.out.println("CENRef"+constructionExample.getObjectConstructionId()+constructionExample);
             request.getSession().setAttribute("constructionExampleNameRef", "constructionExamples.html?exampleId=" + constructionExample.getExampleId() + "&typeId=" + constructionExample.getObjectConstructionId());

             ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstructionForBreadCrump(String.valueOf(constructionExample.getObjectConstructionId()));
             if (objectConstruction != null) {
              //objectConstructionManager.evict(objectConstruction);
               request.getSession().setAttribute("constructionTypeName", objectConstruction.getConstructionType().getName() );
               request.getSession().setAttribute("constructionTypeNameRef", "objectConstructions.html?typeId=" + objectConstruction.getTypeId() + "&objectId=" + objectConstruction.getObjectId());
               //Integer buildingObjectId = objectConstruction.getObjectId();

               //setting the object to the context
               BuildingObject buildingObject = buildingObjectManager.getBuildingObjectForBreadCrump(objectConstruction.getObjectId().toString());
               if (buildingObject != null) {
                //buildingObjectManager.evict(buildingObject);
                request.getSession().setAttribute("buildingObjectName", buildingObject.getName());
                request.getSession().setAttribute("buildingObjectNameRef", "buildingObjects.html?objectId=" + buildingObject.getObjectId());
               }
             }
            }

        }

        String objectConstructionId = extractConstructionIdParam(request);
        if (extractExampleIdParam(request) != null) {
            constructionDefects = constructionDefectManager.getConstructionDefectsByExampleId(exampleId);
            for (int i = 0; i < constructionDefects.size(); i++) {
                ConstructionDefect constructionDefect = (ConstructionDefect) constructionDefects.get(i);
                constructionDefect.setObjectConstructionId(objectConstructionId);
            }
        }

        int size = constructionDefects.size();

        if (constructionDefects.isEmpty()) {
            constructionDefects = constructionDefectManager.getConstructionDefectsZoneByExampleId(exampleId);
            for (int i = 0; i < constructionDefects.size(); i++) {
                ConstructionDefectZone constructionDefect = (ConstructionDefectZone) constructionDefects.get(i);
                constructionDefect.setObjectConstructionId(objectConstructionId);
            }
        } else {
            constructionDefects.addAll(constructionDefectManager.getConstructionDefectsZoneByExampleId(exampleId));
            for (int i = size; i < constructionDefects.size(); i++) {
                ConstructionDefectZone constructionDefect = (ConstructionDefectZone) constructionDefects.get(i);
                constructionDefect.setObjectConstructionId(objectConstructionId);
            }
        }
        return constructionDefects;
  }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response)
            throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }

        processParamEdited(request,response);
        processParamPageNumber(request,response);

        if (isCopyParamPresent(request)) {
          doCopyDefect(request,response);
        }

        if (isDeleteParamPresent(request)) {
          doRemoveDefect(request,response);
        }

        ModelAndView modelAndView = new ModelAndView("constructionDefectList");
        modelAndView.addObject("objectConstructionId", extractConstructionIdParam(request));
        modelAndView.addObject(Constants.CONSTRUCTIONDEFECT_LIST, prepareDefectListForView(request, response));
        return modelAndView;
    }

}
TOP

Related Classes of com.vst.webapp.action.ConstructionDefectController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.