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 keyTypeMap, Class> beanClazz){
JCoDestination jCoDestination = null;
JCoFunction function = null;
List 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 map = new HashMap();
for (Map.Entry 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 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 paramMap,String tableName, Map keyTypeMap, Class> beanClazz){
JCoDestination jCoDestination = null;
JCoFunction function = null;
List list = null;
try {
jCoDestination = SapFactory.getConnection();
function = jCoDestination.getRepository().getFunction(functionName);
JCoParameterList parameterList = function.getImportParameterList();
for (Map.Entry 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 map = new HashMap();
for (Map.Entry 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 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 SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
* @throws
*/
public static List> getList(String functionName, Map paramMap,String tableName, Map keyTypeMap){
JCoDestination jCoDestination = null;
JCoFunction function = null;
List> list = null;
try {
jCoDestination = SapFactory.getConnection();
function = jCoDestination.getRepository().getFunction(functionName);
JCoParameterList parameterList = function.getImportParameterList();
for (Map.Entry 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 map = new HashMap();
for (Map.Entry 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>();
}
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* @Description: 返回值为List,没有入参的情况
* @param functionName 调用的SAP方法名称
* @param keyTypeMap SAP方法返回的参数类型,KEY=返回参数名称,VALUE=返回参数类型;
* @return Map SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
* @throws
*/
public static List> getList(String functionName,String tableName, Map keyTypeMap){
JCoDestination jCoDestination = null;
JCoFunction function = null;
List> 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 map = new HashMap();
for (Map.Entry 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>();
}
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 SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
* @throws
*/
public static Map getMap(String functionName, Map paramMap, Map keyTypeMap){
JCoDestination jCoDestination = null;
JCoFunction function = null;
Map map = null;
try {
jCoDestination = SapFactory.getConnection();
function = jCoDestination.getRepository().getFunction(functionName);
JCoParameterList parameterList = function.getImportParameterList();
for (Map.Entry entry : paramMap.entrySet()) {
parameterList.setValue(entry.getKey(), entry.getValue());
}
function.execute(jCoDestination);
JCoParameterList resultList = function.getExportParameterList();
for (Map.Entry entry : keyTypeMap.entrySet()) {
if(map == null){
map = new HashMap();
}
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 SAP方法返回的参数值,KEY=返回参数名称,VALUE=返回参数值;
* @throws
*/
public static Map getMap(String functionName, Map keyTypeMap){
JCoDestination jCoDestination = null;
JCoFunction function = null;
Map map = null;
try {
jCoDestination = SapFactory.getConnection();
function = jCoDestination.getRepository().getFunction(functionName);
function.execute(jCoDestination);
JCoParameterList resultList = function.getExportParameterList();
for (Map.Entry entry : keyTypeMap.entrySet()) {
if(map == null){
map = new HashMap();
}
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 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 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文件