StationSaveAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.setting.security.station;

import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.security.Station;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.CimSetupAction;
import com.mycim.webapp.forms.security.StationInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 机台控制权限组定义 基本信息页面
 *
 * @author weike.li
 * @date 2019/8/20
 * @since 1.8
 **/
public class StationSaveAction extends CimSetupAction {

    private static final String ON = "on";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        StationInfoForm theform = (StationInfoForm) form;
        String id = theform.getInstanceId().trim().toUpperCase();
        Station station = new Station(id, getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);

        station = (Station) getInstance(station);
        request.setAttribute("dispatchRuleFlag", station.getDispatchRuleFlag());
        request.setAttribute("checkOperationFlag", station.getCheckOperationFlag());

        PropertyUtils.copyProperties(theform, station);
        theform.setPasswordConfirm(station.getPassword());

        if (station.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);

            return mapping.findForward(Constants.MODIFY_KEY);
        }
        request.setAttribute(SessionNames.STATION_KEY, station);
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        StationInfoForm theform = (StationInfoForm) form;

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }
        theform.setInstanceId("");
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward copy(ActionMapping mapping, StationInfoForm theform,
                              HttpServletRequest request) throws Exception {
        Station station = new Station(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);
        station = (Station) getInstance(station);
        station.setTransPerformedby(LocalContext.getUserId());

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }

        String id = request.getParameter(Constants.COPY_KEY);

        station.setInstanceId(id);

        doCopy(theform, station);

        theform.setTransId(Constants.CREATE_KEY);

        request.setAttribute(SessionNames.STATION_KEY, station);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward type(ActionMapping mapping, StationInfoForm theform, HttpServletRequest request) {
        Station station = new Station(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);
        station = (Station) getInstance(station);
        station.setTransPerformedby(LocalContext.getUserId());

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }

        theform.setTransId(request.getParameter(Constants.TYPE_KEY));
        return mapping.findForward(Constants.DETAIL_KEY);
    }

    public ActionForward delete(ActionMapping mapping, StationInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Station station = new Station(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);
        station = (Station) getInstance(station);
        station.setTransPerformedby(LocalContext.getUserId());

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }

        PropertyUtils.copyProperties(station, theform);

        station.setTransId(Constants.DELETE_KEY);

        theform.setInstanceId("");
        process(station);
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward create(ActionMapping mapping, StationInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Station station = new Station(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);
        station = (Station) getInstance(station);
        station.setTransPerformedby(LocalContext.getUserId());

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }

        PropertyUtils.copyProperties(station, theform);

        station.setTransId(Constants.CREATE_KEY);
        process(station);

        station = (Station) getInstance(station);
        request.setAttribute(SessionNames.STATION_KEY, station);

        request.setAttribute("dispatchRuleFlag", station.getDispatchRuleFlag());
        request.setAttribute("checkOperationFlag", station.getCheckOperationFlag());
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, StationInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Station station = new Station(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.STATION_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.STATION_KEY);
        station = (Station) getInstance(station);
        station.setTransPerformedby(LocalContext.getUserId());

        if (theform.getCheckOperationFlag() == null) {
            theform.setCheckOperationFlag("0");
        } else if (ON.equals(theform.getCheckOperationFlag())) {
            theform.setCheckOperationFlag("1");
        }
        if (theform.getDispatchRuleFlag() == null) {
            theform.setDispatchRuleFlag("0");
        } else if (ON.equals(theform.getDispatchRuleFlag())) {
            theform.setDispatchRuleFlag("1");
        }

        PropertyUtils.copyProperties(station, theform);

        station.setTransId(Constants.MODIFY_KEY);
        process(station);

        station = (Station) getInstance(station);
        request.setAttribute(SessionNames.STATION_KEY, station);

        request.setAttribute("dispatchRuleFlag", station.getDispatchRuleFlag());
        request.setAttribute("checkOperationFlag", station.getCheckOperationFlag());
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

}