diff --git a/redaction-report-service-v1/redaction-report-service-api-v1/src/main/java/com/iqser/red/service/redaction/report/v1/api/model/ReportType.java b/redaction-report-service-v1/redaction-report-service-api-v1/src/main/java/com/iqser/red/service/redaction/report/v1/api/model/ReportType.java index e622b53..415aaa2 100644 --- a/redaction-report-service-v1/redaction-report-service-api-v1/src/main/java/com/iqser/red/service/redaction/report/v1/api/model/ReportType.java +++ b/redaction-report-service-v1/redaction-report-service-api-v1/src/main/java/com/iqser/red/service/redaction/report/v1/api/model/ReportType.java @@ -1,5 +1,5 @@ package com.iqser.red.service.redaction.report.v1.api.model; public enum ReportType { - WORD_SINGLE_FILE, EXCEL_TEMPLATE_SINGLE_FILE, EXCEL_TEMPLATE_MULTI_FILE + WORD_SINGLE_FILE, WORD_TEMPLATE_MULTI_FILE, EXCEL_TEMPLATE_SINGLE_FILE, EXCEL_TEMPLATE_MULTI_FILE } diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/pom.xml b/redaction-report-service-v1/redaction-report-service-server-v1/pom.xml index e8e1088..888cc17 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/pom.xml +++ b/redaction-report-service-v1/redaction-report-service-server-v1/pom.xml @@ -14,8 +14,8 @@ 1.0-SNAPSHOT - 1.98.0 - 3.86.0 + 1.160.0 + 3.108.0 diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/MultiFileDocument.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/MultiFileDocument.java new file mode 100644 index 0000000..4d81a54 --- /dev/null +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/MultiFileDocument.java @@ -0,0 +1,19 @@ +package com.iqser.red.service.redaction.report.v1.server.model; + +import java.util.List; + +import org.apache.poi.xwpf.usermodel.XWPFDocument; + +import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class MultiFileDocument { + + private XWPFDocument document; + private String templateId; + +} diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ReportGenerationService.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ReportGenerationService.java index ae0fb1d..8a27c51 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ReportGenerationService.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ReportGenerationService.java @@ -10,6 +10,7 @@ import com.iqser.red.service.redaction.report.v1.server.client.DossierClient; import com.iqser.red.service.redaction.report.v1.server.client.FileStatusClient; import com.iqser.red.service.redaction.report.v1.server.client.RedactionLogClient; import com.iqser.red.service.redaction.report.v1.server.client.ReportTemplateClient; +import com.iqser.red.service.redaction.report.v1.server.model.MultiFileDocument; import com.iqser.red.service.redaction.report.v1.server.model.MultiFileWorkbook; import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry; import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService; @@ -20,6 +21,7 @@ import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.stereotype.Service; import java.io.ByteArrayInputStream; @@ -51,17 +53,29 @@ public class ReportGenerationService { List singleFilesTemplates = new ArrayList<>(); List multiFileWorkbooks = new ArrayList<>(); + List multiFileDocuments = new ArrayList<>(); for (String templateId : reportMessage.getTemplateIds()) { try { ReportTemplate reportTemplate = reportTemplateClient.getReportTemplate(reportMessage.getDossierTemplateId(), templateId); if (reportTemplate.isMultiFileReport()) { - byte[] excelTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId()); - try (ByteArrayInputStream is = new ByteArrayInputStream(excelTemplate)) { - XSSFWorkbook workbook = new XSSFWorkbook(is); - MultiFileWorkbook multiFileWorkbook = new MultiFileWorkbook(workbook, templateId); - multiFileWorkbooks.add(multiFileWorkbook); - } catch (IOException e) { - throw new RuntimeException("Could not generate multifile excel report."); + if(reportTemplate.getFileName().endsWith(".xlsx")) { + byte[] excelTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId()); + try (ByteArrayInputStream is = new ByteArrayInputStream(excelTemplate)) { + XSSFWorkbook workbook = new XSSFWorkbook(is); + MultiFileWorkbook multiFileWorkbook = new MultiFileWorkbook(workbook, templateId); + multiFileWorkbooks.add(multiFileWorkbook); + } catch (IOException e) { + throw new RuntimeException("Could not generate multifile excel report."); + } + } else { + byte[] wordTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId()); + try (ByteArrayInputStream is = new ByteArrayInputStream(wordTemplate)) { + XWPFDocument doc = new XWPFDocument(is); + MultiFileDocument multiFileDocument = new MultiFileDocument(doc, templateId); + multiFileDocuments.add(multiFileDocument); + } catch (IOException e) { + throw new RuntimeException("Could not generate multifile word report."); + } } } else { singleFilesTemplates.add(reportTemplate); @@ -86,9 +100,12 @@ public class ReportGenerationService { excelTemplateReportGenerationService.generateReport(reportEntries, reportMessage.getDossierTemplateId(), multiFileWorkbook.getWorkbook(), fileStatus, project, j == reportMessage.getFileIds() .size() - 1); } + for (MultiFileDocument multiFileDocument : multiFileDocuments) { + wordReportGenerationService.generateReport(ReportType.WORD_TEMPLATE_MULTI_FILE, reportEntries, reportMessage.getDossierTemplateId(), multiFileDocument.getDocument(), fileStatus, project, j == reportMessage.getFileIds() + .size() - 1); + } for (ReportTemplate reportTemplate : singleFilesTemplates) { if (reportTemplate.getFileName().endsWith(".xlsx")) { - byte[] excelTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId()); try (ByteArrayInputStream is = new ByteArrayInputStream(excelTemplate)) { XSSFWorkbook workbook = new XSSFWorkbook(is); @@ -101,10 +118,16 @@ public class ReportGenerationService { throw new RuntimeException("Could not generate singlefile excel report."); } } else { - byte[] template = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, reportMessage.getDossierTemplateId(), reportTemplate, fileStatus, project); - String storageId = reportStorageService.storeObject(reportMessage.getDownloadId(), template); - storedFileInformation.add(new StoredFileInformation(reportMessage.getFileIds() - .get(j), storageId, ReportType.WORD_SINGLE_FILE, reportTemplate.getTemplateId())); + byte[] wordTemplate= reportStorageService.getReportTemplate(reportTemplate.getStorageId()); + try (ByteArrayInputStream is = new ByteArrayInputStream(wordTemplate)) { + XWPFDocument doc = new XWPFDocument(is); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, reportMessage.getDossierTemplateId(), doc, fileStatus, project, true); + byte[] template = wordReportGenerationService.toByteArray(doc); + String storageId = reportStorageService.storeObject(reportMessage.getDownloadId(), template); + storedFileInformation.add(new StoredFileInformation(reportMessage.getFileIds().get(j), storageId, ReportType.WORD_SINGLE_FILE, reportTemplate.getTemplateId())); + } catch (IOException e) { + throw new RuntimeException("Could not generate singlefile word report."); + } } } @@ -120,6 +143,12 @@ public class ReportGenerationService { storedFileInformation.add(new StoredFileInformation(null, storageId, ReportType.EXCEL_TEMPLATE_MULTI_FILE, multiFileWorkbook.getTemplateId())); } + for (MultiFileDocument multiFileDocument : multiFileDocuments) { + byte[] template = wordReportGenerationService.toByteArray(multiFileDocument.getDocument()); + String storageId = reportStorageService.storeObject(reportMessage.getDownloadId(), template); + storedFileInformation.add(new StoredFileInformation( null, storageId, ReportType.WORD_TEMPLATE_MULTI_FILE, multiFileDocument.getTemplateId())); + } + return storedFileInformation; } 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 11d6bd0..662981b 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 @@ -35,6 +35,9 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -51,6 +54,7 @@ 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.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.springframework.stereotype.Service; import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.DossierAttributeConfig; @@ -69,6 +73,7 @@ import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEnt import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -77,15 +82,13 @@ import lombok.extern.slf4j.Slf4j; public class WordReportGenerationService { private final FileAttributesConfigClient fileAttributesClient; - private final ReportStorageService reportStorageService; private final DossierAttributesClient dossierAttributesClient; private final DossierAttributesConfigClient dossierAttributesConfigClient; private final IuclidFunctionService iuclidFunctionService; - public byte[] generateReport(ReportType reportType, List reportEntries, - String dossierTemplateId, ReportTemplate reportTemplate, FileModel fileStatus, - Dossier dossier) { + public XWPFDocument generateReport(ReportType reportType, List reportEntries, String dossierTemplateId, XWPFDocument doc, FileModel fileStatus, + Dossier dossier, boolean isLastFile) { List placeholders = getDefaultPlaceholders(); List imagePlaceholders = new ArrayList<>(); @@ -105,8 +108,7 @@ public class WordReportGenerationService { imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder() .decode(dossierAttribute.getValue().split(",")[1]))); } else { - imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder() - .decode(dossierAttribute.getValue()))); + imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder().decode(dossierAttribute.getValue()))); } } } else { @@ -127,25 +129,25 @@ public class WordReportGenerationService { placeholders.addAll(fileAttributePlaceholders.keySet()); placeholders.addAll(dossierAttributesPlaceholder.keySet()); - byte[] template; - String storageId = reportTemplate.getStorageId(); - template = reportStorageService.getReportTemplate(storageId); - try (ByteArrayInputStream is = new ByteArrayInputStream(template)) { - XWPFDocument doc = new XWPFDocument(is); + try { + for (ImagePlaceholder imagePlaceholder : imagePlaceholders) { replaceImagePlaceholders(doc, imagePlaceholder); } - addTableRows(doc, reportEntries, fileStatus.getFilename(), fileStatus, fileAttributePlaceholders); + XWPFTable table = getRedactionTable(doc); + Map placeholderCellPos = computePlaceholderPos(table); + addTableRows(table, reportEntries, fileStatus.getFilename(), fileStatus, fileAttributePlaceholders, placeholderCellPos); for (String placeholder : placeholders) { String placeholderValue = getPlaceholderValue(placeholder, dossier, fileStatus, fileAttributePlaceholders, dossierAttributesPlaceholder, reportEntries); if (placeholderValue != null) { replaceTextPlaceholders(doc, placeholder, placeholderValue); } } - return toByteArray(doc); - } catch (IOException e) { - throw new RuntimeException(e); + if(!isLastFile) { + readdPlaceholders(table, placeholderCellPos); + } + return doc; } catch (Exception e) { log.error(e.getMessage() + " in file: " + fileStatus.getFilename()); throw new RuntimeException(e); @@ -186,10 +188,8 @@ public class WordReportGenerationService { } - private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus, - Map fileAttributePlaceholders, - Map dossierAttributesPlaceholders, - List reportRedactionEntries) { + private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus, Map fileAttributePlaceholders, + Map dossierAttributesPlaceholders, List reportRedactionEntries) { if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) { return OffsetDateTime.now().format(FORMAT_DATE_ISO); @@ -228,8 +228,7 @@ public class WordReportGenerationService { } - private void replaceParagraphForImagePlaceholder(List paragraphs, - ImagePlaceholder imagePlaceholder) { + private void replaceParagraphForImagePlaceholder(List paragraphs, ImagePlaceholder imagePlaceholder) { for (XWPFParagraph p : paragraphs) { String paragraphText = p.getText(); @@ -324,30 +323,33 @@ public class WordReportGenerationService { } - private void addTableRows(XWPFDocument doc, List reportEntries, String filename, - FileModel fileStatus, Map fileAttributePlaceholders) { + private Map computePlaceholderPos(XWPFTable table) { + Map placeholderCellPos = new HashMap<>(); + + if(table != null) { + int placeholderRow = -1; + for (int j = 0; j < table.getRows().size(); j++) { + for (int i = 0; i < table.getRows().get(j).getTableCells().size(); i++) { + XWPFTableCell cell = table.getRows().get(j).getTableCells().get(i); + if (containsRedactionPlaceholder(cell.getText())) { + placeholderCellPos.put(i, cell.getText()); + placeholderRow = j; + } else if (cell.getText().isEmpty()) { + placeholderCellPos.put(i, ""); + } + } + } + table.removeRow(placeholderRow); + } + return placeholderCellPos; + } + + private void addTableRows(XWPFTable table, List reportEntries, String filename, FileModel fileStatus, Map fileAttributePlaceholders, Map placeholderCellPos) { - XWPFTable table = getRedactionTable(doc); if (table == null) { return; } - Map placeholderCellPos = new HashMap<>(); - - int placeholderRow = -1; - for (int j = 0; j < table.getRows().size(); j++) { - for (int i = 0; i < table.getRows().get(j).getTableCells().size(); i++) { - XWPFTableCell cell = table.getRows().get(j).getTableCells().get(i); - if (containsRedactionPlaceholder(cell.getText())) { - placeholderCellPos.put(i, cell.getText()); - placeholderRow = j; - } else if (cell.getText().isEmpty()) { - placeholderCellPos.put(i, ""); - } - } - } - table.removeRow(placeholderRow); - if (placeholderCellPos.containsValue(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) { var redactionsPerJustification = getRedactionsPerJustification(reportEntries); for (Map.Entry> entry : redactionsPerJustification.entrySet()) { @@ -374,6 +376,14 @@ public class WordReportGenerationService { } } + private void readdPlaceholders(XWPFTable table, Map placeholderCellPos) { + XWPFTableRow newRow = table.createRow(); + for(int i = 0; i < table.getRow(0).getTableCells().size(); i++) { + String placeholder = placeholderCellPos.get(i); + setText(newRow.getCell(i), placeholder); + } + } + private String replaceTextPlaceholderWithEntries(ReportRedactionEntry entry, String filename, String placeholder) { @@ -411,17 +421,13 @@ public class WordReportGenerationService { } - private String replaceSeedsPlaceholder(Map.Entry> entry, String filename, - String placeholder, FileModel fileStatus, + private String replaceSeedsPlaceholder(Map.Entry> entry, String filename, String placeholder, FileModel fileStatus, Map fileAttributePlaceholders) { if (placeholder.equals(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) { - return entry.getValue() - .stream() - .map(ReportRedactionEntry::getPage) - .distinct() - .map(Object::toString) - .collect(Collectors.joining(", ")); + var pages = entry.getValue().stream().map(ReportRedactionEntry::getPage).collect(Collectors.toSet()); + return computePageRanges(pages); + } if (placeholder.equals(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER)) { return entry.getKey(); @@ -442,6 +448,49 @@ public class WordReportGenerationService { } + private String computePageRanges(Set pages) { + + StringBuilder result = new StringBuilder(); + + SortedSet numbers = new TreeSet(pages); + + Integer start = null; + Integer end = null; + + for (Integer num : numbers) { + //initialize + if (start == null || end == null) { + start = num; + end = num; + } + //next number in range + else if (end.equals(num - 1)) { + end = num; + } + //there's a gap + else { + //range length 1 + if (start.equals(end)) { + result.append(start).append(","); + } + //range lenth 2 and more + else { + result.append(start).append("-").append(end).append(","); + } + + start = num; + end = num; + } + } + if (start.equals(end)) { + result.append(start); + } else { + result.append(start).append("-").append(end); + } + return result.toString(); + } + + private Dimension2DDouble getImageDimension(ByteArrayInputStream bais) throws IOException { BufferedImage bufImg = ImageIO.read(bais); @@ -474,12 +523,15 @@ public class WordReportGenerationService { return new ColoredText(textForLine, null); } + private Map> getRedactionsPerJustification(List reportRedactionEntryList) { + return reportRedactionEntryList.stream().sorted(Comparator.comparing(ReportRedactionEntry::getPage)).collect(Collectors.groupingBy(ReportRedactionEntry::getJustification)); } - private byte[] toByteArray(XWPFDocument doc) throws IOException { + @SneakyThrows + public byte[] toByteArray(XWPFDocument doc) { try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { doc.write(byteArrayOutputStream); diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/test/java/com/iqser/red/service/redaction/report/v1/server/RedactionReportIntegrationTest.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/java/com/iqser/red/service/redaction/report/v1/server/RedactionReportIntegrationTest.java index 0823db3..16203b9 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/test/java/com/iqser/red/service/redaction/report/v1/server/RedactionReportIntegrationTest.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/java/com/iqser/red/service/redaction/report/v1/server/RedactionReportIntegrationTest.java @@ -28,6 +28,7 @@ import com.iqser.red.storage.commons.service.StorageService; import lombok.SneakyThrows; import org.apache.commons.io.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -168,15 +169,10 @@ public class RedactionReportIntegrationTest { .build()); ClassPathResource templateResource = new ClassPathResource("templates/Seeds - New Justification Form.docx"); - when(reportStorageService.getReportTemplate(storageId)).thenReturn(IOUtils.toByteArray(templateResource.getInputStream())); - ReportTemplate reportTemplate = ReportTemplate.builder() - .dossierTemplateId("dossierTemplateId") - .templateId("templateId") - .fileName("fileName") - .storageId("storageId") - .uploadDate(OffsetDateTime.now()) - .build(); - byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, reportTemplate, fileStatus, project); + + XWPFDocument doc = new XWPFDocument(templateResource.getInputStream()); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, doc, fileStatus, project, true); + byte[] report = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template_wrg.docx")) { fileOutputStream.write(report); @@ -207,7 +203,7 @@ public class RedactionReportIntegrationTest { List reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping); List reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping); - DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "label", true, "{{dossier.attribute.name}}", DossierAttributeType.TEXT, dossierTemplateId);// + DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "label", true, "{{dossier.attribute.name}}", DossierAttributeType.TEXT, dossierTemplateId); DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "label2", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId); when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2)); @@ -293,27 +289,17 @@ public class RedactionReportIntegrationTest { .build()); ClassPathResource wordTemplateResource = new ClassPathResource("templates/Justification Appendix A1.docx"); - when(reportStorageService.getReportTemplate(storageId)).thenReturn(IOUtils.toByteArray(wordTemplateResource.getInputStream())); - ReportTemplate reportTemplate = ReportTemplate.builder() - .dossierTemplateId("dossierTemplateId") - .templateId("templateId") - .fileName("fileName") - .storageId("storageId") - .uploadDate(OffsetDateTime.now()) - .build(); - byte[] wordReport = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, reportTemplate, fileModel, dossier); + + XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream()); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, doc, fileModel, dossier, true); + byte[] wordReport = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template1.docx")) { fileOutputStream.write(wordReport); } - ReportTemplate reportTemplate2 = ReportTemplate.builder() - .dossierTemplateId("dossierTemplateId") - .templateId("templateId") - .fileName("fileNameReportTemplate2") - .storageId("storageId") - .uploadDate(OffsetDateTime.now()) - .build(); - byte[] wordReport2 = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries2, dossierTemplateId, reportTemplate2, fileModel2, dossier); + doc = new XWPFDocument(wordTemplateResource.getInputStream()); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries2, dossierTemplateId, doc, fileModel2, dossier, true); + byte[] wordReport2 = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template2.docx")) { fileOutputStream.write(wordReport2); } @@ -392,15 +378,10 @@ public class RedactionReportIntegrationTest { .build()); ClassPathResource templateResource = new ClassPathResource("templates/6464 appendix_b EFSA dRAR justification.docx"); - when(reportStorageService.getReportTemplate(storageId)).thenReturn(IOUtils.toByteArray(templateResource.getInputStream())); - ReportTemplate reportTemplate = ReportTemplate.builder() - .dossierTemplateId("dossierTemplateId") - .templateId("templateId") - .fileName("filename") - .storageId("storageId") - .uploadDate(OffsetDateTime.now()) - .build(); - byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, reportTemplate, fileModel, project); + + XWPFDocument doc = new XWPFDocument(templateResource.getInputStream()); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, doc, fileModel, project, true); + byte[] report = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template13.docx")) { fileOutputStream.write(report); @@ -453,7 +434,10 @@ public class RedactionReportIntegrationTest { .storageId("storageId") .uploadDate(OffsetDateTime.now()) .build(); - byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, reportTemplate, fileStatus, project); + + XWPFDocument doc = new XWPFDocument(templateResource.getInputStream()); + wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, doc, fileStatus, project, true); + byte[] report = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/iuclid_report_2.docx")) { fileOutputStream.write(report); @@ -530,13 +514,16 @@ public class RedactionReportIntegrationTest { String dossierTemplateId = "dossierTemplateId"; ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog2817.json"); + ClassPathResource redactionLogResource2 = new ClassPathResource("files/redactionLog.json"); RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class); + RedactionLog redactionLog2 = objectMapper.readValue(redactionLogResource2.getInputStream(), RedactionLog.class); ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMappingNew.json"); List legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() { }); List reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping); + List reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping); when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(new ArrayList<>()); when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(new ArrayList<>()); @@ -544,6 +531,7 @@ public class RedactionReportIntegrationTest { FileModel fileStatus = FileModel.builder().filename("VV123456").build(); + FileModel fileStatus2 = FileModel.builder().filename("second file").build(); Dossier project = Dossier.builder().id("dossierId").dossierName("projectName").build(); @@ -554,16 +542,12 @@ public class RedactionReportIntegrationTest { .storageId(storageId) .build()); - ClassPathResource templateResource = new ClassPathResource("templates/Seeds - New Justification Form.docx"); + ClassPathResource templateResource = new ClassPathResource("templates/Seeds-NewJustificationForm.docx"); when(reportStorageService.getReportTemplate(storageId)).thenReturn(IOUtils.toByteArray(templateResource.getInputStream())); - ReportTemplate reportTemplate = ReportTemplate.builder() - .dossierTemplateId("dossierTemplateId") - .templateId("templateId") - .fileName("fileName") - .storageId("storageId") - .uploadDate(OffsetDateTime.now()) - .build(); - byte[] report = wordReportGenerationService.generateReport(ReportType.WORD_SINGLE_FILE, reportEntries, dossierTemplateId, reportTemplate, fileStatus, project); + XWPFDocument doc = new XWPFDocument(templateResource.getInputStream()); + doc = wordReportGenerationService.generateReport(ReportType.WORD_TEMPLATE_MULTI_FILE, reportEntries, dossierTemplateId, doc, fileStatus, project, false); + doc = wordReportGenerationService.generateReport(ReportType.WORD_TEMPLATE_MULTI_FILE, reportEntries2, dossierTemplateId, doc, fileStatus2, project, true); + byte[] report = wordReportGenerationService.toByteArray(doc); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/seedsReport.docx")) { fileOutputStream.write(report); diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/templates/Seeds-NewJustificationForm.docx b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/templates/Seeds-NewJustificationForm.docx new file mode 100644 index 0000000..80c4eb2 Binary files /dev/null and b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/templates/Seeds-NewJustificationForm.docx differ