# LotRecycledContextValueManagerImpl.java

    package com.mycim.server.ctx.exec.manager.impl;

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.LotRecycledContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.LotRecycledContextValue;
    import com.mycim.valueobject.prp.LotRecycledInfo;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.ArrayList;
    import java.util.List;

    /**
     * from ProcessLoopService
     *
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/1
     **/
    @Service
    @Transactional
    public class LotRecycledContextValueManagerImpl implements LotRecycledContextValueManager {

        private final ContextValueManager contextValueManager;

        private final NamedObjectManager namedObjectManager;

        public LotRecycledContextValueManagerImpl(ContextValueManager contextValueManager, NamedObjectManager namedObjectManager) {
            this.contextValueManager = contextValueManager;
            this.namedObjectManager = namedObjectManager;
        }

        @Override
        public void copyContextValueForProcessVersionWithActivatedEcn(long processRrn, int currentVersion) {
            contextValueManager
                    .copyContextValueForContextKeyWithActivatedEcn(LocalContext.getUserRrn(),"copyByLastProcessVersion",
                                                                   buildContextValueForProcessVersion(processRrn, currentVersion),
                                                                   "CONTEXT_KEY6", StringUtils.toString(currentVersion + 1));
        }

        @Override
        public List<LotRecycledContextValue> getContextValues(Long processRrn, Integer processVersion) {
            List<ContextValue> contextValues = contextValueManager.getContextValues(
                    LocalContext.getFacilityRrn(), buildContextValueForProcessVersion(processRrn, processVersion));

            return buildResult(contextValues);
        }

        @Override
        public List<LotRecycledContextValue> getContextValuesByKeys(Long processRrn, Integer processVersion) {

            List<ContextValue> contextValues = contextValueManager.getContextValuesByContextKey(
                    buildContextValueForProcessVersion(processRrn, processVersion));

            return buildResult(contextValues);
        }

        @Override
        public void deleteContextValueForProcessVersion(Long processRrn, Integer processVersion) {
            contextValueManager.deleteContextValueByContextKey(
                    "deleteProcessVersion", buildContextValueForProcessVersion(processRrn, processVersion));
        }

        @Override
        public void saveProcessLoop(long processRrn, int processVersion, long startRoute, long startStep, long endRoute, long endStep, int loopCount, String status) {
            contextValueManager.createOrUpdateContextValueWithActivatedEcn(
                    buildContextValueInfoForProcessLoop(processRrn, processVersion, startRoute, startStep, endRoute, endStep, loopCount, status));
        }

        @Override
        public LotRecycledContextValue getProcessLoop(long processRrn, int processVer, Long startRoute, Long startStep, Long endRoute, Long endStep) {
            LotRecycledContextValue lotRecycledContextValue = buildContextValueForProcessLoop(
                    processRrn, processVer, startRoute, startStep, endRoute, endStep, null);
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(lotRecycledContextValue);
            if (contextValue != null) {
                lotRecycledContextValue.copy(contextValue);
                return lotRecycledContextValue;
            }
            return null;
        }

        @Override
        public void deleteContextValueForProcessLoop(long processRrn, int processVer, Long startRoute, Long startStep,
                                                     Long endRoute, Long endStep) {
            LotRecycledContextValue delete = buildContextValueForProcessLoop(processRrn, processVer, startRoute, startStep, endRoute, endStep, null);
            contextValueManager.deleteContextValueByContextKey("deleteProcessLoop", delete);
        }

        @Override
        public List<LotRecycledContextValue> getContextValues(String processId, String endRouteId, String endOperationId, int processVersion) {
            List<ContextValue> contextValues = contextValueManager.getContextValues(
                    LocalContext.getFacilityRrn(), buildContextValueForProcessVersion(processId, endRouteId, endOperationId, processVersion));

            return buildResult(contextValues);
        }

        @Override
        public List<LotRecycledContextValue> getContextValuesByStart(String processId, String startRouteId, String startOperationId, int processVersion) {
            List<ContextValue> contextValues = contextValueManager.getContextValues(
                    LocalContext.getFacilityRrn(), buildContextValueForProcessVersionByStart(processId, startRouteId, startOperationId, processVersion));
            return buildResult(contextValues);
        }

        private List<LotRecycledContextValue> buildResult(List<ContextValue> contextValues) {
            List<LotRecycledContextValue> lotRecycledContextValues = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    LotRecycledContextValue lotRecycledContextValue = new LotRecycledContextValue();
                    lotRecycledContextValue.copy(contextValue);
                    lotRecycledContextValues.add(lotRecycledContextValue);
                }
            }
            return lotRecycledContextValues;
        }

        @Override
        public LotRecycledContextValue getContextValues(LotRecycledInfo lotRecycledInfo) {
            LotRecycledContextValue lotRecycledContextValue = buildNewLotRecycledContextValue();
            lotRecycledContextValue.setProcessRrn(lotRecycledInfo.getProcessRrn());
            lotRecycledContextValue.setProcessVersion(lotRecycledInfo.getProcessVersion());
            lotRecycledContextValue.setStartRouteRrn(lotRecycledInfo.getStartRouteRrn());
            lotRecycledContextValue.setStartOperationRrn(lotRecycledInfo.getStartOperationRrn());
            lotRecycledContextValue.setEndRouteRrn(lotRecycledInfo.getEndRouteRrn());
            lotRecycledContextValue.setEndOperationRrn(lotRecycledInfo.getEndOperationRrn());
            ContextValue contextValue = contextValueManager.simulateContextValue(lotRecycledContextValue);
            if (contextValue != null) {
                lotRecycledContextValue.copy(contextValue);
            }
            return lotRecycledContextValue;
        }

        public LotRecycledContextValue buildNewLotRecycledContextValue() {
            LotRecycledContextValue contextValue = new LotRecycledContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(), LocalContext.getFacilityRrn(), ObjectList.CONTEXT_KEY));
            return contextValue;
        }

        private LotRecycledContextValue buildContextValueForProcessLoop(long processRrn, int processVer, Long startRoute, Long startStep,
                                                                        Long endRoute, Long endStep, Integer loopCount) {
            LotRecycledContextValue recycledContextValue = buildContextValueForProcessVersion(processRrn, processVer);
            recycledContextValue.setLoopCount(loopCount);
            recycledContextValue.setStartRouteRrn(startRoute);
            recycledContextValue.setStartOperationRrn(startStep);
            recycledContextValue.setEndRouteRrn(endRoute);
            recycledContextValue.setEndOperationRrn(endStep);

            return recycledContextValue;
        }

        private LotRecycledContextValue buildContextValueInfoForProcessLoop(long processRrn, int processVer, Long startRoute, Long startStep,
                                                                            Long endRoute, Long endStep, Integer loopCount, String status) {
            LotRecycledContextValue recycledContextValue = buildContextValueForProcessVersion(processRrn, processVer);
            recycledContextValue.setLoopCount(loopCount);
            recycledContextValue.setStartRouteRrn(startRoute);
            recycledContextValue.setStartOperationRrn(startStep);
            recycledContextValue.setEndRouteRrn(endRoute);
            recycledContextValue.setEndOperationRrn(endStep);
            recycledContextValue.setStatus(status);

            return recycledContextValue;
        }

        private LotRecycledContextValue buildContextValueForProcessVersion(Long processRrn, Integer processVersion) {
            LotRecycledContextValue contextValue = buildNewLotRecycledContextValue();
            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);
            return contextValue;
        }

        private LotRecycledContextValue buildContextValueForProcessVersion(String processId, String endRouteId, String endOperationId, int processVersion) {
            LotRecycledContextValue contextValue = buildNewLotRecycledContextValue();
            contextValue.setContextKey1(processId);
            contextValue.setContextKey4(endRouteId);
            contextValue.setContextKey5(endOperationId);
            contextValue.setContextKey6(StringUtils.toString(processVersion));
            return contextValue;
        }

        private LotRecycledContextValue buildContextValueForProcessVersionByStart(String processId, String startRouteId, String startOperationId, int processVersion) {
            LotRecycledContextValue contextValue = buildNewLotRecycledContextValue();
            contextValue.setContextKey1(processId);
            contextValue.setContextKey2(startRouteId);
            contextValue.setContextKey3(startOperationId);
            contextValue.setContextKey6(StringUtils.toString(processVersion));
            return contextValue;
        }

    }