AllowableEventSetSaveAction.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.system.event;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.AllowableEventSet;
import com.mycim.valueobject.ems.Event;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.event.AllowableEventSetInfoForm;
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 yanbing.chen
 * @date 2019/8/20
 * @since 1.8
 **/
public class AllowableEventSetSaveAction extends SystemSetupAction {

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

        long facilityRrn = LocalContext.getFacilityRrn();
        AllowableEventSetInfoForm theform = (AllowableEventSetInfoForm) form;

        // Populate the new valid format id
        String id = theform.getInstanceId().trim().toUpperCase();

        AllowableEventSet allowableEventSet = new AllowableEventSet(id, getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                      facilityRrn),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);

        allowableEventSet = (AllowableEventSet) getInstance(allowableEventSet);

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(theform, allowableEventSet);

        if (allowableEventSet.getInstanceRrn() <= 0) {
            theform.setTransId(Constants.CREATE_KEY);
        } else {
            theform.setTransId(Constants.MODIFY_KEY);
            allowableEventSet.setAvailableEvents(baseService.getAvailableEvents(allowableEventSet.getInstanceRrn()));
        }

        request.setAttribute(SessionNames.ALLOWABLEEVENTSET_KEY, allowableEventSet);

        // the following step will occur when in a one2many relation
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        AllowableEventSetInfoForm theform = (AllowableEventSetInfoForm) form;
        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }
        request.removeAttribute(SessionNames.ALLOWABLEEVENTSET_KEY);
        theform.setInstanceId("");
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward copy(ActionMapping mapping, AllowableEventSetInfoForm theform,
                              HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();

        AllowableEventSet allowableEventSet = new AllowableEventSet(theform.getInstanceId(),
                                                                    getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);

        PropertyUtils.copyProperties(allowableEventSet, theform);

        allowableEventSet.setInstanceRrn(getInstanceRrn(theform.getInstanceId(),
                                                        getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.ALLOWABLEEVENTSET_KEY));
        // who operate this object?
        allowableEventSet.setTransPerformedby(user);

        // the new Id is stored in the url COPY key
        String id = request.getParameter(Constants.COPY_KEY);

        allowableEventSet.setInstanceId(id);

        doCopy(theform, allowableEventSet);

        // for logic equal tag, copy is the same action as create
        theform.setTransId(Constants.CREATE_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward members(ActionMapping mapping, AllowableEventSetInfoForm theform,
                                 HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        AllowableEventSet allowableEventSet = new AllowableEventSet(theform.getInstanceId(),
                                                                    getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);
        PropertyUtils.copyProperties(allowableEventSet, theform);

        allowableEventSet.setInstanceRrn(getInstanceRrn(theform.getInstanceId(),
                                                        getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.ALLOWABLEEVENTSET_KEY));
        // who operate this object?
        allowableEventSet.setTransPerformedby(user);

        Event availableEvent = null;
        theform.setTransId(Constants.MODIFY_KEY);

        long eventRrn = 0;

        if (request.getParameter(Constants.MEMBERS_KEY).equals(Constants.DELETE_KEY)) {
            if (request.getParameter(Constants.ITEM_KEY) != null) {
                eventRrn = Long.parseLong(request.getParameter(Constants.ITEM_KEY));
                availableEvent = new Event();
                allowableEventSet.setTransId(Constants.DELETE_KEY);
            }
        } else {
            String availableEventId = theform.getAvailableEventId().trim().toUpperCase();

            Assert.isFalse(StringUtils.isEmpty(availableEventId),
                           Errors.create().key(MessageIdList.EVENT_NOEXIST_ID).content("事件号不存在或者为空值!").build());

            availableEvent = new Event(availableEventId, getNamedSpace(ObjectList.EVENT_KEY, facilityRrn),
                                       ObjectList.EVENT_KEY);

            eventRrn = baseService.getNamedObjectRrn(availableEvent);

            // if no such event exist, do nothing
            Assert.isFalse(eventRrn <= 0,
                           Errors.create().key(MessageIdList.EVENT_NOEXIST_ID).content("事件号不存在或者为空值!").build());

            allowableEventSet.setTransId(Constants.CREATE_KEY);
        }

        availableEvent.setInstanceRrn(eventRrn);

        processDetails(allowableEventSet, availableEvent);
        allowableEventSet.setAvailableEvents(this.baseService.getAvailableEvents(allowableEventSet.getInstanceRrn()));
        // clear id
        theform.setAvailableEventId(null);
        request.setAttribute(SessionNames.ALLOWABLEEVENTSET_KEY, allowableEventSet);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward create(ActionMapping mapping, AllowableEventSetInfoForm theform,
                                HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        AllowableEventSet allowableEventSet = new AllowableEventSet(theform.getInstanceId(),
                                                                    getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);

        PropertyUtils.copyProperties(allowableEventSet, theform);

        allowableEventSet.setInstanceRrn(getInstanceRrn(theform.getInstanceId(),
                                                        getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.ALLOWABLEEVENTSET_KEY));
        // who operate this object?
        allowableEventSet.setTransPerformedby(user);
        PropertyUtils.copyProperties(allowableEventSet, theform);

        if (theform.getRoleId().trim().length() > 0) {
            long roleRrn = getInstanceRrn(theform.getRoleId().trim(), getNamedSpace(ObjectList.ROLE_KEY, facilityRrn),
                                          ObjectList.ROLE_KEY);
            Assert.isFalse(roleRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("Role").build());

            allowableEventSet.setRoleRrn(roleRrn);
        } else {
            allowableEventSet.setRoleRrn(null);
        }
        allowableEventSet.setTransId(Constants.CREATE_KEY);

        // no matter add or modify, they all execute the same function
        process(allowableEventSet);
        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.ALLOWABLEEVENTSET_KEY, allowableEventSet);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, AllowableEventSetInfoForm theform,
                                HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        AllowableEventSet allowableEventSet = new AllowableEventSet(theform.getInstanceId(),
                                                                    getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);

        PropertyUtils.copyProperties(allowableEventSet, theform);

        allowableEventSet.setInstanceRrn(getInstanceRrn(theform.getInstanceId(),
                                                        getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.ALLOWABLEEVENTSET_KEY));
        // who operate this object?
        allowableEventSet.setTransPerformedby(user);

        PropertyUtils.copyProperties(allowableEventSet, theform);

        if (theform.getRoleId().trim().length() > 0) {
            long roleRrn = getInstanceRrn(theform.getRoleId().trim(), getNamedSpace(ObjectList.ROLE_KEY, facilityRrn),
                                          ObjectList.ROLE_KEY);

            Assert.isFalse(roleRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("Role").build());

            allowableEventSet.setRoleRrn(roleRrn);
        } else {
            allowableEventSet.setRoleRrn(null);
        }
        allowableEventSet.setTransId(Constants.MODIFY_KEY);
        theform.setTransId(Constants.MODIFY_KEY);
        // no matter add or modify, they all execute the same function
        process(allowableEventSet);
        allowableEventSet.setAvailableEvents(baseService.getAvailableEvents(allowableEventSet.getInstanceRrn()));
        request.setAttribute(SessionNames.ALLOWABLEEVENTSET_KEY, allowableEventSet);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward delete(ActionMapping mapping, AllowableEventSetInfoForm theform,
                                HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();
        AllowableEventSet allowableEventSet = new AllowableEventSet(theform.getInstanceId(),
                                                                    getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                                    ObjectList.ALLOWABLEEVENTSET_KEY);

        PropertyUtils.copyProperties(allowableEventSet, theform);

        allowableEventSet.setInstanceRrn(getInstanceRrn(theform.getInstanceId(),
                                                        getNamedSpace(ObjectList.ALLOWABLEEVENTSET_KEY,
                                                                      LocalContext.getFacilityRrn()),
                                                        ObjectList.ALLOWABLEEVENTSET_KEY));
        allowableEventSet.setTransPerformedby(user);

        allowableEventSet.setTransId(Constants.DELETE_KEY);

        theform.setInstanceId("");
        // no matter add or modify, they all execute the same function
        process(allowableEventSet);

        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.ALLOWABLEEVENTSET_KEY, allowableEventSet);
        return mapping.findForward(Constants.SETUP_KEY);
    }

    private void processDetails(AllowableEventSet allowableEventSet, Event availableEvent) {
        Relation relation = new Relation();
        long toRrn = availableEvent.getInstanceRrn();
        long fromRrn = allowableEventSet.getInstanceRrn();

        if ((fromRrn != 0) && (toRrn != 0)) {
            relation.setFromRrn(fromRrn);
            relation.setToRrn(toRrn);
            relation.setTransId(allowableEventSet.getTransId());
            relation.setTransPerformedby(allowableEventSet.getTransPerformedby());

            relation.setLinkType(LinkTypeList.ALLOWABLEEVENT_EVENT_KEY);

            if (allowableEventSet.getTransId().equals(Constants.CREATE_KEY)) {
                // avoid duplication
                baseService.addAvailableEventToAllowableEventSet(relation);
            } else if (allowableEventSet.getTransId().equals(Constants.DELETE_KEY)) {
                baseService.removeAvailableEventFromAllowableEventSet(relation);
            }
        }
    }

}