ContextValueEdit.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.contextvalue;

import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.Context;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.Ecn;
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 ContextValueEdit extends CtxAction {

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

        ContextInfoForm theform = (ContextInfoForm) form;
        theform.setEcnFlag(getEcnFlag());

        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(theform, context);

        ContextValue contextValue = new ContextValue();
        PropertyUtils.copyProperties(contextValue, theform);
        contextValue.setContextRrn(context.getInstanceRrn());
        ContextValue currentContext = ctxService.getContextValue(contextValue, LocalContext.getFacilityRrn());
        if (currentContext == null) {
            theform.setTransId(Constants.CREATE_KEY);
            theform.setStatus(null);
        } else {
            if (currentContext.getEcnRrn() != null) {
                Ecn ecn = new Ecn();
                ecn.setInstanceRrn(currentContext.getEcnRrn());
                ecn = (Ecn) this.getInstance(ecn);
                theform.setEcnId(ecn.getInstanceId());
                PropertyUtils.copyProperties(theform, currentContext);
                theform.setTransId(Constants.MODIFY_KEY);
            }
        }
        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward(Constants.EDIT_KEY));
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ContextInfoForm theform = (ContextInfoForm) form;

        Context context = new Context(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.CONTEXT_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.CONTEXT_KEY);
        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        // if user click the Cancel button
        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (new ActionForward(mapping.getInput()));
    }

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

        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        String dest = "modify";
        if (request.getParameter("refresh") != null) {
            theform.setEffectiveDateFrom("");
            theform.setEffectiveDateTo("");
            theform.setEcnId("");
            theform.setEcnRrn(null);
            theform.setStatus(null);
        } else {
            Ecn ecn = new Ecn();
            Long ecnRrn = new Long(request.getParameter("ecnRrn"));

            if (ecnRrn == null) {
                theform.setEffectiveDateFrom("");
                theform.setEffectiveDateTo("");
            } else {
                ecn.setInstanceRrn(ecnRrn.longValue());
                ecn = (Ecn) this.getInstance(ecn);
                theform.setEffectiveDateFrom(ecn.getEffectiveDateFrom());
                theform.setEffectiveDateTo(ecn.getEffectiveDateTo());
                theform.setEcnRrn(ecn.getInstanceRrn());
                theform.setEcnId(ecn.getInstanceId());
                theform.setStatus(ecn.getEcnStatus());
            }
        }
        request.setAttribute(SessionNames.CONTEXT_KEY, context);
        return (mapping.findForward("current"));
    }

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

        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        String dest = "modify";

        theform.setTransId(Constants.CREATE_KEY);
        processEditContextValue(theform, context.getInstanceRrn(), LocalContext.getFacilityRrn(),
                                LocalContext.getUserRrn());

        PropertyUtils.copyProperties(theform, context);

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

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

        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        theform.setTransId(Constants.CREATE_KEY);

        theform.setStatus("ACTIVE");
        processEditContextValue(theform, context.getInstanceRrn(), LocalContext.getFacilityRrn(),
                                LocalContext.getUserRrn(), LocalContext.getUserId());
        PropertyUtils.copyProperties(theform, context);

        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(),
                                      getNamedSpace(ObjectList.CONTEXT_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.CONTEXT_KEY);

        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        theform.setTransId(Constants.MODIFY_KEY);
        processEditContextValue(theform, context.getInstanceRrn(), LocalContext.getFacilityRrn(),
                                LocalContext.getUserRrn());
        PropertyUtils.copyProperties(theform, context);
        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(),
                                      getNamedSpace(ObjectList.CONTEXT_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.CONTEXT_KEY);

        context = (Context) getInstance(context);

        theform.setEcnFlag(getEcnFlag());

        theform.setTransId(Constants.DELETE_KEY);

        PropertyUtils.copyProperties(theform, context);

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

}