# ProcessOperationDescContextValueManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.CharUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.ProcessOperationDescContextValueManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.ProcessOperationDescContextValue;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;
    import java.util.Map;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/3
     **/
    @Service
    @Transactional
    public class ProcessOperationDescContextValueManagerImpl implements ProcessOperationDescContextValueManager {

        private final NamedObjectManager namedObjectManager;

        private final ContextValueManager contextValueManager;

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

        @Override
        public String getOperationSeqAndWait(Long productRrn, Long processRrn, Long routeRrn, Long operationRrn) {
            ProcessOperationDescContextValue operationDescContextValue = buildNewOperationDescContextValue(productRrn,
                                                                                                           processRrn,
                                                                                                           routeRrn,
                                                                                                           operationRrn);

            StringBuilder sb = new StringBuilder();
            List<ContextValue> contextValues = contextValueManager
                    .simulateContextValuesForRecipeAndOperationSeq(operationDescContextValue);
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue ctxValue : contextValues) {
                    // format the correct recipe string
                    sb.append(ctxValue.getResultValue1());
                    String resultValue2 = (null == ctxValue.getResultValue2()) ? "" : ctxValue.getResultValue2();
                    String resultValue3 = (null == ctxValue.getResultValue3()) ? "" : ctxValue.getResultValue3();
                    String resultValue4 = (null == ctxValue.getResultValue4()) ? "" : ctxValue.getResultValue4();

                    sb.append("|");
                    sb.append(resultValue2);

                    sb.append("|");
                    sb.append(resultValue3);

                    sb.append("|");
                    sb.append(resultValue4);

                    sb.append(",");
                }

                if (sb.length() > 0) {
                    if (sb.charAt(sb.length() - 1) == CharUtils.COMMA_SIGN) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                }
            }

            return sb.toString();
        }

        @Override
        public ProcessOperationDescContextValue simulateContextValue(Long productRrn, Long processRrn,
                                                                     Integer processVersion, Long routeRrn,
                                                                     Long operationRrn) {
            ProcessOperationDescContextValue operationDescContextValue = buildNewOperationDescContextValue(productRrn,
                                                                                                           processRrn,
                                                                                                           processVersion,
                                                                                                           routeRrn,
                                                                                                           operationRrn);

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

        @Override
        public String getOperationDesc(Long productRrn, Long processRrn, Integer processVersion, Long routeRrn,
                                       Long operationRrn) {
            ProcessOperationDescContextValue operationDescContextValue = simulateContextValue(productRrn, processRrn,
                                                                                              processVersion, routeRrn,
                                                                                              operationRrn);
            if (operationDescContextValue != null) {
                return operationDescContextValue.getOperationDesc();
            }
            return StringUtils.EMPTY;
        }

        @Override
        public ContextValue getOperationDescContextValue(Long productRrn, Long processRrn, Integer processVersion,
                                                         Long routeRrn, Long operationRrn) {
            ProcessOperationDescContextValue processOperationDescContextValue = buildNewOperationDescContextValue(
                    productRrn, processRrn, processVersion, routeRrn, operationRrn);

            ContextValue contextValue = contextValueManager
                    .getContextValue(processOperationDescContextValue, LocalContext.getFacilityRrn());
            return contextValue;
        }

        @Override
        public String buildContextValue4StepDesc(Long facility, Lot lot) {
            String desc = StringUtils.EMPTY;

            ProcessOperationDescContextValue operationDESCContextValue = buildNewOperationDescContextValue();
            operationDESCContextValue.setProductRrn(lot.getProductRrn());
            operationDESCContextValue.setProcessRrn(lot.getProcessRrn());
            operationDESCContextValue.setOperationRrn(lot.getOperationRrn());
            operationDESCContextValue.setRouteRrn(lot.getRouteRrn());
            operationDESCContextValue.setProcessVersion(lot.getProcessVersion());

            ContextValue contextValue = contextValueManager.getContextValueNoFilter(operationDESCContextValue);
            if (contextValue != null) {
                desc = contextValue.getResultValue1();
            }
            return desc;
        }

        @Override
        public List<ContextValue> getOperationDescContextValueList(long productRrn, long processRrn, int processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            ProcessOperationDescContextValue operationDescContextValue = new ProcessOperationDescContextValue();
            operationDescContextValue.setContextRrn(namedObjectManager
                                                            .getNamedObjectRrn(operationDescContextValue.getContextId(),
                                                                               facilityRrn, ObjectList.CONTEXT_KEY));
            operationDescContextValue.setProductRrn(productRrn);
            operationDescContextValue.setProcessRrn(processRrn);
            operationDescContextValue.setProcessVersion(processVersion);

            return contextValueManager.getContextValuesByContextKey(operationDescContextValue);
        }

        @Override
        public String getOperationDesc(Map<String, Object> contextValueInfo) {
            String desc = StringUtils.EMPTY;

            ProcessOperationDescContextValue operationDESCContextValue = buildNewOperationDescContextValue();
            //        BeanUtils.copyMapToBean(operationDESCContextValue, contextValueInfo);
            operationDESCContextValue.setProcessVersion(MapUtils.getInteger(contextValueInfo, "processVersion"));
            operationDESCContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
            operationDESCContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
            operationDESCContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
            operationDESCContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));

            ContextValue contextValue = contextValueManager.getContextValueNoFilter(operationDESCContextValue);
            if (contextValue != null) {
                desc = contextValue.getResultValue1();
            }
            return desc;
        }

        private ProcessOperationDescContextValue buildNewOperationDescContextValue(Long productRrn, Long processRrn,
                                                                                   Integer processVersion, Long routeRrn,
                                                                                   Long operationRrn) {
            ProcessOperationDescContextValue operationDescContext = buildNewOperationDescContextValue();

            operationDescContext.setProductRrn(productRrn);
            operationDescContext.setProcessRrn(processRrn);
            operationDescContext.setRouteRrn(routeRrn);
            operationDescContext.setOperationRrn(operationRrn);
            operationDescContext.setProcessVersion(processVersion);

            return operationDescContext;
        }

        private ProcessOperationDescContextValue buildNewOperationDescContextValue(Long productRrn, Long processRrn,
                                                                                   Long routeRrn, Long operationRrn) {
            ProcessOperationDescContextValue operationDescContext = buildNewOperationDescContextValue();

            operationDescContext.setProductRrn(productRrn);
            operationDescContext.setProcessRrn(processRrn);
            operationDescContext.setRouteRrn(routeRrn);
            operationDescContext.setOperationRrn(operationRrn);

            return operationDescContext;
        }

        private ProcessOperationDescContextValue buildNewOperationDescContextValue() {
            ProcessOperationDescContextValue operationDescContext = new ProcessOperationDescContextValue();

            operationDescContext.setContextRrn(namedObjectManager.getNamedObjectRrn(operationDescContext.getContextId(),
                                                                                    LocalContext.getFacilityRrn(),
                                                                                    ObjectList.CONTEXT_KEY));

            return operationDescContext;
        }

    }