Skip to Main Content

Java User Groups

Announcement

Testing banner

Java - if records are invalid records, write to a excel file

4281104Jul 2 2020 — edited Sep 17 2020

My code validates if the file has valid records or invalid records**(see invalidApplicantTypeRecords)** if the records are invalid, I need to write it to a file. how do I do that in this code

public class EdcCebaProcessor {

  
public void startProcessing()
  
{
  
List<String> fileNames = inputFileHandler.fetchInputFileNames();
  
for (String fileName : fileNames) {
  
OutputRecords outputRecords = generateOutputRecords(fileName);
  
List<EDCOutputRecord> listOfEdcOutputRecords = outputRecords.getValidOutputRecords();
  
List<EDCOutputRecord> listOfInvalidApplicantTypeRecords = outputRecords.getInvalidApplicantTypeRecord();
  outputFileWriter
.writeIntoFile(listOfEdcOutputRecords);
  inputFileHandler
.moveInputFileToProcessedDirectory(fileName);
  
}
  
}
  
private OutputRecords generateOutputRecords(
  
String fileLocation)
  
{
  
ArrayList<CSVRecord> csvRecords;
  
List<EDCOutputRecord> outputRecords = new ArrayList<>();
  
List<EDCOutputRecord> invalidApplicantTypeRecords = new ArrayList<>();
  
try {
  
EDCOutputRecord outputRecord = null;
  csvRecords
= (ArrayList<CSVRecord>) this.csvRecordsParser.getCSVRecordsFromFile(fileLocation);
  
for (CSVRecord record : csvRecords) {
  
try {
  edcRecordValidator
.validateRecord(record);
  outputRecord
= edcOutputRecordCreator.buildValidRecord(new EDCInputRecord(record));
  
} catch (FieldValidationException e) {
  
if (e.getFailedValidationField().equals(EDCRecordField.APPLICANT_TYPE)) {
  log
.error("Invalid Applicant Type for the record number: " + record.getRecordNumber());
  outputRecord
= edcOutputRecordCreator.buildInvalidBNFormatRecord(new EDCInputRecord(record));
  
// Object to keep track of the Invalid Applicant Type Values.
  invalidApplicantTypeRecords
  
.add(edcOutputRecordCreator.buildInvalidBNFormatRecord(new EDCInputRecord(record)));
  
}
  
}
  outputRecords
.add(outputRecord);
  
}
  
}
  
return new OutputRecords(outputRecords, invalidApplicantTypeRecords);
  
}
}

Comments
Post Details
Added on Jul 2 2020
1 comment
69 views