FutureHoldByProductAction.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.prp.ContextValue;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.wip.Lot;
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.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 FutureHoldByProductAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;

        String productId = theform.getInstanceId();
        Long facilityRrn = LocalContext.getFacilityRrn();

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();

        List<Map> futureholdlistbylot = new ArrayList<>();
        List<Map> futureHoldList = new ArrayList<>();

        long refRrn = this
                .getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDPRODUCT, this.getNamedSpace("CONTEXT", facilityRrn),
                                "CONTEXT");


        List<Map> futureHoldListbyProduct = getFutureHoldListbyProduct(facilityRrn, refRrn, productRrn, 0, 0);

        theform.setEffectCount(null);
        theform.setDepartment(null);
        theform.setOperationId(null);
        theform.setInstanceDesc(item.getInstanceDesc());
        request.setAttribute("productRrn", productRrn);
        request.setAttribute("futureholdlistbyproduct", futureHoldListbyProduct);

        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();
        String userId = LocalContext.getUserId();
        String productId = theform.getInstanceId();
        Long processRrn = theform.getProcessRrn();
        String routeId = theform.getRouteId();
        String holdCode = theform.getReasonCode();
        String operationId = theform.getOperationId();
        String holdReason = theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                theform.getReason();
        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());

        Assert.isFalse(StringUtils.isEmpty(routeId), Errors.create().content("SubPlan ID can not be null!").build());

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

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();

        long refRrn = getInstanceRrn(ContextNames.EEN_CONTEXT_HOLDPRODUCT, 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;

        ContextValue actionCondition = new ContextValue();

        actionCondition.setContextId(ContextNames.EEN_CONTEXT_HOLDPRODUCT);
        actionCondition.setContextRrn(refRrn);
        actionCondition.setContextKey1(String.valueOf(productRrn));
        actionCondition.setContextKey2(processRrn.toString());
        actionCondition.setContextKey3(String.valueOf(routeRrn));
        actionCondition.setContextKey4(String.valueOf(operationRrn));
        actionCondition.setSequenceNumber(new Integer(sequenceNumber));
        actionCondition.setResultValue2(userId);
        actionCondition.setResultValue3(" ");
        actionCondition.setResultValue4(holdCode);
        actionCondition.setResultValue5(holdReason);

        Map transInfo = new HashMap();
        EenAction eenAction = new EenAction();
        eenAction.setActionType(EenActionType.FUTURE_HOLD_PRODUCT_KEY);
        transInfo.put("eenAction", eenAction);
        transInfo.put("user", userId);
        transInfo.put("facilityRrn", facilityRrn);
        transInfo.put("contextValue", actionCondition);

        wipService.insertEenAction(transInfo);

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

    public ActionForward deleteDispatch(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String actionRrn = request.getParameter("actionRrn");
        String seqNum = request.getParameter("seqNum");
        String processId = request.getParameter("processId");
        String productId = theform.getInstanceId();

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

        request.setAttribute("actionRrn", actionRrn);
        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);

        return mapping.findForward(Constants.DELETE_KEY);
    }

    public ActionForward deleteFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        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 productId = request.getParameter("productId");
        String processId = request.getParameter("processId");
        String refRrn = request.getParameter("refRrn");
        Long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        long userRrn = LocalContext.getUserRrn();

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();
        long processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
        long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
        long operationRrn = this.getInstanceRrn(operationId, this.getNamedSpace("OPERATION", facilityRrn), "OPERATION");

        Map map = new HashMap();
        map.put("actionRrn", Long.valueOf(actionRrn));
        map.put("refRrn", Long.valueOf(refRrn));
        map.put("productRrn", new Long(productRrn));
        map.put("processRrn", new Long(processRrn));
        map.put("routeRrn", new Long(routeRrn));
        map.put("operationRrn", new Long(operationRrn));
        map.put("comments", comments);
        map.put("type", "product");
        map.put("transBy", userId);
        map.put("seqNum", Integer.valueOf(seqNum));
        map.put("userRrn", userRrn);

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

}