ReferenceFileSaveAction.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.referenceFile;

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.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.sys.ReferenceFile;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.CimSetupAction;
import com.mycim.webapp.forms.system.ReferenceFileInfoForm;
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;
import java.util.HashMap;
import java.util.List;

/**
 * 类表定义
 *
 * @author weike.li
 * @date 2019/8/20
 * @since 1.8
 **/
public class ReferenceFileSaveAction extends CimSetupAction {

    private static final Long NOTINPUT = new Long(-1);

    private static final int NUM_TWO = 2;

    private static final int NUM_THREE = 3;

    private static final int NUM_FORE = 4;

    private static final int NUM_FIVE = 5;

    private static final int NUM_SIX = 6;

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

        ReferenceFileInfoForm theform = (ReferenceFileInfoForm) form;
        String id = "";
        String formId = theform.getInstanceId();
        if(StringUtils.isNotEmptyTrim(formId)){
            id = theform.getInstanceId().trim().toUpperCase();
        }
        String flag = request.getParameter("flag");
        String type = request.getParameter("type");
        if(StringUtils.isNotEmptyTrim(flag)){
            request.setAttribute("flag",flag);
        }
        if(StringUtils.isNotEmptyTrim(type)){
            request.setAttribute("type",type);
        }
        ReferenceFile referenceFile = new ReferenceFile(id, getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                          LocalContext.getFacilityRrn()),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile = (ReferenceFile) getInstance(referenceFile);
        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);
        PropertyUtils.copyProperties(theform, referenceFile);

        if (referenceFile.getInstanceRrn() <= 0) {
            theform.setTransId(Constants.CREATE_KEY);
            return (mapping.findForward(Constants.MODIFY_KEY));
        }
        theform.setTransId(Constants.MODIFY_KEY);
        theform.setDatabasePassword(referenceFile.getPassword());
        theform.setPassword("");

        return mapping.findForward(Constants.MODIFY_KEY);
    }

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

        request.removeAttribute(SessionNames.REFERENCE_FILE_KEY);
        theform.setInstanceId("");
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward copy(ActionMapping mapping, ReferenceFileInfoForm theform,
                              HttpServletRequest request) throws Exception {
        String user = LocalContext.getUserId();
        String instanceId = theform.getInstanceId().trim().toUpperCase();
        ReferenceFile referenceFile = new ReferenceFile(instanceId, getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                        ObjectList.REFERENCE_FILE_KEY);
        referenceFile.setTransPerformedby(user);
        String id = request.getParameter(Constants.COPY_KEY);

        referenceFile.setInstanceId(id);

        doCopy(theform, referenceFile);

        theform.setTransId(Constants.CREATE_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward delete(ActionMapping mapping, ReferenceFileInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String instanceId = theform.getInstanceId().trim().toUpperCase();
        ReferenceFile referenceFile = new ReferenceFile(instanceId, getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                        ObjectList.REFERENCE_FILE_KEY);
        referenceFile.setTransPerformedby(user);

        referenceFile.setTransId(Constants.DELETE_KEY);

        process(referenceFile);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFile.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "DATA_1_VALUE");

            HashMap map = new HashMap(5);
            map.put("reftable", referenceFile);
            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTableDef");

            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {
                message = this.connectMesSystem(map, facilityRrn, user);

                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        theform.setInstanceId("");

        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward modify(ActionMapping mapping, ReferenceFileInfoForm theform,
                                HttpServletRequest request) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String instanceId = theform.getInstanceId().trim().toUpperCase();
        ReferenceFile referenceFile = new ReferenceFile(instanceId, getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile.setTransPerformedby(user);
        String source = null;

        Long[] rrn = new Long[7];

        if ((rrn[0] = getFieldRrn(theform.getKey1FieldId(), facilityRrn)).longValue() == 0) {
            source = "Key_1_field";
        } else if ((rrn[1] = getFieldRrn(theform.getKey2FieldId(), facilityRrn)).longValue() == 0) {
            source = "Key_2_field";
        } else if ((rrn[NUM_TWO] = getFieldRrn(theform.getData1FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_1_field";
        } else if ((rrn[NUM_THREE] = getFieldRrn(theform.getData2FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_2_field";
        } else if ((rrn[NUM_FORE] = getFieldRrn(theform.getData3FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_3_field";
        } else if ((rrn[NUM_FIVE] = getFieldRrn(theform.getData4FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_4_field";
        } else if ((rrn[NUM_SIX] = getFieldRrn(theform.getData5FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_5_field";
        }

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

        Assert.isFalse(rrn[0].equals(NOTINPUT), Errors.create().key(MessageIdList.REFERENCEFILE_MISSING_KEY_1_FIELD)
                                                      .content("Key_1_field是主键,不能为空!").build());

        referenceFile.setKey1FieldRrn(rrn[0]);

        referenceFile.setKey2FieldRrn(rrn[1].equals(NOTINPUT) ? new Long(0) : rrn[1]);

        referenceFile.setData1FieldRrn(rrn[2].equals(NOTINPUT) ? null : rrn[2]);
        referenceFile.setData2FieldRrn(rrn[3].equals(NOTINPUT) ? null : rrn[3]);
        referenceFile.setData3FieldRrn(rrn[4].equals(NOTINPUT) ? null : rrn[4]);
        referenceFile.setData4FieldRrn(rrn[5].equals(NOTINPUT) ? null : rrn[5]);
        referenceFile.setData5FieldRrn(rrn[6].equals(NOTINPUT) ? null : rrn[6]);

        PropertyUtils.copyProperties(referenceFile, theform);

        referenceFile.setPassword(theform.getPassword());
        referenceFile.setTransId(Constants.MODIFY_KEY);

        process(referenceFile);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFile.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "DATA_1_VALUE");
            HashMap map = new HashMap(5);
            map.put("reftable", referenceFile);

            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTableDef");
            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {

                message = this.connectMesSystem(map, facilityRrn, user);

                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);

        theform.setOldPassword("");
        theform.setPassword("");
        theform.setPasswordConfirm("");

        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute("operationResultMSg", I18nUtils.getMessage("referencefile.operation_result"));

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    private Long getFieldRrn(String fieldId, Long facilityRrn) {
        Long fieldRrn = NOTINPUT;

        if ((fieldId != null) && !"".equals(fieldId.trim())) {
            fieldId = fieldId.trim().toUpperCase();

            fieldRrn = new Long(
                    getInstanceRrn(fieldId, getNamedSpace(ObjectList.FIELD_KEY, facilityRrn), ObjectList.FIELD_KEY));
        }

        return fieldRrn;
    }

    public ActionForward create(ActionMapping mapping, ReferenceFileInfoForm theform,
                                HttpServletRequest request) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String instanceId = theform.getInstanceId().trim().toUpperCase();
        ReferenceFile referenceFile = new ReferenceFile(instanceId, getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                  LocalContext.getFacilityRrn()),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile.setTransPerformedby(user);
        String source = null;

        Long[] rrn = new Long[7];

        if ((rrn[0] = getFieldRrn(theform.getKey1FieldId(), facilityRrn)).longValue() == 0) {
            source = "Key_1_field";
        } else if ((rrn[1] = getFieldRrn(theform.getKey2FieldId(), facilityRrn)).longValue() == 0) {
            source = "Key_2_field";
        } else if ((rrn[NUM_TWO] = getFieldRrn(theform.getData1FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_1_field";
        } else if ((rrn[NUM_THREE] = getFieldRrn(theform.getData2FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_2_field";
        } else if ((rrn[NUM_FORE] = getFieldRrn(theform.getData3FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_3_field";
        } else if ((rrn[NUM_FIVE] = getFieldRrn(theform.getData4FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_4_field";
        } else if ((rrn[NUM_SIX] = getFieldRrn(theform.getData5FieldId(), facilityRrn)).longValue() == 0) {
            source = "Data_5_field";
        }

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

        Assert.isFalse(rrn[0].equals(NOTINPUT), Errors.create().key(MessageIdList.REFERENCEFILE_MISSING_KEY_1_FIELD)
                                                      .content("Key_1_field是主键,不能为空!").build());

        referenceFile.setKey1FieldRrn(rrn[0]);

        referenceFile.setKey2FieldRrn(rrn[1].equals(NOTINPUT) ? new Long(0) : rrn[1]);

        referenceFile.setData1FieldRrn(rrn[2].equals(NOTINPUT) ? null : rrn[2]);
        referenceFile.setData2FieldRrn(rrn[3].equals(NOTINPUT) ? null : rrn[3]);
        referenceFile.setData3FieldRrn(rrn[4].equals(NOTINPUT) ? null : rrn[4]);
        referenceFile.setData4FieldRrn(rrn[5].equals(NOTINPUT) ? null : rrn[5]);
        referenceFile.setData5FieldRrn(rrn[6].equals(NOTINPUT) ? null : rrn[6]);

        PropertyUtils.copyProperties(referenceFile, theform);

        referenceFile.setTransId(Constants.CREATE_KEY);

        process(referenceFile);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFile.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFile.getInstanceId(), "DATA_1_VALUE");
            HashMap map = new HashMap(5);
            map.put("reftable", referenceFile);

            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTableDef");
            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {

                message = this.connectMesSystem(map, facilityRrn, user);

                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);

        theform.setOldPassword("");
        theform.setPassword("");
        theform.setPasswordConfirm("");

        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute("operationResultMSg", I18nUtils.getMessage("referencefile.operation_result"));

        return mapping.findForward(Constants.MODIFY_KEY);
    }

}