001package io.konik.csv.pdf; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileOutputStream; 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.OutputStream; 009 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013import io.konik.PdfHandler; 014import io.konik.csv.mapper.CsvInvoicesReader; 015import io.konik.csv.mapper.CsvInvoicesReader.ConvertedRow; 016import io.konik.csv.mapper.CsvInvoicesReader.Result; 017import io.konik.csv.model.Row; 018 019public class CsvToZUGFeRDConverter { 020 021 private static final Logger log = LoggerFactory.getLogger(CsvToZUGFeRDConverter.class); 022 private static final String DEFAULT_SUFFIX = "_zugferd.pdf"; 023 024 private final CsvInvoicesReader csvInvoicesReader; 025 private final PdfHandler pdfHandler; 026 027 public CsvToZUGFeRDConverter() { 028 this.csvInvoicesReader = new CsvInvoicesReader(); 029 this.pdfHandler = new PdfHandler(); 030 } 031 032 public CsvToZUGFeRDConverter(CsvInvoicesReader csvInvoicesReader, PdfHandler pdfHandler) { 033 this.csvInvoicesReader = csvInvoicesReader; 034 this.pdfHandler = pdfHandler; 035 } 036 037 public void convert(File csvFile, String inputPath, String outputPath) { 038 Result result = csvInvoicesReader.read(csvFile); 039 040 if (result != null) { 041 log.info("CSV file contains {} rows, {} errors", result.getConvertedRows().size(), 042 result.getRowErrors().size()); 043 044 for (final ConvertedRow convertedRow : result.getConvertedRows()) { 045 InputStream input = null; 046 OutputStream output = null; 047 048 log.info("Processing row {}", convertedRow.getRowNumber()); 049 050 try { 051 final Row row = convertedRow.getRow(); 052 053 if (isInputFilePresent(row)) { 054 log.info("Input file for given row present..."); 055 056 String inputFile = getFilePath(inputPath, row.getFile().getInput()); 057 input = new FileInputStream(inputFile); 058 log.info("Input file: {}", inputFile); 059 060 String outputName = row.getFile().getOutput(); 061 if (!(outputName != null && !outputName.isEmpty())) { 062 outputName = row.getFile().getInput().replaceFirst(".pdf", DEFAULT_SUFFIX); 063 } 064 outputName = getFilePath(outputPath, outputName); 065 output = new FileOutputStream(outputName); 066 log.info("Output file: {}", outputName); 067 068 log.info("Starting append invoice process..."); 069 pdfHandler.appendInvoice(convertedRow.getInvoice(), input, output); 070 log.info("Invoice appended to the output file"); 071 } 072 } catch (IOException e) { 073 log.warn("IOException caught: {}", e.getMessage()); 074 } finally { 075 closeStreams(input, output); 076 } 077 } 078 } 079 } 080 081 private static String getFilePath(final String inputPath, final String inputFile) { 082 String result = inputFile; 083 084 if (inputPath != null && !inputPath.isEmpty()) { 085 String path = inputPath; 086 String file = inputFile; 087 088 if (!path.endsWith("/")) { 089 path = String.format("%s/", path); 090 } 091 if (file.startsWith("/")) { 092 file = file.substring(1, file.length() - 1); 093 } 094 095 result = String.format("%s%s", path, file); 096 } 097 098 return result; 099 } 100 101 public void convert(File csvFile) { 102 convert(csvFile, null, null); 103 } 104 105 private boolean isInputFilePresent(Row row) { 106 return row != null && row.getFile().getInput() != null; 107 } 108 109 private void closeStreams(InputStream input, OutputStream output) { 110 try { 111 if (input != null) { 112 input.close(); 113 } 114 115 if (output != null) { 116 output.close(); 117 } 118 } catch (IOException e) { 119 log.warn("IOException caught while closing input or output: {}", e.getMessage()); 120 } 121 } 122 123 public static void main(String[] args) { 124 String inputPath = System.getProperty("inputPath"); 125 String outputPath = System.getProperty("outputPath"); 126 String csvFileName = args[0]; 127 128 File csvFile = new File(csvFileName); 129 130 if (!csvFile.exists()) { 131 throw new IllegalArgumentException(String.format("Csv file with name %s does not exist", csvFileName)); 132 } 133 134 log.info("----------------------------------------------------------"); 135 log.info("CSV file:\t\t{}", csvFile.getAbsolutePath()); 136 log.info("Input path:\t{}", inputPath); 137 log.info("Output path:\t{}", outputPath); 138 log.info("----------------------------------------------------------"); 139 140 CsvToZUGFeRDConverter converter = new CsvToZUGFeRDConverter(); 141 converter.convert(csvFile, inputPath, outputPath); 142 } 143}