Package com.vst.webapp.action

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

package com.vst.webapp.action;

import com.vst.model.DefectType;
import com.vst.model.Reason;
import com.vst.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

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

public class ReasonFormController extends BaseFormController {
    private ReasonManager reasonManager = null;
    private ReasonNameManager reasonNameManager = null;
    private DefectTypeManager defectTypeManager = null;
    private DefectVarityManager defectVarityManager = null;
    private DefectZoneManager defectZoneManager = null;
    private ConstructionTypeManager constructionTypeManager = null;


    public void setReasonNameManager(ReasonNameManager reasonNameManager) {
        this.reasonNameManager = reasonNameManager;
    }

    public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
        this.constructionTypeManager = constructionTypeManager;
    }

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

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

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

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

    public ReasonFormController() {
        setCommandName("reason");
        setCommandClass(Reason.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {


        request.setAttribute("constructionTypeList", constructionTypeManager.getConstructionTypes(null));
        request.setAttribute("reasonNameList", reasonNameManager.getReasonNames(null));

        if (!isFormSubmission(request)) {
            String reasonId = request.getParameter("reasonId");
            Reason reason = null;

            if (!StringUtils.isEmpty(reasonId)) {
                reason = reasonManager.getReason(reasonId);
                request.setAttribute("defectTypeList", defectTypeManager.getDefectTypesByConstructionTypeId(reason.getConstructionType().getConstructionTypeId()));
                request.setAttribute("defectZoneList", defectZoneManager.getDefectZonesByConstructionType(reason.getConstructionType().getConstructionTypeId().toString()));
                request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(reason.getDefectType().getDefectTypeId().toString()));

                reasonManager.evict(reason);
            } else {
                reason = new Reason();
            }
            if (request.getAttribute("defectZoneList") == null && StringUtils.isEmpty(reasonId)) {
                request.setAttribute("defectZoneList", new ArrayList());
                request.setAttribute("emptyDefectZoneList", "true");
            }

            if (request.getAttribute("defectVarityList") == null && StringUtils.isEmpty(reasonId)) {
                request.setAttribute("defectVarityList", new ArrayList());
                request.setAttribute("emptyDefectVarityList", "true");
            }

            if (request.getAttribute("defectTypeList") == null && StringUtils.isEmpty(reasonId)) {
                request.setAttribute("defectTypeList", new ArrayList());
                request.setAttribute("emptyDefectTypeList", "true");
            }

            if (request.getParameter("edited") != null) {
                request.setAttribute("addition", "?edited=1");
                reason.setEdited(true);
            }
            reason.setDocLocation(request.getParameter("docLocation"));

            return reason;
        }

        return super.formBackingObject(request);
    }

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

        Reason reason = (Reason) command;
        boolean isNew = (reason.getReasonId() == null);
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            reasonManager.removeReason(reason.getReasonId().toString());

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

//            List reasonDefects = new ArrayList();
//            List reasonDefectsFromForm = reason.getDefects();
//            for (int i = 0; i < reasonDefectsFromForm.size(); i++) {
//                DefectType defectType = (DefectType) reasonDefectsFromForm.get(i);
//
//
//                reasonDefects.add(defectTypeManager.getDefectType(defectType.getDefectTypeId().toString()));
//            }
//
//            reason.setDefects(reasonDefects);

            Integer constructionTypeId = reason.getConstructionType().getConstructionTypeId();
            if (!constructionTypeId.equals(new Integer(-1))) {
                reason.setConstructionType(constructionTypeManager.getConstructionType(constructionTypeId.toString()));
            }

            Integer defectZoneId = reason.getDefectZone().getDefectZoneId();
            if (!defectZoneId.equals(new Integer(-1))) {
                reason.setDefectZone(defectZoneManager.getDefectZone(defectZoneId.toString()));
            }


            Integer defectTypeId = reason.getDefectType().getDefectTypeId();
            if (!defectTypeId.equals(new Integer(-1))) {
                reason.setDefectType(defectTypeManager.getDefectType(defectTypeId.toString()));
            }

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

            Integer reasonNameId = reason.getReasonName().getReasonNameId();
            if (!reasonNameId.equals(new Integer(-1))) {
                reason.setReasonName(reasonNameManager.getReasonName(reasonNameId.toString()));
            }


            reasonManager.saveReason(reason);

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

        return new ModelAndView("redirect:" + reason.getDocLocation());
    }

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

        Reason reason = (Reason) command;
        request.setAttribute("defectZoneList", new ArrayList());
        request.setAttribute("defectVarityList", new ArrayList());
        if (!reason.getConstructionType().getConstructionTypeId().equals(new Integer(-1))) {
            request.setAttribute("defectZoneList", defectZoneManager.getDefectZonesByConstructionType(reason.getConstructionType().getConstructionTypeId().toString()));
            request.setAttribute("defectTypeList", defectTypeManager.getDefectTypesByConstructionTypeId(reason.getConstructionType().getConstructionTypeId()));
        }

        if (!reason.getDefectType().getDefectTypeId().equals(new Integer(-1))) {
            request.setAttribute("defectVarityList", defectVarityManager.getDefectVaritiesByType(reason.getDefectType().getDefectTypeId().toString()));
        }


        if (request.getParameter("constructionTypeSelect") != null || request.getParameter("defectTypeSelect") != null) {

            return new ModelAndView("reasonForm", "reason", reason);
        }


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

}


TOP

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

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.