Skip to content

Commit

Permalink
email dir filter wkfl option
Browse files Browse the repository at this point in the history
  • Loading branch information
vaughnte committed Mar 10, 2023
1 parent d9ab203 commit 9887baf
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/com/blueseer/edi/WorkFlowMaint.java
Expand Up @@ -543,6 +543,7 @@ public void setddactions() {
ddactions.addItem("APICall");
ddactions.addItem("X12DirFilter");
ddactions.addItem("TrafficDir");
ddactions.addItem("EmailDir");

for (int i = 0; i < ddactions.getItemCount(); i++) {

Expand Down Expand Up @@ -615,6 +616,15 @@ public void setddactions() {
x.add(new String[]{"tffile", ""});
kvs.put("TrafficDir", x);
}
if (i == 10) {
ArrayList<String[]> x = new ArrayList<String[]>();
x.add(new String[]{"indir", ""});
x.add(new String[]{"logfile", ""});
x.add(new String[]{"tffile", ""});
x.add(new String[]{"archdir", ""});
x.add(new String[]{"smtpfrom", ""});
kvs.put("EmailDir", x);
}
}

}
Expand Down
117 changes: 114 additions & 3 deletions src/com/blueseer/edi/ediData.java
Expand Up @@ -33,13 +33,16 @@ of this software and associated documentation files (the "Software"), to deal
import static bsmf.MainFrame.user;
import static com.blueseer.edi.EDILoad.runTranslationSingleFile;
import static com.blueseer.edi.apiUtils.runAPICall;
import static com.blueseer.edi.wfUtils.emailDir;
import static com.blueseer.edi.wfUtils.filterDir;
import static com.blueseer.edi.wfUtils.trafficDir;
import com.blueseer.utl.BlueSeerUtils;
import static com.blueseer.utl.BlueSeerUtils.ConvertIntToYesNo;
import static com.blueseer.utl.BlueSeerUtils.ConvertStringToBool;
import static com.blueseer.utl.BlueSeerUtils.bsret;
import static com.blueseer.utl.BlueSeerUtils.getMessageTag;
import static com.blueseer.utl.OVData.isSMTPServer;
import static com.blueseer.utl.OVData.isSMTPServerBool;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.nio.file.DirectoryStream;
Expand Down Expand Up @@ -1041,7 +1044,7 @@ public static wkf_mstr getWkfMstr(String x) {
}

public static int writeWFLog(wkf_log wkfl, int origparentid, ArrayList<wkfd_log> wkfdl) {

boolean isError = false;
int parentid = -1;
Connection bscon = null;
PreparedStatement ps = null;
Expand All @@ -1058,6 +1061,10 @@ public static int writeWFLog(wkf_log wkfl, int origparentid, ArrayList<wkfd_log>
if (wkfdl != null) {
for (wkfd_log z : wkfdl) {
_addWkfDetlog(parentid, z, bscon, ps, res);
isError = (z.wkfdl_status().equals("1")) ? true : false;
}
if (isError) {
_updateWkfLogStatus(parentid, bscon, ps, "1");
}
}
}
Expand Down Expand Up @@ -1114,6 +1121,17 @@ public static void updateWFLog(int id, String status, String message, String ref
}
}

private static void _updateWkfLogStatus(int id, Connection con, PreparedStatement ps, String status) throws SQLException {

String sql = "update wkf_log set wkfl_status = ? " +
" where wkfl_id = ? ";
ps = con.prepareStatement(sql);
ps.setString(1, status);
ps.setString(2, String.valueOf(id));
ps.executeUpdate();
}


private static int _addWkfLog(wkf_log x, Connection con, PreparedStatement ps, ResultSet res) throws SQLException {
int returnkey = 0;
String sqlInsert = "insert into wkf_log (wkfl_job, wkfl_desc, wkfl_ref, wkfl_status, wkfl_messg "
Expand Down Expand Up @@ -2798,7 +2816,7 @@ public static ArrayList<String> getAllPKSKeysExceptStore() {
}

public static String[] processWorkFlowID(String id) {

// String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
wkf_mstr wkf = getWkfMstr(id);

Expand Down Expand Up @@ -2834,11 +2852,22 @@ public static String[] processWorkFlowID(String id) {

switch (wkd.wkfd_action()) {

case "EmailDir" :
r = wkfaction_emaildirectory(wkd, getWkfdMeta(wkd.wkfd_id(), wkd.wkfd_line()));
lgd[3] = r[0];
lgd[4] = r[1];
if (! r[0].equals("0")) {
logdetail.add(lgd);
break forloop;
}
break;

case "TrafficDir" :
r = wkfaction_trafficdirectory(wkd, getWkfdMeta(wkd.wkfd_id(), wkd.wkfd_line()));
lgd[3] = r[0];
lgd[4] = r[1];
if (! r[0].equals("0")) {
logdetail.add(lgd);
break forloop;
}
break;
Expand All @@ -2848,6 +2877,7 @@ public static String[] processWorkFlowID(String id) {
lgd[3] = r[0];
lgd[4] = r[1];
if (! r[0].equals("0")) {
logdetail.add(lgd);
break forloop;
}
break;
Expand All @@ -2857,6 +2887,7 @@ public static String[] processWorkFlowID(String id) {
lgd[3] = r[0];
lgd[4] = r[1];
if (! r[0].equals("0")) {
logdetail.add(lgd);
break forloop;
}
break;
Expand Down Expand Up @@ -2918,6 +2949,7 @@ public static String[] processWorkFlowID(String id) {
logdetail.add(lgd);
}

boolean isError = false;
String status = "";
ArrayList<wkfd_log> list = new ArrayList<wkfd_log>();
for (String[] s : logdetail) {
Expand All @@ -2933,8 +2965,9 @@ public static String[] processWorkFlowID(String id) {
list.add(x);
if (! s[3].equals("0")) {
status = s[4];
isError = true;
}
}
} // for each log detail

writeWFLog(wkfl,logid,list);

Expand Down Expand Up @@ -3065,6 +3098,84 @@ public static String[] wkfaction_trafficdirectory(wkf_det wkfd, ArrayList<wkfd_m
return r;
}

public static String[] wkfaction_emaildirectory(wkf_det wkfd, ArrayList<wkfd_meta> list) {
String[] r = new String[]{"0",""};

String indir = "";
String logfile = "";
String tffile = "";
String archdir = "";
String smtpfrom = "";

for (wkfd_meta m : list) {
if (m.wkfdm_key().equals("indir")) {
indir = m.wkfdm_value();
}

if (m.wkfdm_key().equals("logfile")) {
logfile = m.wkfdm_value();
}

if (m.wkfdm_key().equals("tffile")) {
tffile = m.wkfdm_value();
}

if (m.wkfdm_key().equals("archdir")) {
archdir = m.wkfdm_value();
}

if (m.wkfdm_key().equals("smtpfrom")) {
smtpfrom = m.wkfdm_value();
}
}



if (smtpfrom.isEmpty()) {
r[0] = "1";
r[1] = "ERROR WorkFlowID: " + wkfd.wkfd_id + " action: " + wkfd.wkfd_action + "->" + "must supply a legitimate from email address ";
return r;
}

Path indirpath = FileSystems.getDefault().getPath(indir);
if (indir.isEmpty() || ! Files.exists(indirpath)) {
r[0] = "1";
r[1] = "ERROR WorkFlowID: " + wkfd.wkfd_id + " action: " + wkfd.wkfd_action + "->" + "indir path does not exist ";
return r;
}

Path archdirpath = FileSystems.getDefault().getPath(archdir);
if (archdir.isEmpty() || ! Files.exists(archdirpath)) {
r[0] = "1";
r[1] = "ERROR WorkFlowID: " + wkfd.wkfd_id + " action: " + wkfd.wkfd_action + "->" + "archdir path does not exist ";
return r;
}


Path tffilepath = FileSystems.getDefault().getPath(tffile);
if (tffile.isEmpty() || ! Files.exists(tffilepath)) {
r[0] = "1";
r[1] = "ERROR WorkFlowID: " + wkfd.wkfd_id + " action: " + wkfd.wkfd_action + "->" + "tffile path does not exist ";
return r;
}


if (! isSMTPServerBool()) {
r[0] = "1";
r[1] = "ERROR WorkFlowID: " + wkfd.wkfd_id + " action: " + wkfd.wkfd_action + "->" + "Missing SMTP server/auth info ";
return r;
}

try {
r = emailDir(indir, logfile, tffile, archdir, smtpfrom);
} catch (IOException ex) {
r[0] = "1";
r[1] = "IOException occurred: " + ex.getMessage();
}

return r;
}


public static String[] wkfaction_filecopy(wkf_det wkfd, ArrayList<wkfd_meta> list) {
String[] r = new String[]{"0",""};
Expand Down
116 changes: 116 additions & 0 deletions src/com/blueseer/edi/wfUtils.java
Expand Up @@ -27,6 +27,8 @@ of this software and associated documentation files (the "Software"), to deal

import static com.blueseer.edi.EDIbs.dfdate;
import static com.blueseer.edi.EDIbs.now;
import static com.blueseer.utl.OVData.sendEmailwSession;
import static com.blueseer.utl.OVData.setEmailSession;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -45,6 +47,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import javax.mail.Session;

/**
*
Expand Down Expand Up @@ -328,5 +331,118 @@ public boolean accept(File f) {
return r;
}

public static String[] emailDir(String indir, String logfile, String tffile, String archdir, String from) throws IOException {

String[] r = new String[]{"0",""};

Path logpath = FileSystems.getDefault().getPath(logfile);
BufferedWriter log = new BufferedWriter(new FileWriter(logpath.toFile(), true));

String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
ArrayList<String[]> trafficarray = new ArrayList<String[]>();
File tf = new File(tffile);
if(tf.exists()) {
FileReader reader = new FileReader(tf);
BufferedReader br = new BufferedReader(reader);
String line = "";
while ((line = br.readLine()) != null) {
trafficarray.add(line.split(":", -1)); // (pattern,destdir,archdir,singlefilename,deleteOrignal)
}
br.close();
reader.close();
} else {
log.write(now + " No Traffic File");
log.write("\n");
log.close();
r[0] = "1";
r[1] = now + " No Traffic File";
return r;
}

// check trafficarray...make sure 5 elements per row
// (pattern,destdir,archdir,singlefilename,deleteOrignal)
boolean isBad = false;
for (String[] s : trafficarray) {
if (s.length != 3) {
isBad = true;
}
}
if (isBad) {
log.write(now + " Bad Format Traffic File");
log.write("\n");
log.close();
r[0] = "1";
r[1] = now + " Bad Format Traffic File";
return r;
}


FileFilter byfiletype = new FileFilter() {
@Override
public boolean accept(File f) {
// return name.endsWith(".txt");
return f.isFile();
}
};

// OK...now let's loop through indir for files

try {
File folder = new File(indir);
File[] listOfFiles = folder.listFiles(byfiletype);

if (listOfFiles.length == 0) {
log.write(now + " " + listOfFiles.length + " No files to process");
log.write("\n");
log.close();
r[0] = "0";
r[1] = now + " " + listOfFiles.length + " No files to process";
return r;
}

r[1] = now + " " + " File Count=" + listOfFiles.length;
log.write(now + " " + " File Count=" + listOfFiles.length );
log.write("\n");

boolean sent = false;
String message = "This is an automated email alert." + '\n';

Session session = setEmailSession();

for (int i = 0; i < listOfFiles.length; i++) {
sent = false;
if (listOfFiles[i].isFile()) {
for (String[] e : trafficarray) {
if (listOfFiles[i].getName().contains(e[0])) {
// found match
sent = true;
Path filepath = FileSystems.getDefault().getPath(indir + listOfFiles[i].getName());
Path archpath = FileSystems.getDefault().getPath(archdir + listOfFiles[i].getName() + "." + now);
log.write(now + " sending email for file: " + listOfFiles[i].getName() + " to " + e[1] + "\n");
log.write("\n");
sendEmailwSession(session, from, e[2], e[1], message, filepath.toString());
Files.copy(filepath, archpath, StandardCopyOption.REPLACE_EXISTING);
Files.delete(filepath);
break;
}

}

}
if (! sent) {
log.write(now + " no match found in config file...skipping " + listOfFiles[i].getName() + "\n");
log.write("\n");
}
}
} catch (IOException ex) {
ex.printStackTrace();
log.write(now + " " + " IOException" );
log.write("\n");
}

log.close();
return r;
}


}

0 comments on commit 9887baf

Please sign in to comment.