RoutineSaveAction.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.routine;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.RootForm;
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 yanbing.chen
 * @date 2019/8/19
 * @since 1.8
 **/
public class RoutineSaveAction extends SystemSetupAction {

    protected static final String ROUTINE_ACTION_KEY = "routineAction";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        initActionUrl(request);
        request.setAttribute("facility", LocalContext.getFacilityRrn());

        RootForm theform = (RootForm) form;
        String id = theform.getInstanceId().toUpperCase().trim();
        NamedObject routine = new NamedObject(id, getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()),
                                              ObjectList.WFL_KEY);
        routine.setObject(ObjectList.WFL_KEY);
        routine.setObjectType(theform.getObjectType());
        routine.setObjectSubtype(ObjectList.WFL_TRANSACTION_KEY);

        // According to the instance's object, retrieve the value object
        routine = getInstance(routine);

        Assert.isFalse(!StringUtils.equalsIgnoreCase(theform.getObjectType(),routine.getObjectType()) &&
                        routine.getInstanceRrn() > 0,Errors.create().key(MessageIdList.WORKFLOW_OBJECT_TYPE_ERROR)
                             .content("This page is not operational {}").args(routine.getObjectType()).build());

        request.setAttribute("ROUTINE", routine);

        PropertyUtils.copyProperties(theform, routine);

        // if a value object does not exist, its instanceRrn will be set to 0
        if (routine.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);
            request.setAttribute("facility", LocalContext.getFacilityRrn());
            return (mapping.findForward("modify"));
        }

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setInstanceDesc(routine.getInstanceDesc());
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        RootForm theform = (RootForm) form;
        request.setAttribute("facility", LocalContext.getFacilityRrn());
        // if user click the Cancel button
        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }
        request.removeAttribute("ROUTINE");
        theform.setInstanceId("");
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward modify(ActionMapping mapping, RootForm theform, HttpServletRequest request) throws Exception {
        request.setAttribute("facility", LocalContext.getFacilityRrn());
        NamedObject routine = new NamedObject();
        routine.setObject(ObjectList.WFL_KEY);
        routine.setNamedSpace(theform.getNamedSpace());
        routine.setInstanceId(theform.getInstanceId());
        routine.setObjectType(theform.getObjectType());
        routine.setObjectSubtype(ObjectList.WFL_TRANSACTION_KEY);
        routine = getInstance(routine);
        routine.setInstanceDesc(theform.getInstanceDesc());
        // who operate this object?
        routine.setTransPerformedby(LocalContext.getUserId());
        routine.setInstanceDesc(theform.getInstanceDesc());
        if ((theform.getRoleId() != null) && !theform.getRoleId().trim().equals("")) {
            long roleRrn = 0;
            roleRrn = getInstanceRrn(theform.getRoleId().trim(),
                                     getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                                     ObjectList.ROLE_KEY, ObjectList.OPERATION_KEY);
            Assert.isFalse(roleRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("Role").build());
        }
        routine.setTransId(Constants.MODIFY_KEY);
        process(routine);
        request.setAttribute("ROUTINE", routine);
        theform.setTransId(Constants.MODIFY_KEY);
        initActionUrl(request);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward delete(ActionMapping mapping, RootForm theform, HttpServletRequest request) throws Exception {
        request.setAttribute("facility", LocalContext.getFacilityRrn());
        NamedObject routine = new NamedObject();
        routine.setObject(ObjectList.WFL_KEY);
        routine.setNamedSpace(theform.getNamedSpace());
        routine.setInstanceId(theform.getInstanceId());
        routine.setObjectType(theform.getObjectType());
        routine.setObjectSubtype(ObjectList.WFL_TRANSACTION_KEY);
        routine = getInstance(routine);
        routine.setInstanceDesc(theform.getInstanceDesc());
        // who operate this object?
        routine.setTransPerformedby(LocalContext.getUserId());
        routine.setTransId(Constants.DELETE_KEY);
        process(routine);
        request.setAttribute("ROUTINE", routine);
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward create(ActionMapping mapping, RootForm theform, HttpServletRequest request) throws Exception {
        request.setAttribute("facility", LocalContext.getFacilityRrn());
        NamedObject routine = new NamedObject();
        routine.setObject(ObjectList.WFL_KEY);
        routine.setNamedSpace(theform.getNamedSpace());
        routine.setInstanceId(theform.getInstanceId());
        routine.setObjectType(theform.getObjectType());
        routine.setObjectSubtype(ObjectList.WFL_TRANSACTION_KEY);
        routine = getInstance(routine);
        routine.setInstanceDesc(theform.getInstanceDesc());
        // who operate this object?
        routine.setTransPerformedby(LocalContext.getUserId());
        routine.setInstanceDesc(theform.getInstanceDesc());
        if ((theform.getRoleId() != null) && !theform.getRoleId().trim().equals("")) {
            long roleRrn = 0;

            roleRrn = getInstanceRrn(theform.getRoleId().trim(),
                                     getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                                     ObjectList.ROLE_KEY, ObjectList.OPERATION_KEY);
            Assert.isFalse(roleRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("Role").build());
        }
        routine.setTransId(Constants.CREATE_KEY);
        process(routine);
        request.setAttribute("ROUTINE", routine);
        theform.setTransId(Constants.MODIFY_KEY);
        initActionUrl(request);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    protected void initActionUrl(HttpServletRequest request){
        request.setAttribute(ROUTINE_ACTION_KEY, "routinesave.do");
    }

}