# ProcessLocationContextManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.beans.BeanUtils;
    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.ProcessLocationContextManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.prp.ContextValue;
    import com.mycim.valueobject.prp.ProcessLocationContextValue;
    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.List;
    import java.util.Map;

    /**
     * @author sandy
     * @version 6.0.0
     * @date 2019/9/3
     **/
    @Service
    @Transactional
    public class ProcessLocationContextManagerImpl implements ProcessLocationContextManager {

        @Autowired
        private ContextValueManager contextValueManager;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Override
        public String getProccessLocation(Map<String, Object> contextValueInfo) {
            ProcessLocationContextValue processLocationContextValue = buildNewProcessLocationContextValueInfo(
                    contextValueInfo);
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(processLocationContextValue);
            if (contextValue != null) {
                return contextValue.getResultValue1();
            }
            return StringUtils.EMPTY;
        }

        @Override
        public ContextValue getProccessLocationContextValue(Long productRrn, Long processRrn, Integer processVersion,
                                                            Long routeRrn, Long operationRrn) {
            ProcessLocationContextValue processLocationContextValue = buildNewProcessLocationContextValueInfo(productRrn,
                                                                                                              processRrn,
                                                                                                              processVersion,
                                                                                                              routeRrn,
                                                                                                              operationRrn);
            ContextValue contextValue = contextValueManager.getContextValueNoFilter(processLocationContextValue);
            return contextValue;
        }

        @Override
        public String getCurrentProcessLocation(Long facilityRrn, Lot lot) {
            String location = StringUtils.EMPTY;
            ProcessLocationContextValue ctx = new ProcessLocationContextValue();
            ctx.setContextRrn(
                    namedObjectManager.getNamedObjectRrn(ctx.getContextId(), facilityRrn, ObjectList.CONTEXT_KEY));
            ctx.setProductRrn(lot.getProductRrn());
            ctx.setProcessRrn(lot.getProcessRrn());
            ctx.setRouteRrn(lot.getRouteRrn());
            ctx.setOperationRrn(lot.getOperationRrn());
            ctx.setProcessVersion(lot.getProcessVersion());
            ContextValue contextValue;
            contextValue = contextValueManager.getContextValueNoFilter(ctx);
            if (contextValue != null && StringUtils.isNotBlank(contextValue.getResultValue1())) {
                location = contextValue.getResultValue1();
            }
            return location;
        }

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

        @Override
        public Map buildContextValueForProcessLocation(Long facilityRrn, Map contextValueInfo) {
            ProcessLocationContextValue processLocationContextValue = new ProcessLocationContextValue();
            BeanUtils.copyMapToBean(processLocationContextValue, contextValueInfo);
            processLocationContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
            processLocationContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
            processLocationContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
            processLocationContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));
            processLocationContextValue.setContextRrn(namedObjectManager
                                                              .getNamedObjectRrn(processLocationContextValue.getContextId(),
                                                                                 namedObjectManager
                                                                                         .getNamedSpace(facilityRrn,
                                                                                                        ObjectList.CONTEXT_KEY),
                                                                                 ObjectList.CONTEXT_KEY));

            ContextValue contextValue = contextValueManager.getContextValueNoFilter(processLocationContextValue);
            if (contextValue != null) {
                contextValueInfo.put("processLocation", contextValue.getResultValue1());
                contextValueInfo.put("activeProcessLocation", 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());
                }
            }
            return contextValueInfo;
        }

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

        private ProcessLocationContextValue buildNewProcessLocationContextValueInfo(Long productRrn, Long processRrn,
                                                                                    Integer processVersion, Long routeRrn,
                                                                                    Long operationRrn) {
            ProcessLocationContextValue processLocationContextValue = buildNewProcessLocationContextValue();

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

            return processLocationContextValue;
        }

        private ProcessLocationContextValue buildNewProcessLocationContextValueInfo(Map<String, Object> lotInfo) {
            ProcessLocationContextValue processLocationContextValue = buildNewProcessLocationContextValue();
            Long productRrn = MapUtils.getLong(lotInfo, "productRrn");
            Long routeRrn = MapUtils.getLong(lotInfo, "routeRrn");
            Long processRrn = MapUtils.getLong(lotInfo, "processRrn");
            Long operationRrn = MapUtils.getLong(lotInfo, "operationRrn");
            Integer processVersion = MapUtils.getInteger(lotInfo, "processVersion");

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

            return processLocationContextValue;
        }

    }