Added extra checks for file format when verifying

This commit is contained in:
Keith Armstrong 2025-01-04 08:33:21 -05:00
parent 61f3968aaf
commit a6abf63e8d
2 changed files with 45 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package com.belkast.soap;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord; import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.DuplicateHeaderMode;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
@ -14,15 +15,22 @@ public class userVerify
System.out.println(); System.out.println();
Scanner myObj = new Scanner(System.in); // Create a Scanner object Scanner myObj = new Scanner(System.in); // Create a Scanner object
debugProcessor.writer(false, "");
debugProcessor.writer(true, "SOAP Client (03.01.2025 build) - Belkast Consulting (c) 2025");
debugProcessor.writer(true, "");
String varPrompt = "Please enter the name of the CSV file to validate : ";
String varFileName; String varFileName;
while (true) { while (true)
System.out.print("Please enter the CSV filename to validate : "); {
varFileName = myObj.nextLine(); // Read user input System.out.print(varPrompt);
varFileName = myObj.nextLine();
File thisFile = new File(varFileName); File thisFile = new File(varFileName);
if (thisFile.exists()) Boolean isValidFile = fileProcessor.validFile(varFileName);
if ( thisFile.exists() && isValidFile )
{ {
break; break;
} }
varPrompt = "\nEither the CSV file does not exist or the CSV file cannot be read!\nPlease enter the name of the CSV file to validate : ";
} }
Boolean varUserBlockEmpty = false; Boolean varUserBlockEmpty = false;
@ -34,22 +42,26 @@ public class userVerify
valueMap.put("N", false); valueMap.put("N", false);
valueMap.put("n", false); valueMap.put("n", false);
while (true) { while (true)
System.out.print("Invalidate the line on empty column values? (Y/n) : "); {
System.out.print("Invalidate a line if there are empty column values? (Y/n) : ");
String varAnswer = myObj.nextLine(); String varAnswer = myObj.nextLine();
String varSanitized = varAnswer.toUpperCase().trim();
if (varAnswer.toUpperCase().equals("Y") || varAnswer.toUpperCase().equals("N") || varAnswer == "") if (varSanitized.equals("Y") || varSanitized.equals("N") || varSanitized.isEmpty())
{ {
varUserBlockEmpty = valueMap.get(varAnswer); varUserBlockEmpty = valueMap.get(varSanitized);
break; break;
} }
} }
System.out.println(); System.out.println();
System.out.println("CSV filename to validate : " + varFileName); debugProcessor.writer(true, "CSV filename to validate : " + varFileName);
System.out.println("CSV invalidate on empty : " + varUserBlockEmpty); debugProcessor.writer(true, "CSV invalidate on empty : " + varUserBlockEmpty);
debugProcessor.writer(true, "");
ArrayList thisCSV = readCSV(varFileName, true, varUserBlockEmpty); ArrayList thisCSV = readCSV(varFileName, true, varUserBlockEmpty);
System.out.println(); debugProcessor.writer(true, "");
Iterator<Map> mapper1 = thisCSV.iterator(); Iterator<Map> mapper1 = thisCSV.iterator();
int varIter = 0; int varIter = 0;
@ -61,13 +73,13 @@ public class userVerify
while (it.hasNext()) while (it.hasNext())
{ {
Map.Entry<Integer, Integer> pair = it.next(); Map.Entry<Integer, Integer> pair = it.next();
System.out.println("record " + varIter + " : [" + pair.getKey() + "] => " + pair.getValue()); debugProcessor.writer(true, "record " + varIter + " : [" + pair.getKey() + "] => " + pair.getValue());
} }
System.out.println(); debugProcessor.writer(true, "");
} }
boolean validfile = (!thisCSV.isEmpty()); boolean validfile = (!thisCSV.isEmpty());
System.out.println("CSV file records : " + varIter); debugProcessor.writer(true, "CSV file records : " + varIter);
System.out.println("CSV file is valid : " + validfile); debugProcessor.writer(true, "CSV file is valid : " + validfile);
System.out.println(); System.out.println();
} }
@ -77,7 +89,8 @@ public class userVerify
int invalidNum = 0; int invalidNum = 0;
ArrayList finalArray = new ArrayList(); ArrayList finalArray = new ArrayList();
File varFile = new File (thisFile); File varFile = new File (thisFile);
if (!varFile.exists()) { if (!varFile.exists())
{
System.out.println("CSV failure error : File not found. Please enter a valid filename!"); System.out.println("CSV failure error : File not found. Please enter a valid filename!");
System.exit(0); System.exit(0);
} }
@ -92,7 +105,8 @@ public class userVerify
{ {
String varHeader = new String(); String varHeader = new String();
varHeader = fileProcessor.readFirstLine(thisFile); varHeader = fileProcessor.readFirstLine(thisFile);
if (varHeader.length() == 0) { if (varHeader.length() == 0)
{
System.out.println("CSV failure error : Header line not found!"); System.out.println("CSV failure error : Header line not found!");
return finalArray; return finalArray;
} }
@ -105,6 +119,7 @@ public class userVerify
.setIgnoreSurroundingSpaces(true) .setIgnoreSurroundingSpaces(true)
.setIgnoreHeaderCase(true) .setIgnoreHeaderCase(true)
.setTrim(true) .setTrim(true)
.setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)
.build(); .build();
Iterable<CSVRecord> records = csvFormat.parse(in); Iterable<CSVRecord> records = csvFormat.parse(in);
@ -114,7 +129,8 @@ public class userVerify
debugProcessor.writer(fromUser, "CSV header tokens : " + varHeader); debugProcessor.writer(fromUser, "CSV header tokens : " + varHeader);
debugProcessor.writer(fromUser, ""); debugProcessor.writer(fromUser, "");
for (CSVRecord record : records) { for (CSVRecord record : records)
{
readNum++; readNum++;
lineNum = record.getParser().getCurrentLineNumber(); lineNum = record.getParser().getCurrentLineNumber();
@ -166,13 +182,12 @@ public class userVerify
parsedCorrectly = true; parsedCorrectly = true;
} }
if (readNum > 0) debugProcessor.writer(fromUser,"");
{ debugProcessor.writer(fromUser, "CSV total lines : " + fileProcessor.numberOfLines(thisFile));
debugProcessor.writer(fromUser,""); debugProcessor.writer(fromUser, "CSV records read : " + String.valueOf(readNum));
} debugProcessor.writer(fromUser, "CSV records passed : " + String.valueOf(readNum - invalidNum));
debugProcessor.writer(fromUser, "CSV lines read : " + String.valueOf(readNum)); debugProcessor.writer(fromUser, "CSV records failed : " + invalidNum);
debugProcessor.writer(fromUser, "CSV lines passed : " + String.valueOf(readNum - invalidNum));
debugProcessor.writer(fromUser, "CSV lines failed : " + invalidNum);
if (exceptionReason != null) if (exceptionReason != null)
{ {
debugProcessor.writer(fromUser, "CSV failure error : " + exceptionReason); debugProcessor.writer(fromUser, "CSV failure error : " + exceptionReason);

View File

@ -17,7 +17,11 @@ public class webService {
static String varKey; static String varKey;
static String varSalt = "1zcHJvdG9ubWFpbC9icmlkZ2UtdjMvdXNlcnMvYnJpZGdlLXZhdWx0LWtleQl0gx"; static String varSalt = "1zcHJvdG9ubWFpbC9icmlkZ2UtdjMvdXNlcnMvYnJpZGdlLXZhdWx0LWtleQl0gx";
public static void main(String... argv) throws Exception { public static void main(String... argv) throws Exception
{
debugProcessor.writer(true, "");
debugProcessor.writer(true, "SOAP Client (03.01.2025 build) - Belkast Consulting (c) 2025");
debugProcessor.writer(true, "");
// @Parameter(names = "--props", // @Parameter(names = "--props",
// description = "Location of the Properties file") // description = "Location of the Properties file")