Pull request #152: RED-4123: TASK0661224 Placeholder for entity of a redaction should be available in reports

Merge in RED/redaction-report-service from RED-4123-rrs1 to master

* commit '9f77f3829f2837128e3014000bbad3132362dfe8':
  RED-4123: TASK0661224 Placeholder for entity of a redaction should be available in reports
This commit is contained in:
Ali Oezyetimoglu 2022-06-15 15:20:19 +02:00
commit 9cac62a47a
12 changed files with 6674 additions and 236 deletions

View File

@ -0,0 +1,10 @@
package com.iqser.red.service.redaction.report.v1.server.client;
import org.springframework.cloud.openfeign.FeignClient;
import com.iqser.red.service.persistence.service.v1.api.resources.DictionaryResource;
@FeignClient(name = "DictionaryResource", url = "${persistence-service.url}")
public interface DictionaryClient extends DictionaryResource {
}

View File

@ -16,7 +16,4 @@ public class PlaceholderInput {
private Map.Entry<String, List<ReportRedactionEntry>> redactionsPerJustificationEntry;
}

View File

@ -16,4 +16,6 @@ public class ReportRedactionEntry {
private String justificationReason;
private String excerpt;
private String value;
private String entityDisplayName;
}

View File

@ -18,6 +18,7 @@ import static com.iqser.red.service.redaction.report.v1.server.service.Placehold
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.JUSTIFICATION_TEXT_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 static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.REDACTION_VALUE_PLACEHOLDER;
import java.awt.image.BufferedImage;
@ -68,9 +69,8 @@ import lombok.extern.slf4j.Slf4j;
public class ExcelReportGenerationService {
@Timed("redactmanager_generateExcelReport")
public void generateExcelReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, SXSSFWorkbook workbook, String dossierName,
FileModel fileModel, ExcelModel excelModel, boolean isLastFile) {
public void generateExcelReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel, String reportTemplateName, SXSSFWorkbook workbook,
String dossierName, FileModel fileModel, ExcelModel excelModel, boolean isLastFile) {
long start = System.currentTimeMillis();
@ -106,8 +106,7 @@ public class ExcelReportGenerationService {
@SneakyThrows
@Timed("redactmanager_excel-addRows")
private void addRows(SXSSFWorkbook workbook, Sheet sheet, Map<CellIdentifier, Cell> copiedCells,
int numberOfRowsToShift, PlaceholderModel placeholderModel, String dossierName,
private void addRows(SXSSFWorkbook workbook, Sheet sheet, Map<CellIdentifier, Cell> copiedCells, int numberOfRowsToShift, PlaceholderModel placeholderModel, String dossierName,
String filename) {
Set<Integer> createdCopyRows = new HashSet<>();
@ -133,8 +132,7 @@ public class ExcelReportGenerationService {
}
private void replacePlaceholders(Cell cell, PlaceholderModel placeholderModel, String dossierName,
String filename) {
private void replacePlaceholders(Cell cell, PlaceholderModel placeholderModel, String dossierName, String filename) {
for (String placeholder : placeholderModel.getPlaceholders()) {
String placeholderValue = getPlaceholderValue(placeholder, dossierName, filename, placeholderModel);
@ -183,8 +181,7 @@ public class ExcelReportGenerationService {
@Timed("redactmanager_excel-addRedactionEntryRows")
private void addRedactionEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename,
ExcelModel excelModel, PlaceholderModel placeholderModel) {
private void addRedactionEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename, ExcelModel excelModel, PlaceholderModel placeholderModel) {
long start = System.currentTimeMillis();
AtomicInteger rowIndex = new AtomicInteger(excelModel.getRedactionPlaceholderRow());
@ -196,8 +193,7 @@ public class ExcelReportGenerationService {
excelModel.getWrittenRows().add(rowIndex.get());
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeholderCellPos.entrySet()) {
Cell cell = sheet.getRow(rowIndex.get()).createCell(entry1.getKey());
cell.setCellValue(entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
cell.setCellValue(entry1.getValue().apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
}
rowIndex.getAndIncrement();
});
@ -210,7 +206,7 @@ public class ExcelReportGenerationService {
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) || text.contains(REDACTION_VALUE_PLACEHOLDER) || text.contains(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER) || text.contains(JUSTIFICATION_TEXT_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) || text.contains(REDACTION_VALUE_PLACEHOLDER) || text.contains(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER) || text.contains(JUSTIFICATION_TEXT_PLACEHOLDER) || text.contains(REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER);
}
@ -244,10 +240,10 @@ public class ExcelReportGenerationService {
return (input) -> input.getEntry().getExcerpt();
}
if (placeholder.equals(REDACTION_VALUE_PLACEHOLDER)) {
return (input) -> input.getEntry().getValue() != null ? input.getEntry()
.getValue()
.replaceAll("\n", " ")
.replaceAll(" ", " ") : "";
return (input) -> input.getEntry().getValue() != null ? input.getEntry().getValue().replaceAll("\n", " ").replaceAll(" ", " ") : "";
}
if (placeholder.equals(REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER)) {
return (input) -> input.getEntry().getEntityDisplayName();
}
return (input) -> "";
}
@ -264,8 +260,7 @@ public class ExcelReportGenerationService {
}
private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell,
PlaceholderModel placeholderModel) throws IOException {
private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell, PlaceholderModel placeholderModel) throws IOException {
for (ImagePlaceholder imagePlaceholder : placeholderModel.getImagePlaceholders()) {
@ -302,8 +297,7 @@ public class ExcelReportGenerationService {
}
private double calculateScale(ByteArrayInputStream imageByteArrayInputStream, float cellWidth,
float cellHeight) throws IOException {
private double calculateScale(ByteArrayInputStream imageByteArrayInputStream, float cellWidth, float cellHeight) throws IOException {
BufferedImage img = ImageIO.read(imageByteArrayInputStream);
@ -324,8 +318,7 @@ public class ExcelReportGenerationService {
}
private String getPlaceholderValue(String placeholder, String dossierName, String filename,
PlaceholderModel placeholderModel) {
private String getPlaceholderValue(String placeholder, String dossierName, String filename, PlaceholderModel placeholderModel) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
return OffsetDateTime.now().format(FORMAT_DATE_ISO);

View File

@ -1,5 +1,26 @@
package com.iqser.red.service.redaction.report.v1.server.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
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.amazonaws.services.kms.model.NotFoundException;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.ReportTemplate;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesConfigClient;
@ -7,20 +28,9 @@ import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesCon
import com.iqser.red.service.redaction.report.v1.server.client.ReportTemplateClient;
import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService;
import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Slf4j
@Service
@ -37,6 +47,7 @@ public class PlaceholderService {
public static final String JUSTIFICATION_TEXT_PLACEHOLDER = "{{redaction.justificationText}}";
public static final String EXCERPT_PLACEHOLDER = "{{redaction.excerpt}}";
public static final String REDACTION_VALUE_PLACEHOLDER = "{{redaction.value}}";
public static final String REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER = "{{redaction.entity.displayName}}";
public static final DateTimeFormatter FORMAT_DATE_ISO = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter FORMAT_DATE_GER = DateTimeFormatter.ofPattern("dd.MM.yyyy");
@ -55,18 +66,18 @@ public class PlaceholderService {
public static final String SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER = "{{function.seeds.redactionGroupedByJustification.pages}}";
public static final String SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER = "{{function.seeds.justification}}";
private final ReportTemplateClient reportTemplateClient;
private final ReportStorageService reportStorageService;
private final DossierAttributesConfigClient dossierAttributesConfigClient;
private final FileAttributesConfigClient fileAttributesClient;
public List<String> getGeneralPlaceholders() {
return List.of(FILE_NAME_PLACEHOLDER, PAGE_PLACEHOLDER, PARAGRAPH_PLACEHOLDER, JUSTIFICATION_PLACEHOLDER, EXCERPT_PLACEHOLDER, FORMAT_DATE_ISO_PLACEHOLDER, FORMAT_DATE_GER_PLACEHOLDER, FORMAT_DATE_ENG_PLACEHOLDER, FORMAT_TIME_ISO_PLACEHOLDER, DOSSIER_NAME_PLACEHOLDER, IUCLID_FUNCTION_PLACEHOLDER, REDACTION_VALUE_PLACEHOLDER, JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER, JUSTIFICATION_TEXT_PLACEHOLDER, SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER, SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER);
return List.of(FILE_NAME_PLACEHOLDER, PAGE_PLACEHOLDER, PARAGRAPH_PLACEHOLDER, JUSTIFICATION_PLACEHOLDER, EXCERPT_PLACEHOLDER, FORMAT_DATE_ISO_PLACEHOLDER, FORMAT_DATE_GER_PLACEHOLDER, FORMAT_DATE_ENG_PLACEHOLDER, FORMAT_TIME_ISO_PLACEHOLDER, DOSSIER_NAME_PLACEHOLDER, IUCLID_FUNCTION_PLACEHOLDER, REDACTION_VALUE_PLACEHOLDER, JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER, JUSTIFICATION_TEXT_PLACEHOLDER, SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER, SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER, REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER);
}
public Set<String> getPlaceholdersOfTemplate(String dossierTemplateId, String reportTemplateId) {
ReportTemplate reportTemplate = reportTemplateClient.getReportTemplate(dossierTemplateId, reportTemplateId);
@ -74,6 +85,7 @@ public class PlaceholderService {
return findGivenPlaceholdersOfTemplate(reportTemplate, allPlaceholders);
}
public List<ReportTemplate> getReportTemplatesByPlaceholder(String dossierTemplateId, String placeholder) {
List<ReportTemplate> reportTemplates = reportTemplateClient.getAvailableReportTemplates(dossierTemplateId);
@ -92,6 +104,7 @@ public class PlaceholderService {
return result;
}
private Set<String> findGivenPlaceholdersOfTemplate(ReportTemplate reportTemplate, Set<String> placeholders) {
try {
@ -112,6 +125,7 @@ public class PlaceholderService {
}
}
private Set<String> findExcelPlaceholders(byte[] template, Set<String> allPlaceholders) {
try (ByteArrayInputStream is = new ByteArrayInputStream(template)) {
@ -144,6 +158,7 @@ public class PlaceholderService {
}
private Set<String> findWordPlaceholders(byte[] template, Set<String> allPlaceholders) {
Set<String> resultPlaceholders = new HashSet<>();
@ -181,6 +196,7 @@ public class PlaceholderService {
}
}
private Set<String> getAllPlaceholders(String dossierTemplateId) {
Set<String> allPlaceholders = new HashSet<>(getGeneralPlaceholders());
@ -196,7 +212,9 @@ public class PlaceholderService {
return allPlaceholders;
}
private void contains(Set<String> allPlaceholders, Set<String> resultPlaceholders, String placeholder) {
for (String searchPlaceholder : allPlaceholders) {
if (StringUtils.contains(placeholder, searchPlaceholder)) {
resultPlaceholders.add(searchPlaceholder);

View File

@ -1,28 +1,28 @@
package com.iqser.red.service.redaction.report.v1.server.service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.iqser.red.service.persistence.service.v1.api.model.annotations.AnnotationStatus;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.legalbasis.LegalBasis;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.v1.model.ChangeType;
import com.iqser.red.service.redaction.v1.model.ManualRedactionType;
import com.iqser.red.service.redaction.v1.model.Rectangle;
import com.iqser.red.service.redaction.v1.model.RedactionLog;
import com.iqser.red.service.redaction.v1.model.RedactionLogEntry;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class RedactionLogConverterService {
public List<ReportRedactionEntry> convertAndSort(RedactionLog redactionLog, List<LegalBasis> legalBasisMappings) {
public List<ReportRedactionEntry> convertAndSort(RedactionLog redactionLog, List<LegalBasis> legalBasisMappings, Map<String, String> mapOfEntityDisplayName) {
List<ReportRedactionEntry> reportEntries = new ArrayList<>();
@ -30,7 +30,7 @@ public class RedactionLogConverterService {
if (entry.isRedacted()) {
if(entry.lastChangeIsRemoved()){
if (entry.lastChangeIsRemoved()) {
return;
}
@ -42,11 +42,14 @@ public class RedactionLogConverterService {
return;
}
if(entry.isFalsePositive()){
if (entry.isFalsePositive()) {
return;
}
if (entry.getManualChanges().size() != 0 && entry.getManualChanges().get(entry.getManualChanges().size() - 1).getManualRedactionType().equals(ManualRedactionType.ADD_LOCALLY) && entry.getManualChanges()
if (entry.getManualChanges().size() != 0 && entry.getManualChanges()
.get(entry.getManualChanges().size() - 1)
.getManualRedactionType()
.equals(ManualRedactionType.ADD_LOCALLY) && entry.getManualChanges()
.get(entry.getManualChanges().size() - 1)
.getAnnotationStatus()
.equals(AnnotationStatus.DECLINED)) {
@ -58,8 +61,7 @@ public class RedactionLogConverterService {
if (pages.isEmpty() || !pages.contains(position.getPage())) {
pages.add(position.getPage());
reportEntries.add(new ReportRedactionEntry(position.getPage(), position.getTopLeft()
.getX(), position.getTopLeft()
reportEntries.add(new ReportRedactionEntry(position.getPage(), position.getTopLeft().getX(), position.getTopLeft()
.getY(), getSection(entry, position), entry.getLegalBasis() + " " + legalBasisMappings.stream()
.filter(lbm -> lbm.getReason().equalsIgnoreCase(entry.getLegalBasis()))
.findAny()
@ -68,8 +70,7 @@ public class RedactionLogConverterService {
.filter(lbm -> lbm.getReason().equalsIgnoreCase(entry.getLegalBasis()))
.findAny()
.map(LegalBasis::getDescription)
.orElse(""), checkTextForNull(entry.getTextBefore()) + entry.getValue() + checkTextForNull(entry
.getTextAfter()), entry.getValue()));
.orElse(""), checkTextForNull(entry.getTextBefore()) + entry.getValue() + checkTextForNull(entry.getTextAfter()), entry.getValue(), mapOfEntityDisplayName.get(entry.getType())));
}
}
}

View File

@ -4,8 +4,9 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@ -13,13 +14,14 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;
import com.iqser.red.service.persistence.service.v1.api.model.annotations.AnnotationStatus;
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;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.type.Type;
import com.iqser.red.service.redaction.report.v1.api.model.ReportRequestMessage;
import com.iqser.red.service.redaction.report.v1.api.model.ReportType;
import com.iqser.red.service.redaction.report.v1.api.model.StoredFileInformation;
import com.iqser.red.service.redaction.report.v1.server.client.DictionaryClient;
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;
@ -30,9 +32,7 @@ import com.iqser.red.service.redaction.report.v1.server.model.MultiFileWorkbook;
import com.iqser.red.service.redaction.report.v1.server.model.PlaceholderModel;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService;
import com.iqser.red.service.redaction.v1.model.ManualChange;
import com.iqser.red.service.redaction.v1.model.RedactionLog;
import com.iqser.red.service.redaction.v1.model.RedactionLogEntry;
import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
import io.micrometer.core.annotation.Timed;
@ -54,6 +54,7 @@ public class ReportGenerationService {
private final RedactionLogClient redactionLogClient;
private final ExcelReportGenerationService excelTemplateReportGenerationService;
private final GeneratePlaceholderService generatePlaceholderService;
private final DictionaryClient dictionaryClient;
@SneakyThrows
@ -110,20 +111,17 @@ public class ReportGenerationService {
long start = System.currentTimeMillis();
var fileStatus = fileStatusClient.getFileStatus(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j));
var fileStatus = fileStatusClient.getFileStatus(reportMessage.getDossierId(), reportMessage.getFileIds().get(j));
generatePlaceholderService.resolveFileAttributeValues(fileStatus, placeholderModel);
List<ReportRedactionEntry> reportEntries = getReportEntries(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j), fileStatus.isExcluded());
List<ReportRedactionEntry> reportEntries = getReportEntries(reportMessage.getDossierId(), reportMessage.getFileIds().get(j), fileStatus.isExcluded());
var isLastFile = j == reportMessage.getFileIds().size() - 1;
for (MultiFileWorkbook multiFileWorkbook : multiFileWorkbooks) {
if (excelModel == null) {
excelModel = excelTemplateReportGenerationService.calculateExcelModel(multiFileWorkbook.getReadWorkBook()
.getSheetAt(0));
excelModel = excelTemplateReportGenerationService.calculateExcelModel(multiFileWorkbook.getReadWorkBook().getSheetAt(0));
}
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholderModel, multiFileWorkbook.getTemplateName(), multiFileWorkbook.getWriteWorkbook(), dossier.getDossierName(), fileStatus, excelModel, isLastFile);
}
@ -139,8 +137,7 @@ public class ReportGenerationService {
}
long end = System.currentTimeMillis();
log.info("Successfully processed {}/{} fileIds for downloadId {}, took {}", i, reportMessage.getFileIds()
.size(), reportMessage.getDownloadId(), end - start);
log.info("Successfully processed {}/{} fileIds for downloadId {}, took {}", i, reportMessage.getFileIds().size(), reportMessage.getDownloadId(), end - start);
i++;
}
@ -160,10 +157,8 @@ public class ReportGenerationService {
}
private StoredFileInformation createReportFromTemplate(Dossier dossier, FileModel fileStatus,
PlaceholderModel placeholderModel, String templateName,
String downloadId, List<ReportRedactionEntry> reportEntries,
ReportTemplate reportTemplate) {
private StoredFileInformation createReportFromTemplate(Dossier dossier, FileModel fileStatus, PlaceholderModel placeholderModel, String templateName, String downloadId,
List<ReportRedactionEntry> reportEntries, ReportTemplate reportTemplate) {
if (reportTemplate.getFileName().endsWith(".xlsx")) {
byte[] excelTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId());
@ -204,15 +199,33 @@ public class ReportGenerationService {
}
RedactionLog redactionLog;
Map<String, String> mapOfEntityDisplayName = new HashMap<>();
try {
redactionLog = redactionLogClient.getRedactionLog(dossierId, fileId, new ArrayList<>(), true, false);
mapOfEntityDisplayName = fillMapOfTypeAndEntityDisplayName(dossierId);
} catch (StorageObjectDoesNotExist e) {
return new ArrayList<>();
}
var legalBasisMappings = redactionLog.getLegalBasis();
return redactionLogConverterService.convertAndSort(redactionLog, legalBasisMappings);
return redactionLogConverterService.convertAndSort(redactionLog, legalBasisMappings, mapOfEntityDisplayName);
}
private Map<String, String> fillMapOfTypeAndEntityDisplayName(String dossierId) {
List<Type> typeList = new ArrayList<>();
typeList.addAll(dictionaryClient.getAllTypesForDossier(dossierId));
typeList.addAll(dictionaryClient.getAllTypesForDossierTemplate(dossierClient.getDossierById(dossierId, false, false).getDossierTemplateId()));
Map<String, String> mapOfEntityDisplayName = new HashMap<>();
for (var type : typeList) {
mapOfEntityDisplayName.put(type.getType(), type.getLabel());
}
mapOfEntityDisplayName.put("manual", "Manual");
return mapOfEntityDisplayName;
}
}

View File

@ -1,35 +1,74 @@
package com.iqser.red.service.redaction.report.v1.server.service;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
import com.iqser.red.service.redaction.report.v1.server.model.*;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
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 javax.imageio.ImageIO;
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_LEGAL_BASIS_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.JUSTIFICATION_TEXT_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 static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.REDACTION_VALUE_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_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.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.*;
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.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
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.PlaceHolderFunctions;
import com.iqser.red.service.redaction.report.v1.server.model.PlaceholderInput;
import com.iqser.red.service.redaction.report.v1.server.model.PlaceholderModel;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@ -40,9 +79,8 @@ public class WordReportGenerationService {
@Timed("redactmanager_generateWordReport")
public XWPFDocument generateWordReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, XWPFDocument doc, FileModel fileModel,
Dossier dossier, boolean isLastFile) {
public XWPFDocument generateWordReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel, String reportTemplateName, XWPFDocument doc,
FileModel fileModel, Dossier dossier, boolean isLastFile) {
long start = System.currentTimeMillis();
@ -55,7 +93,7 @@ public class WordReportGenerationService {
XWPFTable table = getRedactionTable(doc);
var placeholderFunctions = computePlaceholderPos(table, placeholderModel);
addRedactionEntryRows(table, reportEntries, fileModel.getFilename(), placeholderModel, placeholderFunctions);
replaceTextPlaceholders(doc, placeholderModel, dossier.getDossierName(), fileModel.getFilename(), table, reportEntries);
@ -105,8 +143,7 @@ public class WordReportGenerationService {
}
private String getPlaceholderValue(String placeholder, String dossierName, String filename,
PlaceholderModel placeholderModel,
private String getPlaceholderValue(String placeholder, String dossierName, String filename, PlaceholderModel placeholderModel,
List<ReportRedactionEntry> reportRedactionEntries) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
@ -143,8 +180,7 @@ public class WordReportGenerationService {
}
private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs,
ImagePlaceholder imagePlaceholder) {
private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs, ImagePlaceholder imagePlaceholder) {
for (XWPFParagraph p : paragraphs) {
String paragraphText = p.getText();
@ -168,8 +204,7 @@ public class WordReportGenerationService {
@Timed("redactmanager_word-replaceTextPlaceholders")
public void replaceTextPlaceholders(XWPFDocument doc, PlaceholderModel placeholderModel, String dossierName,
String fileName, XWPFTable tableToSkip,
public void replaceTextPlaceholders(XWPFDocument doc, PlaceholderModel placeholderModel, String dossierName, String fileName, XWPFTable tableToSkip,
List<ReportRedactionEntry> reportRedactionEntries) {
Map<String, String> placeHolderValueMap = new HashMap<>();
@ -193,8 +228,7 @@ public class WordReportGenerationService {
}
private void replacePlaceholderInParagraph(List<XWPFParagraph> paragraphs,
Map<String, String> placeholderValueMap) {
private void replacePlaceholderInParagraph(List<XWPFParagraph> paragraphs, Map<String, String> placeholderValueMap) {
for (XWPFParagraph p : paragraphs) {
String paragraphText = p.getText();
@ -256,7 +290,7 @@ 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) || text.contains(REDACTION_VALUE_PLACEHOLDER) || text.contains(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER) || text.contains(JUSTIFICATION_TEXT_PLACEHOLDER) || text.contains(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER) || text.contains(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER) || text.contains("{{") && text.contains("}}");
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) || text.contains(REDACTION_VALUE_PLACEHOLDER) || text.contains(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER) || text.contains(JUSTIFICATION_TEXT_PLACEHOLDER) || text.contains(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER) || text.contains(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER) || text.contains(REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER) || text.contains("{{") && text.contains("}}");
}
@ -283,42 +317,35 @@ public class WordReportGenerationService {
@Timed("redactmanager_word-addRedactionEntryRows")
private void addRedactionEntryRows(XWPFTable table, List<ReportRedactionEntry> reportEntries, String filename,
PlaceholderModel placeholderModel, PlaceHolderFunctions placeHolderFunctions) {
private void addRedactionEntryRows(XWPFTable table, List<ReportRedactionEntry> reportEntries, String filename, PlaceholderModel placeholderModel,
PlaceHolderFunctions placeHolderFunctions) {
if (table == null) {
return;
}
if (placeHolderFunctions.getFoundPlaceHolder()
.contains(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) {
if (placeHolderFunctions.getFoundPlaceHolder().contains(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) {
var redactionsPerJustification = getRedactionsPerJustification(reportEntries);
for (Map.Entry<String, List<ReportRedactionEntry>> entry : redactionsPerJustification.entrySet()) {
XWPFTableRow row = table.createRow();
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
.entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue()
.apply(new PlaceholderInput(filename, null, placeholderModel, entry)));
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder().entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue().apply(new PlaceholderInput(filename, null, placeholderModel, entry)));
}
}
} else {
reportEntries.forEach(entry -> {
XWPFTableRow row = table.createRow();
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
.entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder().entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue().apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
}
});
}
}
private Function<PlaceholderInput, String> getFunctionForPlaceHolder(String placeholder,
Set<String> foundPlaceholders,
PlaceholderModel placeholderModel) {
private Function<PlaceholderInput, String> getFunctionForPlaceHolder(String placeholder, Set<String> foundPlaceholders, PlaceholderModel placeholderModel) {
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
foundPlaceholders.add(FILE_NAME_PLACEHOLDER);
@ -358,19 +385,16 @@ public class WordReportGenerationService {
}
if (placeholder.equals(REDACTION_VALUE_PLACEHOLDER)) {
foundPlaceholders.add(REDACTION_VALUE_PLACEHOLDER);
return (input) -> input.getEntry().getValue() != null ? input.getEntry()
.getValue()
.replaceAll("\n", " ")
.replaceAll(" ", " ") : "";
return (input) -> input.getEntry().getValue() != null ? input.getEntry().getValue().replaceAll("\n", " ").replaceAll(" ", " ") : "";
}
if (placeholder.equals(REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER)) {
foundPlaceholders.add(REDACTION_ENTITY_DISPLAY_NAME_PLACEHOLDER);
return (input) -> input.getEntry().getEntityDisplayName();
}
if (placeholder.equals(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) {
foundPlaceholders.add(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER);
return (input) -> computePageRanges(input.getRedactionsPerJustificationEntry()
.getValue()
.stream()
.map(ReportRedactionEntry::getPage)
.collect(Collectors.toSet()));
return (input) -> computePageRanges(input.getRedactionsPerJustificationEntry().getValue().stream().map(ReportRedactionEntry::getPage).collect(Collectors.toSet()));
}
if (placeholder.equals(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER)) {
@ -467,12 +491,9 @@ public class WordReportGenerationService {
}
private Map<String, List<ReportRedactionEntry>> getRedactionsPerJustification(
List<ReportRedactionEntry> reportRedactionEntryList) {
private Map<String, List<ReportRedactionEntry>> getRedactionsPerJustification(List<ReportRedactionEntry> reportRedactionEntryList) {
return reportRedactionEntryList.stream()
.sorted(Comparator.comparing(ReportRedactionEntry::getPage))
.collect(Collectors.groupingBy(ReportRedactionEntry::getJustification));
return reportRedactionEntryList.stream().sorted(Comparator.comparing(ReportRedactionEntry::getPage)).collect(Collectors.groupingBy(ReportRedactionEntry::getJustification));
}

View File

@ -61,7 +61,7 @@ public class PlaceholderTest {
// Assert
Assert.assertNotNull(placeholders);
Assert.assertEquals(placeholders.size(), 16);
Assert.assertEquals(placeholders.size(), 17);
}
@Test

View File

@ -1,11 +1,47 @@
package com.iqser.red.service.redaction.report.v1.server;
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.FILE_NAME_PLACEHOLDER;
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_PLACEHOLDER;
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_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.SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.utils.OsUtils.getTemporaryDirectory;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
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.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
import com.amazonaws.services.s3.AmazonS3;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.legalbasis.LegalBasis;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.type.Type;
import com.iqser.red.service.redaction.report.v1.server.client.DictionaryClient;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesClient;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesConfigClient;
import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient;
@ -21,33 +57,8 @@ import com.iqser.red.service.redaction.report.v1.server.service.WordReportGenera
import com.iqser.red.service.redaction.report.v1.server.storage.ReportStorageService;
import com.iqser.red.service.redaction.v1.model.RedactionLog;
import com.iqser.red.storage.commons.service.StorageService;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
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;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.FileOutputStream;
import java.util.*;
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.FILE_NAME_PLACEHOLDER;
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_PLACEHOLDER;
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_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.SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.utils.OsUtils.getTemporaryDirectory;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ -92,14 +103,18 @@ public class RedactionReportIntegrationTest {
@Autowired
private GeneratePlaceholderService generatePlaceholderService;
@MockBean
private DictionaryClient dictionaryClient;
@Test
@SneakyThrows
public void testWordJustificationAppendixA1(){
public void testWordJustificationAppendixA1() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A1.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
@ -107,14 +122,7 @@ public class RedactionReportIntegrationTest {
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
FileModel fileModel = FileModel.builder().filename("filename").build();
var placeholders = buildPlaceHolderModel(
Map.of("{{dossier.attribute.ActiveSubstance}}","Aktive Substanz \n Test Return",
"{{dossier.attribute.RapporteurMemberState}}","Reporter Status",
"{{dossier.attribute.Name}}","Dossier Name",
"{{dossier.attribute.Company}}","Firma",
"{{dossier.attribute.Date}}","2021-11-09T23:00:00.000Z"),
Map.of("{{file.attribute.placeholder}}", "Test"),
List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}",IOUtils.toByteArray(imageResource.getInputStream()))));
var placeholders = buildPlaceHolderModel(Map.of("{{dossier.attribute.ActiveSubstance}}", "Aktive Substanz \n Test Return", "{{dossier.attribute.RapporteurMemberState}}", "Reporter Status", "{{dossier.attribute.Name}}", "Dossier Name", "{{dossier.attribute.Company}}", "Firma", "{{dossier.attribute.Date}}", "2021-11-09T23:00:00.000Z"), Map.of("{{file.attribute.placeholder}}", "Test"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}", IOUtils.toByteArray(imageResource.getInputStream()))));
XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream());
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
@ -124,14 +132,15 @@ public class RedactionReportIntegrationTest {
}
}
@Test
@SneakyThrows
public void testWordJustificationAppendixA2(){
public void testWordJustificationAppendixA2() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A2.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
@ -139,14 +148,7 @@ public class RedactionReportIntegrationTest {
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
FileModel fileModel = FileModel.builder().filename("filename").build();
var placeholders = buildPlaceHolderModel(
Map.of("{{dossier.attribute.ActiveSubstance}}","Aktive Substanz \n Test Return",
"{{dossier.attribute.RapporteurMemberState}}","Reporter Status",
"{{dossier.attribute.Name}}","Dossier Name",
"{{dossier.attribute.Company}}","Firma",
"{{dossier.attribute.Date}}","2021-11-09T23:00:00.000Z"),
Map.of("{{file.attribute.placeholder}}", "Test"),
List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}",IOUtils.toByteArray(imageResource.getInputStream()))));
var placeholders = buildPlaceHolderModel(Map.of("{{dossier.attribute.ActiveSubstance}}", "Aktive Substanz \n Test Return", "{{dossier.attribute.RapporteurMemberState}}", "Reporter Status", "{{dossier.attribute.Name}}", "Dossier Name", "{{dossier.attribute.Company}}", "Firma", "{{dossier.attribute.Date}}", "2021-11-09T23:00:00.000Z"), Map.of("{{file.attribute.placeholder}}", "Test"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}", IOUtils.toByteArray(imageResource.getInputStream()))));
XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream());
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
@ -156,14 +158,15 @@ public class RedactionReportIntegrationTest {
}
}
@Test
@SneakyThrows
public void testWordIUCLIDFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
FileModel fileStatus = FileModel.builder().filename("filename").build();
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
@ -181,14 +184,15 @@ public class RedactionReportIntegrationTest {
}
}
@Test
@SneakyThrows
public void testWordSeedReportSingleFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
FileModel fileStatus = FileModel.builder().filename("filename").build();
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
@ -211,13 +215,13 @@ public class RedactionReportIntegrationTest {
@SneakyThrows
public void testWordSeedReportMultiFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
RedactionLog redactionLogSecondFile = objectMapper.readValue(new ClassPathResource("files/excelReportRedactionLog.json").getInputStream(), RedactionLog.class);
List<ReportRedactionEntry> reportEntriesSecondFile = redactionLogConverterService.convertAndSort(redactionLogSecondFile, legalBasisMapping);
List<ReportRedactionEntry> reportEntriesSecondFile = redactionLogConverterService.convertAndSort(redactionLogSecondFile, legalBasisMapping, new HashMap<>());
FileModel fileModelSecondFile = FileModel.builder().filename("secondFile").build();
FileModel fileStatus = FileModel.builder().filename("filename").build();
@ -238,15 +242,14 @@ public class RedactionReportIntegrationTest {
}
@Test
@SneakyThrows
public void testWord6464JustificationAppendix(){
public void testWord6464JustificationAppendix() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, new HashMap<>());
var wordTemplateResource = new ClassPathResource("templates/6464 appendix_b EFSA dRAR justification.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
@ -254,14 +257,7 @@ public class RedactionReportIntegrationTest {
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
FileModel fileModel = FileModel.builder().filename("filename").build();
var placeholders = buildPlaceHolderModel(
Map.of("{{dossier.attribute.ActiveSubstance}}","Aktive Substanz \n Test Return",
"{{dossier.attribute.RapporteurMemberState}}","Reporter Status",
"{{dossier.attribute.Name}}","Dossier Name",
"{{dossier.attribute.Company}}","Firma",
"{{dossier.attribute.Date}}","2021-11-09T23:00:00.000Z"),
Map.of("{{file.attribute.placeholder}}", "Test"),
List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}",IOUtils.toByteArray(imageResource.getInputStream()))));
var placeholders = buildPlaceHolderModel(Map.of("{{dossier.attribute.ActiveSubstance}}", "Aktive Substanz \n Test Return", "{{dossier.attribute.RapporteurMemberState}}", "Reporter Status", "{{dossier.attribute.Name}}", "Dossier Name", "{{dossier.attribute.Company}}", "Firma", "{{dossier.attribute.Date}}", "2021-11-09T23:00:00.000Z"), Map.of("{{file.attribute.placeholder}}", "Test"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}", IOUtils.toByteArray(imageResource.getInputStream()))));
XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream());
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
@ -274,11 +270,13 @@ public class RedactionReportIntegrationTest {
@Test
@SneakyThrows
public void testExcelTemplateReportGenerationSingleFile() {
public void testExcelTemplateReportGenerationSingleFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLogWithManualRedactions.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
Map<String, String> mapOfEntityDisplayName = createEntityDisplayNames(redactionLog);
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, mapOfEntityDisplayName);
FileModel fileModel = FileModel.builder().filename("filename").build();
@ -288,10 +286,9 @@ public class RedactionReportIntegrationTest {
XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
writeWorkbook.createSheet("Sheet1");
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName" ,fileModel, excelModel, true);
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName", fileModel, excelModel, true);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_justification.xlsx")) {
@ -300,14 +297,15 @@ public class RedactionReportIntegrationTest {
}
@Test
@SneakyThrows
public void testExcelTemplateReportGenerationMultiFile() {
public void testExcelTemplateReportGenerationMultiFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
Map<String, String> mapOfEntityDisplayName = createEntityDisplayNames(redactionLog);
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, mapOfEntityDisplayName);
FileModel fileModel = FileModel.builder().filename("filename").build();
@ -317,15 +315,14 @@ public class RedactionReportIntegrationTest {
XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
writeWorkbook.createSheet("Sheet1");
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName" ,fileModel, excelModel, false);
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName", fileModel, excelModel, false);
RedactionLog redactionLogSecondFile = objectMapper.readValue(new ClassPathResource("files/excelReportRedactionLog.json").getInputStream(), RedactionLog.class);
List<ReportRedactionEntry> reportEntriesSecondFile = redactionLogConverterService.convertAndSort(redactionLogSecondFile, legalBasisMapping);
List<ReportRedactionEntry> reportEntriesSecondFile = redactionLogConverterService.convertAndSort(redactionLogSecondFile, legalBasisMapping, mapOfEntityDisplayName);
FileModel fileModelSecondFile = FileModel.builder().filename("secondFile").build();
excelTemplateReportGenerationService.generateExcelReport(reportEntriesSecondFile, placeholders, "test", writeWorkbook,"dossierName" , fileModelSecondFile, excelModel, true);
excelTemplateReportGenerationService.generateExcelReport(reportEntriesSecondFile, placeholders, "test", writeWorkbook, "dossierName", fileModelSecondFile, excelModel, true);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_multifile.xlsx")) {
@ -336,25 +333,26 @@ public class RedactionReportIntegrationTest {
@Test
@SneakyThrows
public void testExcelPlaceholders() {
public void testExcelPlaceholders() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {
});
Map<String, String> mapOfEntityDisplayName = createEntityDisplayNames(redactionLog);
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping, mapOfEntityDisplayName);
var imageResource = new ClassPathResource("files/exampleImage.jpg");
FileModel fileModel = FileModel.builder().filename("filename").build();
ClassPathResource templateResource = new ClassPathResource("templates/Excel Report_PlaceholderTest.xlsx");
var placeholders = buildPlaceHolderModel(Map.of("{{dossier_attribute.test1}}", "replaced_dossier_test1"), Map.of("{{file_attribute.test1}}", "replaced_file_test1", "{{file_attribute.test2}}", "replaced_file_test2"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}",IOUtils.toByteArray(imageResource.getInputStream()))));
var placeholders = buildPlaceHolderModel(Map.of("{{dossier_attribute.test1}}", "replaced_dossier_test1"), Map.of("{{file_attribute.test1}}", "replaced_file_test1", "{{file_attribute.test2}}", "replaced_file_test2"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}", IOUtils.toByteArray(imageResource.getInputStream()))));
XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
writeWorkbook.createSheet("Sheet1");
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook,"dossierName" , fileModel, excelModel, true);
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName", fileModel, excelModel, true);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/PlaceholderTest_justification.xlsx")) {
@ -363,13 +361,37 @@ public class RedactionReportIntegrationTest {
}
private Map<String, String> createEntityDisplayNames(RedactionLog redactionLog) {
Type t1 = new Type();
t1.setLabel("Type 1");
Type t2 = new Type();
t2.setLabel("Type 2");
Type t3 = new Type();
t3.setLabel("Type 3");
List<Type> ednList = new ArrayList<>(Arrays.asList(t1, t2, t3));
Mockito.when(dictionaryClient.getAllTypesForDossier(Mockito.anyString())).thenReturn(ednList);
Mockito.when(dictionaryClient.getAllTypesForDossierTemplate(Mockito.any())).thenReturn(ednList);
Map<String, String> entityDisplayNames = new HashMap<>();
for (var entry : redactionLog.getRedactionLogEntry()) {
if (entry.getType().equals("manual")) {
entityDisplayNames.put(entry.getType(), "Manual");
} else {
entityDisplayNames.put(entry.getType(), ednList.get((int) Math.floor(Math.random() * ednList.size())).getLabel());
}
}
return entityDisplayNames;
}
private PlaceholderModel buildPlaceHolderModel(Map<String, String> dossierAttributes, Map<String, String> fileAttributes, List<ImagePlaceholder> imagePlaceholders) {
private PlaceholderModel buildPlaceHolderModel(Map<String, String> dossierAttributes, Map<String, String> fileAttributes, List<ImagePlaceholder> imagePlaceholders){
var defaultPlaceHolder = new HashSet(Set.of(FILE_NAME_PLACEHOLDER, FORMAT_DATE_ISO_PLACEHOLDER, FORMAT_DATE_GER_PLACEHOLDER, FORMAT_DATE_ENG_PLACEHOLDER, FORMAT_TIME_ISO_PLACEHOLDER, DOSSIER_NAME_PLACEHOLDER, IUCLID_FUNCTION_PLACEHOLDER, SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER, SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER));
defaultPlaceHolder.addAll(dossierAttributes.keySet());
defaultPlaceHolder.addAll(fileAttributes.keySet());
return new PlaceholderModel(defaultPlaceHolder, imagePlaceholders, dossierAttributes, null, fileAttributes);
}
}