RED-1186: replacing placeholders in documents; TODO: adding entries to tables
This commit is contained in:
parent
951a901702
commit
0579710cd2
@ -4,15 +4,11 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.redaction.report.v1.api.model.ReportType;
|
||||
@ -30,7 +26,11 @@ public class WordReportGenerationService {
|
||||
private byte[] appendixA1Template;
|
||||
private byte[] appendixA2Template;
|
||||
private PlaceholderClient placeholderClient;
|
||||
|
||||
public static final String FILENAME_PLACEHOLDER = "{{redaction.filename}}";
|
||||
public static final String PAGE_PLACEHOLDER = "{{redaction.page}}";
|
||||
public static final String PARAGRAPH_PLACEHOLDER = "{{redaction.paragraph}}";
|
||||
public static final String LEGALBASIS_PLACEHOLDER = "{{redaction.legalBasis}}";
|
||||
public static final String REASON_PLACEHOLDER = "{{redaction.reason}}";
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
@ -43,7 +43,7 @@ public class WordReportGenerationService {
|
||||
public byte[] generateReport(ReportType reportType, List<ReportRedactionEntry> reportEntries, String filename) {
|
||||
|
||||
|
||||
List<Placeholder> placeholder = placeholderClient.getPlaceholders();
|
||||
List<Placeholder> placeholders = placeholderClient.getPlaceholders();
|
||||
|
||||
|
||||
byte[] template;
|
||||
@ -58,27 +58,82 @@ public class WordReportGenerationService {
|
||||
|
||||
try (ByteArrayInputStream is = new ByteArrayInputStream(template)) {
|
||||
XWPFDocument doc = new XWPFDocument(is);
|
||||
|
||||
// Replace placeholders.
|
||||
|
||||
for (Placeholder placeholder: placeholders) {
|
||||
replacePlaceholders(doc,placeholder.getPlaceholder(),placeholder.getValue());
|
||||
}
|
||||
addTableRows(doc, reportEntries, filename);
|
||||
return toByteArray(doc);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void replacePlaceholders(XWPFDocument doc, String search, String replace) {
|
||||
replaceParagraph(doc.getParagraphs(), search, replace);
|
||||
for (XWPFTable tbl : doc.getTables()) {
|
||||
for (XWPFTableRow row : tbl.getRows()) {
|
||||
for (XWPFTableCell cell : row.getTableCells()) {
|
||||
replaceParagraph(cell.getParagraphs(), search, replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceParagraph(List<XWPFParagraph> paragraphs, String search, String replace) {
|
||||
for (XWPFParagraph p : paragraphs) {
|
||||
String paragraphText = p.getText();
|
||||
if(paragraphText.contains(search)) {
|
||||
String safeToUseInReplaceAllString = Pattern.quote(search);
|
||||
paragraphText = paragraphText.replaceAll(safeToUseInReplaceAllString, replace);
|
||||
XWPFRun run = p.getRuns().get(0);
|
||||
run.setText(paragraphText,0);
|
||||
int size = p.getRuns().size();
|
||||
for(int i=1; i<size; i++) {
|
||||
p.removeRun(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private XWPFTable getTableWithPlaceholder(XWPFDocument doc) {
|
||||
for (XWPFTable tbl : doc.getTables()) {
|
||||
String tblText = tbl.getText();
|
||||
if(containsPlaceholder(tblText)) {
|
||||
return tbl;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean containsPlaceholder(String place) {
|
||||
return place.contains(FILENAME_PLACEHOLDER)
|
||||
|| place.contains(PAGE_PLACEHOLDER)
|
||||
|| place.contains(PARAGRAPH_PLACEHOLDER)
|
||||
|| place.contains(LEGALBASIS_PLACEHOLDER)
|
||||
|| place.contains(REASON_PLACEHOLDER);
|
||||
}
|
||||
|
||||
private void addTableRows(XWPFDocument doc, List<ReportRedactionEntry> reportEntries, String filename) {
|
||||
|
||||
XWPFTable table = doc.getTables().get(1);
|
||||
//XWPFTable table = doc.getTables().get(1);
|
||||
XWPFTable table = getTableWithPlaceholder(doc);
|
||||
|
||||
// XmlCursor cursor = table.getCTTbl().newCursor();
|
||||
// XWPFParagraph newParagraph = doc.insertNewParagraph(cursor);
|
||||
// XWPFRun run = newParagraph.createRun();
|
||||
// run.setText("Applied rules: EFSA 1 (Vertebrate Authors)");
|
||||
// run.setFontSize(10);
|
||||
for (XWPFTableRow row : table.getRows()) {
|
||||
XWPFTableRow newRow = table.createRow();
|
||||
for (int i=0; i<row.getTableCells().size(); i++) {
|
||||
if(row.getTableCells().get(i).getText().contains(FILENAME_PLACEHOLDER)) {
|
||||
setText(row.getCell(i), filename);
|
||||
}
|
||||
if(row.getTableCells().get(i).getText().contains(PAGE_PLACEHOLDER)) {
|
||||
setText(row.getCell(i), String.valueOf(reportEntries.getPage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Zelle anhand placeholder und value ebenso
|
||||
reportEntries.forEach(entry -> {
|
||||
XWPFTableRow row = table.createRow();
|
||||
setText(row.getCell(0), filename);
|
||||
@ -88,6 +143,14 @@ public class WordReportGenerationService {
|
||||
});
|
||||
}
|
||||
|
||||
private int getCell(XWPFTableRow row,String placeholder) {
|
||||
for (int i=0; i<row.getTableCells().size(); i++) {
|
||||
if(row.getTableCells().get(i).getText().contains(placeholder)) {
|
||||
cell.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setText(XWPFTableCell cell, String value) {
|
||||
|
||||
|
||||
Binary file not shown.
@ -68,7 +68,8 @@ public class RedactionReportIntegrationTest {
|
||||
@Test
|
||||
public void testWordReportGeneration() throws IOException {
|
||||
|
||||
when(placeholderClient.getPlaceholders()).thenReturn(List.of(Placeholder.builder().placeholder("{{dossier.name}}").value("Dossier 1").build()));
|
||||
when(placeholderClient.getPlaceholders()).thenReturn(List.of(Placeholder.builder().placeholder("$dossier.name$").value("Dossier 1").build(),Placeholder.builder().placeholder("[[dossier.name]]").value("Dossier 1").build(),Placeholder.builder().placeholder("{{dossier.name}}").value("Dossier 1").build()));
|
||||
|
||||
|
||||
ClassPathResource redactionLogResource = new ClassPathResource("files/RedactedLog.txt");
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user