Package com.vst.webapp.action

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

package com.vst.webapp.action;

import com.vst.model.*;
import com.vst.service.*;
import com.vst.webapp.util.*;
import com.vst.util.FileHelper;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ConstructionDefectFormController extends BaseFormController {

    private ConstructionDefectManager constructionDefectManager = null;
    private ReasonManager reasonManager = null;
    private DefectZoneManager defectZoneManager = null;
    private DefectTypeManager defectTypeManager = null;
    private DefectVarityManager defectVarityManager = null;
    private ConstructionExampleManager constructionExampleManager = null;
    private AuthentificationElementManager authentificationElementManager = null;
    private ParameterManager parameterManager = null;
    private DangerCategoryManager dangerCategoryManager = null;
    private ObjectConstructionManager objectConstructionManager = null;
    private DetailSymbolManager detailSymbolManager = null;


    public void setDetailSymbolManager(DetailSymbolManager detailSymbolManager) {
        this.detailSymbolManager = detailSymbolManager;
    }

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

    public void setDangerCategoryManager(DangerCategoryManager dangerCategoryManager) {
        this.dangerCategoryManager = dangerCategoryManager;
    }

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

    public void setAuthentificationElementManager(AuthentificationElementManager authentificationElementManager) {
        this.authentificationElementManager = authentificationElementManager;
    }

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

    public void setReasonManager(ReasonManager reasonManager) {
        this.reasonManager = reasonManager;
    }

    public void setDefectZoneManager(DefectZoneManager defectZoneManager) {
        this.defectZoneManager = defectZoneManager;
    }

    public void setDefectTypeManager(DefectTypeManager defectTypeManager) {
        this.defectTypeManager = defectTypeManager;
    }

    public void setDefectVarityManager(DefectVarityManager defectVarityManager) {
        this.defectVarityManager = defectVarityManager;
    }

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

    public ConstructionDefectFormController() {
        setCommandName("constructionDefect");
        setCommandClass(ConstructionDefect.class);
    }


    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {

      System.out.println("iForm backing object method ...");

        if (request.getAttribute("objectConstructionId") != null) {
            request.setAttribute("objectConstructionId", request.getAttribute("objectConstructionId"));
        }

        //    request.setAttribute("reasonList", reasonManager.getReasons(null));
        request.setAttribute("dangerCategoryList", dangerCategoryManager.getDangerCategorys(null));
        //   request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritys(null));
        String constructionDefectId = request.getParameter("constructionDefectId");
        Integer constructionTypeId = null;
        if (request.getParameter("exampleId") != null) {

            ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(request.getParameter("exampleId"));
            constructionExampleManager.evict(constructionExample);
            constructionTypeId = constructionExample.getConstructionType().getConstructionTypeId();
            request.setAttribute("constructionTypeId", constructionTypeId);


            request.setAttribute("defectTypeList", defectTypeManager.getDefectTypesByConstructionTypeId(constructionTypeId));


            request.setAttribute("defectZoneList", defectZoneManager.getDefectZonesByConstructionType(constructionExample.getConstructionType().getConstructionTypeId().toString()));
            request.setAttribute("saveWay", constructionExample.getWayToDefectEsckiz());
            request.setAttribute("loadWay", constructionExample.getWayToDefectEsckiz());

        }

        if (!StringUtils.isEmpty(constructionDefectId)) {
            ConstructionDefect curConstructionDefect = constructionDefectManager.getConstructionDefect(constructionDefectId);
            constructionDefectManager.evict(curConstructionDefect);
            ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(curConstructionDefect.getExampleId().toString());
            constructionExampleManager.evict(constructionExample);
            request.setAttribute("constructionTypeId", constructionExample.getConstructionType().getConstructionTypeId());
            List defectZoneList = defectZoneManager.getDefectZonesByConstructionType(constructionExample.getConstructionType().getConstructionTypeId().toString());
            constructionExampleManager.evict(defectZoneList);
            request.setAttribute("defectZoneList", defectZoneList);
            request.setAttribute("saveWay", constructionExample.getWayToDefectEsckiz());
            request.setAttribute("loadWay", constructionExample.getWayToDefectEsckiz());
        }

        //   if (request.getAttribute("defectVarityList") == null) {
        //      request.setAttribute("defectVarityList", new ArrayList());
        //      request.setAttribute("noVarityList", "true");
        //   }
        //    if (request.getAttribute("reasonList") == null) {
        //        request.setAttribute("reasonList", new ArrayList());
        //       request.setAttribute("noReasonList", "true");
        //   }

        System.out.println("loading all necessary reasons and varities");
        
        if (!isFormSubmission(request)) {

            request.getSession().setAttribute("objectConstructionId", request.getParameter("objectConstructionId"));
            request.setAttribute("objectConstructionId", request.getAttribute("objectConstructionId"));

            ConstructionDefect constructionDefect = null;

            System.out.println("loading all necessary reasons and varities");
           
            if (!StringUtils.isEmpty(constructionDefectId)) {
                constructionDefect = constructionDefectManager.getConstructionDefect(constructionDefectId);
                constructionDefectManager.evict(constructionDefect);
                System.out.println("loading all necessary reasons and varities");//loading all necessary reasons and varities
                request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(constructionDefect.getDefectType().getDefectTypeId().toString()));
                ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());


                request.setAttribute("reasonList", reasonManager.getReasonsByType(constructionDefect.getDefectType().getDefectTypeId().toString(), constructionDefect.getDefectVarity().getVarityId().toString(), constructionDefect.getDefectZone().getDefectZoneId().toString(), String.valueOf(constructionTypeId)));

            } else {
                constructionDefect = new ConstructionDefect();
                request.setAttribute("defectVarityList", new ArrayList());
                request.setAttribute("reasonList", new ArrayList());

            }
            String exampleId = request.getParameter("exampleId");
            if (exampleId != null) {
                constructionDefect.setExampleId(new Integer(exampleId));
            }
            if (request.getParameter("edited") != null) {
                request.setAttribute("addition", "?edited=1");
                constructionDefect.setEdited(true);
            }
            constructionDefect.getDefectVarity().getVarityName();

            constructionDefectManager.makeNotNull(constructionDefect);
            if (request.getAttribute("defectZoneList") == null) {

                request.setAttribute("defectZoneList", new ArrayList());
                request.setAttribute("noDefectZoneList", "true");
            }
            constructionDefect.setDocLocation(request.getParameter("docLocation"));

            return constructionDefect;
        }
        return super.formBackingObject(request);
    }

    public ModelAndView onSubmit
            (HttpServletRequest request,
             HttpServletResponse response,
             Object command,
             BindException errors)
            throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("ientering 'onSubmit' method...");
        }

        ConstructionDefect constructionDefect = (ConstructionDefect) command;
        boolean isNew = (constructionDefect.getConstructionDefectId() == null);
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            constructionDefectManager.removeConstructionDefect(constructionDefect.getConstructionDefectId().toString());

            saveMessage(request, getText("constructionDefect.deleted", locale));
        } else {

            Integer reasonId = constructionDefect.getReason().getReasonId();
            if (!reasonId.equals(new Integer(-1))) {
                constructionDefect.setReason(reasonManager.getReason(reasonId.toString()));
            } else {
                constructionDefect.setReason(null);
            }

            Integer defectZoneId = constructionDefect.getDefectZone().getDefectZoneId();
            if (!defectZoneId.equals(new Integer(-1))) {
                constructionDefect.setDefectZone(defectZoneManager.getDefectZone(defectZoneId.toString()));
            }
            Integer defectTypeId = constructionDefect.getDefectType().getDefectTypeId();
            if (!defectTypeId.equals(new Integer(-1))) {
                constructionDefect.setDefectType(defectTypeManager.getDefectType(defectTypeId.toString()));
            }

            Integer dangerCategoryId = constructionDefect.getDangerCategory().getDangerCategoryId();
            if (!dangerCategoryId.equals(new Integer(-1))) {
                constructionDefect.setDangerCategory(dangerCategoryManager.getDangerCategory(dangerCategoryId.toString()));
            }

            Integer defectVarityId = constructionDefect.getDefectVarity().getVarityId();

            if (!defectVarityId.equals(new Integer(-1))) {
                constructionDefect.setDefectVarity(defectVarityManager.getDefectVarity(defectVarityId.toString()));
            }

            List authentifications = new ArrayList();
            //  List authentificationsFromForm = constructionDefect.getAuthentifications();

            /*  for (int i = 0; i < authentificationsFromForm.size(); i++) {
                DefectAuthentification authentificationElement = (DefectAuthentification) authentificationsFromForm.get(i);
                authentificationElement.setAuthentificationElement(authentificationElementManager.getAuthentificationElement(authentificationElement.getAuthentificationElement().getAuthentificationId().toString()));
                authentifications.add(authentificationElement);
            }*/
            //   constructionDefect.setAuthentifications(authentifications);

            List parameters = new ArrayList();
            List parametersFromForm = constructionDefect.getDefectParameters();

            for (int i = 0; i < parametersFromForm.size(); i++) {
                DefectParameter defectParameter = (DefectParameter) parametersFromForm.get(i);
                defectParameter.setParameter(parameterManager.getParameter(defectParameter.getParameter().getParameterId().toString()));

                parameters.add(defectParameter);
            }
            constructionDefect.setDefectParameters(parameters);


            MultipartHttpServletRequest multipartRequest =
                    (MultipartHttpServletRequest) request;
            CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
            if (file.getSize() > 0) {
                String fileName = ImageUtil.getUniqueJPEGFile(request);
                FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + fileName);
                fileOutputStream.write(file.getBytes());
                fileOutputStream.close();
                InputStream imageStream = ImageUtil.scaleImage(new FileInputStream(FileHelper.getCurrentPath(request) + fileName), 400, 400);

                fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + fileName);
                byte[] bufer = new byte[62000];
                while (imageStream.read(bufer) != -1) {
                    fileOutputStream.write(bufer);
                }
                fileOutputStream.close();
                constructionDefect.setWayToPhoto(fileName);
                constructionDefect.setPhotoBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + fileName)));
            }
            //retrieving the constructionExample on which this defect will be assigned
            ConstructionExample constructionExample1 = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
            //setting the unique number of defect
            if (isNew) {
                constructionDefect.setDefectRelativeNumber(constructionDefectManager.getConstructionDefectRelativeNumberByObjectConstructionId(String.valueOf(constructionExample1.getObjectConstructionId())));

            } else {
                constructionDefectManager.evict(constructionDefect);
            }
            //   constructionDefect.setConstructionDefectPosition(new Integer(0));
            //    constructionDefectManager.saveConstructionDefect(constructionDefect);


            if (constructionExample1.getWayToDefectEsckiz() != null) {
                File eskizFile = new File(FileHelper.getCurrentPath(request) + constructionExample1.getWayToDefectEsckiz());
                if (eskizFile.exists()) {
                    constructionExample1.setDefectEsckizBlob(Hibernate.createBlob(new FileInputStream(FileHelper.getCurrentPath(request) + constructionExample1.getWayToDefectEsckiz())));
                }
            }
            constructionExampleManager.saveConstructionExample(constructionExample1);
            constructionExampleManager.evict(constructionExample1);
            if (isNew) {
                ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
                constructionExample.getExampleDefects().add(constructionDefect);
                constructionExampleManager.saveConstructionExample(constructionExample);

                ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
                String fileName = FileHelper.getCurrentPath(request) + objectConstruction.getWayToDefectMap();
                paintStrength(constructionExample, constructionDefect, fileName, FileHelper.getCurrentPath(request), "save");


            } else {
                constructionDefectManager.saveConstructionDefect(constructionDefect);

                ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
                ObjectConstruction objectConstruction = objectConstructionManager.getObjectConstruction(constructionExample.getObjectConstructionId().toString());
                String fileName = FileHelper.getCurrentPath(request) + objectConstruction.getWayToDefectMap();
                paintStrength(constructionExample, constructionDefect, fileName, FileHelper.getCurrentPath(request), "update");


            }
            String key = (isNew) ? "constructionDefect.added" : "constructionDefect.updated";
            saveMessage(request, getText(key, locale));
            if (constructionDefect.isEdited()) {
                return new ModelAndView("redirect:updating.html?&objectConstructionId=" + request.getSession().getAttribute("objectConstructionId") + "&id=" + constructionDefect.getConstructionDefectId() + "&fieldId=" + request.getParameter("fieldId"));
            }

        }
        request.getSession().setAttribute("madeChanges", new Boolean(true));
        return new ModelAndView("redirect:" + constructionDefect.getDocLocation());
    }


    public ModelAndView processFormSubmission
            (HttpServletRequest request,
             HttpServletResponse response,
             Object command,
             BindException errors)
            throws Exception {


         System.out.println("iFrom backing object method ...");
        ConstructionDefect constructionDefect = (ConstructionDefect) command;
        if (!constructionDefect.getDefectType().getDefectTypeId().equals(new Integer(-1))) {

            //loading the defect varity and reason of this type
            /////       request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(constructionDefect.getDefectType().getDefectTypeId().toString()));
            ConstructionExample constructionExample = constructionExampleManager.getConstructionExample(constructionDefect.getExampleId().toString());
            String constructionType = constructionExample.getConstructionType().getConstructionTypeId().toString();
            //   request.setAttribute("reasonList", reasonManager.getReasonsByType(constructionDefect.getDefectType().getDefectTypeId().toString(), constructionDefect.getDefectVarity().getVarityId().toString(), constructionDefect.getDefectZone().getDefectZoneId().toString(), constructionType));
            request.setAttribute("constructionType", constructionType);
            request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(constructionDefect.getDefectType().getDefectTypeId().toString()));
            request.setAttribute("reasonList", reasonManager.getReasonsByType(constructionDefect.getDefectType().getDefectTypeId().toString(), constructionDefect.getDefectVarity().getVarityId().toString(), constructionDefect.getDefectZone().getDefectZoneId().toString(), String.valueOf(constructionType)));

        } else {
            request.setAttribute("defectVarityList", new ArrayList());
            request.setAttribute("reasonList", new ArrayList());

        }

        if (request.getParameter("save") == null) {
            if (!constructionDefect.getDefectVarity().getVarityId().equals(new Integer(-1))) {

                //loading defect parameters
                List parameterList = parameterManager.getParametersByVarity(constructionDefect.getDefectVarity().getVarityId().toString());
                List parameterDefectList = new ArrayList();
                for (int i = 0; i < parameterList.size(); i++) {
                    Parameter parameter = (Parameter) parameterList.get(i);
                    DefectParameter defectParameter = new DefectParameter();
                    defectParameter.setParameter(parameter);
                    parameterDefectList.add(defectParameter);

                }
                constructionDefect.setDefectParameters(parameterDefectList);
            } else {
                constructionDefect.setDefectParameters(new ArrayList());
            }
        }

        //getting the list of the authentification elements for this zone
        if (!constructionDefect.getDefectZone().getDefectZoneId().equals(new Integer(-1))) {
            List authentificationList = authentificationElementManager.getAuthentificationElementsByZoneId(constructionDefect.getDefectZone().getDefectZoneId().toString());
            List authentificationDefectList = new ArrayList();
            for (int i = 0; i < authentificationList.size(); i++) {
                AuthentificationElement o = (AuthentificationElement) authentificationList.get(i);

                DefectAuthentification defectAuthentification = new DefectAuthentification();
                defectAuthentification.setAuthentificationElement(o);
                authentificationDefectList.add(defectAuthentification);

            }
            //  constructionDefect.setAuthentifications(authentificationDefectList);
        } else {
            //  constructionDefect.setAuthentifications(new ArrayList());

        }

        if (request.getParameter("save") == null) {


            if (request.getParameter("defectTypeSelect") != null) {
                return new ModelAndView("constructionDefectForm", "constructionDefect", constructionDefect);

            }

            if (request.getParameter("defectVaritySelect") != null) {

                return new ModelAndView("constructionDefectForm", "constructionDefect", constructionDefect);

            }


            if (request.getParameter("defectZoneSelect") != null) {

                return new ModelAndView("constructionDefectForm", "constructionDefect", constructionDefect);
            }


            if (request.getParameter("cancel") != null) {
                return new ModelAndView("redirect:constructionDefects.html?objectConstructionId=" + request.getSession().getAttribute("objectConstructionId") + "&exampleId=" + constructionDefect.getExampleId());
            }

            return new ModelAndView("constructionDefectForm", "constructionDefect", constructionDefect);

        }

        return super.processFormSubmission(request,
                response,
                command,
                errors);
    }

    protected void onBind(HttpServletRequest request, Object command) throws Exception {
        // if the user is being deleted, turn off validation
        if (request.getParameter("delete") != null || request.getParameter("defectZoneSelect") != null || request.getParameter("defectVaritySelect") != null || request.getParameter("defectTypeSelect") != null) {
            super.setValidateOnBinding(false);
        } else {
            super.setValidateOnBinding(true);
        }
    }


    private void paintStrength(ConstructionExample constructionExample, ConstructionDefect constructionDefect, String fileName, String fullPath, String action) {


        try {

            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
            ObjectPicture objectPicture = new ObjectPicture();
            try {
                objectPicture = (ObjectPicture) in.readObject();

                if (objectPicture != null) {

                    if (action.equals("update")) {



                        List list = objectPicture.getObjectList();
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i) instanceof Symbols) {
                                Symbols symbols = (Symbols) list.get(i);
                                if (symbols.getType().equals("defect") && symbols.getDefect().getConstructionDefectId().equals(constructionDefect.getConstructionDefectId())) {



                                    List l = symbols.getList();


                                    for (int j = 0; j < l.size(); j++) {



                                        if (l.get(j) instanceof TextLine) {


                                            TextLine textLine = (TextLine) l.get(j);
                                            symbols.getList().remove(textLine);
                                            j--;

                                        }
                                    }

                                    MyPoint point=symbols.getMinPoint();


                                    symbols.getList().add(new TextLine(point.x, point.y-5, String.valueOf("№ " + constructionDefect.getDefectRelativeNumber())));
                                    for (int k = 0; k < constructionDefect.getDefectParameters().size(); k++) {
                                        DefectParameter defectParameter = (DefectParameter) constructionDefect.getDefectParameters().get(k);
                                        symbols.getList().add(new TextLine(point.x, point.y-20 - i * 15, String.valueOf(defectParameter.getParameter() + " " + defectParameter.getParameterValue() + " ")));
                                    }


                                    symbols.setDefect(constructionDefect);
                                    symbols.setType("defect");
                                    symbols.setExample(constructionExample);
                                    symbols.setActivity("old");


                                }
                            }
                        }


                    } else if (action.equals("save")) {
                        DetailSymbol detailSymbol = detailSymbolManager.getDetailSymbol(constructionDefect);
                        objectPicture.addObject(addDetailSymbol(detailSymbol, fullPath, constructionDefect, constructionExample, "defect"));
                    }


                }
            } catch (ClassNotFoundException e) {

            }
            in.close();


            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
            out.writeObject(objectPicture);
            out.close();


        } catch (IOException e) {

        }


    }


    private Symbols addDetailSymbol(DetailSymbol detailSymbol, String fullPath, ConstructionDefect defect, ConstructionExample example, String type) throws ClassNotFoundException {
        ObjectPicture symbolObjectPicture = null;
        if (detailSymbol != null) {
            symbolObjectPicture = ObjectPictureUtil.addDetailSymbol(detailSymbol, fullPath);
        } else {
            symbolObjectPicture = ObjectPictureUtil.addDefaultDetailSymbol(fullPath);
        }


        if ("defect".equals(type) && symbolObjectPicture != null) {

            symbolObjectPicture.addObject(new TextLine(200, 185, String.valueOf("№ " + defect.getDefectRelativeNumber())));
            for (int i = 0; i < defect.getDefectParameters().size(); i++) {
                DefectParameter defectParameter = (DefectParameter) defect.getDefectParameters().get(i);
                symbolObjectPicture.addObject(new TextLine(200, 170 - i * 15, String.valueOf(defectParameter.getParameter() + " " + defectParameter.getParameterValue() + " ")));
            }
        }



        Symbols symbols = new Symbols(symbolObjectPicture);
        symbols.setDefect(defect);
        symbols.setType(type);
        symbols.setExample(example);
        symbols.setActivity("old");


        return symbols;

    }


}
TOP

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

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.