CtxAction.java

package com.mycim.webapp.actions;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.context.spring.SpringContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.server.ctx.service.CtxService;
import com.mycim.server.ecn.service.EcnService;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.prp.Context;
import com.mycim.valueobject.prp.ContextRule;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.Ecn;
import com.mycim.webapp.Constants;
import com.mycim.webapp.forms.ContextInfoForm;
import com.mycim.webapp.forms.RootForm;

import java.util.Iterator;
import java.util.List;

public class CtxAction extends AbstractAction {

    protected CtxService ctxService = SpringContext.getBean(CtxService.class);

    protected EcnService ecnService = SpringContext.getBean(EcnService.class);

    @Override
    protected void doCopy(RootForm theform, NamedObject instance) throws Exception {
        String origId = theform.getInstanceId();

        if (instance instanceof Context) {
            instance = getInstance(instance);
        }

        // populate the new information thru the new instance id
        PropertyUtils.copyProperties(theform, instance);

        // and restore the original Id
        theform.setInstanceId(origId);
    }

    @Override
    public NamedObject getInstance(NamedObject instance) {
        if (instance instanceof Context) {
            return ctxService.getContext((Context) instance);
        } else if (instance instanceof Ecn) {
            return ecnService.getEcn(instance.getInstanceRrn());
        }
        return null;
    }

    protected void deleteContextValue(Context context, Long seq, Long facility, Long userRrn) {
        ContextValue contextValue = this.getContextValueBySeq(context, seq);

        removeReworkAndContextValue(facility, userRrn, contextValue);
    }

    protected ContextValue getContextValueBySeq(Context context, Long seq) {
        ContextValue returnVal = null;
        Iterator it = context.getValues().iterator();

        while (it.hasNext()) {
            ContextValue temp = (ContextValue) it.next();

            if (temp.getSequence() == seq.longValue()) {
                returnVal = temp;

                break;
            }
        }

        return returnVal;
    }

    protected void removeReworkAndContextValue(Long facility, Long userRrn, ContextValue contextValue) {
        // get the rework route tree
        long flowSeqContextRrn = getInstanceRrn(ContextNames.RESEQUENCE_CONTEXT, facility, ObjectList.CONTEXT_KEY);
        ctxService.removeRework(contextValue, facility.longValue(), userRrn, flowSeqContextRrn);
    }

    protected List<ContextRule> getContextRules(long contextRrn) {
        return ctxService.getContextRules(contextRrn);
    }

    protected List<ContextValue> getContextValues(long contextRrn) {
        return ctxService.getContextValues(contextRrn);
    }

    protected void process(NamedObject instance) {

        String action = instance.getTransId();

        if (StringUtils.equals(Constants.CREATE_KEY, action)) {
            if (instance instanceof Context) {
                ctxService.insertContext((Context) instance);
            }
        } else if (StringUtils.equals(Constants.DELETE_KEY, action)) {
            if (instance instanceof Context) {
                ctxService.deleteContext((Context) instance);
            }
        } else if (StringUtils.equals(Constants.MODIFY_KEY, action)) {
            if (instance instanceof Context) {
                ctxService.updateContext((Context) instance);
            }
        }

    }

    protected void processEditContextValue(ContextInfoForm theform, long contextRrn, Long facility,
                                           Long userRrn) throws Exception {
        ContextValue contextValue = new ContextValue();

        // Copy value object properties from form bean
        PropertyUtils.copyProperties(contextValue, theform);
        contextValue.setContextRrn(contextRrn);

        if (theform.getTransId().equalsIgnoreCase(Constants.CREATE_KEY)) {
            validateTheForm(theform);
            ctxService.addContextValue(contextValue, LocalContext.getFacilityRrn(), LocalContext.getUserRrn());
        } else if (theform.getTransId().equalsIgnoreCase(Constants.MODIFY_KEY)) {
            ctxService.updateContextValue(contextValue, contextValue, LocalContext.getFacilityRrn(),
                                          LocalContext.getUserRrn());
        } else if (theform.getTransId().equalsIgnoreCase(Constants.DELETE_KEY)) {
            ctxService.removeContextValue(contextValue, LocalContext.getFacilityRrn(), LocalContext.getUserRrn());
        }
    }

    protected void validateTheForm(ContextInfoForm theform) {
        Assert.isFalse(theform.getEcnRrn() == null || theform.getEcnRrn().longValue() <= 0,
                       Errors.create().key(MessageIdList.CONTEXT_ECN_ID_NOT_AVAIABLE).content("Ecn ID 不可用").build());
    }

    protected void processEditContextValue(ContextInfoForm theform, long contextRrn, Long facility, Long userRrn,
                                           String user) throws Exception {
        ContextValue contextValue = new ContextValue();
        PropertyUtils.copyProperties(contextValue, theform);

        contextValue.setContextRrn(contextRrn);

        if (theform.getTransId().equalsIgnoreCase(Constants.CREATE_KEY)) {
            ctxService.createContextValueWithActivatedEcn(facility, LocalContext.getUserRrn(), LocalContext.getUserId(),
                                                          contextValue);
        } else if (theform.getTransId().equalsIgnoreCase(Constants.MODIFY_KEY)) {
            ctxService.updateContextValueWithActivatedEcn(facility, LocalContext.getUserRrn(), contextValue);
        } else if (theform.getTransId().equalsIgnoreCase(Constants.DELETE_KEY)) {
            ctxService.removeContextValue(contextValue, LocalContext.getUserRrn(), LocalContext.getUserRrn());
        }
    }

}