InstructionEditAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.instruction;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
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.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.alm.EenAction;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.consts.EenActionType;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.StageContextValue;
import com.mycim.valueobject.sys.Facility;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class InstructionEditAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        return mapping.getInputForward();
    }

    // --------------------------------------------------------- Public Methods
    public ActionForward instructionEdit(ActionMapping mapping, LotInfoForm theform,
                                         HttpServletRequest request) throws Exception {
        initLotBaseInfoForJsp(request, true, null, null);
        Long userRrn = LocalContext.getUserRrn();
        String username = LocalContext.getUserId();

        Long facilityRrn = LocalContext.getFacilityRrn();

        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }
        Map cachePageInfo = (Map) WebUtils.getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap();
        }

        if (request.getParameter("edit") != null) {

            String id = theform.getInstanceId().trim().toUpperCase();

            if ("lot".equalsIgnoreCase(theform.getByPlanLotFlag())) {
                Lot lot = lotQueryService.getLot(id, facilityRrn);
                Assert.isFalse((lot == null) || (lot.getLotRrn() <= 0),
                               Errors.create().key(MessageIdList.LOT_MISSING_ID).content("No such lotId").build());
                String lot_status = lot.getLotStatus();
                Assert.isTrue(lot_status != null &&
                                      (lot_status.equalsIgnoreCase("WAITING") || lot_status.equalsIgnoreCase("HOLD") ||
                                              lot_status.equalsIgnoreCase("RUNNING") ||
                                              lot_status.equalsIgnoreCase("DISPATCH") ||
                                              lot_status.equalsIgnoreCase(LotStatus.BANKED)),
                              Errors.create().key(MessageIdList.INSTRUCTIONEDIT_LOT_STATUS_INCORRECT)
                                    .content("批次状态只能为waiting或hold或running或dispatch或banked!").build());
                LotInfoForm lotInfo = new LotInfoForm();
                PropertyUtils.copyProperties(lotInfo, lot);
                lotInfo.setRecipePhysicalId(getRecipePhysicalId(lot));

                //session.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfo);
                //session.setAttribute("stepNumber", lot.getStepNumberInProcess());
                //session.setAttribute("lotRrn", new Long(lot.getLotRrn()));
                //session.setAttribute(SessionNames.LOT_KEY, lot);

                request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfo);
                request.setAttribute("stepNumber", lot.getStepNumberInProcess());
                request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
                request.setAttribute(SessionNames.LOT_KEY, lot);
                cachePageInfo.put("stepNumber", lot.getStepNumberInProcess());
                cachePageInfo.put("lotRrn", new Long(lot.getLotRrn()));
                theform.setCacheLot(WebUtils.getCacheObj2String(lot));
                theform.setCacheLotInfo(WebUtils.getCacheObj2String(lotInfo));

                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                Map conditionMap = BeanUtils.copyBeanToMap(lot);
                lotInfo.setOperationDesc(ctxExecService.getOperationDescByProcessInfo(conditionMap));
                lotInfo.setFlowSeq(ctxExecService.getFlowSeqByProcessInfo(conditionMap));
                // instructionListbyLot = this.getWipSetupManager().getInstructionByProduct(refRrn,
                // lot.getProductRrn(),lot.getProcessRrn());
                List instructionListbyLot = getInstructionByLot(facilityRrn, refRrn, lot);

                //session.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                request.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);

                // // add by mark
                // Collection futureHoldProductList = new ArrayList();
                // long productRefRrn = this.getBaseManager().getNamedObjectRrn("EEN_CONTEXT_HOLDPRODUCT",
                // this.getBaseManager().getNamedSpace(lot.getFacilityRrn().longValue(),
                // "CONTEXT"),
                // "CONTEXT");
                // long routeRrn = this.getBaseManager().getNamedObjectRrn(lot.getRouteId(),
                // this.getBaseManager().getNamedSpace(lot.getFacilityRrn().longValue(),
                // "WFL"),
                // "WFL", "ROUTE");
                // Map productMap = new HashMap();
                // productMap.put("refRrn", new Long(productRefRrn));
                // productMap.put("productRrn", lot.getProductRrn().toString());
                // productMap.put("processRrn", lot.getProcessRrn().toString());
                // productMap.put("routeRrn", String.valueOf(routeRrn));
                // productMap.put("operationRrn", lot.getOperationRrn().toString());
                // productMap.put("lotRrn", new Long(lot.getLotRrn()));
                // futureHoldProductList = this.getLotManager().getFutureholdProductListForQuery(productMap);
                // if (!futureholdListbyLot.isEmpty()) {
                // futureHoldProductList.addAll(futureholdListbyLot);
                // }
                //
                // session.setAttribute("allfutureinfo", futureHoldProductList);

            } else if ("product".equalsIgnoreCase(theform.getByPlanLotFlag())) {
                long productRrn = 0;
                productRrn = this.getInstanceRrn(id, this.getNamedSpace("PRODUCT", facilityRrn), "PRODUCT");
                Assert.isFalse(productRrn == 0,
                               Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("{}产品不存在!").args(id)
                                     .build());

                //session.setAttribute("productRrn", new Long(productRrn));
                request.setAttribute("productRrn", new Long(productRrn));
                cachePageInfo.put("productRrn", new Long(productRrn));

                theform.setInstanceDesc(this.getInstanceDesc(productRrn));
                theform.setNamedSpace(this.getNamedSpace("PRODUCT", facilityRrn));
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                List instructionListbyProduct = ctxExecService.getInstructionByProduct(refRrn, productRrn);

                //session.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);

            } else if ("process".equalsIgnoreCase(theform.getByPlanLotFlag())) {
                long processRrn = 0;
                processRrn = this.getInstanceRrn(id, this.getNamedSpace("WFL", facilityRrn), "WFL");
                Assert.isFalse(processRrn == 0,
                               Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING).content("流程不存在!").args(id)
                                     .build());

                //session.setAttribute("processRrn", new Long(processRrn));
                request.setAttribute("processRrn", new Long(processRrn));
                cachePageInfo.put("processRrn", new Long(processRrn));

                theform.setInstanceDesc(this.getInstanceDesc(processRrn));
                theform.setNamedSpace(this.getNamedSpace("PROCESS", facilityRrn));
                theform.setProcessId(id);
                theform.setProcessRrn(new Long(processRrn));
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                List instructionListbyProcess = ctxExecService.getInstructionByProcess(refRrn, processRrn);

                //session.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);

            } else if ("operation".equalsIgnoreCase(theform.getByPlanLotFlag())) {
                // add by fan ,for future hold by operation
                long operationRrn = 0;
                operationRrn = this.getInstanceRrn(id, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                Assert.isFalse(operationRrn == 0,
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_MISSING).content("步骤号不存在!")
                                     .args(id).build());

                //session.setAttribute("operationRrn", new Long(operationRrn));
                request.setAttribute("operationRrn", new Long(operationRrn));
                cachePageInfo.put("operationRrn", new Long(operationRrn));

                theform.setInstanceDesc(this.getInstanceDesc(operationRrn));
                theform.setNamedSpace(this.getNamedSpace("OPERATION", facilityRrn));
                long refRrn = this
                        .getInstanceRrn("EEN_CONTEXT_HOLDOPERATION", this.getNamedSpace("CONTEXT", facilityRrn),
                                        "CONTEXT");
                List futureholdListbyOperation = ctxExecService.getFutureHoldListbyOperation(refRrn, operationRrn);

                // session.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY,
                // futureholdListbyOperation);
                request.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                cachePageInfo.put(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
            }

            theform.setInstanceId(id);

            // session.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
            request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
            cachePageInfo.put(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        } else if (request.getParameter("addmember") != null) {
            //String flag = (String) session.getAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY);
            String flag = MapUtils.getString(cachePageInfo, SessionNames.FUTUREHOLDFLAGTYPE_KEY);

            if ("lot".equalsIgnoreCase(flag)) {
                String routeId = theform.getReturnRouteId();
                Assert.isFalse((routeId == null) || ("".equalsIgnoreCase(routeId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_ROUTE_CANNOT_BE_NULL)
                                     .content("路径号不能为空!").build());
                String operationId = request.getParameter("returnOperationId");
                Assert.isFalse((operationId == null) || ("".equalsIgnoreCase(operationId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_CANNOT_BE_NULL)
                                     .content("步骤号不能为空!").build());
                String lotId = theform.getInstanceId();
                /*
                 * String holdGroup = theform.getReasonGroup(); if ((holdGroup == null) ||
                 * ("".equalsIgnoreCase(holdGroup))) { request.setAttribute(ErrorDef.MYCIM_ERR_KEY, new
                 * ValidateFailureException( "Hold Group can not be null!")); return (mapping.findForward
                 * ("error"));
                 * }
                 */
                // String holdCode = theform.getReasonCode();
                // if ((holdCode == null) || ("".equalsIgnoreCase(holdCode))) {
                // request.setAttribute(ErrorDef.MYCIM_ERR_KEY,
                // new ValidateFailureException("Hold Code can not be null!"));
                //
                // return (mapping.findForward("error"));
                // }

                String userRRN = theform.getUserId().trim();
                String userName = securityService.getUser(Long.parseLong(userRRN)).getUserName();

                // String holdReason = theform.getDepartment() + " " + theform.getDeptExt().trim()
                // .toUpperCase() +
                // " "
                // + userName.toUpperCase() + " " + theform.getReason();

                Map instructionMap = new HashMap();
                instructionMap.put("context_id", ContextNames.CTX_INSTRUCTION);
                instructionMap.put("context_rrn", getInstanceRrn(ContextNames.CTX_INSTRUCTION,
                                                                 getNamedSpace(ObjectList.CONTEXT_KEY, facilityRrn),
                                                                 ObjectList.CONTEXT_KEY) + "");

                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                Lot lot = lotQueryService.getLot(lotId, facilityRrn.longValue());
                long lotRrn = lot.getLotRrn();
                long productRrn = 0;
                productRrn = lot.getProductRrn().longValue();
                Long processRrn = lot.getProcessRrn();
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                int sequenceNumber = prpService.getMaxSeq(refRrn) + 1;


                instructionMap.put("context_key1", String.valueOf(productRrn));
                instructionMap.put("context_key2", processRrn.toString());
                instructionMap.put("context_key3", String.valueOf(routeRrn));
                instructionMap.put("context_key4", String.valueOf(operationRrn));
                instructionMap.put("context_key6", String.valueOf(lotRrn));
                instructionMap.put("context_key7", theform.getActionPoint());
                instructionMap.put("sequence", String.valueOf(sequenceNumber));
                instructionMap.put("result_value1", theform.getReason());
                instructionMap.put("result_value2", userName);
                String isVerifyRemark = StringUtils.isNotEmpty(theform.getIsVerifyRemark()) ? theform
                        .getIsVerifyRemark() : "N";
                instructionMap.put("result_value3", isVerifyRemark);
                //check lot lock
                // List<Lot> lockLots = new ArrayList<Lot>();
                // lockLots.add(lot);
                // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_ADD_INSTRUCTION, lockLots,
                //                             "Add instruction by lot in InstructionEditAction by: " + username);
                prpService.addContextInstruction(instructionMap);
                //remove lot lock
                // removeLotLock(userRrn, lot, TransactionNames.LOCK_ADD_INSTRUCTION,
                //               "Add instruction by lot over in InstructionEditAction by: " + username);
                // instructionListbyLot = this.getWipSetupManager().getInstructionByProduct(refRrn,
                // productRrn,processRrn);
                List instructionListbyLot = getInstructionByLot(facilityRrn, refRrn, lot);

                //session.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                request.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);

                request.setAttribute("operationId", operationId);
                request.setAttribute("stepNumber", lot.getStepNumberInProcess());
                request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            } else if ("product".equalsIgnoreCase(flag)) {
                String productId = theform.getInstanceId();
                long productRrn = 0;
                productRrn = this.getInstanceRrn(productId, this.getNamedSpace("PRODUCT", facilityRrn), "PRODUCT");
                Long processRrn = theform.getProcessRrn();
                String routeId = theform.getProcessStepIdVersion();
                Assert.isFalse((routeId == null) || ("".equalsIgnoreCase(routeId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_ROUTE_CANNOT_BE_NULL)
                                     .content("路径号不能为空!").build());
                String operationId = theform.getOperationId();
                Assert.isFalse((operationId == null) || ("".equalsIgnoreCase(operationId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_CANNOT_BE_NULL)
                                     .content("步骤号不能为空!").build());
                /*
                 * String holdGroup = theform.getReasonGroup(); if ((holdGroup == null) ||
                 * ("".equalsIgnoreCase(holdGroup))) { request.setAttribute(ErrorDef.MYCIM_ERR_KEY, new
                 * ValidateFailureException( "Hold Group can not be null!")); return (mapping.findForward
                 * ("error"));
                 * }
                 */
                // String holdCode = theform.getReasonCode();
                // if ((holdCode == null) || ("".equalsIgnoreCase(holdCode))) {
                // request.setAttribute(ErrorDef.MYCIM_ERR_KEY,
                // new ValidateFailureException("Hold Code can not be null!"));
                //
                // return (mapping.findForward("error"));
                // }

                String userRRN = theform.getUserId().trim();
                String userName = securityService.getUser(Long.parseLong(userRRN)).getUserName();

                // String holdReason = theform.getDepartment() + " " + theform.getDeptExt().trim()
                // .toUpperCase() +
                // " "
                // + userName.toUpperCase() + " " + theform.getReason();

                Map instructionMap = new HashMap();
                instructionMap.put("context_id", ContextNames.CTX_INSTRUCTION);
                instructionMap.put("context_rrn", getInstanceRrn(ContextNames.CTX_INSTRUCTION,
                                                                 getNamedSpace(ObjectList.CONTEXT_KEY, facilityRrn),
                                                                 ObjectList.CONTEXT_KEY) + "");

                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");

                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                instructionMap.put("context_key1", String.valueOf(productRrn));
                instructionMap.put("context_key2", processRrn.toString());
                instructionMap.put("context_key3", String.valueOf(routeRrn));
                instructionMap.put("context_key4", String.valueOf(operationRrn));
                instructionMap.put("context_key7", theform.getActionPoint());
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                int sequenceNumber = prpService.getMaxSeq(refRrn);
                instructionMap.put("sequence", String.valueOf(sequenceNumber));
                instructionMap.put("result_value1", theform.getReason());
                instructionMap.put("result_value2", userName);
                String isVerifyRemark = StringUtils.isNotEmpty(theform.getIsVerifyRemark()) ? theform
                        .getIsVerifyRemark() : "N";
                instructionMap.put("result_value3", isVerifyRemark);
                prpService.addContextInstruction(instructionMap);
                List instructionListbyProduct = ctxExecService.getInstructionByProduct(refRrn, productRrn);

                // session.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY,
                // instructionListbyProduct);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                request.setAttribute("productRrn", productRrn);
                request.setAttribute("processRrn", processRrn);
            } else if ("process".equalsIgnoreCase(flag)) {
                String processId = theform.getInstanceId();
                long processRrn = 0;
                processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL");
                // Long processRrn = theform.getProcessRrn();
                String routeId = theform.getProcessStepIdVersion();
                Assert.isFalse((routeId == null) || ("".equalsIgnoreCase(routeId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_ROUTE_CANNOT_BE_NULL)
                                     .content("路径号不能为空!").build());
                String operationId = theform.getOperationId();
                Assert.isFalse((operationId == null) || ("".equalsIgnoreCase(operationId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_CANNOT_BE_NULL)
                                     .content("步骤号不能为空!").build());
                /*
                 * String holdGroup = theform.getReasonGroup(); if ((holdGroup == null) ||
                 * ("".equalsIgnoreCase(holdGroup))) { request.setAttribute(ErrorDef.MYCIM_ERR_KEY, new
                 * ValidateFailureException( "Hold Group can not be null!")); return (mapping.findForward
                 * ("error"));
                 * }
                 */
                // String holdCode = theform.getReasonCode();
                // if ((holdCode == null) || ("".equalsIgnoreCase(holdCode))) {
                // request.setAttribute(ErrorDef.MYCIM_ERR_KEY,
                // new ValidateFailureException("Hold Code can not be null!"));
                //
                // return (mapping.findForward("error"));
                // }

                String userRRN = theform.getUserId().trim();
                String userName = securityService.getUser(Long.parseLong(userRRN)).getUserName();

                // String holdReason = theform.getDepartment() + " " + theform.getDeptExt().trim()
                // .toUpperCase() +
                // " "
                // + userName.toUpperCase() + " " + theform.getReason();

                Map instructionMap = new HashMap();
                instructionMap.put("context_id", ContextNames.CTX_INSTRUCTION);
                instructionMap.put("context_rrn", getInstanceRrn(ContextNames.CTX_INSTRUCTION,
                                                                 getNamedSpace(ObjectList.CONTEXT_KEY, facilityRrn),
                                                                 ObjectList.CONTEXT_KEY) + "");

                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");

                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                // instructionMap.put("context_key1", String.valueOf(productRrn));
                instructionMap.put("context_key2", processRrn + "");
                instructionMap.put("context_key3", String.valueOf(routeRrn));
                instructionMap.put("context_key4", String.valueOf(operationRrn));
                instructionMap.put("context_key7", theform.getActionPoint());
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                int sequenceNumber = prpService.getMaxSeq(refRrn);
                instructionMap.put("sequence", String.valueOf(sequenceNumber));
                instructionMap.put("result_value1", theform.getReason());
                instructionMap.put("result_value2", userName);
                String isVerifyRemark = StringUtils.isNotEmpty(theform.getIsVerifyRemark()) ? theform
                        .getIsVerifyRemark() : "N";
                instructionMap.put("result_value3", isVerifyRemark);
                prpService.addContextInstruction(instructionMap);
                List instructionListbyProcess = ctxExecService.getInstructionByProcess(refRrn, processRrn);

                // session.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY,
                // instructionListbyProcess);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                request.setAttribute("processRrn", processRrn);
            } else if ("operation".equalsIgnoreCase(flag)) {
                String operationId = theform.getInstanceId();
                Assert.isFalse((operationId == null) || ("".equalsIgnoreCase(operationId)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_CANNOT_BE_NULL)
                                     .content("步骤号不能为空!").build());

                /*
                 * String holdGroup = theform.getReasonGroup(); if ((holdGroup == null) ||
                 * ("".equalsIgnoreCase(holdGroup))) { request.setAttribute(ErrorDef
                 * .MYCIM_ERR_KEY, new
                 * ValidateFailureException( "Hold Group can not be null!")); return (mapping
                 * .findForward
                 * ("error"));
                 * }
                 */
                String holdCode = theform.getReasonCode();
                Assert.isFalse((holdCode == null) || ("".equalsIgnoreCase(holdCode)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_HOLDCODE_CANNOT_BE_NULL)
                                     .content("暂停代码不能为空!").build());
                String holdReason = theform.getReasonCode() + " " + theform.getDeptExt().trim().toUpperCase() + " " +
                        theform.getUserId().trim().toUpperCase() + " " + theform.getReason();
                ContextValue actionCondition = new ContextValue();
                actionCondition.setContextId("EEN_CONTEXT_HOLDOPERATION");
                actionCondition.setContextRrn(
                        getInstanceRrn("EEN_CONTEXT_HOLDOPERATION", getNamedSpace(ObjectList.CONTEXT_KEY, facilityRrn),
                                       ObjectList.CONTEXT_KEY));

                Assert.isFalse(actionCondition.getContextRrn() <= 0,
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_CONTEXT_MISSING).content("{}情景值不存在!")
                                     .args("EEN_CONTEXT_HOLDOPERATION").build());

                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

                actionCondition.setContextKey1(String.valueOf(operationRrn));
                long refRrn = this
                        .getInstanceRrn("EEN_CONTEXT_HOLDOPERATION", this.getNamedSpace("CONTEXT", facilityRrn),
                                        "CONTEXT");
                int sequenceNumber = prpService.getMaxSeq(refRrn);
                actionCondition.setSequenceNumber(new Integer(sequenceNumber));
                actionCondition.setResultValue2(username);
                actionCondition.setResultValue3(" ");
                actionCondition.setResultValue4(holdCode);
                actionCondition.setResultValue5(holdReason);
                Map transInfo = new HashMap();
                EenAction eenAction = new EenAction();
                eenAction.setActionType(EenActionType.FUTURE_HOLD_OPERATION_KEY);
                transInfo.put("eenAction", eenAction);
                transInfo.put("user", username);
                transInfo.put("facilityRrn", facilityRrn);
                transInfo.put("contextValue", actionCondition);
                wipService.insertEenAction(transInfo);
                List futureholdListbyOperation = ctxExecService.getFutureHoldListbyOperation(refRrn, operationRrn);

                // session.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY,
                // futureholdListbyOperation);
                request.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                cachePageInfo.put(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                request.setAttribute("operationRrn", operationRrn);
            }

            theform.setDepartment("");
            theform.setDeptExt("");
            theform.setReason("");
        } else if (request.getParameter("delete") != null) {
            // String flag = (String) session.getAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY);
            String flag = MapUtils.getString(cachePageInfo, SessionNames.FUTUREHOLDFLAGTYPE_KEY);

            if ("lot".equalsIgnoreCase(flag)) {
                String seqNum = request.getParameter("seqNum");
                String lotId = theform.getInstanceId();
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                String instruction = request.getParameter("instruction");
                String routeId = request.getParameter("routeId");
                String operationId = request.getParameter("operationId");
                String actionPoint = request.getParameter("actionPoint");
                request.setAttribute("actionPoint", actionPoint);
                request.setAttribute("routeId", routeId);
                request.setAttribute("operationId", operationId);
                request.setAttribute("lotId", lotId);
                request.setAttribute("refRrn", new Long(refRrn));
                request.setAttribute("seqNum", seqNum);
                request.setAttribute("instruction", instruction);
            } else if ("product".equalsIgnoreCase(flag)) {
                // request.setCharacterEncoding("gbk");

                String seqNum = request.getParameter("seqNum");
                String productId = theform.getInstanceId();
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                String processId = request.getParameter("processId");
                String routeId = request.getParameter("routeId");
                String operationId = request.getParameter("operationId");
                String instruction = request.getParameter("instruction");
                String actionPoint = request.getParameter("actionPoint");
                request.setAttribute("actionPoint", actionPoint);
                request.setAttribute("productId", productId);
                request.setAttribute("processId", processId);
                request.setAttribute("routeId", routeId);
                request.setAttribute("operationId", operationId);
                request.setAttribute("refRrn", new Long(refRrn));
                request.setAttribute("seqNum", seqNum);
                request.setAttribute("instruction", instruction);
            } else if ("process".equalsIgnoreCase(flag)) {
                // request.setCharacterEncoding("gbk");

                String seqNum = request.getParameter("seqNum");
                String productId = theform.getInstanceId();
                long refRrn = this
                        .getInstanceRrn("CTX_INSTRUCTION", this.getNamedSpace("CONTEXT", facilityRrn), "CONTEXT");
                String processId = request.getParameter("processId");
                String routeId = request.getParameter("routeId");
                String operationId = request.getParameter("operationId");
                String instruction = request.getParameter("instruction");
                String actionPoint = request.getParameter("actionPoint");
                request.setAttribute("actionPoint", actionPoint);
                request.setAttribute("productId", productId);
                request.setAttribute("processId", processId);
                request.setAttribute("routeId", routeId);
                request.setAttribute("operationId", operationId);
                request.setAttribute("refRrn", new Long(refRrn));
                request.setAttribute("seqNum", seqNum);
                request.setAttribute("instruction", instruction);
            } else if ("operation".equalsIgnoreCase(flag)) {
                String actionRrn = request.getParameter("actionRrn");
                String seqNum = request.getParameter("seqNum");
                String operationId = theform.getInstanceId();
                long refRrn = this
                        .getInstanceRrn("EEN_CONTEXT_HOLDOPERATION", this.getNamedSpace("CONTEXT", facilityRrn),
                                        "CONTEXT");
                request.setAttribute("actionRrn", actionRrn);
                request.setAttribute("operationId", operationId);
                request.setAttribute("refRrn", new Long(refRrn));
                request.setAttribute("seqNum", seqNum);
            }

            request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
            request.setAttribute(SessionNames.LOT_INFO_FORM_KEY,
                                 WebUtils.getCacheString2Obj(theform.getCacheLotInfo()));
            Lot lot = (Lot) WebUtils.getCacheString2Obj(theform.getCacheLot());
            if (lot != null) {
                request.setAttribute(SessionNames.LOT_KEY, lot);
                request.setAttribute("stepNumber", lot.getStepNumberInProcess());
                request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
                request.setAttribute("_lotId", lot.getLotId());
                request.setAttribute("productRrn", lot.getProductRrn());
            }
            theform.setCachePageInfo(WebUtils.getCacheObj2String(cachePageInfo));

            return (mapping.findForward("delete"));
        } else if (request.getParameter("dodelete") != null) {
            //String flag = (String) session.getAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY);
            String flag = MapUtils.getString(cachePageInfo, SessionNames.FUTUREHOLDFLAGTYPE_KEY);

            if ("lot".equalsIgnoreCase(flag)) {
                String refRrn = request.getParameter("refRrn");
                String seqNum = request.getParameter("seqNum");
                String routeId = request.getParameter("routeId");
                String actionPoint = request.getParameter("actionPoint");
                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                String operationId = request.getParameter("operationId");
                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

                String lotId = request.getParameter("lotId");
                theform.setInstanceId(lotId);
                Lot lot = lotQueryService.getLot(lotId, facilityRrn.longValue());
                long lotRrn = lot.getLotRrn();
                long productRrn = 0;
                productRrn = lot.getProductRrn().longValue();
                long processRrn = 0;
                processRrn = lot.getProcessRrn().longValue();

                Map map = new HashMap();
                map.put("context_rrn", refRrn);
                map.put("seqNum", seqNum);
                map.put("context_key1", productRrn + "");
                map.put("context_key2", processRrn + "");
                map.put("context_key3", routeRrn + "");
                map.put("context_key4", operationRrn + "");
                map.put("context_key6", lotRrn + "");
                map.put("context_key7", actionPoint);
                map.put("type", "lot");
                //check lot lock
                /*List<Lot> lockLots = new ArrayList<Lot>();
                lockLots.add(lot);
                checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_DEL_INSTRUCTION, lockLots,
                                            "Delete instruction by lot, in InstructionEditAction by:"
                                            + username);*/
                prpService.deleteContextInstruction(map);
                //remove lot lock
                /*removeLotLock(userRrn, lot, TransactionNames.LOCK_DEL_INSTRUCTION,
                              "Delete instruction by lot over, in InstructionEditAction by:" + username);*/
                // instructionListbyLot = this.getWipSetupManager().getInstructionByProduct(new Long(refRrn),
                // productRrn,processRrn);
                List instructionListbyLot = getInstructionByLot(facilityRrn, NumberUtils.toLong(refRrn), lot);

                // session.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                request.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);

                request.setAttribute("operationId", operationId);
                theform.setReturnRouteId(routeId);
                request.setAttribute("lotRrn", lot.getLotRrn());
                request.setAttribute("stepNumber", lot.getStepNumberInProcess());

            } else if ("product".equalsIgnoreCase(flag)) {
                String refRrn = request.getParameter("refRrn");
                String productId = request.getParameter("productId");
                String seqNum = request.getParameter("seqNum");
                String actionPoint = request.getParameter("actionPoint");
                long productRrn = 0;
                productRrn = this.getInstanceRrn(productId, this.getNamedSpace("PRODUCT", facilityRrn), "PRODUCT");
                String processId = request.getParameter("processId");
                long processRrn = 0;
                processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                String routeId = request.getParameter("routeId");
                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                String operationId = request.getParameter("operationId");
                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

                Map map = new HashMap();

                map.put("context_rrn", refRrn);
                map.put("seqNum", seqNum);
                map.put("context_key1", productRrn + "");
                map.put("context_key2", processRrn + "");
                map.put("context_key3", routeRrn + "");
                map.put("context_key4", operationRrn + "");
                map.put("context_key7", actionPoint);
                map.put("type", "product");
                prpService.deleteContextInstruction(map);
                List instructionListbyProduct = ctxExecService
                        .getInstructionByProduct(Long.parseLong(refRrn), productRrn);

                // session.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);

                theform.setInstanceDesc(this.getInstanceDesc(productRrn));
                theform.setNamedSpace(this.getNamedSpace("PRODUCT", facilityRrn));
                theform.setInstanceId(productId);

                theform.setProcessStepIdVersion(routeId);
                request.setAttribute("productRrn", new Long(productRrn));
            } else if ("process".equalsIgnoreCase(flag)) {
                String refRrn = request.getParameter("refRrn");
                // String productId = request.getParameter("productId");
                String seqNum = request.getParameter("seqNum");
                // long productRrn = 0;
                // productRrn = this.getInstanceRrn(productId, this.getNamedSpace("PRODUCT", facilityRrn),
                // "PRODUCT");
                String processId = request.getParameter("processId");
                String actionPoint = request.getParameter("actionPoint");
                long processRrn = 0;
                processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                String routeId = request.getParameter("routeId");
                long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
                String operationId = request.getParameter("operationId");
                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

                Map map = new HashMap();

                map.put("context_rrn", refRrn);
                map.put("seqNum", seqNum);
                // map.put("context_key1", productRrn + "");
                map.put("context_key2", processRrn + "");
                map.put("context_key3", routeRrn + "");
                map.put("context_key4", operationRrn + "");
                map.put("context_key7", actionPoint);
                map.put("type", "process");
                prpService.deleteContextInstruction(map);
                List instructionListbyProcess = ctxExecService
                        .getInstructionByProcess(Long.parseLong(refRrn), processRrn);

                // session.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY,
                // instructionListbyProcess);
                request.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);

                theform.setInstanceDesc(this.getInstanceDesc(processRrn));
                theform.setNamedSpace(this.getNamedSpace("PROCESS", facilityRrn));
                theform.setInstanceId(processId);
                theform.setProcessRrn(new Long(processRrn));
                theform.setProcessStepIdVersion(routeId);
                request.setAttribute("processRrn", processRrn);
            } else if ("operation".equalsIgnoreCase(flag)) {
                String actionRrn = request.getParameter("actionRrn");
                String refRrn = request.getParameter("refRrn");
                String seqNum = request.getParameter("seqNum");

                String operationId = request.getParameter("operationId");
                long operationRrn = this
                        .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                String comments = theform.getLotComments();
                Assert.isFalse((comments == null) || ("".equalsIgnoreCase(comments)),
                               Errors.create().key(MessageIdList.INSTRUCTIONEDIT_COMMENTS_CANNOT_BE_NULL)
                                     .content("备注不能为空!").build());
                Map map = new HashMap();
                map.put("actionRrn", actionRrn);
                map.put("refRrn", refRrn);
                map.put("operationRrn", new Long(operationRrn));
                map.put("comments", comments);
                map.put("type", "operation");
                map.put("transBy", username);
                wipService.insertFutureHoldCommentsByOperation(map);
                deleteContextValue((new Long(refRrn)).longValue(), actionRrn, seqNum, userRrn);
                List futureholdListbyOperation = ctxExecService
                        .getFutureHoldListbyOperation((new Long(refRrn)).longValue(), operationRrn);

                // session.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY,
                // futureholdListbyOperation);
                request.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                cachePageInfo.put(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);

                theform.setInstanceDesc(this.getInstanceDesc(operationRrn));
                theform.setNamedSpace(this.getNamedSpace("OPERATION", facilityRrn));
                theform.setInstanceId(operationId);
                request.setAttribute("operationRrn", operationRrn);
            }
        } else if (request.getParameter("selectFlag") != null) {
            String reasonGroup = theform.getReasonGroup();
            request.setAttribute("reasonGroupValue", reasonGroup);
            String operationId = request.getParameter("returnOperationId");
            request.setAttribute("operationId", operationId);
        } else if (request.getParameter("cancel") != null) {
            // String flag = (String) session.getAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY);
            String flag = MapUtils.getString(cachePageInfo, SessionNames.FUTUREHOLDFLAGTYPE_KEY);

            if ("product".equalsIgnoreCase(flag)) {
                String id = request.getParameter("productId");
                String refRrn = request.getParameter("refRrn");
                long productRrn = 0;
                productRrn = this.getInstanceRrn(id, this.getNamedSpace("PRODUCT", facilityRrn), "PRODUCT");
                theform.setInstanceDesc(this.getInstanceDesc(productRrn));
                theform.setNamedSpace(this.getNamedSpace("PRODUCT", facilityRrn));
                String routeId = request.getParameter("routeId");
                theform.setProcessStepIdVersion(routeId);
                theform.setInstanceId(id);
                List instructionListbyProduct = ctxExecService
                        .getInstructionByProduct(Long.parseLong(refRrn), productRrn);

                request.setAttribute(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PRODUCT_KEY, instructionListbyProduct);
                request.setAttribute("productRrn", new Long(productRrn));
            } else if ("process".equalsIgnoreCase(flag)) {
                String id = request.getParameter("processId");
                String refRrn = request.getParameter("refRrn");
                long processRrn = 0;
                processRrn = this.getInstanceRrn(id, this.getNamedSpace("WFL", facilityRrn), "WFL");
                theform.setInstanceDesc(this.getInstanceDesc(processRrn));
                theform.setNamedSpace(this.getNamedSpace("PROCESS", facilityRrn));
                String routeId = request.getParameter("routeId");
                theform.setProcessStepIdVersion(routeId);
                theform.setInstanceId(id);
                theform.setProcessRrn(new Long(processRrn));
                List instructionListbyProcess = ctxExecService
                        .getInstructionByProcess(Long.parseLong(refRrn), processRrn);

                request.setAttribute(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_PROCESS_KEY, instructionListbyProcess);
                request.setAttribute("processRrn", processRrn);
            } else if ("lot".equalsIgnoreCase(flag)) {
                String id = request.getParameter("lotId");
                String refRrn = request.getParameter("refRrn");
                theform.setInstanceId(id);
                String routeId = request.getParameter("routeId");
                theform.setReturnRouteId(routeId);
                String operationId = request.getParameter("operationId");
                request.setAttribute("operationId", operationId);
                String lotId = request.getParameter("lotId");
                theform.setInstanceId(lotId);
                Lot lot = lotQueryService.getLot(lotId, facilityRrn.longValue());
                List instructionListbyLot = getInstructionByLot(facilityRrn, NumberUtils.toLong(refRrn), lot);

                request.setAttribute(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                cachePageInfo.put(SessionNames.INSTRUCTION_BY_LOT_KEY, instructionListbyLot);
                request.setAttribute("lotRrn", lot.getLotRrn());
                request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            } else if ("operation".equalsIgnoreCase(flag)) {
                String id = request.getParameter("operationId");
                String refRrn = request.getParameter("refRrn");
                long operationRrn = 0;
                operationRrn = this.getInstanceRrn(id, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
                theform.setInstanceDesc(this.getInstanceDesc(operationRrn));
                theform.setNamedSpace(this.getNamedSpace("OPERATION", facilityRrn));
                theform.setInstanceId(id);
                List futureholdListbyOperation = ctxExecService
                        .getFutureHoldListbyOperation((new Long(refRrn)).longValue(), operationRrn);

                request.setAttribute(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                cachePageInfo.put(SessionNames.FUTUREHOLDLIST_BYOPERATION_KEY, futureholdListbyOperation);
                request.setAttribute("operationRrn", operationRrn);
            }
        }
        request.setAttribute(SessionNames.FUTUREHOLDFLAGTYPE_KEY, theform.getByPlanLotFlag());
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, WebUtils.getCacheString2Obj(theform.getCacheLotInfo()));
        Lot lot = (Lot) WebUtils.getCacheString2Obj(theform.getCacheLot());
        if (lot != null) {
            request.setAttribute(SessionNames.LOT_KEY, lot);
            request.setAttribute("stepNumber", lot.getStepNumberInProcess());
            request.setAttribute("lotRrn", new Long(lot.getLotRrn()));
            request.setAttribute("_lotId", lot.getLotId());
            request.setAttribute("productRrn", lot.getProductRrn());
        }
        theform.setCachePageInfo(WebUtils.getCacheObj2String(cachePageInfo));
        return mapping.findForward("instructionedit");
    } // end of perform()

    public List getInstructionByLot(Long facilityRrn, long refRrn, Lot lot) {
        List instructionListbyLot = ctxExecService.getInstructionByLot(refRrn, lot.getProductRrn(), lot.getLotRrn());
        setFlowSeqAndStepdesc(facilityRrn, lot, instructionListbyLot);

        return instructionListbyLot;
    }

    public void setFlowSeqAndStepdesc(Long facilityRrn, Lot lot, List futureHoldList) {

        if (futureHoldList != null) {
            Iterator it = futureHoldList.iterator();
            while (it.hasNext()) {
                Map futureHoldMap = (Map) it.next();
                futureHoldMap.put("processVersion", lot.getProcessVersion());
                futureHoldMap.put("facilityRrn", facilityRrn);
                futureHoldMap.put("processRrn", lot.getProcessRrn());
                futureHoldMap.put("productRrn", lot.getProductRrn());
                futureHoldMap.put("flowSeq", this.ctxExecService.getFlowSeqByProcessInfo(futureHoldMap));
                futureHoldMap.put("stepDesc", ctxExecService.getOperationDescByProcessInfo(futureHoldMap));
                Map<String, Object> stageMap = buildContextValue(new Facility(facilityRrn), futureHoldMap);
                futureHoldMap.put("stageId", MapUtils.getString(stageMap, "stageId", StringUtils.EMPTY));
            }
            sortFutureHoldInfosByFlowSeq(futureHoldList);
        }
    }

    private void deleteContextValue(long refRrn, String actionRrn, String seqNum, Long userRrn) {
        ctxService.deleteContextValueByActionRrn(refRrn, actionRrn, Integer.parseInt(seqNum), userRrn);
        wipService.deleteEenAction(Long.parseLong(actionRrn));
    }

    private Map buildContextValue(Facility facility, Map contextValueInfo) {
        StageContextValue stageContextValue = new StageContextValue();
        stageContextValue.setProductRrn(MapUtils.getLong(contextValueInfo, "productRrn"));
        stageContextValue.setProcessRrn(MapUtils.getLong(contextValueInfo, "processRrn"));
        stageContextValue.setOperationRrn(MapUtils.getLong(contextValueInfo, "operationRrn"));
        stageContextValue.setRouteRrn(MapUtils.getLong(contextValueInfo, "routeRrn"));
        stageContextValue.setProcessVersion(MapUtils.getIntValue(contextValueInfo, "processVersion"));
        stageContextValue.setContextRrn(
                this.getInstanceRrn(stageContextValue.getContextId(), facility.getInstanceRrn(), "CONTEXT"));
        // // 未按照规则取值,取值不需要seq,置为空,后续需要seq的话,直接释放即可
        // stageContextValue.setOperationSeq("");
        // stageContextValue.setRouteSeq("");
        ContextValue contextValue = ctxService.getContextValueNoFilter(stageContextValue);
        if (contextValue != null) {
            // contextValue = this.getPrpManager().FilterContextValueToId(contextValue);
            contextValueInfo.put("stageId", contextValue.getResultValue1());
            contextValueInfo.put("activeStageId", 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;
    }

    protected void sortFutureHoldInfosByFlowSeq(List futureHoldInfos) {
        Collections.sort(futureHoldInfos, new Comparator() {

            @Override
            public int compare(Object paramObject1, Object paramObject2) {
                Map info1 = (Map) paramObject1;
                Map info2 = (Map) paramObject2;
                int flowSeq1 = MapUtils.getIntValue(info1, "flowSeq", 0);
                int flowSeq2 = MapUtils.getIntValue(info2, "flowSeq", 0);
                return (flowSeq1 - flowSeq2);
            }
        });
    }

}