RecipeContextManagerImpl.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.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.dao.RecipeContextManagerDao;
import com.mycim.server.ctx.exec.manager.RecipeContextManager;
import com.mycim.server.ctx.manager.ContextValueManager;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.RecipeContextValue;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * @author shijie.deng
 * @date 2019/8/31
 **/
@Service
@Transactional
public class RecipeContextManagerImpl implements RecipeContextManager {

    final static private String ECN_STATUS_ACTIVE = "ACTIVE";

    private final ContextValueManager contextValueManager;

    private final NamedObjectManager namedObjectManager;

    private final RecipeContextManagerDao recipeContextManagerDao;

    public RecipeContextManagerImpl(ContextValueManager contextValueManager, NamedObjectManager namedObjectManager,
                                    RecipeContextManagerDao recipeContextManagerDao) {
        this.contextValueManager = contextValueManager;
        this.namedObjectManager = namedObjectManager;
        this.recipeContextManagerDao = recipeContextManagerDao;
    }

    @Override
    public Long getOperationRecipeRrn(long operationRrn) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValue(operationRrn);

        recipeContextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(recipeContextValue.getContextId(),
                                                                              LocalContext.getFacilityRrn(),
                                                                              ObjectList.CONTEXT_KEY));
        recipeContextValue.setOperationRrn(operationRrn);

        ContextValue contextValue = contextValueManager.getContextValueNoFilter(recipeContextValue);
        if (contextValue == null) {
            return 0L;
        }
        recipeContextValue.copy(contextValue);
        return recipeContextValue.getRecipeRrn();
    }

    @Override
    public String getOperationRecipeId(long operationRrn) {
        return namedObjectManager.getNamedObjectId(getOperationRecipeRrn(operationRrn));
    }

    @Override
    public void addOperationRecipe(long operationRrn, String recipeId) {

        if (StringUtils.isNotEmpty(recipeId)) {
            long recipeRrn = namedObjectManager
                    .getNamedObjectRrn(recipeId, LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY);
            Assert.isFalse(recipeRrn == 0,
                           Errors.create().key(MessageIdList.RECIPE_MISSING).content("The Recipe {} does not exist!")
                                 .args(recipeId).build());

            RecipeContextValue contextValue = buildNewRecipeContextValueForOperation(operationRrn, recipeRrn);

            contextValueManager.addContextValueNOFilter(contextValue, LocalContext.getUserRrn());
        }
    }

    @Override
    public String getRecipeString(Map parameter) {
        //TODO:此方法返回的实际上是RRN,有误解 需要优化
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForLot(parameter);

        StringBuilder sb = new StringBuilder();

        for (ContextValue contextValue : contextValueManager.simulateContextValues(recipeContextValue)) {
            if (contextValue == null) {
                continue;
            }
            sb.append(contextValue.getResultValue1());

            if (contextValue.getResultValue2() != null) {
                sb.append("|");
                sb.append(contextValue.getResultValue2());
            }
            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 String getRecipeString(Lot lot) {
        //TODO:此方法返回的实际上是RRN,有误解 需要优化
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForLot(lot);

        StringBuilder sb = new StringBuilder();

        for (ContextValue contextValue : contextValueManager.simulateContextValues(recipeContextValue)) {
            if (contextValue == null) {
                continue;
            }
            sb.append(contextValue.getResultValue1());

            if (contextValue.getResultValue2() != null) {
                sb.append("|");
                sb.append(contextValue.getResultValue2());
            }
            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 List<Long> getRecipeRrnList(Map<String, Object> lotInfo) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForLotInfo(lotInfo);

        return getRecipeRrnList(recipeContextValue);
    }

    @Override
    public List<Long> getRecipeRrnList(Lot lot) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForLot(lot);

        return getRecipeRrnList(recipeContextValue);
    }

    @Override
    public int countContextValue(long recipeRrn) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForRecipe(recipeRrn);
        return contextValueManager.countContextValueByResultValue(recipeContextValue);
    }

    @Override
    public List<ContextValue> getContextValues(String recipeId) {
        return getContextValues(
                namedObjectManager.getNamedObjectRrn(recipeId, LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY));
    }

    @Override
    public List<ContextValue> getContextValues(long recipeRrn) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForRecipe(recipeRrn);
        List<ContextValue> contextValues = recipeContextManagerDao.getContextValues(recipeContextValue);

        for (ContextValue c : contextValues) {
            contextValueManager.filterContextValueToId(c);
        }
        return contextValues;
    }

    @Override
    public String getProcessRecipeId(Long productRrn, Integer productVersion, Long processRrn, Integer processVersion,
                                     Long routeRrn, Long operationRrn) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForProcess(productRrn, productVersion,
                                                                                     processRrn, processVersion,
                                                                                     routeRrn, operationRrn);
        ContextValue contextValue = contextValueManager.simulateContextValue(recipeContextValue);
        if (contextValue != null && StringUtils.isNotBlank(contextValue.getResultValue1())) {
            return namedObjectManager.getInstanceId(Long.parseLong(contextValue.getResultValue1()));
        }
        return null;
    }

    @Override
    public ContextValue getProcessRecipeContextValue(Long productRrn, Integer productVersion, Long processRrn,
                                                     Integer processVersion, Long routeRrn, Long operationRrn) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForProcess(productRrn, productVersion,
                                                                                     processRrn, processVersion,
                                                                                     routeRrn, operationRrn);
        ContextValue contextValue = contextValueManager
                .getContextValue(recipeContextValue, LocalContext.getFacilityRrn());
        return contextValue;
    }

    @Override
    public void removeOperationRecipe(long operationRrn) {
        if (operationRrn > 0) {
            RecipeContextValue contextValue = buildNewRecipeContextValueForOperation(operationRrn, null);

            contextValueManager.removeContextValueNotFilter(contextValue, LocalContext.getUserRrn());
        }
    }

    @Override
    public List getRecipeRrnAndParam(Map tempInfo) {
        String contextId = ContextNames.RECIPE_CONTEXT;
        MapUtils.getLong(tempInfo, "");
        MapUtils.getString(tempInfo, "");
        Long facilityRrn = MapUtils.getLong(tempInfo, "facilityRrn");
        Long productRrn = MapUtils.getLong(tempInfo, "productRrn");
        Long routeRrn = MapUtils.getLong(tempInfo, "routeRrn");
        Long operationRrn = MapUtils.getLong(tempInfo, "operationRrn");
        Long processRrn = MapUtils.getLong(tempInfo, "processRrn");
        Long technologyRrn = MapUtils.getLong(tempInfo, "technologyRrn");
        Long lotRrn = MapUtils.getLong(tempInfo, "lotRrn");
        Long equipmentModel = MapUtils.getLong(tempInfo, "equipmentModel");
        Long entityRrn = MapUtils.getLong(tempInfo, "entityRrn");
        String productLayer = MapUtils.getString(tempInfo, "productLayer");
        String routeSeq = MapUtils.getString(tempInfo, "routeSeq");
        String operationSeq = MapUtils.getString(tempInfo, "operationSeq");
        String processVersion = MapUtils.getString(tempInfo, "processVersion");
        String productVersion = MapUtils.getString(tempInfo, "productVersion");
        String prevOperationRrn = MapUtils.getString(tempInfo, "prevOperationRrn");
        String lotStatus = MapUtils.getString(tempInfo, "lotStatus");
        long facility = 1;

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

        ContextValue ctxValue = new ContextValue();

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

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

        if (StringUtils.equalsIgnoreCase(LotStatus.BANKED, lotStatus) ||
                StringUtils.equalsIgnoreCase(LotStatus.OUTSOURCING, lotStatus)) {
            if (prevOperationRrn != null) {
                ctxValue.setContextKey1("" + prevOperationRrn);
            }
        } else {
            if (operationRrn != null) {
                ctxValue.setContextKey1("" + operationRrn);
            }
        }

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

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

        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 (productLayer != null) {
            ctxValue.setContextKey8("" + productLayer);
        }

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

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

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

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

        List list = new ArrayList();
        List<ContextValue> contextValues = contextValueManager.simulateContextValuesForRecipeAndOperationSeq(ctxValue);
        for (Object obj : contextValues) {
            ctxValue = (ContextValue) obj;
            Map map = new HashMap();
            if (ctxValue == null) {
                continue;
            }

            map.put("recipeRrn", ctxValue.getResultValue1());
            if (ctxValue.getResultValue2() != null) {
                map.put("recipeParam", ctxValue.getResultValue2());
            }
            if (StringUtils.isNotEmpty(ctxValue.getResultValue5())) {
                map.put("bomRrn", ctxValue.getResultValue5());
            }
            list.add(map);
        }
        return list;
    }

    @Override
    public List<ContextValue> getRecipeContextValueList(long productRrn, Integer productVersion, long processRrn,
                                                        int processVersion) {
        long facilityRrn = LocalContext.getFacilityRrn();
        RecipeContextValue recipeContextValue = new RecipeContextValue();
        recipeContextValue.setContextRrn(namedObjectManager
                                                 .getNamedObjectRrn(recipeContextValue.getContextId(), facilityRrn,
                                                                    ObjectList.CONTEXT_KEY));

        recipeContextValue.setProductRrn(productRrn);
        recipeContextValue.setProductVersion(productVersion);
        recipeContextValue.setProcessRrn(processRrn);
        recipeContextValue.setProcessVersion(processVersion);

        return contextValueManager.getContextValuesByContextKey(recipeContextValue);
    }

    @Override
    public List<ContextValue> getRecipeContextValueList(Long processRrn, Integer processVersion) {
        long facilityRrn = LocalContext.getFacilityRrn();
        RecipeContextValue recipeContextValue = new RecipeContextValue();
        recipeContextValue.setContextRrn(namedObjectManager
                                                 .getNamedObjectRrn(recipeContextValue.getContextId(), facilityRrn,
                                                                    ObjectList.CONTEXT_KEY));

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

        return contextValueManager.getContextValuesByContextKey(recipeContextValue);
    }

    @Override
    public RecipeContextValue getRecipeContextValue(Lot lot) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValueForLot(lot);

        ContextValue contextValue = contextValueManager.simulateContextValue(recipeContextValue);
        if (contextValue == null) {
            return null;
        }

        recipeContextValue.copy(contextValue);

        return recipeContextValue;
    }

    @Override
    public String parseRecipeId(String recipeString) {
        StringBuilder sb = new StringBuilder();

        if (StringUtils.isNotEmpty(recipeString)) {
            String[] recipes = recipeString.split(",");
            for (int i = 0; i < recipes.length; i++) {
                String recipe = recipes[i];

                String recipeRrn = recipe;
                if (recipe.contains("|")) {
                    recipeRrn = recipe.substring(0, recipe.indexOf("|"));
                }

                if (StringUtils.isNotEmpty(recipeRrn)) {
                    String recipeId = namedObjectManager.getInstanceId(Long.parseLong(recipeRrn));
                    sb.append(recipeId);
                }
                if (i < recipes.length - 1) {
                    sb.append(",");
                }
            }
        }

        return sb.toString();
    }

    @Override
    public String parseRecipeParam(String recipeString) {
        StringBuffer sb = new StringBuffer();

        if (StringUtils.isNotEmpty(recipeString)) {
            String[] recipes = recipeString.split(",");
            for (int i = 0; i < recipes.length; i++) {
                String recipe = recipes[i];

                String recipeparam = "";
                if (recipe.contains("|")) {
                    recipeparam = recipe.substring(0, recipe.indexOf("|"));
                }

                sb.append(recipeparam);
                if (i < recipes.length - 1) {
                    sb.append(",");
                }
            }
        }
        return sb.toString();
    }

    @Override
    public void recordRecipeUsedTime(Lot lot) {
        RecipeContextValue contextValue = getRecipeContextValue(lot);
        if (contextValue != null && StringUtils.isNotBlank(contextValue.getCheckUnit()) &&
                contextValue.getFrequency() > 0) {
            contextValue.setLastUsedTime(System.currentTimeMillis());
            contextValueManager.updateContextValue(contextValue, contextValue, LocalContext.getFacilityRrn(),
                                                   LocalContext.getUserRrn());
        }
    }

    @Override
    public Map getRecipe4ViewPrpAction(Map condition) {
        String contextId = "RECIPE_CONTEXT";
        Long facilityRrn = (Long) condition.get("facilityRrn");
        Long productRrn = (Long) condition.get("productRrn");
        String routeRrn = (String) condition.get("routeRrn");
        Long operationRrn = (Long) condition.get("operationRrn");
        Long technologyRrn = (Long) condition.get("technologyRrn");
        Long lotRrn = (Long) condition.get("lotRrn");
        Long equipmentModel = (Long) condition.get("equipmentModel");
        Long entityRrn = (Long) condition.get("entityRrn");
        String productLayer = (String) condition.get("productLayer");
        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));
        }

        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 (productLayer != null) {
            ctxValue.setContextKey8("" + productLayer);
        }

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

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

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

        if (StringUtils.isNotBlank(productVersion)) {
            ctxValue.setContextKey12(productVersion);
        }
        return getRecipe4ViewPrpAction(ctxValue);
    }

    @Override
    public RecipeContextValue buildContextValue(Map tempInfo) {

        RecipeContextValue recipeContextValue = new RecipeContextValue();
        Long facilityRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "facilityRrn");
        Long productRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "productRrn");

        String routeRrn = org.apache.commons.collections.MapUtils.getString(tempInfo, "routeRrn");
        Long operationRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "operationRrn");
        Long processRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "processRrn");
        Long lotRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "lotRrn");
        Long equipmentModel = org.apache.commons.collections.MapUtils.getLong(tempInfo, "equipmentModel");
        Long entityRrn = org.apache.commons.collections.MapUtils.getLong(tempInfo, "entityRrn");
        String productLayer = org.apache.commons.collections.MapUtils.getString(tempInfo, "productLayer");
        String routeSeq = org.apache.commons.collections.MapUtils.getString(tempInfo, "routeSeq");
        String operationSeq = org.apache.commons.collections.MapUtils.getString(tempInfo, "operationSeq");
        String processVersion = org.apache.commons.collections.MapUtils.getString(tempInfo, "processVersion");
        Integer productVersion = MapUtils.getInteger(tempInfo, "productVersion");
        long facility = 1;

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

        recipeContextValue.setOperationRrn(operationRrn);
        recipeContextValue.setRouteRrn(new Long(routeRrn));
        recipeContextValue.setProcessRrn(processRrn);
        recipeContextValue.setProductRrn(productRrn);
        recipeContextValue.setEquipmentModel(equipmentModel);
        recipeContextValue.setMainEqpt(entityRrn);
        recipeContextValue.setLotRrn(lotRrn);
        recipeContextValue.setProductLayer(productLayer);
        recipeContextValue.setRouteSeq(routeSeq);
        recipeContextValue.setOperationSeq(operationSeq);
        recipeContextValue.setProcessVersion(new Integer(processVersion));
        recipeContextValue.setProductVersion(productVersion);

        recipeContextValue.setContextRrn(namedObjectManager
                                                 .getNamedObjectRrn(recipeContextValue.getContextId(), facility,
                                                                    ObjectList.CONTEXT_KEY));

        return recipeContextValue;
    }

    @Override
    public void deleteContextValue(Long facilityRrn, Long userRrn, String transId,
                                   RecipeContextValue recipeContextValue) {
        if (recipeContextValue.getContextRrn() <= 0) {
            recipeContextValue.setContextRrn(getRecipeContextRrn(facilityRrn));
        }

        contextValueManager.deleteContextValueByContextKey(transId, recipeContextValue);
    }

    public List<Long> getRecipeRrnList(RecipeContextValue recipeContextValue) {
        List<ContextValue> contextValues = contextValueManager
                .simulateContextValuesForRecipeAndOperationSeq(recipeContextValue);

        List<Long> recipeList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(contextValues)) {
            for (ContextValue contextValue : contextValues) {
                recipeList.add(Long.parseLong(contextValue.getResultValue1()));
            }
        }

        return recipeList;
    }

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

    private long getRecipeContextRrn(Long facilityRrn) {
        return namedObjectManager.getNamedObjectRrn(ContextNames.RECIPE_CONTEXT, facilityRrn, ObjectList.CONTEXT_KEY);
    }

    private RecipeContextValue buildNewRecipeContextValueForProcess(Long productRrn, Integer productVersion,
                                                                    Long processRrn, Integer processVersion,
                                                                    Long routeRrn, Long operationRrn) {
        RecipeContextValue contextValue = buildNewRecipeContextValue();

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

    private RecipeContextValue buildNewRecipeContextValueForLotInfo(Map<String, Object> lotInfo) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValue();
        Long productRrn = MapUtils.getLong(lotInfo, "productRrn");
        Long routeRrn = MapUtils.getLong(lotInfo, "routeRrn");
        Long operationRrn = MapUtils.getLong(lotInfo, "operationRrn");
        Long processRrn = MapUtils.getLong(lotInfo, "processRrn");
        Long technologyRrn = MapUtils.getLong(lotInfo, "technologyRrn");
        Long lotRrn = MapUtils.getLong(lotInfo, "lotRrn");
        Long equipmentModel = MapUtils.getLong(lotInfo, "equipmentModel");
        Long entityRrn = MapUtils.getLong(lotInfo, "entityRrn");
        String productLayer = MapUtils.getString(lotInfo, "productLayer");
        String routeSeq = MapUtils.getString(lotInfo, "routeSeq");
        String operationSeq = MapUtils.getString(lotInfo, "operationSeq");
        Integer processVersion = MapUtils.getInteger(lotInfo, "processVersion");
        Long prevOperationRrn = MapUtils.getLong(lotInfo, "prevOperationRrn");
        String lotStatus = MapUtils.getString(lotInfo, "lotStatus");
        Integer productVersion = MapUtils.getInteger(lotInfo, "productVersion");

        recipeContextValue.setOperationRrn(operationRrn);
        recipeContextValue.setProductRrn(productRrn);
        recipeContextValue.setRouteRrn(routeRrn);
        recipeContextValue.setProcessRrn(technologyRrn);
        recipeContextValue.setLotRrn(lotRrn);
        recipeContextValue.setEquipmentModel(equipmentModel);
        recipeContextValue.setMainEqpt(entityRrn);
        recipeContextValue.setProductLayer(productLayer);
        recipeContextValue.setRouteSeq(routeSeq);
        recipeContextValue.setOperationSeq(operationSeq);
        recipeContextValue.setProcessVersion(processVersion);
        recipeContextValue.setProductVersion(productVersion);

        if (StringUtils.equalsIgnoreCase(LotStatus.BANKED, lotStatus) ||
                StringUtils.equalsIgnoreCase(LotStatus.OUTSOURCING, lotStatus)) {
            if (prevOperationRrn != null) {
                recipeContextValue.setOperationRrn(prevOperationRrn);
            }
        }

        return recipeContextValue;
    }

    private RecipeContextValue buildNewRecipeContextValueForLot(Map parameter) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValue();

        Long productRrn = MapUtils.getLong(parameter, "productRrn");
        long routeRrn = MapUtils.getLongValue(parameter, "routeRrn");
        Long operationRrn = MapUtils.getLong(parameter, "operationRrn");
        Long technologyRrn = MapUtils.getLong(parameter, "technologyRrn");
        Long lotRrn = MapUtils.getLong(parameter, "lotRrn");
        Long equipmentModel = MapUtils.getLong(parameter, "equipmentModel");
        Long entityRrn = MapUtils.getLong(parameter, "entityRrn");
        String productLayer = MapUtils.getString(parameter, "productLayer");
        String routeSeq = MapUtils.getString(parameter, "routeSeq");
        String operationSeq = MapUtils.getString(parameter, "operationSeq");
        int processVersion = MapUtils.getIntValue(parameter, "processVersion");
        Integer productVersion = MapUtils.getInteger(parameter, "productVersion");

        recipeContextValue.setProductRrn(productRrn);
        recipeContextValue.setProductVersion(productVersion);
        recipeContextValue.setRouteRrn(routeRrn);
        recipeContextValue.setOperationRrn(operationRrn);
        recipeContextValue.setProcessRrn(technologyRrn);
        recipeContextValue.setLotRrn(lotRrn);
        recipeContextValue.setEquipmentModel(equipmentModel);
        recipeContextValue.setMainEqpt(entityRrn);
        recipeContextValue.setProductLayer(productLayer);
        recipeContextValue.setRouteSeq(routeSeq);
        recipeContextValue.setOperationSeq(operationSeq);
        recipeContextValue.setProcessVersion(processVersion);

        return recipeContextValue;
    }

    private RecipeContextValue buildNewRecipeContextValueForLot(Lot lot) {
        RecipeContextValue recipeContextValue = buildNewRecipeContextValue();
        Long productRrn = lot.getProductRrn();
        Long routeRrn = lot.getRouteRrn();
        Long operationRrn = lot.getOperationRrn();
        Long technologyRrn = lot.getProcessRrn();
        Long lotRrn = lot.getLotRrn();
        Long equipmentModel = null;
        Long entityRrn = lot.getEqptRrn();
        String productLayer = lot.getProductLayer();
        String routeSeq = lot.getRouteSeq();
        String operationSeq = lot.getOperationSeq();
        Integer processVersion = lot.getProcessVersion();
        Integer productVersion = lot.getProductVersion();
        if (StringUtils.equalsIgnoreCase(LotStatus.BANKED, lot.getLotStatus()) ||
                StringUtils.equalsIgnoreCase(LotStatus.OUTSOURCING, lot.getLotStatus())) {
            operationRrn = lot.getPrevOperationRrn();
        }
        recipeContextValue.setProductRrn(productRrn);
        recipeContextValue.setRouteRrn(routeRrn);
        recipeContextValue.setOperationRrn(operationRrn);
        recipeContextValue.setProcessRrn(technologyRrn);
        recipeContextValue.setLotRrn(lotRrn);
        recipeContextValue.setEquipmentModel(equipmentModel);
        recipeContextValue.setMainEqpt(entityRrn);
        recipeContextValue.setProductLayer(productLayer);
        recipeContextValue.setRouteSeq(routeSeq);
        recipeContextValue.setOperationSeq(operationSeq);
        recipeContextValue.setProcessVersion(processVersion);
        recipeContextValue.setProductVersion(productVersion);

        return recipeContextValue;
    }

    private RecipeContextValue buildNewRecipeContextValue(long operationRrn) {
        RecipeContextValue contextValue = buildNewRecipeContextValue();

        contextValue.setOperationRrn(operationRrn);
        return contextValue;
    }

    private RecipeContextValue buildNewRecipeContextValueForRecipe(long recipeRrn) {
        RecipeContextValue contextValue = buildNewRecipeContextValue();

        contextValue.setRecipeRrn(recipeRrn);
        return contextValue;
    }

    private RecipeContextValue buildNewRecipeContextValueForOperation(long operationRrn, Long recipeRrn) {
        RecipeContextValue contextValue = buildNewRecipeContextValue();

        contextValue.setStatus(ECN_STATUS_ACTIVE);
        contextValue.setOperationRrn(operationRrn);
        contextValue.setRecipeRrn(recipeRrn);
        return contextValue;
    }

    private Map getRecipe4ViewPrpAction(ContextValue contextValue) {

        Map recipeMap = new HashMap();
        String where4RecipeByProduct = " AND context_key1 = '" + contextValue.getContextKey1() + "' AND context_key2 " +
                "= ' ' AND context_key3 = ' ' AND context_key4 " + "= '" + contextValue.getContextKey4() + "' AND " +
                "context_key5 = ' ' AND context_key6 = ' ' AND context_key7 " + "= ' '" + " AND context_key8 = ' ' " +
                "AND context_key9 = ' ' AND context_key10 " + "= ' '";
        List values = recipeContextManagerDao
                .getContextValuesForRecipeAndOperationSeq(contextValue.getContextRrn(), where4RecipeByProduct);
        if (values != null && !values.isEmpty()) {
            ContextValue ctxValue = (ContextValue) values.iterator().next();
            StringBuffer sb = new StringBuffer();
            sb.append(ctxValue.getResultValue1());

            if (ctxValue.getResultValue2() != null) {
                sb.append("|");
                sb.append(ctxValue.getResultValue2());
            }
            recipeMap.put("product", sb.toString());
        }

        String where4RecipeByRoute =
                "AND context_key1 = '" + contextValue.getContextKey1() + "' AND context_key2 = " + "'" +
                        contextValue.getContextKey2() + "' AND context_key3 = ' ' AND context_key4 = ' ' AND " +
                        "context_key5 = " + "' ' " +
                        "AND context_key6 = ' ' AND context_key7 = ' ' AND context_key8 = ' ' " +
                        "AND context_key9 = ' ' AND context_key10 = ' '";
        values = recipeContextManagerDao
                .getContextValuesForRecipeAndOperationSeq(contextValue.getContextRrn(), where4RecipeByRoute);
        if (values != null && !values.isEmpty()) {
            ContextValue ctxValue = (ContextValue) values.iterator().next();
            StringBuffer sb = new StringBuffer();
            sb.append(ctxValue.getResultValue1());

            if (ctxValue.getResultValue2() != null) {
                sb.append("|");
                sb.append(ctxValue.getResultValue2());
            }
            recipeMap.put("route", sb.toString());
        }

        return recipeMap;
    }

}