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