added support for bulk speech export from Speaker
This commit is contained in:
parent
704d71dd75
commit
8d00ef14d4
3 changed files with 81 additions and 5 deletions
|
@ -15,14 +15,29 @@ import java.time.LocalDateTime;
|
|||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static org.texttechnologylab.project.gruppe_05_1.Main.RESOURCES_DIR;
|
||||
import static org.texttechnologylab.project.gruppe_05_1.Main.TEMP_EXPORT_DIR;
|
||||
import static org.texttechnologylab.project.gruppe_05_1.database.MongoPprUtils.*;
|
||||
|
||||
public class TeXUtil {
|
||||
private static String BEGIN_DOCUMENT = "\\begin{document}";
|
||||
private static String END_DOCUMENT = "\\end{document}";
|
||||
private static final String PREAMBLE = readFileContentFromTeXDir();
|
||||
private static final String BEGIN_DOCUMENT = "\\begin{document}\n";
|
||||
private static final String END_DOCUMENT = "\\end{document}";
|
||||
private static final String TABLEOFCONTENTS = "\\tableofcontents\n\\newpage\n";
|
||||
private static final String NEWPAGE = "\\newpage\n";
|
||||
|
||||
private static String readFileContentFromTeXDir() {
|
||||
try {
|
||||
return Files.readString(new File(RESOURCES_DIR, "tex/preamble.tex").toPath());
|
||||
} catch (IOException e) {
|
||||
Logger.error("Failed to read file content from tex directory.");
|
||||
Logger.error(e.getMessage());
|
||||
Logger.debug(Arrays.toString(e.getStackTrace()));
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getSpeechToTexComponent(String speechId) {
|
||||
createTempDir();
|
||||
|
@ -44,8 +59,25 @@ public class TeXUtil {
|
|||
|
||||
public static String getExportedSpeechBase64StringBySpeechId(String speechId) throws IOException, InterruptedException {
|
||||
// Read preamble from resources directory tex/preamble.tex
|
||||
String preamble = Files.readString(new File(RESOURCES_DIR, "tex/preamble.tex").toPath()).replace("$$EXPORTCATEGORY$$", "Speech " + speechId);
|
||||
return convertTexToBase64PDF(preamble + BEGIN_DOCUMENT + getSpeechToTexComponent(speechId) + END_DOCUMENT);
|
||||
return convertTexToBase64PDF(PREAMBLE.replace("$$EXPORTCATEGORY$$", "Speech " + speechId) + BEGIN_DOCUMENT + getSpeechToTexComponent(speechId) + END_DOCUMENT);
|
||||
}
|
||||
|
||||
public static String getBulkExportedSpeechBase64StringFromSpeakerById(String speakerId) throws IOException, InterruptedException {
|
||||
// Fetch all speechIDs from the speaker
|
||||
List<String> speechIds = getSpeechIdsBySpeakerId(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));
|
||||
tex.append(NEWPAGE);
|
||||
}
|
||||
tex.append(END_DOCUMENT);
|
||||
|
||||
return convertTexToBase64PDF(tex.toString());
|
||||
}
|
||||
|
||||
public static String convertTexToBase64PDF(String tex) throws IOException, InterruptedException {
|
||||
|
|
|
@ -67,7 +67,7 @@ public class RESTHandler {
|
|||
app.get("/reden", SpeechController::listAllSpeeches); // zeige alle Reden an (Filtern möglich)
|
||||
|
||||
app.get("/export/speech/{id}", SpeechesExportController::exportSpeech); // exportiere eine Rede als PDF
|
||||
//app.get("/portfolio/{id}/export", SpeechesExportController::exportSpeechesFromParlamentarier); // exportiere alle Reden eines Parlamentariers 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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,4 +56,48 @@ public class SpeechesExportController {
|
|||
Logger.debug(Arrays.toString(e.getStackTrace()));
|
||||
}
|
||||
}
|
||||
|
||||
@OpenApi(
|
||||
summary = "Get all speeches from a speaker as a PDF",
|
||||
description = "Returns a LaTeX generated pdf of all speaches of a selected speech",
|
||||
operationId = "getSpeechesFromSpeakerExport",
|
||||
path = "/export/speaker/{id}",
|
||||
methods = HttpMethod.GET,
|
||||
tags = {"Export", "Speeches", "PDF"},
|
||||
responses = {
|
||||
@OpenApiResponse(status = "200")
|
||||
})
|
||||
public static void exportSpeechesFromSpeaker(Context ctx) {
|
||||
byte[] pdfBytes = new byte[0];
|
||||
try {
|
||||
pdfBytes = Base64.getDecoder().decode(getBulkExportedSpeechBase64StringFromSpeakerById(ctx.pathParam("id")));
|
||||
} catch (Exception e) {
|
||||
Logger.error("Failed to generate Export of Speeches from Speaker with ID " + ctx.pathParam("id"));
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue