From 320f4dbdc25f44852a773f0fba47dd82af5c350c Mon Sep 17 00:00:00 2001 From: s5260822 Date: Sat, 22 Mar 2025 22:20:54 +0100 Subject: [PATCH] added request for all speeches exort and improved fetching speeds --- .../gruppe_05_1/database/MongoPprUtils.java | 16 +++++-- .../project/gruppe_05_1/export/TeXUtil.java | 35 ++++++++++++-- .../project/gruppe_05_1/rest/RESTHandler.java | 1 + .../rest/SpeechesExportController.java | 46 ++++++++++++++++++- 4 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java index ce9e17e..8e17b32 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java @@ -731,12 +731,22 @@ public class MongoPprUtils { Logger.info("Updating Metadata Collection: end"); } - public static List getSpeechIdsBySpeakerId(String speakerId) { - List speechIds = new ArrayList<>(); + public static List getSpeechesBySpeakerId(String speakerId) { + List speechIds = new ArrayList<>(); Document filter = new Document("speakerId", Integer.parseInt(speakerId)); List docs = getSpeechCollection().find(filter).into(new ArrayList<>()); for (Document doc : docs) { - speechIds.add(doc.getString("speechKey")); + speechIds.add(new Speech_MongoDB_Impl(doc, true)); + } + return speechIds; + } + + public static List getAllSpeeches() { + List speechIds = new ArrayList<>(); + Document filter = new Document(); + List docs = getSpeechCollection().find(filter).into(new ArrayList<>()); + for (Document doc : docs) { + speechIds.add(new Speech_MongoDB_Impl(doc, true)); } return speechIds; } diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/export/TeXUtil.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/export/TeXUtil.java index 4b27306..6f357c6 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/export/TeXUtil.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/export/TeXUtil.java @@ -52,6 +52,17 @@ public class TeXUtil { return tex.toString().replace("$$SPEAKERINFO$$", speaker.toTeX()); } + public static String getSpeechToTexComponent(Speech speech) { + createTempDir(); + Speaker_MongoDB_Impl speaker = getSpeakerById(String.valueOf(speech.getSpeakerId())); + + StringBuilder tex = new StringBuilder(); + + tex.append(speech.toTeX()); + + return tex.toString().replace("$$SPEAKERINFO$$", speaker.toTeX()); + } + public static String getExportedSpeechBase64StringBySpeechId(String speechId) throws IOException, InterruptedException { // Read preamble from resources directory tex/preamble.tex return convertTexToBase64PDF(PREAMBLE.replace("$$EXPORTCATEGORY$$", "Speech " + speechId) + BEGIN_DOCUMENT + getSpeechToTexComponent(speechId) + END_DOCUMENT); @@ -59,15 +70,33 @@ public class TeXUtil { public static String getBulkExportedSpeechBase64StringFromSpeakerById(String speakerId) throws IOException, InterruptedException { // Fetch all speechIDs from the speaker - List speechIds = getSpeechIdsBySpeakerId(speakerId); + List speechIds = getSpeechesBySpeakerId(speakerId); StringBuilder tex = new StringBuilder(); tex.append(PREAMBLE.replace("$$EXPORTCATEGORY$$", "Speaker ID" + speakerId)); tex.append(BEGIN_DOCUMENT); tex.append(TABLEOFCONTENTS); - for (String speechId : speechIds) { - tex.append(getSpeechToTexComponent(speechId)); + for (Speech speech : speechIds) { + tex.append(getSpeechToTexComponent(speech)); + tex.append(NEWPAGE); + } + tex.append(END_DOCUMENT); + + return convertTexToBase64PDF(tex.toString()); + } + + public static String getBulkExportedAllSpeechBase64String() throws IOException, InterruptedException { + // Fetch all speechIDs from the speaker + List speechIds = getAllSpeeches(); + + StringBuilder tex = new StringBuilder(); + + tex.append(PREAMBLE.replace("$$EXPORTCATEGORY$$", "all speeches")); + tex.append(BEGIN_DOCUMENT); + tex.append(TABLEOFCONTENTS); + for (Speech speech : speechIds) { + tex.append(getSpeechToTexComponent(speech)); tex.append(NEWPAGE); } tex.append(END_DOCUMENT); diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/RESTHandler.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/RESTHandler.java index d3d9691..11c3b3a 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/RESTHandler.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/RESTHandler.java @@ -69,5 +69,6 @@ public class RESTHandler { app.get("/export/speech/{id}", SpeechesExportController::exportSpeech); // exportiere eine Rede als PDF app.get("/export/speaker/{id}", SpeechesExportController::exportSpeechesFromSpeaker); // exportiere alle Reden eines Parlamentariers als PDF //app.get("/topic/{topic}/export", SpeechesExportController::exportSpeechesWithTopic); // exportiere alle Reden zu einem Thema als PDF + app.get("/export/all", SpeechesExportController::exportAllSpeeches); // exportiere alle Reden als PDF CAUTION!!!: This will take forever but is required in the exercise } } diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/SpeechesExportController.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/SpeechesExportController.java index 8a156b4..79ba6a0 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/SpeechesExportController.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/rest/SpeechesExportController.java @@ -59,7 +59,7 @@ public class SpeechesExportController { @OpenApi( summary = "Get all speeches from a speaker as a PDF", - description = "Returns a LaTeX generated pdf of all speaches of a selected speech", + description = "Returns a LaTeX generated pdf of all speeches of a selected speech", operationId = "getSpeechesFromSpeakerExport", path = "/export/speaker/{id}", methods = HttpMethod.GET, @@ -100,4 +100,48 @@ public class SpeechesExportController { Logger.debug(Arrays.toString(e.getStackTrace())); } } + + @OpenApi( + summary = "Get all speeches as a PDF", + description = "Returns a LaTeX generated pdf of all speeches", + operationId = "getAllSpeeches", + path = "/export/all", + methods = HttpMethod.GET, + tags = {"Export", "Speeches", "PDF"}, + responses = { + @OpenApiResponse(status = "200") + }) + public static void exportAllSpeeches(Context ctx) { + byte[] pdfBytes = new byte[0]; + try { + pdfBytes = Base64.getDecoder().decode(getBulkExportedAllSpeechBase64String()); + } catch (Exception e) { + Logger.error("Failed to generate Export of all Speeches"); + Logger.error(e.getMessage()); + Logger.debug(Arrays.toString(e.getStackTrace())); + } + + // Set the response content type to PDF + ctx.contentType("application/pdf"); + + ByteArrayInputStream stream = new ByteArrayInputStream(pdfBytes); + if (stream.available() == 0) { + Logger.error("PDF stream is empty."); + ctx.result("Internal Server Error"); + ctx.status(500); + return; + } + // Send the PDF as a response + ctx.result(stream); + + try { + // delete the temporary folder + deleteTeXTempDirContents(); + Logger.debug("Temporary folder deleted."); + } catch (IOException e) { + Logger.error("Failed to delete temporary folder."); + Logger.error(e.getMessage()); + Logger.debug(Arrays.toString(e.getStackTrace())); + } + } }