added export based on Topic

This commit is contained in:
s5260822 2025-03-22 22:33:27 +01:00
parent 320f4dbdc2
commit dabac9e316
4 changed files with 75 additions and 3 deletions

View file

@ -750,4 +750,14 @@ public class MongoPprUtils {
}
return speechIds;
}
public static List<Speech> getAllSpeechesWithTopic(String topic) {
List<Speech> speechIds = new ArrayList<>();
Document filter = new Document("analysisResults.topics.topic", topic);
List<Document> docs = getSpeechCollection().find(filter).into(new ArrayList<>());
for (Document doc : docs) {
speechIds.add(new Speech_MongoDB_Impl(doc, true));
}
return speechIds;
}
}

View file

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

View file

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

View file

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