SplitRunCardFutureHoldAction.java

package com.mycim.webapp.actions.splitruncardlot;


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.server.runcard.service.RunCardLotInqService;
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.TransactionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.RunCardLotInfo;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
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.HashMap;
import java.util.List;
import java.util.Map;

public class SplitRunCardFutureHoldAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        Long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = WebUtils.getParameter("lotId", request);

        Lot lot = lotInqService.getLot(lotId);
        Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("Cannot Find Lot!").build());
        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());

        RunCardLotInfo lotInfo = runCardLotInqService.getRunCardLotInfo(lot.getLotRrn());
        lot.setProcessRrn(lotInfo.getProcessRrn());
        lot.setProcessVersion(lotInfo.getProcessVersion());

        long refRrn = getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDLOT, LocalContext.getFacilityRrn(),
                                     ObjectList.CONTEXT_KEY);
        List<Map> futureholdlistbylot = futureholdlistbylot = getFutureHoldListbyLot(refRrn, lot);


        theform.setEffectCount(null);
        theform.setDepartment(null);
        request.setAttribute("lotId", lot.getLotId());
        request.setAttribute("futureholdlistbylot", futureholdlistbylot);
        request.setAttribute("priority", wipQueryService
                .getHotflagSplicingPriority(new Integer(lot.getHotFlag()), lot.getPriority(), facilityRrn));
        request.setAttribute("lotInfo", lotInfo);
        return mapping.findForward("init");
    }


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

        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);
        wipService.insertEenAction(transInfo);

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

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

        Long facilityRrn = LocalContext.getFacilityRrn();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        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);

        wipService.deleteFutureHold(map);


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

}