1
0
Fork 0

Add REST API for Caruna parser

This commit is contained in:
John Ahlroos 2021-07-02 16:36:38 +03:00
parent b1f4a74152
commit bec6898e38
Signed by: john
GPG Key ID: 258D0F70DB84CD5D
6 changed files with 45 additions and 13 deletions

View File

@ -1 +1 @@
application.mainClass='com.devsoap.parsers.caruna.Parser'
application.mainClass='com.devsoap.parsers.caruna.CarunaParser'

View File

@ -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);
});
}

View File

@ -0,0 +1,10 @@
<meta charset="UTF-8">
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="carunaFile">Caruna File:</label>
<input type="file" id="carunaFile" name="caruna.pdf" />
<input type="submit" value="Submit files">
</form>
</body>
</html>

View File

@ -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,

View File

@ -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'

View File

@ -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<PrintStream> 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<Path> processor) {
try {
var file = Files.createTempFile(fileUpload.getFilename(), fileUpload.getExtension());