泛微E8和SAP对接工具类

package com.customcode.sap.util;

import com.customcode.util.NewXmActionLog;
import com.sap.mw.jco.JCO;
import weaver.interfaces.sap.SAPConn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA
 *
 * @author
 * @createDate 2018-10-20 22:49
 * @version:v1.0
 */
public class SapUtils {
    private NewXmActionLog log = new NewXmActionLog();
    private String logName = "SapUtils";

    /**
     * 执行SAP
     *
     * @param functionName RFC函数名称
     * @param importParams 输入参数
     * @param importTable  输入表
     * @param exportParams 输出参数
     * @param exportTable  输出表
     * @param myConnection sap连接
     * @return Map 含 exportParameter 输出参数 exportTable 输出表
     */
    public Map sapExecute(String functionName, Map importParams, Map>> importTable, Map exportParams, Map>> exportTable, JCO.Client myConnection) {
        /*存储最终结果:含 exportParameter 输出参数 exportTable 输出表*/
        Map result = new HashMap();
        try {
            //1.0、开启SAP连接
            myConnection.connect();
            SAPConn sapConn = new SAPConn();
            JCO.Function function = sapConn.excuteBapi(functionName, myConnection);
            if (null == function) {
                log.error(logName, "SAP_RFC中没有此函数:" + functionName);
            }
            /*1.1、输入参数 */
            try {
                if (null != importParams && !importParams.isEmpty()) {
                    JCO.ParameterList importParameterList = function.getImportParameterList();
                    Set keySet = importParams.keySet();
                    for (String key : keySet) {
                        importParameterList.setValue(importParams.get(key), key);
                    }
                }
            } catch (Exception e) {
                log.error(logName, "SAP输入参数异常:" + e);
            }
            /*1.1.2、输入表 */
            try {
                if (null != importTable && !importTable.isEmpty()) {
                    JCO.ParameterList tableParameterList = function.getTableParameterList();
                    // 表名集合
                    Set keySet = importTable.keySet();
                    for (String key : keySet) {
                        JCO.Table table = tableParameterList.getTable(key);
                        List> paramsList = importTable.get(key);
                        for (int li = 0; li < paramsList.size(); li++) {
                            // 要先追加行才能写入
                            table.appendRow();
                            Map paramsMap = paramsList.get(li);
                            Set paramsKeySet = paramsMap.keySet();
                            for (String paramKey : paramsKeySet) {
                                table.setValue(paramsMap.get(paramKey), paramKey);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.error(logName, "SAP输入表异常:" + e);
            }
            /*1.2、执行配置好的function */
            myConnection.execute(function);
            /*1.3、输出参数 */
            try {
                if (exportParams != null && !exportParams.isEmpty()) {
                    JCO.ParameterList exportParameterList = function.getExportParameterList();
                    int fieldCount = exportParameterList.getFieldCount();
                    for (int i = 0; i < fieldCount; i++) {
                        JCO.Field field = exportParameterList.getField(i);
                        exportParams.put(field.getName(), field.getValue());
                    }
                    result.put("exportParameter", exportParams);
                }
            } catch (Exception e) {
                log.error(logName, "SAP输出参数异常:" + e);
            }
            /*1.4、输出表 :一个表用一个list存多行,一个行用一个map存储*/
            try {
                if (null != exportTable && !exportTable.isEmpty()) {
                    JCO.ParameterList tableParameterList = function.getTableParameterList();
                    Set keySet = exportTable.keySet();
                    for (String key : keySet) {
                        /*表*/
                        JCO.Table table = tableParameterList.getTable(key);
                        JCO.Field field = null;
                        List> list = new ArrayList>();
                        /*行*/
                        int numRows = table.getNumRows();
                        for (int i = 0; i < numRows; i++) {
                            Map map = new HashMap();
                            /*列*/
                            int numColumns = table.getNumColumns();
                            for (int j = 0; j < numColumns; j++) {
                                field = table.getField(j);
                                map.put(field.getName(), field.getValue());
                            }
                            list.add(map);
                            /*下一行*/
                            table.nextRow();
                        }
                        exportTable.put(key, list);
                    }
                    result.put("exportTable", exportTable);
                }
            } catch (Exception e) {
                log.error(logName, "SAP输出表异常:" + e);
            }
            /*1.5、结果*/
            result.put("status", 200);
            result.put("message", "end");
        } catch (Exception ex) {
            result.put("status", 500);
            result.put("message", "SAP Exception:" + ex);
        } finally {
            if (null != myConnection) {
                myConnection.disconnect();
            }
        }
        return result;
    }
}

你可能感兴趣的:(异构系统集成常用工具类篇,java,SAP,泛微E8)