`
wfzhanga
  • 浏览: 67999 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JAVA与Sap交互技术RFC

阅读更多
public class SapFunction {
    //JCo 返回参数类型定义
    public static final Integer Type_String = 1;
    public static final Integer Type_Int = 2;
    public static final Integer Type_Long = 3;
    public static final Integer Type_Double = 4;
    public static final Integer Type_Date = 5;
    public static final Integer Type_Float = 6;
    
    /** 
     * @Description: 返回值为List,有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param paramMap  SAP方法所需要的参数MAP,KEY=传入参数名称,VALUE=传入参数值;
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  List<?>  SAP方法返回的List,?为指定类型;
     * @throws 
    */ 
    public static List<?> getList(String functionName,String tableName, Map<String,Integer> keyTypeMap, Class<?> beanClazz){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        List<Object> list = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            function.execute(jCoDestination);
            JCoTable tb = function.getTableParameterList().getTable(tableName);
            for (int i = 0; i < tb.getNumRows(); i++) {  
                tb.setRow(i);
                Map<String,Object> map = new HashMap<String,Object>();
                for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                    switch(entry.getValue().intValue()){
                        case 1:
                            map.put(entry.getKey(), tb.getString(entry.getKey()));
                            break;
                        case 2:
                            map.put(entry.getKey(), tb.getInt(entry.getKey()));
                            break;
                        case 3:
                            map.put(entry.getKey(), tb.getLong(entry.getKey()));
                            break;
                        case 4:
                            map.put(entry.getKey(), tb.getDouble(entry.getKey()));
                            break;
                        case 5:
                            map.put(entry.getKey(), tb.getDate(entry.getKey()));
                            break;
                        case 6:
                            map.put(entry.getKey(), tb.getFloat(entry.getKey()));
                            break;
                    }
                }
                if(list == null){
                    list = new ArrayList<Object>();
                }
                Object obj = CommonUtils.getBean(map, beanClazz);
                list.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /** 
     * @Description: 返回值为List,有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param paramMap  SAP方法所需要的参数MAP,KEY=传入参数名称,VALUE=传入参数值;
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  List<?>  SAP方法返回的List,?为指定类型;
     * @throws 
    */ 
    public static List<?> getList(String functionName, Map<String,Object> paramMap,String tableName, Map<String,Integer> keyTypeMap, Class<?> beanClazz){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        List<Object> list = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            JCoParameterList parameterList = function.getImportParameterList();
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                parameterList.setValue(entry.getKey(), entry.getValue());
            }
            function.execute(jCoDestination);
            JCoTable tb = function.getTableParameterList().getTable(tableName);
            for (int i = 0; i < tb.getNumRows(); i++) {  
                tb.setRow(i);
                Map<String,Object> map = new HashMap<String,Object>();
                for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                    switch(entry.getValue().intValue()){
                        case 1:
                            map.put(entry.getKey(), tb.getString(entry.getKey()));
                            break;
                        case 2:
                            map.put(entry.getKey(), tb.getInt(entry.getKey()));
                            break;
                        case 3:
                            map.put(entry.getKey(), tb.getLong(entry.getKey()));
                            break;
                        case 4:
                            map.put(entry.getKey(), tb.getDouble(entry.getKey()));
                            break;
                        case 5:
                            map.put(entry.getKey(), tb.getDate(entry.getKey()));
                            break;
                        case 6:
                            map.put(entry.getKey(), tb.getFloat(entry.getKey()));
                            break;
                    }
                }
                if(list == null){
                    list = new ArrayList<Object>();
                }
                Object obj = CommonUtils.getBean(map, beanClazz);
                list.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /** 
     * @Description: 返回值为List,有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param paramMap  SAP方法所需要的参数MAP,KEY=传入参数名称,VALUE=传入参数值;
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  Map<String,Object>  SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
     * @throws 
    */ 
    public static List<Map<String,Object>> getList(String functionName, Map<String,Object> paramMap,String tableName, Map<String,Integer> keyTypeMap){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        List<Map<String,Object>> list = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            JCoParameterList parameterList = function.getImportParameterList();
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                parameterList.setValue(entry.getKey(), entry.getValue());
            }
            function.execute(jCoDestination);
            JCoTable tb = function.getTableParameterList().getTable(tableName);
            for (int i = 0; i < tb.getNumRows(); i++) {  
                tb.setRow(i);
                Map<String,Object> map = new HashMap<String,Object>();
                for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                    switch(entry.getValue().intValue()){
                        case 1:
                            map.put(entry.getKey(), tb.getString(entry.getKey()));
                            break;
                        case 2:
                            map.put(entry.getKey(), tb.getInt(entry.getKey()));
                            break;
                        case 3:
                            map.put(entry.getKey(), tb.getLong(entry.getKey()));
                            break;
                        case 4:
                            map.put(entry.getKey(), tb.getDouble(entry.getKey()));
                            break;
                        case 5:
                            map.put(entry.getKey(), tb.getDate(entry.getKey()));
                            break;
                        case 6:
                            map.put(entry.getKey(), tb.getFloat(entry.getKey()));
                            break;
                    }
                }
                if(list == null){
                    list = new ArrayList<Map<String,Object>>();
                }
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /** 
     * @Description: 返回值为List,没有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  Map<String,Object>  SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
     * @throws 
    */ 
    public static List<Map<String,Object>> getList(String functionName,String tableName, Map<String,Integer> keyTypeMap){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        List<Map<String,Object>> list = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            function.execute(jCoDestination);
            JCoTable tb = function.getTableParameterList().getTable(tableName);
            for (int i = 0; i < tb.getNumRows(); i++) {  
                tb.setRow(i);
                Map<String,Object> map = new HashMap<String,Object>();
                for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                    switch(entry.getValue().intValue()){
                        case 1:
                            map.put(entry.getKey(), tb.getString(entry.getKey()));
                            break;
                        case 2:
                            map.put(entry.getKey(), tb.getInt(entry.getKey()));
                            break;
                        case 3:
                            map.put(entry.getKey(), tb.getLong(entry.getKey()));
                            break;
                        case 4:
                            map.put(entry.getKey(), tb.getDouble(entry.getKey()));
                            break;
                        case 5:
                            map.put(entry.getKey(), tb.getDate(entry.getKey()));
                            break;
                        case 6:
                            map.put(entry.getKey(), tb.getFloat(entry.getKey()));
                            break;
                    }
                }
                if(list == null){
                    list = new ArrayList<Map<String,Object>>();
                }
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /** 
     * @Description: 返回值为MAP,但有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param paramMap  SAP方法所需要的参数MAP,KEY=传入参数名称,VALUE=传入参数值;
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  Map<String,Object>  SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
     * @throws 
    */ 
    public static Map<String,Object> getMap(String functionName, Map<String,Object> paramMap, Map<String,Integer> keyTypeMap){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        Map<String,Object> map = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            JCoParameterList parameterList = function.getImportParameterList();
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                parameterList.setValue(entry.getKey(), entry.getValue());
            }
            function.execute(jCoDestination);
            JCoParameterList resultList = function.getExportParameterList();
            for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                if(map == null){
                    map = new HashMap<String,Object>();
                }
                switch(entry.getValue().intValue()){
                    case 1:
                        map.put(entry.getKey(), resultList.getString(entry.getKey()));
                        break;
                    case 2:
                        map.put(entry.getKey(), resultList.getInt(entry.getKey()));
                        break;
                    case 3:
                        map.put(entry.getKey(), resultList.getLong(entry.getKey()));
                        break;
                    case 4:
                        map.put(entry.getKey(), resultList.getDouble(entry.getKey()));
                        break;
                    case 5:
                        map.put(entry.getKey(), resultList.getDate(entry.getKey()));
                        break;
                    case 6:
                        map.put(entry.getKey(), resultList.getFloat(entry.getKey()));
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    
    
    /** 
     * @Description: 返回值为MAP,但没有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param keyTypeMap  SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
     * @return  Map<String,Object>  SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
     * @throws 
    */ 
    public static Map<String,Object> getMap(String functionName, Map<String,Integer> keyTypeMap){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        Map<String,Object> map = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            function.execute(jCoDestination);
            JCoParameterList resultList = function.getExportParameterList();
            for (Map.Entry<String, Integer> entry : keyTypeMap.entrySet()) {
                if(map == null){
                    map = new HashMap<String,Object>();
                }
                switch(entry.getValue().intValue()){
                    case 1:
                        map.put(entry.getKey(), resultList.getString(entry.getKey()));
                        break;
                    case 2:
                        map.put(entry.getKey(), resultList.getInt(entry.getKey()));
                        break;
                    case 3:
                        map.put(entry.getKey(), resultList.getLong(entry.getKey()));
                        break;
                    case 4:
                        map.put(entry.getKey(), resultList.getDouble(entry.getKey()));
                        break;
                    case 5:
                        map.put(entry.getKey(), resultList.getDate(entry.getKey()));
                        break;
                    case 6:
                        map.put(entry.getKey(), resultList.getFloat(entry.getKey()));
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    
    
    /** 
     * @Description: 返回值为String,但没有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param paramMap  SAP方法所需要的参数MAP,KEY=传入参数名称,VALUE=传入参数值;
     * @param valueKey  SAP方法返回的参数名称;
     * @return  String  SAP方法返回的参数值;
     * @throws 
    */ 
    public static String getString(String functionName, Map<String,Object> paramMap, String valueKey){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            JCoParameterList parameterList = function.getImportParameterList();
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                parameterList.setValue(entry.getKey(), entry.getValue());
            }
            function.execute(jCoDestination);
            JCoParameterList resultList = function.getExportParameterList();
            return resultList.getString(valueKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    /** 
     * @Description: 返回值为String,但没有入参的情况
     * @param functionName  调用的SAP方法名称
     * @param valueKey  SAP方法返回的参数名称;
     * @return  String  SAP方法返回的参数值;
     * @throws 
    */ 
    public static String getString(String functionName, String valueKey){
        JCoDestination jCoDestination = null;
        JCoFunction function = null;
        try {
            jCoDestination = SapFactory.getConnection();
            function = jCoDestination.getRepository().getFunction(functionName);
            function.execute(jCoDestination);
            JCoParameterList resultList = function.getExportParameterList();
            return resultList.getString(valueKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

public class SapFactory {
    
    private static SapFactory instance = null;
    private static String abap_as_pooled = "ABAP_AS_WITH_POOL";
    private SapFactory(){
    }
    
    public static JCoDestination getConnection(){
        if(instance == null){
            instance = new SapFactory();
            instance.init();
        }
        return instance.connection();
    }
    
    private void init(){
        Properties connectProperties = new Properties();
        abap_as_pooled = AppContext.getSystemProperty("formFlowListener.abap_as_pooled");
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, AppContext.getSystemProperty("formFlowListener.jco_ashost"));                //连接IP
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  AppContext.getSystemProperty("formFlowListener.jco_sysnr"));                 //系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, AppContext.getSystemProperty("formFlowListener.jco_client"));                //SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   AppContext.getSystemProperty("formFlowListener.jco_user"));                  //SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, AppContext.getSystemProperty("formFlowListener.jco_passwd"));                //密码
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   AppContext.getSystemProperty("formFlowListener.jco_lang"));                  //登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, AppContext.getSystemProperty("formFlowListener.jco_pool_capacity"));  //最大连接数  
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, AppContext.getSystemProperty("formFlowListener.jco_peak_limit"));        //最大连接线程
        createDataFile(abap_as_pooled, "jcoDestination", connectProperties);
    }

    private void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }
    
    private JCoDestination connection(){
        JCoDestination destination =null;
        try {
            destination = JCoDestinationManager.getDestination(abap_as_pooled);
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return destination;
    }
}

 

需要引入的 jar与dll文件

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics