Triggering alert (Email) using java mapping

Click here for the tutorial

Java Mapping 

/**
* @author Sabyasachi Masanta
*/

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;

public class CreateAlert_1 implements StreamTransformation{

private Map param = null;
AbstractTrace 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) throws StreamTransformationException{

AbstractTrace trace = (AbstractTrace) param.get(StreamTransformationConstants.MAPPING_TRACE);
String MSGID = (String)param.get(StreamTransformationConstants.MESSAGE_ID);
String sender = (String)param.get(StreamTransformationConstants.SENDER_SERVICE);
String sndNamespace = (String)param.get(StreamTransformationConstants.SENDER_NAME);
String receiver = (String)param.get(StreamTransformationConstants.RECEIVER_SERVICE);
String recNamespace = (String)param.get(StreamTransformationConstants.RECEIVER_NAME);
OutputStreamWriter buf_writer = new OutputStreamWriter(out);
InputStreamReader buf_reader = new InputStreamReader(in);
String mat = new String();
String name1 = new String();
String qty = new String();
String price = new String();
String totPrice = new String();
String ip_cat = "Z_SABU_TEST_01";

try {
buf_writer.write(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>",
0,
39);
buf_writer.write(
"<ns0:Alert_In_MT xmlns:ns0=\"http://Sabyasachi.Test\">");
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("Product_ID");
NodeList n2 = document.getElementsByTagName("Qty");
NodeList n3 = document.getElementsByTagName("Unit_Price");

Node node1, node2, node3;
Element e1, e2, e3;
NodeList list1, list2, list3;
for (int s = 0; s < n1.getLength(); s++) {
node1 = n1.item(s);
e1 = (Element) node1;
list1 = e1.getChildNodes();
mat = list1.item(0).getNodeValue();
trace.addWarning("Product_ID = " + mat);
CreateAlert_1 ca = new CreateAlert_1();
trace.addWarning("Message_ID = " + MSGID);
trace.addWarning("Sender = " + sender);
trace.addWarning("Send namespae = " + sndNamespace);
trace.addWarning("Receiver = " + receiver);
trace.addWarning("Rec namespace = " + recNamespace);
name1 = ca.callAlert( mat , ip_cat, trace, MSGID, sender, sndNamespace, receiver, recNamespace );
trace.addWarning("Product_Name = " + name1);
buf_writer.write("<Product_ID>" + mat + "</Product_ID>");
buf_writer.flush(); 

buf_writer.write("<Product_Name>" + name1 + "</Product_Name>");
buf_writer.flush();

node2 = n2.item(s);
e2 = (Element) node2;
list2 = e2.getChildNodes();
qty = list2.item(0).getNodeValue();
buf_writer.write("<Qty>" + qty + "</Qty>");
buf_writer.flush();

node3 = n3.item(s);
e3 = (Element) node3;
list3 = e3.getChildNodes();
price = list3.item(0).getNodeValue();
buf_writer.write("<Unit_Price>" + price + "</Unit_Price>");
buf_writer.flush();

int i = Integer.parseInt(qty);
int j = Integer.parseInt(price);
int tot = i * j ;
totPrice = Integer.toString(tot);
buf_writer.write("<Total_Price>" + totPrice + "</Total_Price>");
buf_writer.flush();

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

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

public String callAlert(String prd, String IP_CAT, AbstractTrace trace, String MSGID, String sender, String sndNamespace, String receiver, String recNamespace) {
trace.addWarning("Reached in the callAlert method");
String msg = new String();
String rs = new String();
msg = getProduct(prd, trace);

trace.addWarning("getProduct productID = " + msg);
if (msg.equalsIgnoreCase("") ) {
msg = "Product ID " + prd + " not present in database.";
trace.addWarning("Call the Bridge method = " + msg);
bridge(IP_CAT, msg, trace, MSGID, sender, sndNamespace, receiver, recNamespace);
trace.addWarning("End of Call the Bridge method");
}
return msg;
}

public String getProduct(String prod_id, AbstractTrace trace) {

trace.addWarning("Reached in the getProduct method");
trace.addWarning("getProduct parameter = " + prod_id);
String prod = new String();
Connection con = null;
String url = "jdbc:mysql://IP address:3306/";
String db = "SABU";
String driver = "com.mysql.jdbc.Driver";
String user = "UserID";
String pass = "Password";
try {
trace.addWarning("Reached in first TRY");
Class.forName(driver).newInstance();
trace.addWarning("Class.forName(driver).newInstance();");
con = DriverManager.getConnection(url + db, user, pass);
trace.addWarning("con = DriverManager.getConnection(url + db, user, pass);");
try {
trace.addWarning("Reached in SECOND TRY");
Statement st = con.createStatement();
String query = "SELECT OUTPUT FROM xilookup where INPUT = '" + prod_id + "'";
trace.addWarning("Query String = " + query);
ResultSet res = st.executeQuery(query);
while (res.next()) {
prod = res.getString("OUTPUT");
trace.addWarning("Inside res.next(), OUTPUT = " + prod);

}
con.close();
} catch (SQLException s) {

}
} catch (Exception e) {
trace.addWarning("Reached in CATCH for first TRY");
// e.printStackTrace();
}
trace.addWarning("Return product name from getProduct = " + prod);
return prod;

}

public void bridge(String code, String prd, AbstractTrace trace, String MSGID, String sender, String sndNamespace, String receiver, String recNamespace) {
trace.addWarning("Reached in the bridge method");

String result1 = null;
try {

// Create a client connection to a dedicated R/3 system
client = JCO.createClient( "200", // SAP client
"User ID", // userid
"Password", // password
"EN", // language
"IP Address or host name", // host name
"00"); // system number

// Open the connection
client.connect();

} catch (Exception ex) {

if (client != null) {
client.disconnect();
}
} finally {
trace.addWarning("Alert category:" + code);
trace.addWarning("Alert message:" + prd);
callRFM(code, prd, MSGID, sender, sndNamespace, receiver, recNamespace);
//ensure that a connection is closed after the function module has been executed 
client.disconnect();
}

}

public void callRFM(String code, String prd, String MSGID, String sender, String sndNamespace, String receiver, String recNamespace) {
String result = null;

// Creating repository 
JCO.Repository mRepository = new JCO.Repository("Alert Test", client);
// Creating function template 
IFunctionTemplate funcTemp =
mRepository.getFunctionTemplate("Z_CREATE_ALERT");
// Getting JCO function 
JCO.Function function = funcTemp.getFunction();
// Fill in input parameters
JCO.ParameterList input = function.getImportParameterList();
input.setValue(code, "IP_CATEGORY");
input.setValue(prd, "MESSAGE");
input.setValue(MSGID, "MESSAGE_ID");
input.setValue(sender, "SENDER_SERVICE");
input.setValue(sndNamespace, "SENDER_INTERFACE");
input.setValue(receiver, "RECEIVER_SERVICE");
input.setValue(recNamespace, "RECEIER_INTERFACE");
client.execute(function);

}

}

 

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