diff --git a/caruna-invoice/build.gradle b/caruna-invoice/build.gradle index 683f10f..b3bda90 100644 --- a/caruna-invoice/build.gradle +++ b/caruna-invoice/build.gradle @@ -1 +1 @@ -application.mainClass='com.devsoap.parsers.caruna.Parser' \ No newline at end of file +application.mainClass='com.devsoap.parsers.caruna.CarunaParser' \ No newline at end of file diff --git a/caruna-invoice/src/main/java/com/devsoap/parsers/caruna/Parser.java b/caruna-invoice/src/main/java/com/devsoap/parsers/caruna/CarunaParser.java similarity index 94% rename from caruna-invoice/src/main/java/com/devsoap/parsers/caruna/Parser.java rename to caruna-invoice/src/main/java/com/devsoap/parsers/caruna/CarunaParser.java index 3d12861..4b96283 100644 --- a/caruna-invoice/src/main/java/com/devsoap/parsers/caruna/Parser.java +++ b/caruna-invoice/src/main/java/com/devsoap/parsers/caruna/CarunaParser.java @@ -5,6 +5,7 @@ import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDate; @@ -14,7 +15,7 @@ import java.time.format.TextStyle; import java.util.*; import java.util.regex.Pattern; -public class Parser { +public class CarunaParser { private static final Pattern DATE_RANGE_PATTERN = Pattern.compile("(\\d\\d?\\.\\d\\d?\\.\\d\\d\\d\\d) - (\\d\\d?\\.\\d\\d?\\.\\d\\d\\d\\d)"); private static final Pattern PERUSMAKSU_PATTERN = Pattern.compile("Perusmaksu.* (\\d*,\\d\\d) (EUR|€)"); @@ -55,15 +56,18 @@ public class Parser { public static void main(String[] args) { var filename = args[0]; var file = Paths.get(filename); + run(file, System.out); + } - System.out.println("Kuukausi,Perusmaksu (energia),Perusmaksu (siirto),Päiväenergia (kWh),Päiväenergia " + + public static void run(Path carunaFile, PrintStream result) { + result.println("Kuukausi,Perusmaksu (energia),Perusmaksu (siirto),Päiväenergia (kWh),Päiväenergia " + "(EUR),Yöenergia (kWh),Yöenergia (EUR),Päiväsiirto (kWh),Päiväsiirto (EUR),Yösiirto (kWh)" + ",Yösiirto (EUR),Vero"); - parse(file).forEach((n,p) -> { + parse(carunaFile).forEach((n,p) -> { var csv = String.format("%s,,%.02f,,,,,%d, %.02f,%d, %.02f, %.02f", n, p.basicPay, p.transferDayKwh, p.transferDayTotal, p.transferNightKwh, p.transferNightTotal, p.tax); - System.out.println(csv); + result.println(csv); }); } diff --git a/caruna-invoice/src/main/resources/com/devsoap/parsers/caruna/upload.html b/caruna-invoice/src/main/resources/com/devsoap/parsers/caruna/upload.html new file mode 100644 index 0000000..2607265 --- /dev/null +++ b/caruna-invoice/src/main/resources/com/devsoap/parsers/caruna/upload.html @@ -0,0 +1,10 @@ + + + +
+ + + +
+ + \ No newline at end of file diff --git a/composite-parsers/src/main/java/com/devsoap/parsers/composites/CarunaHelenParser.java b/composite-parsers/src/main/java/com/devsoap/parsers/composites/CarunaHelenParser.java index fb35c7a..51e289e 100644 --- a/composite-parsers/src/main/java/com/devsoap/parsers/composites/CarunaHelenParser.java +++ b/composite-parsers/src/main/java/com/devsoap/parsers/composites/CarunaHelenParser.java @@ -1,5 +1,7 @@ package com.devsoap.parsers.composites; +import com.devsoap.parsers.caruna.CarunaParser; + import java.io.PrintStream; import java.nio.file.Path; import java.util.HashSet; @@ -15,7 +17,7 @@ public class CarunaHelenParser { } public static void run(Path carunaFile, Path helenFile, PrintStream result) { - var carunaPeriods = com.devsoap.parsers.caruna.Parser.parse(carunaFile); + var carunaPeriods = CarunaParser.parse(carunaFile); var nightSiirto = carunaPeriods.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().transferNightKwh)); var daySiirto = carunaPeriods.entrySet().stream() @@ -30,7 +32,7 @@ public class CarunaHelenParser { months.addAll(helenPeriods.keySet()); months.forEach(month -> { var hp = helenPeriods.getOrDefault(month, new com.devsoap.parsers.helen.Parser.Period()); - var cp = carunaPeriods.getOrDefault(month, new com.devsoap.parsers.caruna.Parser.Period()); + var cp = carunaPeriods.getOrDefault(month, new CarunaParser.Period()); var csv = String.format("%s,%.02f,%.02f,%d,%.02f,%d,%.02f,%d,%.02f,%d,%.02f,%.02f", month, hp.basicPay, cp.basicPay, hp.dayEnergy, hp.dayEnergyEur, hp.nightEnergy, diff --git a/rest-api/build.gradle b/rest-api/build.gradle index ff408f4..b8f9ab6 100644 --- a/rest-api/build.gradle +++ b/rest-api/build.gradle @@ -2,6 +2,7 @@ dependencies { implementation 'io.javalin:javalin:3.13.8' implementation "org.thymeleaf:thymeleaf:3.0.11.RELEASE" + implementation project(':caruna-invoice') implementation project(':composite-parsers') implementation 'org.apache.logging.log4j:log4j-core:2.11.0' diff --git a/rest-api/src/main/java/com/devsoap/parsers/api/Api.java b/rest-api/src/main/java/com/devsoap/parsers/api/Api.java index 437ec7c..776e599 100644 --- a/rest-api/src/main/java/com/devsoap/parsers/api/Api.java +++ b/rest-api/src/main/java/com/devsoap/parsers/api/Api.java @@ -1,5 +1,6 @@ package com.devsoap.parsers.api; +import com.devsoap.parsers.caruna.CarunaParser; import com.devsoap.parsers.composites.CarunaHelenParser; import io.javalin.Javalin; import io.javalin.http.Context; @@ -37,17 +38,23 @@ public class Api { app.routes(() -> { get(ctx -> { ctx.result("PDF Parsers REST API"); }); path("parsers", () -> { + + path(CarunaParser.class.getSimpleName(), () -> { + get(ctx -> ctx.render(renderParserStaticPage(CarunaParser.class, "upload.html"))); + post(ctx -> { + withUploadedFile(ctx.uploadedFiles().get(0), carunaFile -> { + renderParserOutput(ctx, result -> CarunaParser.run(carunaFile, result), "caruna-report.csv"); + }); + }); + }); + path(CarunaHelenParser.class.getSimpleName(), () -> { get(ctx -> ctx.render(renderParserStaticPage(CarunaHelenParser.class, "upload.html"))); post(ctx -> { withUploadedFile(ctx.uploadedFiles().get(1), carunaFile -> { withUploadedFile(ctx.uploadedFiles().get(0), helenFile -> { - var resultStream = new ByteArrayOutputStream(); - try (var result = new PrintStream(resultStream)) { - CarunaHelenParser.run(carunaFile, helenFile, result); - } - renderReport(ctx, new ByteArrayInputStream(resultStream.toByteArray()), - "caruna-helen-report.csv"); + renderParserOutput(ctx, result -> + CarunaHelenParser.run(carunaFile, helenFile, result), "caruna-helen-report.csv"); }); }); }); @@ -56,6 +63,14 @@ public class Api { }); } + private static void renderParserOutput(Context ctx, Consumer parserCommand, String filename) { + var resultStream = new ByteArrayOutputStream(); + try (var result = new PrintStream(resultStream)) { + parserCommand.accept(result); + } + renderReport(ctx, new ByteArrayInputStream(resultStream.toByteArray()), filename); + } + private static void withUploadedFile(UploadedFile fileUpload, Consumer processor) { try { var file = Files.createTempFile(fileUpload.getFilename(), fileUpload.getExtension());