package weaver.interfaces.workflow.action;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
import weaver.soa.workflow.request.RequestInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BAPI_PO_CREATE
implements Action
{
private Log log;
// The MySAP.com system we gonna be using
static final String SID = "R3";
// private static final JCO.TYPE_CHAR null = null;
// The repository we will be using
IRepository repository;
public BAPI_PO_CREATE()
{
log = LogFactory.getLog((weaver.interfaces.workflow.action.BAPI_PO_CREATE.class).getName());
try {
// Add a connection pool to the specified system
// The pool will be saved in the pool list to be used
// from other threads by JCO.getClient(SID).
// The pool must be explicitely removed by JCO.removeClientPool(SID)
JCO.addClientPool( SID, // Alias for this pool
10, // Max. number of connections
"200", // SAP client
"dev", // userid
"142536", // password
"EN", // language
"192.168.0.12", // host name
"00" );
// Create a new repository
// The repository caches the function and structure definitions
// to be used for all calls to the system SID. The creation of
// redundant instances cause performance and memory waste.
repository = JCO.createRepository("MYRepository", SID);
}
catch (JCO.Exception ex) {
System.out.println("Caught an exception: \n" + ex);
}
}
// Retrieves and prints information about the remote system
public void systemInfo()
{
try {
// Get a function template from the repository
IFunctionTemplate ftemplate = repository.getFunctionTemplate("RFC_SYSTEM_INFO");
// if the function definition was found in backend system
if(ftemplate != null) {
// Create a function from the template
JCO.Function function = ftemplate.getFunction();
// Get a client from the pool
JCO.Client client = JCO.getClient(SID);
// We can call 'RFC_SYSTEM_INFO' directly since it does not need any input parameters
client.execute(function);
// The export parameter 'RFCSI_EXPORT' contains a structure of type 'RFCSI'
JCO.Structure s = function.getExportParameterList().getStructure("RFCSI_EXPORT");
// Use enumeration to loop over all fields of the structure
System.out.println("System info for " + SID + ":\n" +
"--------------------");
for (JCO.FieldIterator e = s.fields(); e.hasMoreElements(); ) {
JCO.Field field = e.nextField();
System.out.println(field.getName() + ":\t" + field.getString());
}//for
System.out.println("\n\n");
// Release the client into the pool
JCO.releaseClient(client);
}
else {
System.out.println("Function RFC_SYSTEM_INFO not found in backend system.");
}
}
catch (Exception ex) {
System.out.println("Caught an exception: \n" + ex);
}
}
// Retrieves and displays a sales order list
public void salesOrders()
{
JCO.Client client = null;
try {
// Get a function template from the repository
IFunctionTemplate ftemplate = repository.getFunctionTemplate("BAPI_PO_CREATE");
// if the function definition was found in backend system
if(ftemplate != null) {
// Create a function from the template
JCO.Function function = ftemplate.getFunction();
// Get a client from the pool
client = JCO.getClient(SID);
//ZYBTEST--- Fill in import parameters
JCO.Structure header = function.getImportParameterList().getStructure("PO_HEADER");
header.setValue("20100409","DOC_DATE");
header.setValue("NB","DOC_TYPE");
header.setValue("F","DOC_CAT");
header.setValue("1100","CO_CODE");
header.setValue("1100","PURCH_ORG");
header.setValue("136","PUR_GROUP");
header.setValue("0000100000","VENDOR");
// header.setValue("java create","OUR_REF");
//ZYBTEST--- Fill in tables parameters : PO_ITEMS
JCO.Table tline = function.getTableParameterList().getTable("PO_ITEMS");
tline.appendRow();
tline.setValue("10","PO_ITEM");
tline.setValue("000000000010261623","MATERIAL");
tline.setValue("000000000010261623","PUR_MAT");
tline.setValue("1102","PLANT");
//ZYBTEST--- Fill in tables parameters : PO_ITEM_SCHEDULES
JCO.Table tsline = function.getTableParameterList().getTable("PO_ITEM_SCHEDULES");
tsline.appendRow();
tsline.setValue("10","PO_ITEM");
tsline.setValue("1","SERIAL_NO");
tsline.setValue("20100408","DELIV_DATE");
tsline.setValue("2","QUANTITY");
// Call the remote system
client.execute(function);
// get return message
JCO.ParameterList ret = function.getExportParameterList();
System.out.println("return message,PO NO.: " + ret.getString("PURCHASEORDER"));
// commit bapi_po_create when necessary:
// function = this.createFunction("BAPI_TRANSACTION_COMMIT");
// get return table
JCO.Table ret_table = function.getTableParameterList().getTable("RETURN");
System.out.println("return tables entries:" + ret_table.getNumRows()); //ZYB add:get table lines
ret_table.writeHTML("c:\\flight_list.html");
if (ret_table.getNumRows() > 0)
{
do {
System.out.println("-----------------------------------------");
for (JCO.FieldIterator e = ret_table.fields(); e.hasMoreElements(); )
{
JCO.Field field = e.nextField();
System.out.println(field.getName() + ":\t" + field.getString());
}
}while(ret_table.nextRow());
}
else{
System.out.println("No results found");
} //if
}
else { // if ftemplate = null
System.out.println("Function BAPI_PO_GETLIST not found in backend system.");
}//if
}
catch (Exception ex) {
System.out.println("Caught an exception: \n" + ex);
}
finally {
// Release the client to the pool
JCO.releaseClient(client);
}
}
protected void cleanUp() {
JCO.removeClientPool(SID);
}
public static void main(String[] argv)
{
BAPI_PO_CREATE e = new BAPI_PO_CREATE();
e.systemInfo();
e.salesOrders();
e.cleanUp();
}
public String execute(RequestInfo arg0) {
System.out.println("Caught an exception" );
log.info("Caught an exception1");
return "1";
}
}