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 8e17b32..c513548 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 @@ -750,4 +750,14 @@ public class MongoPprUtils { } return speechIds; } + + public static List getAllSpeechesWithTopic(String topic) { + List speechIds = new ArrayList<>(); + Document filter = new Document("analysisResults.topics.topic", topic); + 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 6f357c6..3e4318b 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 @@ -86,7 +86,7 @@ public class TeXUtil { return convertTexToBase64PDF(tex.toString()); } - public static String getBulkExportedAllSpeechBase64String() throws IOException, InterruptedException { + public static String getBulkExportedAllSpeechesBase64String() throws IOException, InterruptedException { // Fetch all speechIDs from the speaker List speechIds = getAllSpeeches(); @@ -104,6 +104,24 @@ public class TeXUtil { return convertTexToBase64PDF(tex.toString()); } + public static String getBulkExportedAllSpeechesWithTopicBase64String(String topic) throws IOException, InterruptedException { + // Fetch all speechIDs from the speaker + List speechIds = getAllSpeechesWithTopic(topic); + + StringBuilder tex = new StringBuilder(); + + tex.append(PREAMBLE.replace("$$EXPORTCATEGORY$$", "Speeches with topic " + topic)); + tex.append(BEGIN_DOCUMENT); + tex.append(TABLEOFCONTENTS); + for (Speech speech : speechIds) { + tex.append(getSpeechToTexComponent(speech)); + tex.append(NEWPAGE); + } + tex.append(END_DOCUMENT); + + return convertTexToBase64PDF(tex.toString()); + } + public static String convertTexToBase64PDF(String tex) throws IOException, InterruptedException { // Create a temporary directory File tempDir = new File(TEMP_EXPORT_DIR); 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 11c3b3a..f30d669 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 @@ -68,7 +68,7 @@ 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/topic/{topic}", 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 79ba6a0..2930fc7 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 @@ -114,7 +114,7 @@ public class SpeechesExportController { public static void exportAllSpeeches(Context ctx) { byte[] pdfBytes = new byte[0]; try { - pdfBytes = Base64.getDecoder().decode(getBulkExportedAllSpeechBase64String()); + pdfBytes = Base64.getDecoder().decode(getBulkExportedAllSpeechesBase64String()); } catch (Exception e) { Logger.error("Failed to generate Export of all Speeches"); Logger.error(e.getMessage()); @@ -144,4 +144,48 @@ public class SpeechesExportController { Logger.debug(Arrays.toString(e.getStackTrace())); } } + + @OpenApi( + summary = "Get all speeches with specific topic as a PDF", + description = "Returns a LaTeX generated pdf of all speeches with specific topic", + operationId = "getAllSpeechesWithTopic", + path = "/export/topic/{topic}", + methods = HttpMethod.GET, + tags = {"Export", "Speeches", "PDF"}, + responses = { + @OpenApiResponse(status = "200") + }) + public static void exportSpeechesWithTopic(Context ctx) { + byte[] pdfBytes = new byte[0]; + try { + pdfBytes = Base64.getDecoder().decode(getBulkExportedAllSpeechesWithTopicBase64String(ctx.pathParam("topic"))); + } catch (Exception e) { + Logger.error("Failed to generate Export of all Speeches with Topic " + ctx.pathParam("topic")); + 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())); + } + } }