Implementation of Bridge without using BPM (Using JAVA mapping)

Click here for the tutorial

Java Mapping

package packageForJavaMapping;

/**
* @author Sabyasachi Masanta
*/

import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.Map;
import java.util.HashMap;
import com.sap.aii.mapping.api.MappingTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.*;

import com.sap.mw.jco.JCO;
import com.sap.mw.jco.IFunctionTemplate;
import java.util.StringTokenizer;


public class BridgeWithoutBpm_Test implements StreamTransformation {
private Map param = null;
private MappingTrace trace = null;
JCO.Client client = null;
public void setParameter(Map param) {
this.param = param;
if (param == null) {
this.param = new HashMap();
}
}

public void execute(InputStream in, OutputStream out) {
OutputStreamWriter buf_writer = new OutputStreamWriter(out);
InputStreamReader buf_reader = new InputStreamReader(in);
String mat = new String();
String name = new String();
try {
buf_writer.write(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",
0,
39);
buf_writer.write(
"<ns0:Inbound_MT xmlns:ns0=\"urn:Sabyasachi_Java_Mapping_1\">");
buf_writer.write("<Result>", 0, 8);
buf_writer.flush();
} catch (IOException e) {
};

try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(in);

NodeList n1 = document.getElementsByTagName("COMPANYCODEID");

Node node1;
Element e1;
NodeList list1;
for (int s = 0; s < n1.getLength(); s++) {
node1 = n1.item(s);
e1 = (Element) node1;
list1 = e1.getChildNodes();
mat = list1.item(0).getNodeValue();

// Call the bridge method
name = bridge(mat);
int n = 0;
StringTokenizer st = new StringTokenizer(name,",");
while (st.hasMoreTokens()) {
if (n == 0)
{
buf_writer.write("<COMP_NAME>" + st.nextToken() + "</COMP_NAME>");
n = n + 1;
}
else if (n == 1)
{
buf_writer.write("<CITY>" + st.nextToken() + "</CITY>");
n = n + 1;
}

}



buf_writer.flush();

}
} catch (Exception e) {
e.printStackTrace();
}

try {
buf_writer.write("</Result>", 0, 9);
buf_writer.write("</ns0:Inbound_MT>", 0, 17);
buf_writer.flush();
buf_writer.close();
} catch (IOException e) {
};
}

public String bridge(String code) {

String result1 = null;
try {

// Create a client connection to a dedicated R/3 system
client = JCO.createClient( "800", // SAP client
"user_id", // userid
"password", // password
"EN", // language
"<Provide IP Address here>", // host name
"11"); // system number

// Open the connection
client.connect();

} catch (Exception ex) {

if (client != null) {
client.disconnect();
}
} finally {
result1 = callRFM(code);
//ensure that a connection is closed after the function module has been executed 
client.disconnect();
}
return result1;
}

public String callRFM(String code) {
String result = null;

// Creating repository 
JCO.Repository mRepository = new JCO.Repository("JCO Tutorial", client);
// Creating function template 
IFunctionTemplate funcTemp =
mRepository.getFunctionTemplate("BAPI_COMPANYCODE_GETDETAIL");
// Getting JCO function 
JCO.Function function = funcTemp.getFunction();
// Fill in input parameters
JCO.ParameterList input = function.getImportParameterList();
input.setValue(code, "COMPANYCODEID");
client.execute(function);
// Get table containing the orders
JCO.Structure returnStructure =
function.getExportParameterList().getStructure(
"COMPANYCODE_DETAIL");

if (returnStructure.getNumFields() > 0) {
result = returnStructure.getString("COMP_NAME") + "," + returnStructure.getString("CITY");

} else {
// System.out.println("No record found");
result = "No record found" + "," + "No record found";

}

return result;
}
}

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image