Commit ecfcbc77 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Replace MarketingCampaignPreSend.java

parent ff3b4f1e
package ibase.webitm.ejb.wsfa.transactions;
package ibase.marketingCampaign;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -13,6 +16,7 @@ import java.util.List;
import ibase.utility.BaseLogger;
import ibase.utility.E12GenericUtility;
import ibase.utility.EMail;
import ibase.utility.MailInfo;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ValidatorEJB;
......@@ -23,6 +27,11 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
E12GenericUtility genericUtility = new E12GenericUtility();
ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
private static String readHtmlFile(String filePath) throws IOException {
BaseLogger.log("3", null, null, "Calling readHtmlFile method");
return new String(Files.readAllBytes(Paths.get(filePath)));
}
private String buildMailXMLStr(HashMap<String, Object> mailFormatDetails, UserInfoBean userInfo) {
BaseLogger.log("2", null, null,
......@@ -32,10 +41,16 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
// Fetch mail format details from the database
String emailID = mailFormatDetails.get("SEND_TO").toString();
BaseLogger.log("3", null, null, "emailID [" + emailID + "]");
BaseLogger.log("3", null, null, "buildMailXMLStr emailID [" + emailID + "]");
String body = mailFormatDetails.get("BODY").toString();
BaseLogger.log("3", null, null, "body [" + body + "]");
String bodyFilePath = mailFormatDetails.get("BODY").toString();
String body = "";
try {
body = readHtmlFile(bodyFilePath);
} catch (IOException e1) {
BaseLogger.log("3", null, null, "Exception in readHtmlFile method [" + emailID + "]");
e1.printStackTrace();
}
String formatCode = "";
......@@ -108,14 +123,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
if (rs.next()) {
mailListName = rs.getString("mailing_list");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "mail list name [" + mailListName + "]");
// Used to get all email Id against the mail_list_id(campaign_template) from
......@@ -127,16 +135,13 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
while (rs.next()) {
mailList.add(rs.getString("email_id"));
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "mail list [" + mailList + "]");
//To check weather email Id present or not in mailing_list
if(mailList.isEmpty()) {
retString = itmDBAccessEJB.getErrorString("", "NMAILIST", "", "", conn);
return retString;
}
conn.setAutoCommit(false);
// To add entries in mktg_cmpaign_log table
for (String emilId : mailList) {
......@@ -149,14 +154,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
BaseLogger.log("3", null, null, "email for insert [" + emilId + "]");
rs = pstmt.executeQuery();
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
}
conn.commit();
......@@ -168,14 +166,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
formatCode = rs.getString("campaign_template");
batchCount = rs.getInt("batch_count");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "format code [" + formatCode + "]");
sql = "select subject,body_text from mail_format where format_code = ?";
......@@ -186,14 +177,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
subject = rs.getString("subject");
bodyText = rs.getString("body_text");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "subject [" + subject + "]");
BaseLogger.log("3", null, null, "body text [" + bodyText + "]");
......@@ -220,26 +204,25 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
try {
conn = getConnection();
conn.setAutoCommit(false); // Begin transaction
conn.setAutoCommit(false);
// Get email IDs with status 'N'
sql = "SELECT email_id FROM mktg_campaign_log WHERE status = 'N' AND campaign_id = ? FETCH NEXT ? ROWS ONLY";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, tranId);
pstmt.setInt(2, batchCount); // Adjust according to your logic
pstmt.setInt(2, batchCount);
rs = pstmt.executeQuery();
while (rs.next()) {
mailListForMailSend.add(rs.getString("email_id"));
}
BaseLogger.log("3", null, null, "Mail list for mail sending [" + mailListForMailSend + "]");
conn.commit();
} catch (Exception e) {
e.printStackTrace();
BaseLogger.log("0", null, null, "Exception in getUpdatedEmail: " + e.getMessage());
if (conn != null) {
try {
conn.rollback(); // Rollback on exception
conn.rollback();
BaseLogger.log("0", null, null, "Transaction rolled back due to exception.");
} catch (SQLException se) {
se.printStackTrace();
......@@ -261,6 +244,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
String sql = "";
String fromEmailId = "", retString = "";
String status = "";
String errMsg = "";
ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
List<String> mailListForMailSend;
boolean updateFlag = false;
......@@ -304,18 +288,20 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
emailStatus = email.sendMail(mailXMLStr, "ITM", userInfo);
BaseLogger.log("3", null, null, "Email status for [" + emailId + "]: " + emailStatus);
status = "S";
errMsg = "None";
} catch (Exception e) {
BaseLogger.log("3", null, null,
"Error sending email to [" + emailId + "]: " + e.getMessage());
status = "E";
errMsg = e.getMessage();
}
// Update the log
sql = "UPDATE mktg_campaign_log SET status = ? WHERE email_id = ? AND campaign_id = ?";
sql = "UPDATE mktg_campaign_log SET status = ?, err_msg = ? WHERE email_id = ? AND campaign_id = ?";
try (PreparedStatement updatePstmt = conn.prepareStatement(sql)) {
updatePstmt.setString(1, status);
updatePstmt.setString(2, emailId);
updatePstmt.setString(3, tranId);
updatePstmt.setString(2, errMsg);
updatePstmt.setString(3, emailId);
updatePstmt.setString(4, tranId);
int affectedRows = updatePstmt.executeUpdate();
BaseLogger.log("3", null, null,
"Rows affected by update for emailId [" + emailId + "]: " + affectedRows);
......@@ -324,7 +310,6 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
"Error updating log for emailId [" + emailId + "]: " + e.getMessage());
conn.rollback();
}
conn.commit();
}
}
......@@ -360,7 +345,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
String userEmail = "";
String formatCode = "";
String subject = "";
String bodyText = "";
String bodyText = "", msg = "";
// boolean updateFlag = false;
String retString = "";
......@@ -379,16 +364,9 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
if (rs.next()) {
userEmail = rs.getString("email_id");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
//Check weather email id present or not against loged in user
if(userEmail == null) {
closeResources(rs, pstmt);
// Check weather email id present or not against loged in user
if (userEmail == null) {
BaseLogger.log("3", null, null, "In null condition [" + userEmail + "]");
retString = itmDBAccessEJB.getErrorString("", "NULEMAILID", "", "", conn);
return retString;
......@@ -402,14 +380,7 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
if (rs.next()) {
formatCode = rs.getString("campaign_template");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "format code [" + formatCode + "]");
sql = "select subject,body_text from mail_format where format_code = ?";
......@@ -420,15 +391,8 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
subject = rs.getString("subject");
bodyText = rs.getString("body_text");
}
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
BaseLogger.log("3", null, null, "subject [" + subject + "]");
closeResources(rs, pstmt);
BaseLogger.log("3", null, null, "New subject [" + subject + "]");
BaseLogger.log("3", null, null, "body text [" + bodyText + "]");
HashMap<String, Object> mailFormatDetails = new HashMap<>();
......@@ -436,20 +400,24 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
mailFormatDetails.put("MAIL_SERVER", "192.168.137.1");
mailFormatDetails.put("SUBJECT", subject);
mailFormatDetails.put("BODY", bodyText);
EMail email = new EMail();
email.setMailFrom(mailFormatDetails.get("MAIL_SERVER").toString()); // Set the email sender address
System.out.println("getMailFrom1....::" + email.getMailFrom());
String mailXMLStr = buildMailXMLStr(mailFormatDetails, userInfo);
String emailStatus = email.sendMail(mailXMLStr, "ITM", userInfo);
System.out.println("emailStatus....::" + emailStatus);
String msg = emailStatus.equals("S") ? "VTMAILSUCC" : "MAILFAILED";
retString = itmDBAccessEJB.getErrorString("", msg, "", "", conn);
return retString;
try {
BaseLogger.log("3", null, null, "Before sendMail method calling");
String emailStatus = email.sendMail(mailXMLStr, "ITM", userInfo);
// msg = emailStatus.equals("S") ? "VTMAILSUCC" : "MAILFAILED";
BaseLogger.log("3", null, null, "Email Statsus[" + emailStatus + "]");
retString = itmDBAccessEJB.getErrorString("", "VTMAILSUCC", "", "", conn);
} catch (Exception e) {
retString = itmDBAccessEJB.getErrorString("", "MAILFAILED", "", "", conn);
System.out.println("Exception in senMail method" + e.getMessage() + ":");
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Exception :SfaTime :itemChanged(Document,String):" + e.getMessage() + ":");
System.out.println("Exception in EmailSendToMe method" + e.getMessage() + ":");
e.printStackTrace();
} finally {
closeResources(rs, pstmt, conn);
......@@ -482,4 +450,21 @@ public class MarketingCampaignPreSend extends ActionHandlerEJB {
}
}
private void closeResources(ResultSet rs, PreparedStatement pstmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
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