Question:
Hi
can any body post some sample code for in java that shows how to
get and retrieve the avaiable stock. The following tow code sample
do not the rigth thing.
-- sample code 1 ----------
/**
* Returns the stock quant for the given material, plant warehouse combination.
* The result is use for getting the stock data.
*
* @param material Material number
* @param plant Plant identification
* @param warehouse Warehouse identification
* @return quant
*/
private int getStockQuant (String material, String plant, String warehouse) {
int quant = -1;
if ( ! ((aRepository == null) || (aConnection == null)) ) {
IFunctionTemplate functionTemplate = aRepository.getFunctionTemplate("BAPI_WHSE_STOCK_GET_LIST");
JCO.Function function = new JCO.Function(functionTemplate);
function.getImportParameterList().setValue(material, "MATERIAL");
function.getImportParameterList().setValue(plant, "PLANT");
function.getImportParameterList().setValue(warehouse, "WAREHOUSENUMBER");
// call the sap function
try {
aConnection.execute(function);
}
catch (JCO.Exception ex) {
JOptionPane.showMessageDialog(null,
"ABAP Execption - '" + ex.getKey() + "\n'" +
ex.toString(),
"Application ERROR", JOptionPane.INFORMATION_MESSAGE);
}
// get the data
JCO.Table returnTable = function.getTableParameterList().getTable("STOCKKEY");
if (returnTable.getNumRows() > 0) {
returnTable.firstRow();
System.out.println("Return has " + returnTable.getNumRows() + " rows");
do {
System.err.println("Client: " + returnTable.getString("QUANT") + " Warehousenumber: " + returnTable.getString("WHSENUMBER") + " Quant: " + returnTable.getInt("QUANT"));
quant = returnTable.getInt("QUANT");
} while (returnTable.nextRow());
}
else {
System.err.println("StockKey - table no entries found");
}
}
return quant;
}
/**
*
*/
private String getMaterialStock (String material, String plant, String warehouse) {
String stock = "#NA";
int quant = getStockQuant(material, plant, warehouse);
if ( (quant != -1) && ! ((aRepository == null) || (aConnection == null)) ) {
IFunctionTemplate functionTemplate = aRepository.getFunctionTemplate("BAPI_WHSE_STOCK_GET_DETAIL");
JCO.Function function = new JCO.Function(functionTemplate);
function.getImportParameterList().setValue(quant, "QUANT");
function.getImportParameterList().setValue(warehouse, "WHSENUMBER");
// call the sap function
try {
aConnection.execute(function);
}
catch (JCO.Exception ex) {
JOptionPane.showMessageDialog(null,
"ABAP Execption - '" + ex.getKey() + "\n'" +
ex.toString(),
"Application ERROR", JOptionPane.INFORMATION_MESSAGE);
}
JCO.Table returnTable = function.getTableParameterList().getTable("STOCKDATA");
if (returnTable.getNumRows() > 0) {
returnTable.firstRow();
System.out.println("StockData has " + returnTable.getNumRows() + " rows");
do {
if ( returnTable.getString("MATERIAL").equals(material) ) {
System.out.println("Available Stock: " + returnTable.getString("AVAIL_STCK"));
//System.out.println("Stock as String: " + returnTable.getString("AVAIL_STCK"));
//System.out.println("Stock as String: " + returnTable.getString("AVAIL_STCK"));
stock = new String(returnTable.getString("AVAIL_STCK"));
}
} while (returnTable.nextRow());
}
else {
System.err.println("StockData - table no entries found");
}
}
return stock;
}
---code sample 2 --------
/**
* Retuns the available stock for the given material
* MBEW-LBKUM
*
* @param material Material number
* @return amount retrieved
*/
private String getMaterialStock1 (String material) {
String amount = new String("#NA");
if ( ! ((aRepository == null) || (aConnection == null)) ) {
IFunctionTemplate functionTemplate = aRepository.getFunctionTemplate("TABLE_ENTRIES_GET_VIA_RFC");
JCO.Function function = new JCO.Function(functionTemplate);
//function.getImportParameterList().setValue("DE","LANGU");
function.getImportParameterList().setValue("MBEW", "TABNAME");
JCO.Table selTable = function.getTableParameterList().getTable("SEL_TAB");
selTable.appendRow();
//for queryString you can place native sql where clause
String queryString = "MATNR = '" + material + "'";
selTable.setValue(queryString, "ZEILE");
//nameTable.setValue("LABST","FIELDNAME");
// call the sap function
try {
aConnection.execute(function);
}
catch (JCO.Exception ex) {
JOptionPane.showMessageDialog(null,
"ABAP Execption - '" + ex.getKey() + "\n'" +
ex.toString(),
JedmsApp.APPNAME, JOptionPane.INFORMATION_MESSAGE);
}
// check if the call was successfull
JCO.Field returnCode = function.getExportParameterList().getField("RC");
if(returnCode.getString().equals("0")) {
int offset = 0;
int len = 0;
// get the value
JCO.Table returnTable = function.getTableParameterList().getTable("TABENTRY");
if (returnTable.getNumRows() > 0) {
// get the metadata to retrieve the information
JCO.Table nameTable = function.getTableParameterList().getTable("NAMETAB");
System.out.println("NAMETAB: " + nameTable);
// get the offset of the material status
nameTable.firstRow();
do {
if (nameTable.getString("FIELDNAME").equals("LBKUM")) {
System.out.println("Offset: " + nameTable.getString("OFFSET"));
try {
offset = Integer.parseInt(nameTable.getString("OFFSET")) + 1;
}
catch (NumberFormatException e) {
// could not convert to integer
System.err.println("Conversion exception in offset: " + e.getLocalizedMessage());
offset = -1;
}
// get length
System.out.println("Length: " + nameTable.getString("DDLEN"));
try {
len = Integer.parseInt(nameTable.getString("DDLEN"));
}
catch (NumberFormatException e) {
// could not convert to integer
System.err.println("Conversion exception in length: " + e.getLocalizedMessage());
len = -1;
}
break;
}
} while ( nameTable.nextRow() );
System.out.println("MBEW Table:\n" + returnTable.toString());
System.out.println("Return has " + returnTable.getNumRows() + " rows");
// start with the first row
returnTable.firstRow();
do {
StringTokenizer tok = new StringTokenizer(returnTable.toString(), "\n");
//System.out.println("Tokens: " + tok.countTokens());
int lineCount = 0;
while (tok.hasMoreTokens()) {
String line = tok.nextElement().toString().trim();
//System.out.println("Line: " + lineCount + "->" + line + "<-");
if (lineCount == 7) {
// found the data line to extract the product state
//System.out.println("Line: " + lineCount + "->" + line + "<-");
System.out.println("Line: " + lineCount + "->" + line + "<-");
//amount = new String((char[])line.substring(offset, offset + len));
amount = new String(line.copyValueOf(line.toCharArray(), offset, len));
break;
}
lineCount++;
}
break;
} while (returnTable.nextRow());
}
else {
System.err.println("MBEW - table no entries found");
}
}
}
/*
try {
amount = Integer.parseInt(res);
}
catch (NumberFormatException e) {
// could not convert to integer
System.err.println("Conversion exception: " + e.getLocalizedMessage());
amount = -1;
}
*/
System.out.println("getMaterialStock1: " + amount);
return amount;
}
------
regards
andreas
Answer:
in your second code sample you create the StringTokenizer with returnTable.toString() as parameter. JCO.Table toString() method does return the whole table as a String you could just print out to the screen. but what you want is the content of the table so you chould use:
tok = new StringTokenizer(returnTable.getString("ENTRY"));
this creates a StringTokenizer with the one line that is in your return table. Then you should be able to extract the amount from this line.
HTH
jane