# EdcLotContextManagerImpl.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.MapUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.EdcLotContextManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.EdcLotContextValue;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * @author Luopeng.Wang
     * @version 6.0.0
     * @date 2019/9/9
     **/

    @Service
    @Transactional
    public class EdcLotContextManagerImpl implements EdcLotContextManager {

        @Autowired
        private ContextValueManager contextValueManager;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Override
        public ContextValue getEdcLotContextValue(Long productRrn, Integer productVersion, Long processRrn,
                                                  Integer processVersion, Long routeRrn, Long operationRrn) {
            EdcLotContextValue edcLotContextValue = buildNewEdcLotContextValueForProcess(productRrn, productVersion,
                                                                                         processRrn, processVersion,
                                                                                         routeRrn, operationRrn);
            ContextValue contextValue = contextValueManager
                    .getContextValue(edcLotContextValue, LocalContext.getFacilityRrn());
            return contextValue;
        }

        @Override
        public List<ContextValue> getEdcContextValueList(long productRrn, Integer productVersion, long processRrn,
                                                         int processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            EdcLotContextValue edcLotContextValue = new EdcLotContextValue();
            edcLotContextValue.setContextRrn(namedObjectManager
                                                     .getNamedObjectRrn(edcLotContextValue.getContextId(), facilityRrn,
                                                                        ObjectList.CONTEXT_KEY));
            edcLotContextValue.setProductRrn(productRrn);
            edcLotContextValue.setProductVersion(productVersion);
            edcLotContextValue.setProcessRrn(processRrn);
            edcLotContextValue.setProcessVersion(processVersion);
            edcLotContextValue.setContextKey9("MOVEOUT");
            return contextValueManager.getContextValuesByContextKey(edcLotContextValue);
        }

        @Override
        public List<ContextValue> getEdcContextValueList(Long processRrn, Integer processVersion) {
            long facilityRrn = LocalContext.getFacilityRrn();
            EdcLotContextValue edcLotContextValue = new EdcLotContextValue();
            edcLotContextValue.setContextRrn(namedObjectManager
                                                     .getNamedObjectRrn(edcLotContextValue.getContextId(), facilityRrn,
                                                                        ObjectList.CONTEXT_KEY));
            edcLotContextValue.setProcessRrn(processRrn);
            edcLotContextValue.setProcessVersion(processVersion);
            edcLotContextValue.setContextKey9("MOVEOUT");
            return contextValueManager.getContextValuesByContextKey(edcLotContextValue);
        }

        @Override
        public EdcLotContextValue getEdcLotContextValue(Lot lot, Equipment equipment, String actionPoint) {
            EdcLotContextValue edcLotContextValue = new EdcLotContextValue();
            long contextRrn = namedObjectManager
                    .getNamedObjectRrn(edcLotContextValue.getContextId(), LocalContext.getFacilityRrn(),
                                       ObjectList.CONTEXT_KEY);
            if (contextRrn > 0) {
                edcLotContextValue.setContextRrn(contextRrn);
                edcLotContextValue.setProductRrn(lot.getProductRrn());
                edcLotContextValue.setProductVersion(lot.getProductVersion());
                edcLotContextValue.setProcessRrn(lot.getProcessRrn());
                edcLotContextValue.setRouteRrn(lot.getRouteRrn());
                edcLotContextValue.setOperationRrn(lot.getOperationRrn());
                edcLotContextValue.setRecipeRrn(lot.getRecipeRrn());
                edcLotContextValue.setLotRrn(new Long(lot.getLotRrn()));
                if (equipment != null && equipment.getInstanceRrn() > 0) {
                    edcLotContextValue.setEquipmentModel(equipment.getEquipmentModelRrn());
                    edcLotContextValue.setMainEqpt(new Long(equipment.getInstanceRrn()));
                }
                edcLotContextValue.setActionPoint(actionPoint);
                edcLotContextValue.setRouteSeq(lot.getRouteSeq());
                edcLotContextValue.setOperationSeq(lot.getOperationSeq());
                edcLotContextValue.setProcessVersion(lot.getProcessVersion());
                ContextValue contextValue = contextValueManager.simulateContextValue(edcLotContextValue);
                if (contextValue != null) {
                    edcLotContextValue.copy(contextValue);
                }
            }
            return edcLotContextValue;
        }

        @Override
        public Map getEdcContextValueByLot(Map condition) {
            Map result = new HashMap();
            String contextId = "EDC_CONTEXT_LOT";
            Long facilityRrn = MapUtils.getLong(condition, "facilityRrn");
            Long productRrn = MapUtils.getLong(condition, "productRrn");
            Long routeRrn = MapUtils.getLong(condition, "routeRrn");
            Long operationRrn = MapUtils.getLong(condition, "operationRrn");
            Long technologyRrn = MapUtils.getLong(condition, "technologyRrn");
            Long lotRrn = MapUtils.getLong(condition, "lotRrn");
            Long equipmentModel = MapUtils.getLong(condition, "equipmentModel");
            Long entityRrn = MapUtils.getLong(condition, "entityRrn");
            String actionPoint = MapUtils.getString(condition, "actionPoint");
            Long recipeRrn = MapUtils.getLong(condition, "recipeRrn");
            String routeSeq = MapUtils.getString(condition, "routeSeq");
            String operationSeq = MapUtils.getString(condition, "operationSeq");
            String processVersion = MapUtils.getString(condition, "processVersion");
            String productVersion = MapUtils.getString(condition, "productVersion");
            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)) {
                String namedSpace = namedObjectManager.getNamedSpace(facility, ObjectList.CONTEXT_KEY);
                ctxValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextId, namedSpace, ObjectList.CONTEXT_KEY,
                                                                            ObjectList.CONTEXT_TYPE_KEY));
            }

            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 (equipmentModel != null) {
                ctxValue.setContextKey5("" + equipmentModel);
            }
            if (entityRrn != null) {
                ctxValue.setContextKey6("" + entityRrn);
            }
            if (lotRrn != null) {
                ctxValue.setContextKey7("" + lotRrn);
            }
            if (actionPoint != null) {
                ctxValue.setContextKey9(actionPoint);
            }
            if (recipeRrn != null) {
                ctxValue.setContextKey10(recipeRrn + "");
            }

            if (StringUtils.isNotBlank(routeSeq)) {
                ctxValue.setContextKey11(routeSeq);
            }

            if (StringUtils.isNotBlank(operationSeq)) {
                ctxValue.setContextKey12(operationSeq);
            }

            if (StringUtils.isNotBlank(processVersion)) {
                ctxValue.setContextKey13(processVersion);
            }

            if (StringUtils.isNotBlank(productVersion)) {
                ctxValue.setContextKey14(productVersion);
            }

            List<ContextValue> contextValues = contextValueManager.simulateContextValues(ctxValue);
            for (ContextValue contextValue : contextValues) {
                if (contextValue != null) {
                    result.put("PARAMETERSET", contextValue.getResultValue1());
                    result.put("ROUTE", contextValue.getResultValue2());
                    result.put("OPERATION", contextValue.getResultValue3());
                    result.put("PARAMETERSETSPEC", contextValue.getResultValue4());
                    continue;
                }
            }

            return result;
        }

        @Override
        public Map buildContextValueForEdcLot(Long facilityRrn, Map contextValueInfo) {
            EdcLotContextValue edcLotContextValue = new EdcLotContextValue();

            // BeanUtils.copyMapToBean(edcLotContextValue, contextValueInfo);
            edcLotContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
            edcLotContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));
            edcLotContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
            edcLotContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
            edcLotContextValue.setProductVersion(MapUtils.getInteger(contextValueInfo, "productVersion"));
            edcLotContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(edcLotContextValue.getContextId(),
                                                                                  namedObjectManager
                                                                                          .getNamedSpace(facilityRrn,
                                                                                                         ObjectList.CONTEXT_KEY),
                                                                                  ObjectList.CONTEXT_KEY));
            edcLotContextValue.setProcessVersion(MapUtils.getIntValue(contextValueInfo, "processVersion"));
            edcLotContextValue.setContextKey9("MOVEOUT");

            ContextValue contextValue = contextValueManager.getContextValueNoFilter(edcLotContextValue);
            if (contextValue != null) {
                contextValueInfo.put("parameterSetRrn", contextValue.getResultValue1());
                contextValue = contextValueManager.filterContextValueToId(contextValue);
                contextValueInfo.put("parameterSetId", contextValue.getResultValue1());
                if (contextValue.getEcnRrn() != null && contextValue.getEcnRrn() > 0) {
                    contextValueInfo.put("ecnRrn", contextValue.getEcnRrn());
                    contextValueInfo.put("status", contextValue.getStatus());
                    contextValueInfo.put("effectiveDateFrom", contextValue.getEffectiveDateFrom());
                    contextValueInfo.put("effectiveDateTo", contextValue.getEffectiveDateTo());
                }
                if (StringUtils.isNotBlank(contextValue.getResultValue1())) {
                    contextValueInfo.put("hasSetEDCFlag", true);
                }
            }

            return contextValueInfo;
        }

        @Override
        public boolean isValidDescForEDCPlan(long contextRrn, long productRrn, long processRrn, long operationRrn,
                                             String desc) {
            boolean isValidDescForEDCPlan = contextValueManager
                    .isValidDescForEDCPlan(contextRrn, productRrn, processRrn, operationRrn, desc);
            if (!isValidDescForEDCPlan) {
                isValidDescForEDCPlan = contextValueManager
                        .isValidInactiveDescForEDCPlan(contextRrn, productRrn, processRrn, operationRrn, desc);
            }
            return isValidDescForEDCPlan;
        }

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

        private EdcLotContextValue buildNewEdcLotContextValueForProcess(Long productRrn, Integer productVersion,
                                                                        Long processRrn, Integer processVersion,
                                                                        Long routeRrn, Long operationRrn) {
            EdcLotContextValue contextValue = buildNewEdcLotContextValue();

            contextValue.setProductRrn(productRrn);
            contextValue.setProductVersion(productVersion);
            contextValue.setProcessRrn(processRrn);
            contextValue.setProcessVersion(processVersion);
            contextValue.setRouteRrn(routeRrn);
            contextValue.setOperationRrn(operationRrn);
            return contextValue;
        }

    }