Wind与S的集成

简介: 物料同步,我们首先要搞清楚,同步发生在何时何地。打开组织的实用程序 打开工作流模板管理 找到图纸发布流程 下载到本地 点击运行,然后使用wcadmin账户登录系统 看到下面的流程图在靠后的某个OR节点,执行发送物料的表达式(即java代码的方法) 我们仔细看看这部分表达式 代码的具体内容如下: 错误信息=ext.

物料同步,我们首先要搞清楚,同步发生在何时何地。
打开组织的实用程序
image

打开工作流模板管理
image

找到图纸发布流程
image

下载到本地
image
image
image

点击运行,然后使用wcadmin账户登录系统
image

看到下面的流程图
在靠后的某个OR节点,执行发送物料的表达式(即java代码的方法)
image

我们仔细看看这部分表达式
image

代码的具体内容如下:

错误信息=ext.pwst.erp.ERPManager.sendMaterial((wt.fc.WTObject)primaryBusinessObject);
if(错误信息.equals("success")){
   result="通过";
}else{
   result="错误处理";
}

上述代码就是Windchill与SAP集成涉及到的核心方法的调用。
上述核心代码存放在Windchill服务器的什么位置呢?
我们使用xftp连接Windchill服务器进行查看,如下图所示
image
那么上述java文件的具体内容是怎样的呢?
我们进入自己的开发环境(Server2016)的eclipse中查看开发环境下,SAP集成代码的内容
image

package ext.pwst.erp;

import java.util.Vector;
import org.apache.log4j.Logger;
import com.ptc.core.meta.server.TypeIdentifierUtility;
import com.sap.mw.jco.JCO;
import ext.pwst.doc.util.IBAHelper;
import wt.change2.ChangeHelper2;
import wt.change2.WTChangeActivity2;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.maturity.MaturityHelper;
import wt.maturity.PromotionNotice;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.VersionControlHelper;

public class ERPManager implements RemoteAccess {
    
    public static Logger log = Logger.getLogger(ERPManager.class);
    // 1. 
    public static String sendMaterial(WTObject obj){
        try{
            if(!RemoteMethodServer.ServerFlag){
                return (String)RemoteMethodServer.getDefault().invoke("sendMaterial", 
                        ERPManager.class.getName(), null, new Class[]{WTObject.class}, new Object[]{obj});
            }else{
                QueryResult qr = null;
                Vector<WTPart> vecPart = new Vector<WTPart>();
                if(obj instanceof PromotionNotice){
                    PromotionNotice pn = (PromotionNotice)obj;
                    qr = MaturityHelper.service.getPromotionTargets(pn);
                }else if(obj instanceof WTChangeActivity2){
                    WTChangeActivity2 eca = (WTChangeActivity2)obj;
                    qr = ChangeHelper2.service.getChangeablesAfter(eca);
                }
                if(qr != null){
                    while (qr.hasMoreElements()) {
                        WTObject o = (WTObject)qr.nextElement();
                        if(o instanceof WTPart){
                            WTPart part = (WTPart)o;
                            vecPart.add(part);
                        }
                    }
                }
                //调用2 sendMToSAP()
                String ret = sendMToSAP(vecPart);
                return ret;
            }
        }catch(Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }
    
    
    /**
     * 2. 发送物料到ERP系统
     * @param part
     * @param mark
     * @return
     * @throws Exception
     */
    public static String sendMToSAP(Vector<WTPart> vecPart) throws Exception{
        boolean mark = false;
        StringBuffer buf = new StringBuffer();
        JCO.Client mConnection = SAPClientUtil.getConnectedClient();
        JCO.Function function = SAPClientUtil.createFunction("Z_PDM_MATERIAL", mConnection);//函数名
        JCO.Table input = function.getTableParameterList().getTable("IM_TAB");
        for(int i = 0; i < vecPart.size(); i++){
            WTPart part = vecPart.get(i);
            part = (WTPart)VersionControlHelper.service.getLatestIteration(part, false);
            input.appendRow();
            log.debug("[sendMToSAP]partnumber:" + part.getNumber());
            //调用3 setTableValue()
            setTableValue(input, part);
            mark = true;
        }
        if(mark){
            mConnection.execute(function);
            JCO.Table output = function.getTableParameterList().getTable("IT_RETURN");
            output.firstRow();
            do{
                String flag = (String)output.getValue("TYPE");
                String number = (String)output.getValue("MATNR");
                String message = (String)output.getValue("MESSAGE");
                log.debug("number:" + number + ";flag:" + flag + ";message:" + message);
                if(flag.equals("E")){
                    if(message.trim().length() > 0)
                        buf.append("物料编号为:" + number + " 的物料,传送erp失败。message:" + message + "\n");
                }
            }while(output.nextRow());
        }
        if(buf.toString().length() == 0){
            buf.append("success");
        }
        return buf.toString();
    }
    
    // 3. 
    public static void setTableValue(JCO.Table input, WTPart part) throws WTException{
        String factory = "";
        String pinpai = "";
        double penqimianji = 0;
        double zhongliang = 0;
        String bianma = "";
        String miaoshu = "";
        String zhongyaodu = "";
        String chanpinxinghao = "";
        String type = TypeIdentifierUtility.getTypeIdentifier(part).getTypename();
        if(type.contains("PWSTXTB.xtbparts")){
            factory = "2000";
            miaoshu = part.getName();
            bianma = part.getNumber();
            penqimianji = IBAHelper.getIBAFloatValue(part, "com.tsmi.pengqimianji");
            zhongliang = IBAHelper.getIBAFloatValue(part, "com.tsmi.zhongliang");
            pinpai = IBAHelper.getIBAStringValue(part, "com.tsmi.pinpai"); 
            pinpai = pinpai == null ? "":pinpai;
            String xinghao = IBAHelper.getIBAStringValue(part, "com.tsmi.xinghao");
            //如果物料型号属性存在且型号编码不超过18位,则型号作为物料号传给erp,否则传编号
            if(xinghao != null && xinghao.length() > 0){
                if(xinghao.length() > 18){
                    bianma = part.getNumber();
                    miaoshu = part.getName() + " " + xinghao;
                }else{
                    bianma = xinghao;
                    miaoshu = part.getName() + " " + part.getNumber();
                }
            }
        }else{
            //技术部部件  重要度,产品型号
            factory = "1000";
            //重要度    01 安全 ;02 重要;03一般
        }
        String unit = part.getDefaultUnit().toString().toUpperCase();
        //调用4 toERPUnit()
        unit = toERPUnit(unit);
        log.debug("[setTableValue]bianma:" + bianma + " miaoshu:" + miaoshu + " unit:" + unit 
                 + " zhongliang:" + zhongliang + " penqimianji:" + penqimianji
                + " zhongyaodu:" + zhongyaodu + " chanpinxinghao:" + chanpinxinghao + " factory:" + factory + " pinpai:" + pinpai);
        input.setValue(bianma, "MATNR");   //物料编码
        input.setValue(miaoshu, "MAKTX");     //物料描述
        input.setValue(unit, "MEINS");         //单位
        input.setValue("Z001", "MTART");     //物料类型
        input.setValue(zhongliang, "NTGEW");     //重量
        input.setValue(penqimianji, "VOLUM");     //涂装面积
        input.setValue(zhongyaodu, "TEMPB");     //重要度
        input.setValue(chanpinxinghao, "WRKST");     //产品型号
        input.setValue("1000", "PRCTR");     //利润中心
        input.setValue(pinpai, "ZEINR");     //品牌
        input.setValue("X", "XMCNG");        //是否允许负库存
        input.setValue(factory, "WERKS");     //工厂
    }
    
    //4.
    public static String toERPUnit(String unit){
        if(unit.equals("EA")){
            return "EA";
        }else if(unit.equals("KG")){
            return "KG";
        }else if(unit.equals("M")){
            return "M";
        }else if(unit.equals("L")){
            return "L";
        }else if(unit.equals("SQ_M")){
            return "M2";
        }else if(unit.equals("MIN")){
            return "MIN";
        }else if(unit.equals("CU_M")){
            return "M3";
        }else if(unit.equals("KM")){
            return "KM";
        }else if(unit.equals("MM")){
            return "MM";
        }else if(unit.equals("CAN")){
            return "CAN";
        }else if(unit.equals("DEG")){
            return "DEG";
        }else if(unit.equals("BLANK")){
            return "EA";
        }else if(unit.equals("PC")){
            return "PC";
        }else{
            return "";
        }
    }
}
package ext.pwst.erp;

import java.util.HashMap;
import java.util.Map;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;

import ext.pwst.conf.ConfigUtil;

public class SAPClientUtil {
    public static JCO.Client getConnectedClient(){
        
        String clientV = ConfigUtil.getProperty("SAP_CLIENT");
        String user = ConfigUtil.getProperty("SAP_USRE");
        String pwd = ConfigUtil.getProperty("SAP_PASSWORD");
        String lang = ConfigUtil.getProperty("SAP_LANG");
        String ipAddress = ConfigUtil.getProperty("SAP_IPADDRESS");
        String sync = ConfigUtil.getProperty("SAP_SYSNR");
        
        JCO.Client client = JCO.createClient(clientV, user, pwd, lang, ipAddress, sync);
        client.connect();
        
        return client;
    }

    public static JCO.Function createFunction(String name, JCO.Client client) throws Exception {
        JCO.Repository repository = new JCO.Repository("ARAsoft", client);
        try {
            IFunctionTemplate ft = repository.getFunctionTemplate(name.toUpperCase());
            if (ft == null)
                return null;
            return ft.getFunction();
        } catch (Exception ex) {
            throw new Exception("Problem retrieving JCO.Function object.");
        }
    }
    
    public static JCO.Table callRFC(String funcName, String paramName, String paramValue, String returnTab) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put(paramName, paramValue);
        
        return callRFC(funcName, params, returnTab);
    }
    
    public static JCO.Table callRFC(String funcName, String paramName, String paramValue, String returnTab, JCO.Client client) throws Exception{
        Map<String, String> params = new HashMap<String, String>();
        params.put(paramName, paramValue);
        
        return callRFC(funcName, params, returnTab, client);
    }
    
    public static JCO.Table callRFC(String funcName, Map<String, String> params, String returnTab) throws Exception{
        return callRFC(funcName, params).getTableParameterList().getTable(returnTab);
    }
    
    public static JCO.Table callRFC(String funcName, Map<String, String> params, String returnTab, JCO.Client client) throws Exception{
        return callRFC(funcName, params, client).getTableParameterList().getTable(returnTab);
    }
    
    public static JCO.Function callRFC(String funcName, Map<String, String> params) throws Exception{
        JCO.Client client = null;
        try{
            client = SAPClientUtil.getConnectedClient();
            return callRFC(funcName, params, client);
        }catch(Exception e){
            throw e;
        }finally{
            if(client != null){
                client.disconnect();
                client = null;
            }
        }
    }
    
    public static JCO.Function callRFC(String funcName, Map<String, String> params, JCO.Client client) throws Exception{
        client = SAPClientUtil.getConnectedClient();
        JCO.Function function = createFunction(funcName, client);
        JCO.ParameterList input = function.getImportParameterList();
        if(params != null){
            for(String key : params.keySet())
                input.setValue(params.get(key), key);
        }
        
        client.execute(function);
        return function;
    }
}
目录
相关文章
|
监控 人机交互 芯片
西门子S7-200 SMART的功能和特点有哪些?STEP7-Micro/WIN SMART编程软件简介
西门子S7-200 SMART是西门子公司针对中国小型自动化市场客户需求设计研发的一款高性价比小型PLC产品。S7-200 SMART CPU将微处理器、集成电源、输入输出电路组合到一个设计紧凑的外壳中,已形成功能强大的小型plc。面板包含电源接线端子、直流24V电源输出端子、数字量输入输出接线端子、CPU状态指示灯、IO状态指示灯、存储卡插槽、以太网接口、RS485接口等。
西门子S7-200 SMART的功能和特点有哪些?STEP7-Micro/WIN SMART编程软件简介
|
Web App开发 Java 数据安全/隐私保护
Edge 支持导入 Chrome 数据;MacBook Air 有望于本月推出;Perl 5.36 发布 | 思否周刊
Edge 支持导入 Chrome 数据;MacBook Air 有望于本月推出;Perl 5.36 发布 | 思否周刊
156 0
|
存储 缓存 openCL
Rock Pi开发笔记(一):Rock Pi系列arm产品方案快速落地方案介绍
Rock Pi开发笔记(一):Rock Pi系列arm产品方案快速落地方案介绍
Rock Pi开发笔记(一):Rock Pi系列arm产品方案快速落地方案介绍
LightSwitch:小试一“.NET研究”下微软开发框架LightSwitch
  之前知道微软在做一个数据驱动的开发框架LightSwitch,但没有用过。前一阵子,一个客户的技术负责人和我说起LightSwitch,所以这次TechNet也就去听了一下LightSwitch的讲座。
728 0
|
Web App开发 存储 SQL
一起谈.NET技术,微软 Share Point 2010 企业应用解决方案
  SharePoint 2010功能及改进   SharePoint 2010分为两个版本SharePoint Foundation 2010和SharePoint Server 2010,其中SharePoint Foundation 2010是WSS 3.0的升级,而SharePoint Server 2010则是MOSS 2007的升级(文中如无特别说明则SharePoint 2010是二者统称)。
1398 0
|
物联网
ZigBee Silicon Labs/Ember EFR32MG 2.9 Mighty Gecko开发套件开箱
一、外包装 二、内部核心部件(整体) 三、内部核心部件(部分)
1666 0
|
物联网 API
ZigBee Silicon Labs/Ember EFR32MG/EM357 1.1 总体框架
开启ZigBee新篇章,陆续更新,欢迎关注~ 第一章 总体框架 (已发布 V1 20180201)ZigBee Silicon Labs/Ember EFR32MG/EM357 1.1 总体框架(
2225 0