/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jcoapp;
/**
*
* @author luolai
*/
import com.sap.mw.jco.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class SAP extends Object {
int count;
JCO.Client mConnection;
JCO.Repository mRepository;
String[] SAPInterfaces;
JCO.Function function = null;
JCO.Table codes = null;
JCO.Table DATA = null;
//private String p = scanApp.class.getResource("logon.properties").getPath();
private Configuration cf = new Configuration("logon.properties");//.\\src\\jcoapp\\logon.properties
LogInfo loginfo = new LogInfo();
public SAP() {
try {
// Logon info
mConnection = JCO.createClient(cf.getValue("jco.client.client"),
cf.getValue("jco.client.user"), cf.getValue("jco.client.passwd"),
cf.getValue("jco.client.lang"), cf.getValue("jco.client.ashost"),
cf.getValue("jco.client.sysnr")); // system number
mConnection.connect();
mRepository = new JCO.Repository("ARAsoft", mConnection);
System.out.println("SAP连接成功");
loginfo.appendLog("SAP连接成功");
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
public String str2int(String str) {
String value = "";
value = str;
if (value.equals("")) {
value = "0";
}
return String.valueOf(Integer.parseInt(value));
}
public HashMap getTableFunction(String functionName, String tableName) {
Map<String, String> m = new HashMap<String, String>();
try {
function = this.createFunction(functionName);
if (function == null) {
System.out.println(functionName + " not found in SAP.");
loginfo.appendLog(functionName + " not found in SAP.");
System.exit(1);
}
mConnection.execute(function);
codes = function.getTableParameterList().getTable(tableName);
System.out.println(Integer.toString(codes.getNumRows()));
loginfo.appendLog(Integer.toString(codes.getNumRows()));
System.out.println(Integer.toString(codes.getFieldCount()));
loginfo.appendLog(Integer.toString(codes.getFieldCount()));
StringBuffer logtxt = new StringBuffer();
for (int j = 0; j < codes.getFieldCount(); j++) {
codes.setRow(0);
System.out.print(codes.getField(j).getName() + "\t");
logtxt.append(codes.getField(j).getName() + "\t");
}
System.out.println(logtxt);
loginfo.appendLog(logtxt.toString());
logtxt = new StringBuffer();
for (int i = 0; i < codes.getNumRows(); i++) {
codes.setRow(i);
logtxt = new StringBuffer();
for (int j = 0; j < codes.getFieldCount(); j++) {
System.out.print(codes.getString(j).toString() + "\t");
logtxt.append(codes.getString(j).toString() + "\t");
}
System.out.println();
loginfo.appendLog(logtxt.toString() + "\t");
m.put(str2int(codes.getString("TRAN_TYPE").toString()) + str2int(codes.getString("TRAN_CODE").toString()) + str2int(codes.getString("ACTN_CODE").toString()),
codes.getString("ZFUNCTION").toString());
//System.out.println(codes.getString("ZFUNCTION"));
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
return (HashMap) m;
}
public String callFunction(String functionName, String fileName) {
String ret1 = "";
String ret2 = "";
StringBuffer retMessage = new StringBuffer("");
StringBuffer strLog = new StringBuffer("");
JCO.Table table = null;
try {
function = this.createFunction(functionName);
if (function == null) {
System.out.println(functionName + " not found in SAP.");
LogInfo.appendLog(functionName + " not found in SAP.");
System.exit(1);
}
JCO.ParameterList input = function.getImportParameterList();
input.setValue(fileName, "FILENAME");
mConnection.execute(function);
ret1 = function.getExportParameterList().getString("P_MSGTYP");
ret2 = function.getExportParameterList().getString("P_LOGNUMBER");
table = function.getTableParameterList().getTable("TZLOG");
System.out.println("SAP RFC feedback: P_MSGTYP=" + ret1);
LogInfo.appendLog("SAP RFC feedback: P_MSGTYP=" + ret1);
//System.out.println(Integer.toString(table.getNumRows()));
//System.out.println(Integer.toString(table.getFieldCount()));
for (int j = 0; j < table.getFieldCount(); j++) {
table.setRow(0);
System.out.print(table.getField(j).getName() + "\t");
strLog.append(table.getField(j).getName() + "\t");
}
LogInfo.appendLog(strLog.toString());
strLog = new StringBuffer("");
System.out.println();
for (int i = 0; i < table.getNumRows(); i++) {
table.setRow(i);
if (i == 0) {
retMessage.append(table.getString("LOGNUMBER").toString() + ":");
}
retMessage.append(table.getString("DATA").toString());
for (int j = 0; j < table.getFieldCount(); j++) {
if (ret1.equals("") && j == 5) {
ret1 = table.getString(j).toString();
if (!ret1.equals("")) {
System.out.println("SAP RFC feedback: P_MSGTYP=" + ret1);
LogInfo.appendLog("SAP RFC feedback: P_MSGTYP=" + ret1);
}
}
System.out.print(table.getString(j).toString() + "\t");
strLog.append(table.getString(j).toString() + "\t");
}
LogInfo.appendLog(strLog.toString());
System.out.println();
}
//System.out.println(ret2);
if (!retMessage.toString().equals("")) {
ret1 = ret1 + "[" + retMessage.toString() + "]";
}
} catch (Exception ex) {
ex.printStackTrace();
return ex.getMessage();
//return ret1;
}
return ret1;
}
public JCO.Function createFunction(String name) throws Exception {
try {
IFunctionTemplate ft =
mRepository.getFunctionTemplate(name.toUpperCase());
if (ft == null) {
return null;
}
return ft.getFunction();
} catch (Exception ex) {
throw new Exception("Problem retrieving JCO.Function object.");
}
}
public static void main(String str[]) {
SAP app = new SAP();
app.getTableFunction("ZWMF_GET_FUNCTION", "ITAB");
app.callFunction("ZWMF_WMIMM_IN_PO", "/sapinstall/WMIMM/PIX/0_PIX000011.XML");
}
}
java调用SAP RFC函数 2
最新推荐文章于 2022-11-28 10:46:53 发布