ContextSaveAction.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.context;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
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.consts.SessionNames;
import com.mycim.valueobject.prp.Context;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.CtxAction;
import com.mycim.webapp.forms.ContextInfoForm;
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 ContextSaveAction extends CtxAction {

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

        ContextInfoForm theform = (ContextInfoForm) form;

        String id = theform.getInstanceId().trim().toUpperCase();

        Context context = new Context(id, getNamedSpace(ObjectList.CONTEXT_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.CONTEXT_KEY);

        context = (Context) getInstance(context);

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(theform, context);
        context.setLanguage(I18nUtils.getCurrentLanguage().toString());
        context = ctxService.getContextLbl(context);
        request.setAttribute(SessionNames.CONTEXT_KEY, context);

        if (StringUtils.isNotEmpty(request.getParameter("contextEdit"))) {
            return (mapping.findForward(Constants.VALUES_KEY));
        }
        if (context.getInstanceRrn() <= 0) {
            theform.setTransId(Constants.CREATE_KEY);
            theform.setInheritFlag("O");

            return (mapping.findForward(Constants.MODIFY_KEY));
        } else {
            theform.setTransId(Constants.MODIFY_KEY);

            request.setAttribute(SessionNames.CONTEXT_KEY, context);
            return (mapping.findForward(Constants.MODIFY_KEY));
        }
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ContextInfoForm theform = (ContextInfoForm) form;
        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }

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

    public ActionForward copy(ActionMapping mapping, ContextInfoForm theform,
                              HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

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

        context.setInstanceId(id);

        doCopy(theform, context);

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

        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.MODIFY_KEY));
    }

    public ActionForward create(ActionMapping mapping, ContextInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(context, theform);
        String roleId = theform.getRoleId().trim();

        if (roleId.length() > 0) {
            long roleRrn = getInstanceRrn(roleId, getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                                          ObjectList.ROLE_KEY);

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

            context.setRoleRrn(new Long(roleRrn));
        } else {
            context.setRoleRrn(null);
        }

        // validate all of search (add by kane)
        validateForm(theform, LocalContext.getFacilityRrn());
        context.setTransId(Constants.CREATE_KEY);

        this.process(context);
        context = (Context) getInstance(context);
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.MODIFY_KEY));
    }

    public ActionForward modify(ActionMapping mapping, ContextInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(context, theform);
        String roleId = theform.getRoleId().trim();

        if (roleId.length() > 0) {
            long roleRrn = getInstanceRrn(roleId, getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                                          ObjectList.ROLE_KEY);

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

            context.setRoleRrn(new Long(roleRrn));
        } else {
            context.setRoleRrn(null);
        }

        // validate all of search (add by kane)
        validateForm(theform, LocalContext.getFacilityRrn());
        context.setTransId(Constants.MODIFY_KEY);

        this.process(context);
        context = (Context) getInstance(context);
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.MODIFY_KEY));
    }

    public ActionForward delete(ActionMapping mapping, ContextInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

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

        context.setTransId(Constants.DELETE_KEY);
        theform.setInstanceId("");

        this.process(context);
        context = (Context) getInstance(context);

        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.SETUP_KEY));
    }

    public ActionForward rule(ActionMapping mapping, ContextInfoForm theform,
                              HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(context, theform);
        context.setRules(this.getContextRules(context.getInstanceRrn()));

        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.RULES_KEY));
    }

    public ActionForward value(ActionMapping mapping, ContextInfoForm theform,
                               HttpServletRequest request) throws Exception {
        Context context = new Context(theform.getInstanceId(), theform.getNamedSpace(), ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        context.setTransPerformedby(LocalContext.getUserId());

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

        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.VALUES_KEY));
    }

    public void validateForm(ContextInfoForm theform, Long facility) throws Exception {
        // for the No.1
        if (!theform.getContextField1().trim().equals("")) {
            checkField(theform.getContextType1(), theform.getContextField1().trim(), "ContextField1", facility);
        }

        if (!theform.getContextField2().trim().equals("")) {
            checkField(theform.getContextType2(), theform.getContextField2().trim(), "ContextField2", facility);
        }

        if (!theform.getContextField3().trim().equals("")) {
            checkField(theform.getContextType3(), theform.getContextField3().trim(), "ContextField3", facility);
        }

        if (!theform.getContextField4().trim().equals("")) {
            checkField(theform.getContextType4(), theform.getContextField4().trim(), "ContextField4", facility);
        }

        if (!theform.getContextField5().trim().equals("")) {
            checkField(theform.getContextType5(), theform.getContextField5().trim(), "ContextField5", facility);
        }

        if (!theform.getContextField6().trim().equals("")) {
            checkField(theform.getContextType6(), theform.getContextField6().trim(), "ContextField6", facility);
        }

        if (!theform.getContextField7().trim().equals("")) {
            checkField(theform.getContextType7(), theform.getContextField7().trim(), "ContextField7", facility);
        }

        if (!theform.getContextField8().trim().equals("")) {
            checkField(theform.getContextType8(), theform.getContextField8().trim(), "ContextField8", facility);
        }

        if (!theform.getContextField9().trim().equals("")) {
            checkField(theform.getContextType9(), theform.getContextField9().trim(), "ContextField9", facility);
        }

        if (!theform.getContextField10().trim().equals("")) {
            checkField(theform.getContextType10(), theform.getContextField10().trim(), "ContextField10", facility);
        }

        if (!theform.getContextField11().trim().equals("")) {
            checkField(theform.getContextType11(), theform.getContextField11().trim(), "ContextField11", facility);
        }

        if (!theform.getContextField12().trim().equals("")) {
            checkField(theform.getContextType12(), theform.getContextField12().trim(), "ContextField12", facility);
        }

        if (!theform.getContextField13().trim().equals("")) {
            checkField(theform.getContextType13(), theform.getContextField13().trim(), "ContextField13", facility);
        }

        if (!theform.getContextField14().trim().equals("")) {
            checkField(theform.getContextType14(), theform.getContextField14().trim(), "ContextField14", facility);
        }

        if (!theform.getContextField15().trim().equals("")) {
            checkField(theform.getContextType15(), theform.getContextField15().trim(), "ContextField15", facility);
        }

        if (!theform.getResultField1().trim().equals("")) {
            checkField(theform.getResultType1(), theform.getResultField1().trim(), "ResultField1", facility);
        }

        if (!theform.getResultField2().trim().equals("")) {
            checkField(theform.getResultType2(), theform.getResultField2().trim(), "ResultField2", facility);
        }

        if (!theform.getResultField3().trim().equals("")) {
            checkField(theform.getResultType3(), theform.getResultField3().trim(), "ResultField3", facility);
        }

        if (!theform.getResultField4().trim().equals("")) {
            checkField(theform.getResultType4(), theform.getResultField4().trim(), "ResultField4", facility);
        }

        if (!theform.getResultField5().trim().equals("")) {
            checkField(theform.getResultType5(), theform.getResultField5().trim(), "ResultField5", facility);
        }

        if (!theform.getResultField6().trim().equals("")) {
            checkField(theform.getResultType6(), theform.getResultField6().trim(), "ResultField6", facility);
        }

        if (!theform.getResultField7().trim().equals("")) {
            checkField(theform.getResultType7(), theform.getResultField7().trim(), "ResultField7", facility);
        }

        if (!theform.getResultField8().trim().equals("")) {
            checkField(theform.getResultType8(), theform.getResultField8().trim(), "ResultField8", facility);
        }

        if (!theform.getResultField9().trim().equals("")) {
            checkField(theform.getResultType9(), theform.getResultField9().trim(), "ResultField9", facility);
        }

        if (!theform.getResultField10().trim().equals("")) {
            checkField(theform.getResultType10(), theform.getResultField10().trim(), "ResultField10", facility);
        }

        if (!theform.getResultField11().trim().equals("")) {
            checkField(theform.getResultType11(), theform.getResultField11().trim(), "ResultField11", facility);
        }

        if (!theform.getResultField12().trim().equals("")) {
            checkField(theform.getResultType12(), theform.getResultField12().trim(), "ResultField12", facility);
        }

        if (!theform.getResultField13().trim().equals("")) {
            checkField(theform.getResultType13(), theform.getResultField13().trim(), "ResultField13", facility);
        }

        if (!theform.getResultField14().trim().equals("")) {
            checkField(theform.getResultType14(), theform.getResultField14().trim(), "ResultField14", facility);
        }

        if (!theform.getResultField15().trim().equals("")) {
            checkField(theform.getResultType15(), theform.getResultField15().trim(), "ResultField15", facility);
        }
    }

    private void checkField(String objectType, String fieldValue, String fieldName, Long facility) {
        long rrn = 0;
        String objectValue = null;

        if (objectType.equalsIgnoreCase("field")) {
            rrn = getInstanceRrn(fieldValue, getNamedSpace(ObjectList.FIELD_KEY, facility), ObjectList.FIELD_KEY);

            Assert.isFalse(rrn <= 0, Errors.create().key(MessageIdList.CONTEXT_FIELD_INVALID).content("在{}中发现无效字段")
                                           .args(fieldName).build());
        } else if (objectType.equalsIgnoreCase("OBJECTTYPE")) {
            objectValue = this.getInfoToShow("$$OBJECT", fieldValue);

            Assert.isFalse(objectValue.equals(null) || objectValue.equals("N/A"),
                           Errors.create().key(MessageIdList.CONTEXT_OBJECT_INVALID).content("在{}处发现无效对象")
                                 .args(fieldName).build());
        } else if (objectType.equalsIgnoreCase("REFERENCEFILE")) {
            rrn = getInstanceRrn(fieldValue, getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facility),
                                 ObjectList.REFERENCE_FILE_KEY);

            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.CONTEXT_REFERENCE_FILE_INVALID).content("在{}处发现无效的引用文件")
                                 .args(fieldName).build());
        }
    }

    private String getInfoToShow(String tableName, String dataToStore) {
        return sysService.translateRefValue(tableName, dataToStore);
    }

}