FutureHoldByLotAction.java

package com.mycim.webapp.actions.lot.futurehold;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
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.HoldCodeNames;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
import org.apache.commons.compress.utils.Sets;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Luopeng.Wang
 * @version 6.0.0
 * @date 2019/9/24
 **/
public class FutureHoldByLotAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;

        Lot lot = initLotBaseInfoForJsp(request, true, null, null);

        List<Map> futureholdlistbylot = new ArrayList<>();
        List<Map> futureHoldList = new ArrayList<>();
        if (lot != null && lot.getLotRrn() > 0) {
            long refRrn = getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDLOT, LocalContext.getFacilityRrn(),
                                         ObjectList.CONTEXT_KEY);
            futureholdlistbylot = getFutureHoldListbyLot(refRrn, lot);
            request.setAttribute("lotId", lot.getLotId());

            futureHoldList = getPreOrPostFutureHoldForLot(LocalContext.getFacilityRrn(), lot,
                                                          ContextNames.EEN_CONTEXT_HOLDLOT,
                                                          ContextNames.EEN_CONTEXT_HOLDPRODUCT);
        }

        Assert.isFalse(lot == null || !(StringUtils.equals(lot.getLotStatus(), LotStatus.WAITING) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.HOLD) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNING) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.DISPATCH) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.BANKED) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNINGHOLD)),
                       Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow!")
                             .build());

        super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));

        theform.setEffectCount(null);
        theform.setDepartment(null);
        request.setAttribute("futureholdlistbylot", futureholdlistbylot);
        request.setAttribute("allfutureinfo", futureHoldList);

        return mapping.findForward(Constants.INIT_KEY);
    }

    public ActionForward addFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        Long facilityRrn = LocalContext.getFacilityRrn();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        String lotId = theform.getLotId();
        String routeId = theform.getReturnRouteId();
        String holdCode = theform.getReasonCode();
        String holdEffectCount = StringUtils.isEmpty(theform.getEffectCount()) ? "999999" : theform.getEffectCount();
        String operationId = request.getParameter("returnOperationId");
        String holdReason = theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                theform.getReason();
        // List<Lot> lockLots = new ArrayList<>();
        Assert.isFalse(StringUtils.isEmpty(operationId), Errors.create().content("Step ID can not be null!").build());

        Assert.isFalse(StringUtils.isEmpty(holdCode), Errors.create().content("Hold Code can not be null!").build());

        checkRCHoldCode(holdCode);

        int holdReasonLen = holdReason.replaceAll("[^\\x00-\\xff]", "***").length();
        Assert.isFalse(holdReasonLen > 256, Errors.create().key(MessageIdList.LOT_HOLDREASONTOOLONG)
                                                  .content("holdReason length can not be over 256!").build());

        long refRrn = getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDLOT, LocalContext.getFacilityRrn(),
                                     ObjectList.CONTEXT_KEY);
        long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
        long operationRrn = this.getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");
        int sequenceNumber = prpService.getMaxSeq(refRrn) + 1;
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());

        ContextValue actionCondition = new ContextValue();

        actionCondition.setContextId(ContextNames.EEN_CONTEXT_HOLDLOT);
        actionCondition.setContextRrn(refRrn);
        actionCondition.setContextKey1(String.valueOf(routeRrn));
        actionCondition.setContextKey2(String.valueOf(operationRrn));
        actionCondition.setContextKey3(String.valueOf(lot.getLotRrn()));

        actionCondition.setSequenceNumber(sequenceNumber);
        actionCondition.setResultValue2(userId);
        actionCondition.setResultValue3(" ");
        actionCondition.setResultValue4(holdCode);
        actionCondition.setResultValue5(holdReason);

        actionCondition.setResultValue6(holdEffectCount);
        Map transInfo = new HashMap();
        EenAction eenAction = new EenAction();
        eenAction.setActionType(EenActionType.FUTURE_HOLD_LOT_KEY);
        transInfo.put("eenAction", eenAction);
        transInfo.put("user", userId);
        transInfo.put("facilityRrn", facilityRrn);
        transInfo.put("contextValue", actionCondition);
        transInfo.put("byLotFlag", "Y");
        transInfo.put("lot", lot);
        String createSeq = request.getParameter("returnFlowSeq");
        StringBuffer transComments = new StringBuffer(TransactionNames.BY_LOT_CREATE_FUTURE_HOLD);
        transComments.append(", Flow seq:").append(createSeq).append(" Hold Code:").append(holdCode)
                     .append(" Hold Comment:").append(theform.getReason());
        transInfo.put("transComments", transComments);
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_ADD_FUTUREHOLD_BY_LOT, lockLots,
        //                             "Add futureHold by lot in FutureHoldByLotAction by: " + userId);
        try {
            wipService.insertEenAction(transInfo,lot.getLotRrn());
            // removeLotLock(userRrn, lot, TransactionNames.LOCK_ADD_FUTUREHOLD_BY_LOT,
            //               "Delete futureHold by lot in FutureHoldByLotAction by: " + userId);
        } catch (Exception e) {
            // removeLotLockWhenTransError(userRrn, lockLots, TransactionNames.LOCK_ADD_FUTUREHOLD_BY_LOT,
            //                             "Delete futureHold by lot in FutureHoldByLotAction by: " + userId);
            throw e;
        }

        return init(mapping, form, request, response);
    }

    public ActionForward deleteDispatch(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String lotId = theform.getLotId();
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String actionRrn = request.getParameter("actionRrn");
        String seqNum = request.getParameter("seqNum");
        String delSeq = request.getParameter("delSeq");
        String holdCode = request.getParameter("holdCode");
        String holdReason = request.getParameter("holdReason");

        long refRrn = getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDLOT, LocalContext.getFacilityRrn(),
                                     ObjectList.CONTEXT_KEY);

        request.setAttribute("actionRrn", actionRrn);
        request.setAttribute("routeId", routeId);
        request.setAttribute("operationId", operationId);
        request.setAttribute("lotId", lotId);
        request.setAttribute("refRrn", refRrn);
        request.setAttribute("seqNum", seqNum);
        request.setAttribute("delSeq", delSeq);
        request.setAttribute("holdCode", holdCode);
        if (StringUtils.isNotBlank(holdReason) && holdReason.indexOf(".") > -1) {
            request.setAttribute("holdReason", holdReason.substring(holdReason.indexOf(".") + 1));
        }

        return mapping.findForward(Constants.DELETE_KEY);
    }

    public ActionForward deleteFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String lotId = theform.getLotId();
        String comments = theform.getLotComments();
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String actionRrn = request.getParameter("actionRrn");
        String seqNum = request.getParameter("seqNum");
        String refRrn = request.getParameter("refRrn");
        Long facilityRrn = LocalContext.getFacilityRrn();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        String delSeq = request.getParameter("delSeq");
        String holdCode = request.getParameter("holdCode");
        String holdReason = request.getParameter("holdReason");
        if (StringUtils.isEmpty(holdReason)) {
            holdReason = StringUtils.EMPTY;
        }

        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);

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());
        Map map = new HashMap();
        map.put("actionRrn", new Long(actionRrn));
        map.put("refRrn", new Long(refRrn));
        map.put("routeRrn", new Long(routeRrn));
        map.put("operationRrn", new Long(operationRrn));
        map.put("lotRrn", lot.getLotRrn());
        map.put("comments", comments);
        map.put("type", "lot");
        map.put("transBy", userId);
        map.put("seqNum", Integer.valueOf(seqNum));
        map.put("userRrn", userRrn);
        map.put("byLotFlag", "Y");
        map.put("lot", lot);
        StringBuffer transComments = new StringBuffer(TransactionNames.BY_LOT_DELETE_FUTURE_HOLD);
        transComments.append(", Flow seq:").append(delSeq).append(" Hold Code:").append(holdCode)
                     .append(" Hold Comment:").append(holdReason);
        map.put("transComments", transComments);
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_DEL_FUTUREHOLD_BY_LOT, lockLots,
        //                             "Delete futureHold by lot in FutureHoldByLotAction by " + userId);
        wipService.deleteFutureHold(map);

        return init(mapping, form, request, response);
    }

    public ActionForward checkSRCFutureHoldInfo(Map map) {

        Long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = (String) map.get("lotId");
        String routeId = (String) map.get("routeId");
        String operationId = (String) map.get("operationId");

        long routeRrn = getInstanceRrn(routeId, getNamedSpace(routeId, facilityRrn), ObjectList.WFL_KEY,
                                       ObjectList.ROUTE_KEY, ObjectList.PROCEDURE_KEY);

        long operationRrn = getInstanceRrn(operationId, facilityRrn, ObjectList.OPERATION_KEY);
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        String msg = "";
        List<Map> futureHoldList = getAllFutureHoldForLot(facilityRrn, lot);
        for (Map futureHold : futureHoldList) {
            String holdCode = MapUtils.getString(futureHold, "holdCode");
            if (StringUtils.equals(HoldCodeNames.SRCHOLD_KEY, holdCode)) {
                Assert.isFalse(routeRrn == MapUtils.getLongValue(futureHold, "routeRrn") &&
                                       operationRrn == MapUtils.getLongValue(futureHold, "operationRrn"),
                               Errors.create().content("This step had SRC hold!").build());
            }
        }

        return WebUtils.NULLActionForward;
    }

}