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<String, Object> sapExecute(String functionName, Map<String, Object> importParams, Map<String, List<Map<String, Object>>> importTable, Map<String, Object> exportParams, Map<String, List<Map<String, Object>>> exportTable, JCO.Client myConnection) {
/*存储最终结果:含 exportParameter 输出参数 exportTable 输出表*/
Map<String, Object> result = new HashMap<String, Object>();
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<String> 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<String> keySet = importTable.keySet();
for (String key : keySet) {
JCO.Table table = tableParameterList.getTable(key);
List<Map<String, Object>> paramsList = importTable.get(key);
for (int li = 0; li < paramsList.size(); li++) {
// 要先追加行才能写入
table.appendRow();
Map<String, Object> paramsMap = paramsList.get(li);
Set<String> 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<String> keySet = exportTable.keySet();
for (String key : keySet) {
/*表*/
JCO.Table table = tableParameterList.getTable(key);
JCO.Field field = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
/*行*/
int numRows = table.getNumRows();
for (int i = 0; i < numRows; i++) {
Map<String, Object> map = new HashMap<String, Object>();
/*列*/
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;
}
}
泛微E8和SAP对接工具类
最新推荐文章于 2024-05-15 16:08:31 发布