RED-2759: Quote replacement text in reports

This commit is contained in:
deiflaender 2021-11-15 09:19:49 +01:00
parent 213826e8b8
commit c5a51a6d3e
2 changed files with 70 additions and 26 deletions

View File

@ -1,5 +1,51 @@
package com.iqser.red.service.redaction.report.v1.server.service;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.DOSSIER_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.EXCERPT_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FILE_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ENG;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ENG_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_GER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_GER_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ISO;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ISO_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_TIME_ISO;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_TIME_ISO_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.IUCLID_FUNCTION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.JUSTIFICATION_PARAGRAPH_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.JUSTIFICATION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.JUSTIFICATION_REASON_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.PAGE_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.PARAGRAPH_PLACEHOLDER;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Dimension2DDouble;
import org.apache.poi.util.Units;
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.springframework.stereotype.Service;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.DossierAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.ReportTemplate;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier;
@ -14,27 +60,11 @@ import com.iqser.red.service.redaction.report.v1.server.model.ColoredText;
import com.iqser.red.service.redaction.report.v1.server.model.ImagePlaceholder;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Dimension2DDouble;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.regex.Pattern;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.*;
import javax.imageio.ImageIO;
@Slf4j
@Service
@RequiredArgsConstructor
public class WordReportGenerationService {
@ -110,6 +140,7 @@ public class WordReportGenerationService {
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
log.error(e.getMessage() + " in file: " + fileStatus.getFilename());
throw new RuntimeException(e);
}
}
@ -201,7 +232,8 @@ public class WordReportGenerationService {
run.setText("", 0);
run.addBreak();
Dimension2DDouble dim = getImageDimension(is2);
run.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, "image.jpg", Units.toEMU(dim.getWidth()), Units.toEMU(dim.getHeight()));
run.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, "image.jpg", Units.toEMU(dim.getWidth()), Units.toEMU(dim
.getHeight()));
int size = p.getRuns().size();
for (int i = 1; i < size; i++) {
p.removeRun(1);
@ -232,8 +264,14 @@ public class WordReportGenerationService {
for (XWPFParagraph p : paragraphs) {
String paragraphText = p.getText();
if (paragraphText.contains(search)) {
String safeToUseInReplaceAllString = Pattern.quote(search);
paragraphText = paragraphText.replaceAll(safeToUseInReplaceAllString, replace);
String escapedSearch = Pattern.quote(search);
String escapedReplace = Matcher.quoteReplacement(replace);
try {
paragraphText = paragraphText.replaceAll(escapedSearch, escapedReplace);
} catch (Exception e) {
log.error("Could not replace {} with {}", escapedSearch, escapedReplace);
throw new RuntimeException(String.format("Could not replace %s with %s", escapedSearch, escapedReplace), e);
}
int size = p.getRuns().size();
for (int i = 0; i <= size; i++) {
p.removeRun(0);
@ -262,7 +300,8 @@ public class WordReportGenerationService {
for (XWPFTable tbl : doc.getTables()) {
String tblText = tbl.getText();
if (tblText.contains(PAGE_PLACEHOLDER) || tblText.contains(PARAGRAPH_PLACEHOLDER) || tblText.contains(JUSTIFICATION_PLACEHOLDER) || tblText.contains(EXCERPT_PLACEHOLDER) || tblText.contains(JUSTIFICATION_PARAGRAPH_PLACEHOLDER) || tblText.contains(JUSTIFICATION_REASON_PLACEHOLDER)) {
if (tblText.contains(PAGE_PLACEHOLDER) || tblText.contains(PARAGRAPH_PLACEHOLDER) || tblText.contains(JUSTIFICATION_PLACEHOLDER) || tblText
.contains(EXCERPT_PLACEHOLDER) || tblText.contains(JUSTIFICATION_PARAGRAPH_PLACEHOLDER) || tblText.contains(JUSTIFICATION_REASON_PLACEHOLDER)) {
return tbl;
}
}
@ -272,7 +311,9 @@ public class WordReportGenerationService {
private boolean containsRedactionPlaceholder(String text) {
return text.contains(FILE_NAME_PLACEHOLDER) || text.contains(PAGE_PLACEHOLDER) || text.contains(PARAGRAPH_PLACEHOLDER) || text.contains(JUSTIFICATION_PLACEHOLDER) || text.contains(EXCERPT_PLACEHOLDER) || text.contains(JUSTIFICATION_PARAGRAPH_PLACEHOLDER) || text.contains(JUSTIFICATION_REASON_PLACEHOLDER);
return text.contains(FILE_NAME_PLACEHOLDER) || text.contains(PAGE_PLACEHOLDER) || text.contains(PARAGRAPH_PLACEHOLDER) || text
.contains(JUSTIFICATION_PLACEHOLDER) || text.contains(EXCERPT_PLACEHOLDER) || text.contains(JUSTIFICATION_PARAGRAPH_PLACEHOLDER) || text
.contains(JUSTIFICATION_REASON_PLACEHOLDER);
}
@ -302,7 +343,7 @@ public class WordReportGenerationService {
reportEntries.forEach(entry -> {
XWPFTableRow row = table.createRow();
for (Map.Entry<Integer, String> entry1 : placeholderCellPos.entrySet()) {
if(!entry1.getValue().isEmpty()) {
if (!entry1.getValue().isEmpty()) {
setText(row.getCell(entry1.getKey()), replaceTextPlaceholderWithEntries(entry, filename, entry1.getValue()));
} else {
setText(row.getCell(entry1.getKey()), "");
@ -340,6 +381,7 @@ public class WordReportGenerationService {
private Dimension2DDouble getImageDimension(ByteArrayInputStream bais) throws IOException {
BufferedImage bufImg = ImageIO.read(bais);
bais.reset();
@ -359,6 +401,7 @@ public class WordReportGenerationService {
run.setText(value);
}
private ColoredText getColor(String textForLine) {
if (textForLine.contains("<[<color:")) {
@ -369,6 +412,7 @@ public class WordReportGenerationService {
return new ColoredText(textForLine, null);
}
private byte[] toByteArray(XWPFDocument doc) throws IOException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

View File

@ -723,7 +723,7 @@
{
"id": "f60f40d36864bbf386ffb4689693a1ca",
"type": "CBI_address",
"value": "Syngenta Crop Protection AG",
"value": "$.Friedrich",
"reason": "Address found",
"matchedRule": 2,
"legalBasis": "Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",