FieldSaveAction.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.field;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.sys.CheckRule;
import com.mycim.valueobject.sys.Field;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.system.FieldInfoForm;
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.List;

/**
 * @author weike li
 * @version 0.0.1
 * @since 2019/8/16 18:18
 **/
public class FieldSaveAction extends SystemSetupAction {

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

        if (form == null) {
            form = new FieldInfoForm();
            request.setAttribute(mapping.getAttribute(), form);
        }
        FieldInfoForm theform = (FieldInfoForm) form;

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

        Field field = new Field(id, getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);

        field = (Field) getInstance(field);

        PropertyUtils.copyProperties(theform, field);

        if (field.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);
            return (mapping.findForward(Constants.MODIFY_KEY));
        }

        field.setCheckRules(getCheckRules(field));
        request.setAttribute(SessionNames.FIELD_KEY, field);

        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 {
        FieldInfoForm theform = (FieldInfoForm) form;

        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }

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

    public ActionForward copy(ActionMapping mapping, FieldInfoForm form, HttpServletRequest request) throws Exception {

        Field field = new Field(form.getInstanceId(),
                                getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);
        field = (Field) getInstance(field);
        field.setTransPerformedby(LocalContext.getUserId());

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

        field.setInstanceId(id);

        doCopy(form, field);

        form.setTransId(Constants.CREATE_KEY);

        request.setAttribute(SessionNames.FIELD_KEY, field);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward members(ActionMapping mapping, FieldInfoForm form, HttpServletRequest request) {

        Field field = new Field(form.getInstanceId(),
                                getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);
        field = (Field) getInstance(field);
        field.setTransPerformedby(LocalContext.getUserId());

        if (request.getParameter(Constants.MEMBERS_KEY).equals(Constants.DELETE_KEY) &&
                (request.getParameter(Constants.ITEM_KEY) != null)) {
            Long ruleNum = new Long(request.getParameter(Constants.ITEM_KEY));
            Relation relation = new Relation();
            relation.setFromRrn(new Long(field.getInstanceRrn()));
            relation.setToRrn(ruleNum);

            deleteRelation(relation);

            processScreens(field.getInstanceRrn(), getCheckRules(field));

            field.setCheckRules(getCheckRules(field));

            form.setTransId(Constants.MODIFY_KEY);

            field.setCheckRules(getCheckRules(field));
            request.setAttribute(SessionNames.FIELD_KEY, field);
            return mapping.findForward(Constants.MODIFY_KEY);
        }

        form.setTransId(Constants.CREATE_KEY);

        if (request.getParameter(Constants.ITEM_KEY) != null) {
            String ruleNum = request.getParameter(Constants.ITEM_KEY);
            List<CheckRule> checkRules = getCheckRules(field);

            for (CheckRule checkRule : checkRules) {
                if (checkRule.getRuleNum().equals(new Long(ruleNum))) {
                    form.setRuleNum(checkRule.getRuleNum());
                    form.setRuleId(checkRule.getRuleId());
                    form.setParameter1(checkRule.getParameter1());
                    form.setParameter2(checkRule.getParameter2());
                    form.setGroupNum(checkRule.getGroupNum());
                }
            }

            field.setCheckRules(checkRules);
            request.setAttribute(SessionNames.FIELD_KEY, field);
            form.setTransId(Constants.MODIFY_KEY);
        }

        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    private void deleteRelation(Relation relation) {
        baseService.deleteRelation(relation);
    }

    private void processScreens(long fieldRrn, List<CheckRule> validators) {
        sysService.batchUpdateScreensForField(fieldRrn, validators);
    }

    public ActionForward delete(ActionMapping mapping, FieldInfoForm form, HttpServletRequest request) {

        Field field = new Field(form.getInstanceId(),
                                getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);
        field = (Field) getInstance(field);
        field.setTransPerformedby(LocalContext.getUserId());
        field.setTransId(Constants.DELETE_KEY);

        process(field);

        form.setInstanceId("");
        request.setAttribute(SessionNames.FIELD_KEY, field);
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward create(ActionMapping mapping, FieldInfoForm form,
                                HttpServletRequest request) throws Exception {
        Field field = new Field(form.getInstanceId(),
                                getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);
        field = (Field) getInstance(field);
        field.setTransPerformedby(LocalContext.getUserId());

        if ((form.getLabelId() != null) && !form.getLabelId().trim().equals("")) {
            String labelId = form.getLabelId().trim().toUpperCase();
            Long labelRrn = new Long(0);

            labelRrn = new Long(
                    getInstanceRrn(labelId, getNamedSpace(ObjectList.LABEL_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.LABEL_KEY));

            Assert.isFalse(labelRrn.longValue() <= 0,
                           Errors.create().key(MessageIdList.LABEL_MISSING_ID).content("卷标号为空或不存在!").build());

            field.setLabelRrn(labelRrn);
        }

        PropertyUtils.copyProperties(field, form);

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

        field = (Field) getInstance(field);
        field.setCheckRules(getCheckRules(field));
        form.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.FIELD_KEY, field);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, FieldInfoForm form,
                                HttpServletRequest request) throws Exception {

        Field field = new Field(form.getInstanceId(),
                                getNamedSpace(ObjectList.FIELD_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.FIELD_KEY);
        field = (Field) getInstance(field);
        field.setTransPerformedby(LocalContext.getUserId());

        if ((form.getLabelId() != null) && !form.getLabelId().trim().equals("")) {
            String labelId = form.getLabelId().trim().toUpperCase();
            Long labelRrn = new Long(0);

            labelRrn = new Long(
                    getInstanceRrn(labelId, getNamedSpace(ObjectList.LABEL_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.LABEL_KEY));

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

            field.setLabelRrn(labelRrn);
        }

        PropertyUtils.copyProperties(field, form);

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

        field = (Field) getInstance(field);
        field.setCheckRules(getCheckRules(field));
        form.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.FIELD_KEY, field);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

}