SapphireProductAction.java

package com.mycim.webapp.actions.bonding;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.MiscUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.ReticleGroup;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.SapphireProduct;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.SapphireProductForm;
import org.apache.commons.beanutils.PropertyUtils;
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 SapphireProductAction extends PrpSetupAction {

    public static final String TECHNOLOGYS_KEY = "technologys";

    public static final String RETICLEGROUPS_KEY = "reticleGroups";

    @Override
    public String inItMethod() {
        return "editSapphireProduct";
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        return mapping.findForward(Constants.INIT_KEY);
    }

    /**
     * 编辑Bonding产品设置
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward editSapphireProduct(ActionMapping mapping, SapphireProductForm theform,
                                             HttpServletRequest request,
                                             HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String id;
        Collection technologys;
        Collection reticleGroups;
        List<Map<String, String>> materials;

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

        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        if (item.getInstanceRrn() <= 0) {
            PropertyUtils.copyProperties(theform, item);
            request.setAttribute(SessionNames.ITEM_KEY, item);
            theform.setTransId(Constants.CREATE_KEY);
            return mapping.findForward(Constants.CREATE_KEY);
        }
        Assert.isTrue(ObjectList.SAPPHIRE.equals(item.getObjectType()),
                      Errors.create().key(MessageIdList.PRODUCT_NOT_BONDING_PRODUCT).content("不是绑定类型的产品").build());

        technologys = prpService.getProductTechnologys(item.getInstanceRrn());
        reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
        materials = getProductAndMaterialRelationForShow(item.getInstanceRrn());

        item.setProductBins(prpService.getProductBins(item.getInstanceRrn()));
        item.setProductTechnologys(technologys);
        item.setProductReticleGroups(reticleGroups);

        SapphireProduct sapphireProduct = prpService.getSapphireProduct(item.getInstanceRrn());

        PropertyUtils.copyProperties(theform, sapphireProduct);
        PropertyUtils.copyProperties(theform, item);
        // 展示最大使用天数
        theform.setExpiredDateView(sapphireProduct.getExpiredDate() / 86400);

        request.setAttribute(SessionNames.ITEM_KEY, item);
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);
        theform.setMaterialList(materials);
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 添加Bonding产品
     *
     * @return editSapphireProduct(mapping, theform, request, response)
     * @author Qiansheng.wang
     */
    public ActionForward createSapphireProduct(ActionMapping mapping, SapphireProductForm theform,
                                               HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {

        String username = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();

        String id = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        item.setInstanceId(theform.getInstanceId());
        item.setInstanceDesc(theform.getInstanceDesc());
        item.setObjectType(theform.getObjectType());
        item.setProductEngineerRrn(getProductEngineerRrn(theform.getProductEngineerId(), facilityRrn));
        item.setSubproductFlag(MiscUtils.parseCheckBox(theform.getSubproductFlag()));

        item.setTransPerformedby(username);
        item.setTransId(Constants.CREATE_KEY);

        SapphireProduct sapphireProduct = setSapphireProduct(item, theform);

        prpService.insertSapphireProduct(item, sapphireProduct);

        theform.setTransId(Constants.MODIFY_KEY);

        return editSapphireProduct(mapping, theform, request, response);
    }

    /**
     * 修改Bonding产品
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @throws Exception
     * @author Qiansheng.wang
     */
    public ActionForward modifySapphireProduct(ActionMapping mapping, SapphireProductForm theform,
                                               HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        String username = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();

        theform.setProductEngineerRrn(getProductEngineerRrn(theform.getProductEngineerId(), facilityRrn));

        String id = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        PropertyUtils.copyProperties(item, theform);

        item.setTransPerformedby(username);
        item.setTransId(Constants.MODIFY_KEY);

        SapphireProduct sapphireProduct = setSapphireProduct(item, theform);

        prpService.updateSapphireProduct(item, sapphireProduct);

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));
        List<Map> technologys = prpService.getProductTechnologys(item.getInstanceRrn());
        List<Map> reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);
        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 刪除Bonding产品
     *
     * @return mapping.findForward(Constants.INIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward deleteSapphireProduct(ActionMapping mapping, SapphireProductForm theform,
                                               HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        String username = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();

        SapphireProduct sapphireProduct = new SapphireProduct();

        String id = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        item.setTransPerformedby(username);
        item.setTransId(Constants.DELETE_KEY);

        prpService.deleteSapphireProduct(item, sapphireProduct);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));

        return mapping.findForward(Constants.INIT_KEY);
    }

    /**
     * 获取 产品工程师组RRN
     *
     * @return rrn
     * @author Qiansheng.wang
     */
    public long getProductEngineerRrn(String productEngineerId, Long facilityRrn) throws Exception {
        long rrn = 0;
        if (StringUtils.isNotBlank(productEngineerId)) {
            rrn = this.getInstanceRrn(productEngineerId.toUpperCase(),
                                      this.getNamedSpace(ObjectList.USERGROUP_KEY, facilityRrn),
                                      ObjectList.USERGROUP_KEY);

            Assert.isFalse(rrn <= 0, Errors.create().content("Product Engineer Group ").build());
        }
        return rrn;
    }

    /**
     * 设置Bonding产品的值
     *
     * @return sapphireProduct
     * @author Qiansheng.wang
     */
    public SapphireProduct setSapphireProduct(Item item, SapphireProductForm theform) {
        validateSapphireProduct(theform);

        SapphireProduct sapphireProduct = new SapphireProduct();

        sapphireProduct.setProductRrn(item.getInstanceRrn());

        // 设置最大使用时间
        Long day = Long.valueOf(theform.getExpiredDateView());
        sapphireProduct.setExpiredDate(day * 24 * 3600);
        sapphireProduct.setTargetCount(Long.valueOf(theform.getTargetCount()));
        sapphireProduct.setUnitDate("s");
        sapphireProduct.setWarningCount(Long.valueOf(0));

        return sapphireProduct;

    }

    /**
     * 检查Bonding产品设置的值
     *
     * @author Qiansheng.wang
     */
    public void validateSapphireProduct(SapphireProductForm theform) {
        Assert.isFalse(StringUtils.isEmpty(StringUtils.toString(theform.getExpiredDateView())) ||
                               Long.valueOf(theform.getExpiredDateView()) < 0, Errors.create().content(
                "Maximum number of " + "days: please enter " + "an " + "integer greater " + "than or equal to 0!")
                                                                                     .build());
        Assert.isFalse(
                StringUtils.isEmpty(StringUtils.toString(theform.getTargetCount())) || theform.getTargetCount() <= 0,
                Errors.create().content("Maximum usage: please enter an integer " + "greater than 0!").build());
    }

    /**
     * 添加工艺流程信息
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward addTechnology(ActionMapping mapping, SapphireProductForm theform, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String username = LocalContext.getUserId();

        String instanceId = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(instanceId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        // Assert.isTrue(prpService.checkProductTechnologysPermission(item.getInstanceRrn()),  暂时没有此功能
        //               Errors.create().key(MessageIdList.PROCESS_NO_PERMISSION_VIEW).content("There" + " is" + " a " +
        //                                                                                             "process under " +
        //                                                                                             "the product, but" +
        //                                                                                             " you do not have" +
        //                                                                                             " permission to " +
        //                                                                                             "view").build());
        //Collection technologys = (Collection) request.getAttribute(TECHNOLOGYS_KEY);
        List<Map> technologys = prpService.getProductTechnologys(item.getInstanceRrn());

        boolean addFlag = getAddFlag(technologys, theform.getProcessId());
        if (addFlag) {
            String id = theform.getProcessId().trim().toUpperCase();
            ProcessPlanning processPlanning = new ProcessPlanning(id, getNamedSpace(ObjectList.WFL_KEY, facilityRrn),
                                                                  ObjectList.WFL_KEY);
            processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

            Assert.isTrue(processPlanning.getInstanceRrn() > 0,
                          Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("对象{}为找到")
                                .args("Process Technology").build());
            String defaultValue = getDefaultValue(theform.getProcessFlag(), technologys);

            Map<String, Object> technologyMap = new HashMap<String, Object>();
            technologyMap.put("productrrn", new Long(item.getInstanceRrn()));
            technologyMap.put("processrrn", new Long(processPlanning.getInstanceRrn()));
            technologyMap.put("defaultflag", defaultValue);
            technologyMap.put("transId", Constants.ADD_KEY);
            technologyMap.put("transPerformedby", username);
            prpService.insertProductTechnology(technologyMap);

            Map<String, Object> job = new HashMap<String, Object>();
            job.put("PRODUCT_RRN", StringUtils.toString(item.getInstanceRrn()));
            job.put("PROCESS_RRN", StringUtils.toString(processPlanning.getInstanceRrn()));
            //todo 工艺流程相关 暂时todo
            //                SchedulerHelper.createProcessScheduler(job);

            HashMap<String, Object> addInfo = new HashMap<String, Object>();
            addInfo.put("instancerrn", StringUtils.toString(processPlanning.getInstanceRrn()));
            addInfo.put("instanceid", processPlanning.getInstanceId());
            addInfo.put("instancedesc", processPlanning.getInstanceDesc());
            addInfo.put("default", defaultValue);
            technologys.add(addInfo);
        }

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setProcessId(null);
        theform.setProcessFlag(null);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));
        List<Map> reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);
        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 删除工艺流程信息
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward deleteTechnology(ActionMapping mapping, SapphireProductForm theform,
                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();

        String id = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        ///Collection technologys = (Collection) request.getAttribute(TECHNOLOGYS_KEY);
        List<Map> technologys = prpService.getProductTechnologys(item.getInstanceRrn());

        String deleteRrn = request.getParameter("delechnology");

        Iterator it = technologys.iterator();
        while (it.hasNext()) {
            HashMap map = (HashMap) it.next();

            if (((String) map.get("instancerrn")).equalsIgnoreCase(deleteRrn)) {

                Map<String, Object> technologyMap = new HashMap<String, Object>();
                technologyMap.put("productrrn", new Long(item.getInstanceRrn()));
                technologyMap.put("processrrn", new Long(deleteRrn));
                technologyMap.put("transId", Constants.DELETE_KEY);
                technologyMap.put("transPerformedby", username);
                prpService.deleteProductTechnology(technologyMap);

                it.remove();

                break;
            }
        }

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));
        List<Map> reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);
        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 添加光罩组信息
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward addReticleGroup(ActionMapping mapping, SapphireProductForm theform, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String username = LocalContext.getUserId();

        String instanceId = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(instanceId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        //Collection reticleGroups = (Collection) request.getAttribute(RETICLEGROUPS_KEY);
        List<Map> reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());

        boolean addFlag = getAddFlag(reticleGroups, theform.getReticleGroupId());
        if (addFlag) {
            String id = theform.getReticleGroupId().trim().toUpperCase();
            ReticleGroup reticleGroup = new ReticleGroup(id, getNamedSpace(ObjectList.RETICLEFAMILY_KEY, facilityRrn),
                                                         ObjectList.RETICLEFAMILY_KEY);
            NamedObject namedObject = baseService.getNamedObject(reticleGroup);
            reticleGroup.copyNamedObject(namedObject);
            ;
            if (reticleGroup == null) {
                reticleGroup = new ReticleGroup();
            }
            Assert.isTrue(reticleGroup.getInstanceRrn() > 0,
                          Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("对象{}未找到")
                                .args("Process Reticle Group").build());
            String defaultValue = getDefaultValue(theform.getReticleGroupFlag(), reticleGroups);

            HashMap<String, Object> reticleGroupMap = new HashMap<String, Object>();
            reticleGroupMap.put("productrrn", new Long(item.getInstanceRrn()));
            reticleGroupMap.put("reticleGroupRrn", new Long(reticleGroup.getInstanceRrn()));
            reticleGroupMap.put("defaultflag", defaultValue);
            reticleGroupMap.put("transId", Constants.ADD_KEY);
            reticleGroupMap.put("transPerformedby", username);
            prpService.insertProductReticleGroup(reticleGroupMap);

            HashMap<String, Object> addInfo = new HashMap<String, Object>();
            addInfo.put("instancerrn", StringUtils.toString(reticleGroup.getInstanceRrn()));
            addInfo.put("instanceid", reticleGroup.getInstanceId());
            addInfo.put("instancedesc", reticleGroup.getInstanceDesc());
            addInfo.put("default", defaultValue);
            reticleGroups.add(addInfo);
        }

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setReticleGroupId(null);
        theform.setReticleGroupFlag(null);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));
        List<Map> technologys = prpService.getProductTechnologys(item.getInstanceRrn());
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);
        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 删除光罩组信息
     *
     * @return mapping.findForward(Constants.EDIT_KEY)
     * @author Qiansheng.wang
     */
    public ActionForward deleteReticleGroup(ActionMapping mapping, SapphireProductForm theform,
                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();

        String instanceId = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(instanceId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        //Collection reticleGroups = (Collection) request.getAttribute(RETICLEGROUPS_KEY);
        List<Map> reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
        String deleteRrn = request.getParameter("delreticlegroup");

        Iterator it = reticleGroups.iterator();
        while (it.hasNext()) {
            HashMap mapp = (HashMap) it.next();

            if (((String) mapp.get("instancerrn")).equalsIgnoreCase(deleteRrn)) {

                HashMap<String, Object> reticleGroupMap = new HashMap<String, Object>();
                reticleGroupMap.put("productrrn", new Long(item.getInstanceRrn()));
                reticleGroupMap.put("reticleGroupRrn", new Long(deleteRrn));
                reticleGroupMap.put("transId", Constants.DELETE_KEY);
                reticleGroupMap.put("transPerformedby", username);
                prpService.deleteProductReticleGroup(reticleGroupMap);

                it.remove();

                break;
            }
        }

        theform.setTransId(Constants.MODIFY_KEY);
        theform.setMaterialList(getProductAndMaterialRelationForShow(item.getInstanceRrn()));
        List<Map> technologys = prpService.getProductTechnologys(item.getInstanceRrn());
        request.setAttribute(TECHNOLOGYS_KEY, technologys);
        request.setAttribute(RETICLEGROUPS_KEY, reticleGroups);

        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 获取 工艺流程 或 光罩组 的默认值
     *
     * @return defaultValue
     * @author Qiansheng.wang
     */
    public String getDefaultValue(String flag, Collection map) {
        String defaultValue;

        if ((flag != null) && "on".equalsIgnoreCase(flag)) {
            defaultValue = "Y";

            Iterator its = map.iterator();
            while (its.hasNext()) {
                HashMap mapp = (HashMap) its.next();
                mapp.put("default", "N");
            }

        } else {
            defaultValue = "N";
        }
        return defaultValue;
    }

    /**
     * 获取 添加 工艺流程 或 光罩组 的标记
     *
     * @return addFlag
     * @author Qiansheng.wang
     */
    public Boolean getAddFlag(Collection map, String id) {
        boolean addFlag = true;

        Iterator it = map.iterator();
        while (it.hasNext()) {
            HashMap mapp = (HashMap) it.next();

            if (((String) mapp.get("instanceid")).equalsIgnoreCase(id)) {
                addFlag = false;

                break;
            }
        }
        return addFlag;
    }

    /**
     * 删除物料链接
     *
     * @param mapping
     * @param theform
     * @param request
     * @return
     */
    public ActionForward delMaterial(ActionMapping mapping, SapphireProductForm theform, HttpServletRequest request,
                                     HttpServletResponse response) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();

        String instanceId = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(instanceId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        long productRrn = item.getInstanceRrn();

        long delMaterialRrn = WebUtils.getParameterLong("delMaterial", request);
        if (delMaterialRrn >= 0) {
            deleteProductAndMaterialRelation(productRrn, delMaterialRrn, user);
        }

        theform.setMaterialList(getProductAndMaterialRelationForShow(productRrn));
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 添加或更新物料链接
     *
     * @param mapping
     * @param theform
     * @param request
     * @return
     */
    public ActionForward addMaterial(ActionMapping mapping, SapphireProductForm theform, HttpServletRequest request,
                                     HttpServletResponse response) {
        Long facilityRrn = LocalContext.getFacilityRrn();

        String instanceId = theform.getInstanceId().trim().toUpperCase();
        Item item = new Item(instanceId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();

        String materialId = StringUtils.trimToUpperCase(theform.getMaterialId());
        Assert.isFalse(StringUtils.isEmpty(materialId),
                       Errors.create().content("The material id cannot be empty!").build());

        NamedObject material = null;
        material = baseService.getNamedObject(
                new NamedObject(materialId, getNamedSpace(ObjectList.ITEM_KEY, facilityRrn), ObjectList.ITEM_KEY));

        Assert.isFalse(material == null || material.getInstanceRrn() <= 0,
                       Errors.create().content("The material id cannot be " + "empty!").build());

        String defaultFlag;
        if ("on".equalsIgnoreCase(theform.getMaterialFlag())) {
            defaultFlag = "Y";
        } else {
            defaultFlag = "N";
        }

        addProductAndMaterialRelation(productRrn, material, defaultFlag);

        theform.setMaterialId("");
        theform.setMaterialList(getProductAndMaterialRelationForShow(productRrn));
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.EDIT_KEY);
    }

    /**
     * 删除产品与物料的链接关系
     *
     * @param productRrn
     * @param materialRrn
     */
    private void deleteProductAndMaterialRelation(Long productRrn, Long materialRrn, String user) {
        Relation relation = new Relation(productRrn, materialRrn, LinkTypeList.PRODUCT_TO_MATERIAL_KEY);
        relation.setTransPerformedby(user);
        baseService.deleteRelation(relation);
    }

    /**
     * 添加或更新产品与物料的链接关系
     *
     * @param productRrn
     * @param defaultFlag
     */
    private void addProductAndMaterialRelation(Long productRrn, NamedObject material, String defaultFlag) {
        Relation relation = new Relation(productRrn, material.getInstanceRrn(), LinkTypeList.PRODUCT_TO_MATERIAL_KEY);
        relation.setTransPerformedby(material.getTransPerformedby());
        relation.setTransId(material.getTransId());
        relation.setAttributedata1(defaultFlag == null ? "N" : defaultFlag);
        baseService.insertOrUpdateRelation(relation);
    }

}