Commit aef40615 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Added P N Rao WhatsApp Config source files

parent 0c5e2eac
package ibase.communication.utility;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ibase.system.config.ConnDriver;
import ibase.utility.BaseLogger;
import ibase.utility.E12GenericUtility;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.utility.ITMException;
public class CommUtility {
public String getValue(String currToken, String xmlString, UserInfoBean userInfoBean) throws Exception {
String result = "";
if(currToken.indexOf("df_") != -1) {
BaseLogger.log("3", null, null, "To with 'df_' :[" + currToken + "]");
int startBracket = currToken.indexOf("<");
int endBracket = currToken.indexOf(">");
if (startBracket != -1 && endBracket != -1) {
String trimmedToken = currToken.substring(startBracket, endBracket + 1);
BaseLogger.log("2",null,null,"E12SMSComp.sendSMS trimmedToken:: ["+trimmedToken+"]");
String argument = customizeDataStr(trimmedToken, xmlString);
BaseLogger.log("2",null,null,"E12SMSComp.sendSMS argument:: ["+argument+"]");
String functionName = currToken.substring(0, startBracket);
BaseLogger.log("2",null,null,"E12SMSComp.sendSMS functionName:: ["+functionName+"]");
result = executeSQLFunction(functionName, argument, userInfoBean);
BaseLogger.log("2",null,null,"E12SMSComp.sendSMS result:: ["+result+"]");
}
}
else if ( currToken.indexOf("<") != -1 )
{
BaseLogger.log("3",null,null,"To with '<' :["+currToken+"]");
result = customizeDataStr(currToken, xmlString);
}
else
{
result = currToken;
}
BaseLogger.log("3",null,null,"result :::["+result+"]");
return result;
}
private String customizeDataStr(String fieldName, String domString)throws Exception//prerna
{
String fieldValue ="";
String replacedFieldValue ="";
String currMsgstr ="";
StringBuffer msgBuff = new StringBuffer();
try
{
BaseLogger.log("3",null,null, "fieldName:: "+fieldName);
BaseLogger.log("3",null,null, "domString:: "+domString);
if(fieldName.indexOf("<") != -1)
{
StringTokenizer msgToken = new StringTokenizer(fieldName,">");
BaseLogger.log("3",null,null, "msgToken:: "+msgToken);
while(msgToken.hasMoreTokens())
{
currMsgstr = msgToken.nextToken();
if(currMsgstr.indexOf("<") != -1 )
{
int startBracket = currMsgstr.indexOf("<");
msgBuff.append(currMsgstr.substring(0, startBracket));
fieldValue = currMsgstr.substring(startBracket+1, currMsgstr.length());
replacedFieldValue = getValueOfFieldFromDom(domString, fieldValue);
BaseLogger.log("3",null,null, "replacedFieldValue:: "+replacedFieldValue);
msgBuff.append(replacedFieldValue);
}
}
if(!(fieldName.endsWith(">")))
{
String tempStr = fieldName.substring(fieldName.lastIndexOf(">")+1,fieldName.length());
BaseLogger.log("3",null,null, "tempStr:: "+tempStr);
msgBuff.append(tempStr);
}
}
else
{
msgBuff.append(fieldName);
}
BaseLogger.log("3",null,null," msg buff ["+msgBuff.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
return (msgBuff.toString());
}
private String getValueOfFieldFromDom(String domStr,String fieldRequired)throws Exception
{
Node parentNode = null;
Document dom =null;
String childNodeName = null;
String childNodeName1 = null;
String fieldValue = null;
String fromNode =null;
ArrayList taskList = new ArrayList();
try
{
E12GenericUtility genericUtility = new E12GenericUtility();
dom = genericUtility.parseString(domStr);
if(fieldRequired.indexOf(".") != -1)
{
fromNode = fieldRequired.substring(0,fieldRequired.indexOf("."));
fieldRequired = fieldRequired.substring(fieldRequired.indexOf(".")+1,fieldRequired.length());
}
else
{
fromNode = (fromNode == null)?"Detail1":fromNode;
}
NodeList detailList = dom.getElementsByTagName(fromNode);
int detListLength = detailList.getLength();
for(int ctr = 0; ctr < detListLength; ctr++)
{
Node currDetailNode = detailList.item(ctr);
NodeList childList = currDetailNode.getChildNodes();
int childCtr = childList.getLength();
for(int chCnt=0; chCnt<childCtr; chCnt++)
{
Node childNode = childList.item(chCnt);
childNodeName = childNode.getNodeName();
if(childNodeName.equalsIgnoreCase(fieldRequired) && childNode.getFirstChild() != null)
{
fieldValue = childNode.getFirstChild().getNodeValue().trim();
BaseLogger.log("3",null,null,"fieldRequired ["+fieldRequired+"] fieldValue ["+fieldValue+"]");
}
}
}
fieldValue = (fieldValue == null)?"":fieldValue;
}
catch(Exception e)
{
throw e;
}
return (fieldValue);
}
private String executeSQLFunction(String functionName, String argument, UserInfoBean userInfoBean) throws ITMException {
BaseLogger.log("2",null,null,"Calling executeSQLFunction method ["+functionName +"::"+argument+"]");
Connection conn = null;
CallableStatement callableStatement = null;
ActionHandlerEJB actionHandlerEJB = new ActionHandlerEJB();
String result = null;
ConnDriver connDriver = new ConnDriver();
try {
conn = connDriver.getConnectDB(userInfoBean.getTransDB());
String sql = "{ ? = call " + functionName + "(?) }";
BaseLogger.log("2",null,null,"E12SMSComp.executeSQLFunction functionBody ["+sql+"]");
callableStatement = conn.prepareCall(sql);
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.setString(2, argument);
callableStatement.execute();
result = callableStatement.getString(1);
BaseLogger.log("2",null,null,"E12SMSComp.executeSQLFunction result:: ["+result+"]");
}
catch (Exception e) {
BaseLogger.log("3", null, null, "Exception in E12SMSComp.executeSQLFunction [" + E12GenericUtility.getStackTrace(e) + "]");
throw new ITMException(e); }
finally {
try {
if (callableStatement != null) {
callableStatement.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
return result;
}
}
\ No newline at end of file
package ibase.communication;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import ibase.communication.utility.CommUtility;
import ibase.utility.BaseLogger;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.DBAccessEJB;
public class WhatsAppComm {
private DBAccessEJB dbAccess = null;
public String sendWhatsApp(String actionInfo, String WhatsappXMLString,String receiptMobNumber, UserInfoBean userInfo, String mailXmlString, String tranXmlString) {
BaseLogger.log("3", null, null, " SendWhatsApp Test 19-May-25 Ams @@####### ");
BaseLogger.log("3", null, null, "WhatsappXMLString ==> " + WhatsappXMLString);
BaseLogger.log("3", null, null, " ActionInfo====> " +actionInfo);
BaseLogger.log("3", null, null, " receiptMobNumber==> " +receiptMobNumber);
BaseLogger.log("3", null, null, " tranXmlString==> " +tranXmlString);
try {
String smsTemplateId = null;
CommUtility commUtility = new CommUtility();
String result = commUtility.getValue(receiptMobNumber, tranXmlString, userInfo);
BaseLogger.log("3", null, null, "result::: [" + result + "]");
receiptMobNumber = result;
BaseLogger.log("3", null, null, " receiptMobNumber==> " +receiptMobNumber);
String templateName = getDBAccess().getDBColumnValue("MAIL_FORMAT", "TEMPLATE_NAME",
(" FORMAT_CODE = '" + actionInfo + "' "), userInfo.getTransDB());
BaseLogger.log("3", null, null, "templateName::: [" + templateName + "]");
// Added by Amol S to get the template name for SMS and WhatsApp message, which are stored in JSON format -- Start
if (templateName != null && templateName.trim().startsWith("{")) {
BaseLogger.log("3", null, null, "Inside the if for whtsapp New ==>" + templateName + "]");
// Parse as JSON
JSONObject jsonObj = new JSONObject(templateName);
smsTemplateId = jsonObj.optString("whatsapp", templateName);
BaseLogger.log("3", null, null, "smsTemplateId ==>" + smsTemplateId + "]");
} else {
smsTemplateId = templateName;
}
// Added by Amol S to get the template name for SMS and WhatsApp message, which are stored in JSON format -- End
JSONObject jsonObj = new JSONObject(WhatsappXMLString);
String endpoint = jsonObj.getString("endpoint");
String accessToken = jsonObj.getString("token_id");
String broadcast_name = jsonObj.getString("broadcast_name");
JSONArray parameters = jsonObj.getJSONArray("parameters");
/*
JSONArray parameterArray = new JSONArray();
JSONObject parameterObj = new JSONObject();
parameterObj.put("name", "1");
parameterObj.put("value", "123456");
parameterArray.put(parameterObj);
*/
JSONObject jsonBodyObj = new JSONObject();
jsonBodyObj.put("template_name", smsTemplateId);
jsonBodyObj.put("broadcast_name", broadcast_name);
jsonBodyObj.put("parameters", parameters);
String jsonBody = jsonBodyObj.toString();
BaseLogger.log("3", null, null, "jsonBody:: [" + jsonBody + "]");
BaseLogger.log("3", null, null, "accessToken::: [" + accessToken + "]");
endpoint=endpoint+receiptMobNumber;
BaseLogger.log("3", null, null, "New ENDPOINT==>::: [" + endpoint + "]");
URL url = new URL(endpoint);
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
// conn.setRequestProperty("Content-Type", "application/json-patch+json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = jsonBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
BaseLogger.log("3", null, null, "WhatsApp API Response Code: " + responseCode);
InputStream is;
is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
if (responseCode == 200 || responseCode == 201) {
BaseLogger.log("3", null, null, "WA Message sent successfully response code: [" + responseCode +"]"
+ "Response Body ["+ response.toString() +"]");
} else {
BaseLogger.log("3", null, null, "Failed to send WhatsApp message. HTTP code: " + responseCode
+ "Response Body ["+ response.toString() +"]");
}
conn.disconnect();
} catch (Exception e) {
BaseLogger.log("3", null, null, "Error sending WhatsApp message: " + e.getMessage());
e.printStackTrace();
}
return WhatsappXMLString;
}
private DBAccessEJB getDBAccess() {
try {
if (dbAccess == null) {
dbAccess = new DBAccessEJB();
}
}
catch (Exception e) {
e.printStackTrace();
}
return dbAccess;
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment