# BorContextValueManagerImpl.java

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

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

    import java.util.HashMap;
    import java.util.Map;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/27
     **/
    @Service
    @Transactional
    public class BorContextValueManagerImpl implements BORContextValueManager {

        private final NamedObjectManager namedObjectManager;

        private final ContextValueManager contextValueManager;

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

        @Override
        public Long getBomRrn(Lot lot) {

            BORContextValue borContextValue = buildNewBorContextValue(lot);

            ContextValue contextValue = contextValueManager.simulateContextValue(borContextValue);

            if (contextValue == null) {
                return null;
            }

            borContextValue.copy(contextValue);

            return borContextValue.getBOMRrn();
        }

        @Override
        public Long getBomRrn(Map lotInfo) {

            BORContextValue borContextValue = buildNewBorContextValue(lotInfo);

            ContextValue contextValue = contextValueManager.simulateContextValue(borContextValue);

            if (contextValue == null) {
                return null;
            }

            borContextValue.copy(contextValue);

            return borContextValue.getBOMRrn();
        }

        @Override
        public Map getBorbyContext(Map tempInfo) {

            Map returnInfo = new HashMap();

            String contextId = "BOR_CONTEXT";
            Long facilityRrn = (Long) tempInfo.get("facilityRrn");
            Long productRrn = (Long) tempInfo.get("productRrn");
            String routeRrn = (String) tempInfo.get("routeRrn");
            Long operationRrn = (Long) tempInfo.get("operationRrn");
            Long technologyRrn = (Long) tempInfo.get("technologyRrn");
            Long lotRrn = (Long) tempInfo.get("lotRrn");

            long facility = 1;

            if (facilityRrn != null) {
                facility = facilityRrn.longValue();
            }

            // Operation, Route ID, Technology ID, Product ID, Lot
            // call prp setup manager's get context value method to retrive recipe string
            ContextValue ctxValue = new ContextValue();

            if ((contextId != null) && (facilityRrn != null)) {
                ctxValue.setContextRrn(
                        namedObjectManager.getNamedObjectRrn(contextId, facilityRrn.longValue(), ObjectList.CONTEXT_KEY));
            }

            if (facilityRrn != null) {
                facility = facilityRrn.longValue();
            }

            if (operationRrn != null) {
                ctxValue.setContextKey1("" + operationRrn);
            }

            if (routeRrn != null) {
                ctxValue.setContextKey2("" + routeRrn);
            }

            if (technologyRrn != null) {
                ctxValue.setContextKey3("" + technologyRrn);
            }

            if (productRrn != null) {
                ctxValue.setContextKey4("" + productRrn);
            }

            if (lotRrn != null) {
                ctxValue.setContextKey5("" + lotRrn);
            }

            ctxValue = contextValueManager.simulateContextValue(ctxValue);

            if (ctxValue != null) {
                returnInfo.put("borRrn", new Long(ctxValue.getResultValue1()));
            }

            return returnInfo;
        }

        private BORContextValue buildNewBorContextValue() {
            BORContextValue contextValue = new BORContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            return contextValue;
        }

        private BORContextValue buildNewBorContextValue(Map lotInfo) {
            BORContextValue borContextValue = buildNewBorContextValue();

            Long productRrn = MapUtils.getLongValue(lotInfo, "productRrn");
            long routeRrn = MapUtils.getLongValue(lotInfo, "routeRrn");
            Long operationRrn = MapUtils.getLongValue(lotInfo, "operationRrn");
            Long technologyRrn = (Long) lotInfo.get("technologyRrn");
            Long lotRrn = (Long) lotInfo.get("lotRrn");

            borContextValue.setProductRrn(productRrn);
            borContextValue.setProcessRrn(routeRrn);
            borContextValue.setRouteRrn(technologyRrn);
            borContextValue.setOperationRrn(operationRrn);
            borContextValue.setLotRrn(lotRrn);

            return borContextValue;
        }

        private BORContextValue buildNewBorContextValue(Lot lot) {
            BORContextValue borContextValue = buildNewBorContextValue();

            borContextValue.setProductRrn(lot.getProductRrn());
            borContextValue.setProcessRrn(lot.getProcessRrn());
            borContextValue.setRouteRrn(lot.getRouteRrn());
            borContextValue.setOperationRrn(lot.getOperationRrn());
            borContextValue.setRecipeRrn(lot.getRecipeLogicalRrn());

            return borContextValue;
        }

    }