ErpProductMappingAction.java

package com.mycim.webapp.actions.erp;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.erp.ErpMesProductBindBean;
import com.mycim.valueobject.prp.Item;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.WorkOrderForm;
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.List;

public class ErpProductMappingAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        WorkOrderForm workOrderForm = getForm(form);
        List<ErpMesProductBindBean> notBindList = erpService.queryNotBindMesProduct();
        request.setAttribute("notBindList", notBindList == null ? new ArrayList<>() : notBindList);
        return mapping.findForward("init");
    }

    public ActionForward initBindList(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        return mapping.findForward("initBind");
    }

    public ActionForward queryBindMesProductList(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                           HttpServletResponse response) throws Exception {
        WorkOrderForm workOrderForm = getForm(form);
        String mesProductId = workOrderForm.getMesProductId();
        String erpOrderId   = workOrderForm.getProductDesc();

        ErpMesProductBindBean empbb = new ErpMesProductBindBean();
        empbb.setMesProductId(mesProductId);
        empbb.setErpOrderId(erpOrderId);
        Page page = new Page(1, 10000L);
        page = erpService.queryBindMesProduct(page, empbb);
        request.setAttribute("bindMesProductList", page.getResults());
        return initBindList(mapping, workOrderForm, request, response);
    }

    public ActionForward addBindMesProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        WorkOrderForm workOrderForm = getForm(form);
        String mesProductId = workOrderForm.getSdsProduct();
        String sapProductId = workOrderForm.getProductId();
        String sapOrderId   = workOrderForm.getWorkorderId();
        String sapMandt     = workOrderForm.getSapMandt();
        String sapWerks     = workOrderForm.getSapWerks();

        checkParams(mesProductId,sapProductId,sapOrderId,sapMandt,sapWerks);
        Item mesProduct = getProduct(mesProductId);
        erpService.insertBindMesProduct(new ErpMesProductBindBean(mesProductId, mesProduct.getInstanceRrn(), sapProductId, sapOrderId, sapMandt, sapWerks));
        WebUtils.setSuccessMsg(request);
        return init(mapping, workOrderForm, request, response);
    }

    public ActionForward updateBindMesProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        WorkOrderForm workOrderForm = getForm(form);
        String mesProductId = workOrderForm.getSdsProduct();
        String sapProductId = workOrderForm.getProductId();
        String sapOrderId   = workOrderForm.getWorkorderId();
        String sapMandt     = workOrderForm.getSapMandt();
        String sapWerks     = workOrderForm.getSapWerks();

        checkParams(mesProductId,sapProductId,sapOrderId,sapMandt,sapWerks);
        Item mesProduct = getProduct(mesProductId);
        erpService.updateBindMesProduct(new ErpMesProductBindBean(mesProductId, mesProduct.getInstanceRrn(), sapProductId, sapOrderId, sapMandt, sapWerks));
        WebUtils.setSuccessMsg(request);
        return queryBindMesProductList(mapping, workOrderForm, request, response);
    }

    public ActionForward deleteBindMesProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        WorkOrderForm workOrderForm = getForm(form);
        String mesProductId = request.getParameter("sdsProductId");
        String sapProductId = request.getParameter("productId");
        String sapOrderId   = request.getParameter("workorderId");
        String sapMandt     = request.getParameter("sapMandt");
        String sapWerks     = request.getParameter("sapWerks");

        checkParams(mesProductId,sapProductId,sapOrderId,sapMandt,sapWerks);
        Item mesProduct = getProduct(mesProductId);
        erpService.deleteBindMesProduct(new ErpMesProductBindBean(mesProductId, mesProduct.getInstanceRrn(), sapProductId, sapOrderId, sapMandt, sapWerks));
        WebUtils.setSuccessMsg(request);
        return queryBindMesProductList(mapping, workOrderForm, request, response);
    }

    private Item getProduct(String productId){
        Item mesProduct = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, LocalContext.getFacilityRrn()), ObjectList.PRODUCT_KEY);
        mesProduct = prpService.getItem(mesProduct);
        Assert.isFalse(mesProduct == null || mesProduct.getInstanceRrn() <= 0, Errors.create().content("Missing Product ID!").build());
        return mesProduct;
    }


    private void checkParams(String mesProductId, String sapProductId, String sapOrderId, String sapMandt, String sapWerks) {
        Assert.isFalse(StringUtils.isBlank(mesProductId), Errors.create().content("Missing MES Product ID!").build());
        Assert.isFalse(StringUtils.isBlank(sapProductId), Errors.create().content("Missing ERP Product ID!").build());
        Assert.isFalse(StringUtils.isBlank(sapOrderId), Errors.create().content("Missing WorkOrder ID!").build());
        Assert.isFalse(StringUtils.isBlank(sapMandt), Errors.create().content("Missing Mandt!").build());
        Assert.isFalse(StringUtils.isBlank(sapWerks), Errors.create().content("Missing Werks!").build());
    }

}