上一篇: sap-使用 JCO 调用bapi创建物料主数据          下一篇: abap-BAPI_MATERIAL_SAVEDATA 扩展字段的使用

sap-基于jco2.x的简单封装

原文:http://blog.csdn.net/fangkailove/archive/2010/08/18/5820365.aspx

公司引入一套基于 java 的 bpm系统,同时需要和 sap  系统集成,为了方便调用 bapi ,将 jco 做了简单的封装:

  1. package com.hs.commUtil;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6. import com.sap.mw.jco.IFunctionTemplate;  
  7. import com.sap.mw.jco.IMetaData;  
  8. import com.sap.mw.jco.JCO;  
  9. import com.sap.mw.jco.JCO.Client;  
  10. import com.sap.mw.jco.JCO.Function;  
  11. import com.sap.mw.jco.JCO.Repository;  
  12. import com.sap.mw.jco.JCO.Structure;  
  13. import com.sap.mw.jco.JCO.Table;  
  14.   
  15. public  class SapRfc   
  16. {  
  17.     /** 
  18.      * 获取一个SAP的连接,并处于连接状态 
  19.      * @return 
  20.      */  
  21.     public static Client createJcoConn()  
  22.     {  
  23.         Properties prop = new Properties();  
  24.         try{  
  25.             String currpath = new File("").getAbsolutePath()+"\\plugs\\SAP_HOST.jcoDestination";  
  26.             InputStream in = new FileInputStream(new File(currpath));  
  27.             if(in != null){  
  28.                 prop.load(in);  
  29.                 String ashost = prop.getProperty("jco.client.ashost");  
  30.                 String clientnr = prop.getProperty("jco.client.client");  
  31.                 String sysnr = prop.getProperty("jco.client.sysnr");  
  32.                 String user = prop.getProperty("jco.client.user");  
  33.                 String pwd = prop.getProperty("jco.client.passwd");  
  34.                 String lang = prop.getProperty("jco.client.lang");  
  35.                   
  36.                 //String poolCpt = prop.getProperty("jco.destination.pool_capacity");  
  37.                 //String pealLmt = prop.getProperty("jco.destination.peak_limit");  
  38.                   
  39.                 Client myConnection = JCO.createClient(clientnr,   
  40.                         user,   
  41.                         pwd,   
  42.                         lang,   
  43.                         ashost,   
  44.                         sysnr);  
  45.                 myConnection.connect();  
  46.                 return myConnection;  
  47.             }  
  48.         }catch(Exception e){  
  49.             e.printStackTrace(System.err);  
  50.         }  
  51.         return null;  
  52.           
  53.     }  
  54.       
  55.     /*** 
  56.      * 关闭指定的 SAP 连接 
  57.      * @param myConnection SAP 连接 
  58.      */  
  59.     public static void closeJcoConn(Client myConnection)  
  60.     {  
  61.         if(myConnection != null && myConnection.isAlive())  
  62.         {  
  63.             myConnection.disconnect();  
  64.         }  
  65.     }  
  66.       
  67.     /*** 
  68.      * 获取SAP的对像仓库 
  69.      * @param myConnection SAP连接 
  70.      * @return 
  71.      */  
  72.     public static Repository getRepository(Client myConnection)  
  73.     {  
  74.         Repository repository = new Repository("", myConnection);  
  75.         return repository;  
  76.     }  
  77.       
  78.     /*** 
  79.      * 获取SAP的RFC函数(BAPI) 
  80.      * @param funName 
  81.      * @param myConnection 
  82.      * @return 
  83.      */  
  84.     public static Function getFunction(String funName,Client myConnection)  
  85.     {  
  86.         Repository myRepository = getRepository(myConnection);  
  87.         return getFunction(funName, myRepository);        
  88.     }  
  89.       
  90.     /** 
  91.      * 获取SAP的RFC函数(BAPI) 
  92.      * @param funName 
  93.      * @param myRepository 
  94.      * @return 
  95.      */  
  96.     public static Function getFunction(String funName,Repository myRepository)  
  97.     {  
  98.         IFunctionTemplate template = myRepository.getFunctionTemplate(funName.toUpperCase());  
  99.         if  (template != null)   
  100.         {  
  101.             Function fun = template.getFunction();  
  102.             return fun;       
  103.         }  
  104.         return null;          
  105.     }  
  106.       
  107.     /** 
  108.      * 获取SAP的结构 
  109.      * @param structureName 
  110.      * @param myRepository 
  111.      * @return 
  112.      */  
  113.     public static Structure getGeneralStructure(String structureName,Repository myRepository)  
  114.     {  
  115.         IMetaData structureDefinition = myRepository.getStructureDefinition(structureName.toUpperCase());  
  116.         if (structureDefinition!=null)  
  117.         {  
  118.             Structure myStructure = JCO.createStructure(structureDefinition);  
  119.             return myStructure;  
  120.         }  
  121.         return null;  
  122.     }  
  123.       
  124.     /*** 
  125.      * 获取SAP的结构 
  126.      * @param structureName 
  127.      * @param myConnection 
  128.      * @return 
  129.      */  
  130.     public static Structure getGeneralStructure(String structureName,Client myConnection)  
  131.     {  
  132.         Repository myRepository = getRepository(myConnection);  
  133.         return getGeneralStructure(structureName,myRepository);       
  134.     }  
  135.       
  136.     /*** 
  137.      * 获取SAP的表 
  138.      * @param tableName 
  139.      * @param myRepository 
  140.      * @return 
  141.      */  
  142.     public static Table getGeneralTable(String tableName,Repository myRepository)  
  143.     {  
  144.         IMetaData tableDefinition = myRepository.getTableDefinition(tableName.toUpperCase());  
  145.         if (tableDefinition!=null)  
  146.         {  
  147.             Table myTable = JCO.createTable(tableDefinition);  
  148.             return myTable;  
  149.         }  
  150.         return null;  
  151.     }  
  152.       
  153.     /*** 
  154.      * 获取SAP的表 
  155.      * @param tableName 
  156.      * @param myConnection 
  157.      * @return 
  158.      */  
  159.     public static Table getGeneralTable(String tableName,Client myConnection)  
  160.     {  
  161.         Repository myRepository = getRepository(myConnection);  
  162.         return getGeneralTable(tableName,myRepository);           
  163.     }  
  164.       
  165.     /*** 
  166.      * 提交在这个连接上的操作 
  167.      * @param myConnection 
  168.      */  
  169.     public static void commit(Client myConnection)  
  170.     {  
  171.         Function fun = getFunction("BAPI_TRANSACTION_COMMIT", myConnection);  
  172.         if (fun!=null)  
  173.         {  
  174.             myConnection.execute(fun);  
  175.         }         
  176.     }  
  177.       
  178.     /*** 
  179.      * 回滚在这个连接上的操作 
  180.      * @param myConnection 
  181.      */  
  182.     public static void rollback(Client myConnection)  
  183.     {         
  184.         Function fun = getFunction("BAPI_TRANSACTION_ROLLBACK", myConnection);  
  185.         if (fun!=null)  
  186.         {  
  187.             myConnection.execute(fun);  
  188.         }     
  189.     }  
  190.       
  191. }