added request for all speeches exort and improved fetching speeds

This commit is contained in:
s5260822 2025-03-22 22:20:54 +01:00
parent 22ea6f047c
commit 320f4dbdc2
4 changed files with 91 additions and 7 deletions

View file

@ -731,12 +731,22 @@ public class MongoPprUtils {
Logger.info("Updating Metadata Collection: end");
}
public static List<String> getSpeechIdsBySpeakerId(String speakerId) {
List<String> speechIds = new ArrayList<>();
public static List<Speech> getSpeechesBySpeakerId(String speakerId) {
List<Speech> speechIds = new ArrayList<>();
Document filter = new Document("speakerId", Integer.parseInt(speakerId));
List<Document> 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<Speech> getAllSpeeches() {
List<Speech> speechIds = new ArrayList<>();
Document filter = new Document();
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

@ -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<String> speechIds = getSpeechIdsBySpeakerId(speakerId);
List<Speech> 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<Speech> 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);

View file

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

View file

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