From e24813074f21a46a1ba1945b761abaf32f6c0180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20Eifl=C3=A4nder?= Date: Thu, 12 Aug 2021 14:07:34 +0200 Subject: [PATCH] RED-1982: Fixed not replaced placeholders --- .../service/WordReportGenerationService.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/WordReportGenerationService.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/WordReportGenerationService.java index 6efddd4..ad6f7e3 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/WordReportGenerationService.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/WordReportGenerationService.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -30,9 +31,11 @@ import javax.annotation.PostConstruct; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.util.Units; +import org.apache.poi.xwpf.usermodel.IBodyElement; 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.XWPFSDT; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; @@ -90,22 +93,25 @@ public class WordReportGenerationService { Map dossierAttributesPlaceholder = new HashMap<>(); //KEY:placeholder, VALUE:value - for (DossierAttribute dossierAttribute : dossierAttributes.getDossierAttributeList()) { - for (DossierAttributeConfig attribute : dossierAttributesConfig.getDossierAttributeConfigs()) { - if (dossierAttribute.getDossierAttributeId().equals(attribute.getId())) { - if (attribute.getType().equals(DossierAttributeConfig.DossierAttributeType.IMAGE)) { + for (DossierAttributeConfig attributeConfig : dossierAttributesConfig.getDossierAttributeConfigs()) { + for (DossierAttribute dossierAttribute : dossierAttributes.getDossierAttributeList()) { + if (dossierAttribute.getDossierAttributeId().equals(attributeConfig.getId())) { + if (attributeConfig.getType().equals(DossierAttributeConfig.DossierAttributeType.IMAGE)) { - if(dossierAttribute.getValue().startsWith("data:")){ - imagePlaceholders.add(new ImagePlaceholder(attribute.getPlaceholder(), Base64.getDecoder() + if (dossierAttribute.getValue().startsWith("data:")) { + imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder() .decode(dossierAttribute.getValue().split(",")[1]))); } else { - imagePlaceholders.add(new ImagePlaceholder(attribute.getPlaceholder(), Base64.getDecoder().decode(dossierAttribute.getValue()))); + imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder() + .decode(dossierAttribute.getValue()))); } } else { - dossierAttributesPlaceholder.put(attribute.getPlaceholder(), dossierAttribute.getValue()); + dossierAttributesPlaceholder.put(attributeConfig.getPlaceholder(), dossierAttribute.getValue()); } } - + } + if (!dossierAttributesPlaceholder.containsKey(attributeConfig.getPlaceholder())) { + dossierAttributesPlaceholder.put(attributeConfig.getPlaceholder(), ""); } } @@ -126,10 +132,10 @@ public class WordReportGenerationService { try (ByteArrayInputStream is = new ByteArrayInputStream(template)) { XWPFDocument doc = new XWPFDocument(is); - addTableRows(doc, reportEntries, fileStatus.getFilename()); for (ImagePlaceholder imagePlaceholder : imagePlaceholders) { - replaceParagraphForImagePlaceholder(doc.getParagraphs(), imagePlaceholder); + replaceImagePlaceholders(doc, imagePlaceholder); } + addTableRows(doc, reportEntries, fileStatus.getFilename()); for (String placeholder : placeholders) { String placeholderValue = getPlaceholderValue(placeholder, dossier, fileStatus, fileAttributePlaceholders, dossierAttributesPlaceholder); if (placeholderValue != null) { @@ -145,6 +151,19 @@ public class WordReportGenerationService { } + private void replaceImagePlaceholders(XWPFDocument doc, ImagePlaceholder imagePlaceholder) { + + replaceParagraphForImagePlaceholder(doc.getParagraphs(), imagePlaceholder); + for (XWPFTable tbl : doc.getTables()) { + for (XWPFTableRow row : tbl.getRows()) { + for (XWPFTableCell cell : row.getTableCells()) { + replaceParagraphForImagePlaceholder(cell.getParagraphs(), imagePlaceholder); + } + } + } + } + + private List getDefaultPlaceholders() { List defPlaceholders = new ArrayList<>();