# ReworkRouteContextValueManagerImpl.java

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

    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.ReworkRouteContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.NamedSpaceList;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.Operation;
    import com.mycim.valueobject.prp.ReworkRouteContextValue;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/1
     **/
    @Service
    @Transactional
    public class ReworkRouteContextValueManagerImpl implements ReworkRouteContextValueManager {

        private final ContextValueManager contextValueManager;

        private final NamedObjectManager namedObjectManager;

        public ReworkRouteContextValueManagerImpl(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_KEY8",
                                                                   StringUtils.toString(currentVersion + 1));

        }

        @Override
        public ReworkRouteContextValue buildNewReworkRouteContextValue() {
            ReworkRouteContextValue contextValue = new ReworkRouteContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            return contextValue;
        }

        @Override
        public void deleteContextValueForProcessVersion(long instanceRrn, int instanceVersion) {
            contextValueManager.deleteContextValueByContextKey("deleteProcessVersion",
                                                               buildContextValueForProcessVersion(instanceRrn,
                                                                                                  instanceVersion));
        }

        @Override
        public List<ReworkRouteContextValue> getContextValues(long processRrn, int processVersion) {
            List<ContextValue> contextValues = contextValueManager.getContextValues(LocalContext.getFacilityRrn(),
                                                                                    buildContextValueForProcessVersion(
                                                                                            processRrn, processVersion));
            List<ReworkRouteContextValue> reworkRouteContextValues = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    ReworkRouteContextValue routeContextValue = new ReworkRouteContextValue();
                    routeContextValue.copy(contextValue);
                    reworkRouteContextValues.add(routeContextValue);
                }
            }
            return reworkRouteContextValues;
        }

        @Override
        public List<ReworkRouteContextValue> getContextValuesByContextKey(long processRrn, int processVersion) {
            List<ContextValue> contextValues = contextValueManager
                    .getContextValuesByContextKey(buildContextValueForProcessVersion(processRrn, processVersion));
            List<ReworkRouteContextValue> reworkRouteContextValues = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    ReworkRouteContextValue routeContextValue = new ReworkRouteContextValue();
                    routeContextValue.copy(contextValue);
                    reworkRouteContextValues.add(routeContextValue);
                }
            }
            return reworkRouteContextValues;
        }

        @Override
        public long getProcessReworkRouteRrn(Long processRrn, Integer processVersion, Long routeRrn, Long operationRrn) {
            ReworkRouteContextValue routeContextValue = buildContextValueForProcess(processRrn, processVersion, routeRrn,
                                                                                    operationRrn);

            ContextValue contextValue = contextValueManager.simulateContextValue(routeContextValue);
            if (contextValue != null) {
                return NumberUtils.createLong(StringUtils.trimToNull(contextValue.getResultValue1()));
            }
            return 0L;
        }

        @Override
        public List<ContextValue> getReworkContextValueList(long processRrn, int processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            ReworkRouteContextValue reworkRouteContextValue = new ReworkRouteContextValue();
            reworkRouteContextValue.setContextRrn(namedObjectManager
                                                          .getNamedObjectRrn(reworkRouteContextValue.getContextId(),
                                                                             facilityRrn, ObjectList.CONTEXT_KEY));
            reworkRouteContextValue.setProcessRrn(processRrn);
            reworkRouteContextValue.setProcessVersion(processVersion);
            return contextValueManager.getContextValuesByContextKey(reworkRouteContextValue);
        }

        @Override
        public ContextValue getReworkRouteContextValueByLot(Lot lot) {
            ReworkRouteContextValue reworkRouteContextValue = buildNewReworkRouteContextValue();

            reworkRouteContextValue.setProductRrn(lot.getProductRrn());
            reworkRouteContextValue.setProcessRrn(lot.getProcessRrn());
            reworkRouteContextValue.setRouteRrn(lot.getRouteRrn());
            reworkRouteContextValue.setOperationRrn(lot.getOperationRrn());
            reworkRouteContextValue.setEntityRrn(lot.getEqptRrn());
            reworkRouteContextValue.setLotRrn(lot.getLotRrn());
            reworkRouteContextValue.setProcessVersion(lot.getProcessVersion());
            ContextValue contextValue = contextValueManager.simulateContextValue(reworkRouteContextValue);
            return contextValue;
        }

        @Override
        public ReworkRouteContextValue getProcessReworkRoute(Lot lot) {
            ReworkRouteContextValue reworkRouteContextValue = buildNewReworkRouteContextValue();

            reworkRouteContextValue.setProductRrn(lot.getProductRrn());
            reworkRouteContextValue.setProcessRrn(lot.getProcessRrn());
            reworkRouteContextValue.setRouteRrn(lot.getRouteRrn());
            reworkRouteContextValue.setOperationRrn(lot.getOperationRrn());
            reworkRouteContextValue.setEntityRrn(lot.getEqptRrn());
            reworkRouteContextValue.setLotRrn(lot.getLotRrn());
            reworkRouteContextValue.setProcessVersion(lot.getProcessVersion());

            ContextValue contextValue = contextValueManager.simulateContextValue(reworkRouteContextValue);
            if (contextValue != null) {
                reworkRouteContextValue.copy(contextValue);
            }
            return reworkRouteContextValue;
        }

        @Override
        public ContextValue buildReworkCondition(Operation operation) {
            ContextValue contextValue = new ContextValue();
            contextValue.setContextId("REWORK_ROUTE_CONTEXT");
            contextValue.setContextRrn(namedObjectManager
                                               .getNamedObjectRrn("REWORK_ROUTE_CONTEXT", NamedSpaceList.DEFAULT_KEY,
                                                                  ObjectList.CONTEXT_KEY));
            contextValue.setStatus("ACTIVE");
            SimpleDateFormat formatter = new java.text.SimpleDateFormat(DateUtils.DATE_FORMAT4DAY);
            java.util.Date currentTime = new java.util.Date(System.currentTimeMillis());
            contextValue.setEffectiveDateFrom(formatter.format(currentTime));

            int i = 0;
            contextValue.setSequenceNumber(new Integer(0));
            if (operation.getInstanceRrn() != 0) {
                contextValue.setContextKey4(operation.getInstanceRrn() + "");
                i++;
            }
            Assert.isFalse(i == 0, Errors.create().content("At least one is required!").build());

            contextValue.setResultValue1(operation.getReworkRouteRrn() + "");
            contextValue.setResultValue4(operation.getMaxReworkTimes());
            return contextValue;
        }

        private ReworkRouteContextValue buildContextValueForProcessVersion(Long processRrn, Integer processVersion) {
            ReworkRouteContextValue contextValue = buildNewReworkRouteContextValue();

            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);

            return contextValue;
        }

        private ReworkRouteContextValue buildContextValueForProcess(Long processRrn, Integer processVersion, Long routeRrn,
                                                                    Long operationRrn) {
            ReworkRouteContextValue contextValue = buildNewReworkRouteContextValue();

            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);
            contextValue.setRouteRrn(routeRrn);
            contextValue.setOperationRrn(operationRrn);
            return contextValue;
        }

    }