Pull request #148: RED-4036

Merge in RED/redaction-report-service from RED-4036 to master

* commit '93fcc061f94a33d4c23175291c922c4af95225c5':
  RED-4036: Reenabled all replacememts in excel
  RED-4036: Cleanup unittests
  RED-4036: Reenabled seeds report
This commit is contained in:
Dominique Eiflaender 2022-05-20 14:55:50 +02:00
commit 813a4384ad
20 changed files with 578 additions and 847 deletions

View File

@ -8,9 +8,13 @@ import org.springframework.boot.actuate.autoconfigure.security.servlet.Managemen
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
@EnableAsync @EnableAsync
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class}) @SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
@Import({DefaultWebMvcConfiguration.class, MessagingConfiguration.class}) @Import({DefaultWebMvcConfiguration.class, MessagingConfiguration.class})
@ -27,4 +31,9 @@ public class Application {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
} }

View File

@ -0,0 +1,13 @@
package com.iqser.red.service.redaction.report.v1.server.model;
import org.apache.poi.ss.usermodel.Cell;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class ExcelCell {
private Cell cell;
private int columnWidth;
private int rowHeights;
}

View File

@ -1,6 +1,8 @@
package com.iqser.red.service.redaction.report.v1.server.model; package com.iqser.red.service.redaction.report.v1.server.model;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
@ -15,10 +17,13 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public class ExcelModel { public class ExcelModel {
private Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos; private Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos;
private int placeholderRow; private int redactionPlaceholderRow;
private Map<Integer, Integer> cellWidths = new HashMap<>(); private Map<Integer, Integer> cellWidths = new HashMap<>();
private Map<CellIdentifier, Cell> cellsToCopy = new HashMap<>(); private Map<CellIdentifier, Cell> cellsToCopyBeforeRedactionPlaceholderRow = new HashMap<>();
private boolean copyDone; private Map<CellIdentifier, Cell> cellsToCopyAfterRedactionPlaceholderRow = new HashMap<>();
private List<Integer> writtenRows = new ArrayList<>();
private boolean rowsBeforeRedactionEntryRowsAdded;
} }

View File

@ -0,0 +1,16 @@
package com.iqser.red.service.redaction.report.v1.server.model;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class PlaceHolderFunctions {
private Map<Integer, Function<PlaceholderInput, String>> functionPerPlaceHolder;
private Set<String> foundPlaceHolder;
}

View File

@ -3,7 +3,20 @@ package com.iqser.red.service.redaction.report.v1.server.model;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public interface PlaceholderInput { import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class PlaceholderInput {
private String filename;
private ReportRedactionEntry entry;
private PlaceholderModel placeholderModel;
private Map.Entry<String, List<ReportRedactionEntry>> redactionsPerJustificationEntry;
} }

View File

@ -1,20 +1,22 @@
package com.iqser.red.service.redaction.report.v1.server.model; package com.iqser.red.service.redaction.report.v1.server.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@RequiredArgsConstructor
public class PlaceholderModel { public class PlaceholderModel {
private List<String> placeholders; private final Set<String> placeholders;
private List<ImagePlaceholder> imagePlaceholders; private final List<ImagePlaceholder> imagePlaceholders;
private Map<String, String> dossierAttributesPlaceholder; private final Map<String, String> dossierAttributesValueByPlaceholder;
private Map<String, String> fileAttributePlaceHolders; private final Map<String, String> fileAttributeIdByPlaceholder;
private Map<String, String> fileAttributeValueByPlaceholder;
} }

View File

@ -1,13 +0,0 @@
package com.iqser.red.service.redaction.report.v1.server.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class TextPlaceholderInput implements PlaceholderInput {
private String filename;
private ReportRedactionEntry entry;
}

View File

@ -26,8 +26,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -40,10 +38,6 @@ import java.util.regex.Pattern;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import com.iqser.red.service.redaction.report.v1.server.model.CellIdentifier;
import com.iqser.red.service.redaction.report.v1.server.model.ExcelModel;
import com.iqser.red.service.redaction.report.v1.server.model.PlaceholderModel;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.ClientAnchor;
@ -53,21 +47,17 @@ import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.DossierAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttribute;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttributeType;
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.dossier.file.FileModel;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesClient; import com.iqser.red.service.redaction.report.v1.server.model.CellIdentifier;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesConfigClient; import com.iqser.red.service.redaction.report.v1.server.model.ExcelModel;
import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient;
import com.iqser.red.service.redaction.report.v1.server.model.ImagePlaceholder; import com.iqser.red.service.redaction.report.v1.server.model.ImagePlaceholder;
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 com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.model.TextPlaceholderInput;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -75,71 +65,94 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@SuppressWarnings("PMD") public class ExcelReportGenerationService {
public class ExcelTemplateReportGenerationService {
@SuppressWarnings("checkstyle:ParameterAssignment") @Timed("generateExcelReport")
public ExcelModel generateReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel, public void generateExcelReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, SXSSFWorkbook workbook, FileModel fileStatus, String reportTemplateName, SXSSFWorkbook workbook, String dossierName,
Dossier dossier, boolean isLastFile, ExcelModel excelModel) { FileModel fileModel, ExcelModel excelModel, boolean isLastFile) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
try { try {
for (Sheet sheet : workbook) { for (Sheet sheet : workbook) {
if(!excelModel.isCopyDone()) {
if (!excelModel.isRowsBeforeRedactionEntryRowsAdded()) {
for (Map.Entry<Integer, Integer> colummWidthEntry : excelModel.getCellWidths().entrySet()) { for (Map.Entry<Integer, Integer> colummWidthEntry : excelModel.getCellWidths().entrySet()) {
sheet.setColumnWidth(colummWidthEntry.getKey(), colummWidthEntry.getValue()); sheet.setColumnWidth(colummWidthEntry.getKey(), colummWidthEntry.getValue());
} }
Set<Integer> createdRows = new HashSet<>(); addRows(workbook, sheet, excelModel.getCellsToCopyBeforeRedactionPlaceholderRow(), 0, placeholderModel, dossierName, fileModel.getFilename());
for (Map.Entry<CellIdentifier, Cell> cellsToCopyEntry : excelModel.getCellsToCopy().entrySet()) { excelModel.setRowsBeforeRedactionEntryRowsAdded(true);
if (!createdRows.contains(cellsToCopyEntry.getKey().getRowIndex())) {
sheet.createRow(cellsToCopyEntry.getKey().getRowIndex());
createdRows.add(cellsToCopyEntry.getKey().getRowIndex());
} }
var createdCell = sheet.getRow(cellsToCopyEntry.getKey().getRowIndex()) addRedactionEntryRows(sheet, reportEntries, fileModel.getFilename(), excelModel, placeholderModel);
.createCell(cellsToCopyEntry.getKey().getColumnIndex());
createdCell.setCellValue(cellsToCopyEntry.getValue().getStringCellValue()); if (isLastFile) {
CellStyle newCellStyle = workbook.createCellStyle(); addRows(workbook, sheet, excelModel.getCellsToCopyAfterRedactionPlaceholderRow(), excelModel.getWrittenRows()
newCellStyle.cloneStyleFrom(cellsToCopyEntry.getValue().getCellStyle()); .size(), placeholderModel, dossierName, fileModel.getFilename());
createdCell.setCellStyle(newCellStyle);
} }
excelModel.setCopyDone(true);
} }
excelModel = addEntryRows(sheet, reportEntries, fileStatus.getFilename(), isLastFile, excelModel); log.info("Report Generation took: {} for file with id {}, pageCount: {}, redactionLogEntryCount: {}, reportName: {}, className: {}", System.currentTimeMillis() - start, fileModel.getId(), fileModel.getNumberOfPages(), reportEntries.size(), reportTemplateName, getClass().getSimpleName());
// for (ImagePlaceholder imagePlaceholder : placeholderModel.getImagePlaceholders()){
// replacePlaceholdersForImagePlaceholder(workbook, sheet, imagePlaceholder);
// }
// for (String placeholder : placeholderModel.getPlaceholders()) {
// String placeholderValue = getPlaceholderValue(placeholder, dossier, fileStatus, placeholderModel.getFileAttributePlaceHolders(), placeholderModel.getDossierAttributesPlaceholder());
// if (placeholderValue != null) {
// replacePlaceholdersForTextPlaceholder(sheet, placeholder, placeholderValue);
// }
// }
}
long end = System.currentTimeMillis();
log.info("Report Generation took: {} for file with id {}, pageCount: {}, redactionLogEntryCount: {}, reportName: {}, className: {}", end - start, fileStatus.getId(), fileStatus.getNumberOfPages(), reportEntries.size(), reportTemplateName, getClass().getSimpleName());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return excelModel;
} }
public ExcelModel caculateExcelModel(Sheet sheet) { @SneakyThrows
@Timed("excel-addRows")
private void addRows(SXSSFWorkbook workbook, Sheet sheet, Map<CellIdentifier, Cell> copiedCells,
int numberOfRowsToShift, PlaceholderModel placeholderModel, String dossierName,
String filename) {
Set<Integer> createdCopyRows = new HashSet<>();
for (Map.Entry<CellIdentifier, Cell> cellsToCopyEntry : copiedCells.entrySet()) {
int indexToAddRow = cellsToCopyEntry.getKey().getRowIndex() + numberOfRowsToShift;
if (!createdCopyRows.contains(indexToAddRow)) {
sheet.createRow(indexToAddRow);
createdCopyRows.add(indexToAddRow);
}
var createdCell = sheet.getRow(indexToAddRow).createCell(cellsToCopyEntry.getKey().getColumnIndex());
createdCell.setCellValue(cellsToCopyEntry.getValue().getStringCellValue());
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.cloneStyleFrom(cellsToCopyEntry.getValue().getCellStyle());
createdCell.setCellStyle(newCellStyle);
createdCell.getRow().setHeight(cellsToCopyEntry.getValue().getRow().getHeight());
replacePlaceholders(createdCell, placeholderModel, dossierName, filename);
replacePlaceholdersForImagePlaceholder(workbook, sheet, createdCell, placeholderModel);
}
}
private void replacePlaceholders(Cell cell, PlaceholderModel placeholderModel, String dossierName,
String filename) {
for (String placeholder : placeholderModel.getPlaceholders()) {
String placeholderValue = getPlaceholderValue(placeholder, dossierName, filename, placeholderModel);
if (placeholderValue != null) {
replacePlaceholdersForCell(cell, placeholder, placeholderValue);
}
}
}
@Timed("calculateExcelModel")
public ExcelModel calculateExcelModel(Sheet sheet) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos = new HashMap<>(); Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos = new HashMap<>();
Map<Integer, Integer> columnWidths = new HashMap<>(); Map<Integer, Integer> columnWidths = new HashMap<>();
Map<CellIdentifier, Cell> cellsToCopy = new HashMap<>(); Map<CellIdentifier, Cell> cellsToCopyBeforePlaceholderRow = new HashMap<>();
Map<CellIdentifier, Cell> cellsToCopyAfterPlaceholderRow = new HashMap<>();
int placeholderRow = -1; int placeholderRow = -1;
for (int j = 0; j < sheet.getLastRowNum() + 1; j++) { for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {
Row actualRow = sheet.getRow(j); Row actualRow = sheet.getRow(j);
@ -147,43 +160,51 @@ public class ExcelTemplateReportGenerationService {
for (int i = 0; i < actualRow.getLastCellNum(); i++) { for (int i = 0; i < actualRow.getLastCellNum(); i++) {
Cell cell = sheet.getRow(j).getCell(i); Cell cell = sheet.getRow(j).getCell(i);
if (cell != null) { if (cell != null) {
if (containsRedactionPlaceholder(cell.getStringCellValue())) {
int columnWidth = sheet.getColumnWidth(i); int columnWidth = sheet.getColumnWidth(i);
columnWidths.put(i, columnWidth); columnWidths.put(i, columnWidth);
if (containsRedactionPlaceholder(cell.getStringCellValue())) {
placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getStringCellValue())); placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getStringCellValue()));
placeholderRow = j; placeholderRow = j;
} else { } else {
cellsToCopy.put(new CellIdentifier(j, i), cell); if (placeholderRow == -1) {
cellsToCopyBeforePlaceholderRow.put(new CellIdentifier(j, i), cell);
} else {
cellsToCopyAfterPlaceholderRow.put(new CellIdentifier(j, i), cell);
}
} }
} }
} }
} }
} }
log.debug("Calculate Placeholder Cells took: {}", System.currentTimeMillis() - start); log.debug("Calculate Placeholder Cells took: {}", System.currentTimeMillis() - start);
return new ExcelModel(placeholderCellPos, placeholderRow, columnWidths, cellsToCopy, false); return new ExcelModel(placeholderCellPos, placeholderRow, columnWidths, cellsToCopyBeforePlaceholderRow, cellsToCopyAfterPlaceholderRow, new ArrayList<>(), false);
} }
@SuppressWarnings("checkstyle:ParameterAssignment") @Timed("excel-addRedactionEntryRows")
private ExcelModel addEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename, private void addRedactionEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename,
boolean isLastFile, ExcelModel excelModel) { ExcelModel excelModel, PlaceholderModel placeholderModel) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
AtomicInteger rowIndex = new AtomicInteger(excelModel.getPlaceholderRow()); AtomicInteger rowIndex = new AtomicInteger(excelModel.getRedactionPlaceholderRow());
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos = excelModel.getPlaceholderCellPos(); Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos = excelModel.getPlaceholderCellPos();
reportEntries.forEach(entry -> { reportEntries.forEach(entry -> {
sheet.createRow(rowIndex.get()); sheet.createRow(rowIndex.get());
for (Map.Entry<Integer, Function<TextPlaceholderInput, String>> entry1 : placeholderCellPos.entrySet()) { 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 cell = sheet.getRow(rowIndex.get()).createCell(entry1.getKey());
cell.setCellValue(entry1.getValue().apply(new TextPlaceholderInput(filename, entry))); cell.setCellValue(entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
} }
rowIndex.getAndIncrement(); rowIndex.getAndIncrement();
}); });
log.debug("Adding rows took: {}", System.currentTimeMillis() - start);
excelModel.setPlaceholderRow(rowIndex.getAndIncrement()); excelModel.setRedactionPlaceholderRow(rowIndex.getAndIncrement());
return excelModel;
log.debug("Adding rows took: {}", System.currentTimeMillis() - start);
} }
@ -193,10 +214,10 @@ public class ExcelTemplateReportGenerationService {
} }
private Function<TextPlaceholderInput, String> getFunctionForPlaceHolder(String placeholder) { private Function<PlaceholderInput, String> getFunctionForPlaceHolder(String placeholder) {
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) { if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return TextPlaceholderInput::getFilename; return PlaceholderInput::getFilename;
} }
if (placeholder.equals(PAGE_PLACEHOLDER)) { if (placeholder.equals(PAGE_PLACEHOLDER)) {
return (input) -> String.valueOf(input.getEntry().getPage()); return (input) -> String.valueOf(input.getEntry().getPage());
@ -228,15 +249,12 @@ public class ExcelTemplateReportGenerationService {
.replaceAll("\n", " ") .replaceAll("\n", " ")
.replaceAll(" ", " ") : ""; .replaceAll(" ", " ") : "";
} }
// TODO in case placeholder is invalid -> do not replace with empty string but throw previus exception
return (input) -> ""; return (input) -> "";
} }
private void replacePlaceholdersForTextPlaceholder(Sheet sheet, String search, String replace) { private void replacePlaceholdersForCell(Cell cell, String search, String replace) {
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getStringCellValue().contains(search)) { if (cell.getStringCellValue().contains(search)) {
String safeToUseInReplaceAllString = Pattern.quote(search); String safeToUseInReplaceAllString = Pattern.quote(search);
String escapedReplace = Matcher.quoteReplacement(replace); String escapedReplace = Matcher.quoteReplacement(replace);
@ -244,26 +262,21 @@ public class ExcelTemplateReportGenerationService {
cell.setCellValue(str); cell.setCellValue(str);
} }
} }
}
}
private void replacePlaceholdersForImagePlaceholder(XSSFWorkbook workbook, Sheet sheet, private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell,
ImagePlaceholder imagePlaceholder) throws IOException { PlaceholderModel placeholderModel) throws IOException {
for (ImagePlaceholder imagePlaceholder : placeholderModel.getImagePlaceholders()) {
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getStringCellValue().contains(imagePlaceholder.getPlaceholder())) { if (cell.getStringCellValue().contains(imagePlaceholder.getPlaceholder())) {
try (ByteArrayInputStream is = new ByteArrayInputStream(imagePlaceholder.getImage())) { try (ByteArrayInputStream is = new ByteArrayInputStream(imagePlaceholder.getImage())) {
// ByteArrayInputStream scaledImage = getScaledImage(is, PixelUtil.widthUnits2Pixel((short) sheet.getColumnWidth(cell.getColumnIndex())), PixelUtil.heightUnits2Pixel(cell.getRow().getHeight()));
// is.reset();
double factor = calculateScale(is, PixelUtil.widthUnits2Pixel((short) sheet.getColumnWidth(cell.getColumnIndex())), PixelUtil.heightUnits2Pixel(cell.getRow() double factor = calculateScale(is, PixelUtil.widthUnits2Pixel((short) sheet.getColumnWidth(cell.getColumnIndex())), PixelUtil.heightUnits2Pixel(cell.getRow()
.getHeight())); .getHeight()));
is.reset(); is.reset();
int pictureIdx = workbook.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG); int pictureIdx = workbook.addPicture(is.readAllBytes(), SXSSFWorkbook.PICTURE_TYPE_JPEG);
is.reset(); is.reset();
//Returns an object that handles instantiating concrete classes //Returns an object that handles instantiating concrete classes
@ -282,7 +295,7 @@ public class ExcelTemplateReportGenerationService {
picture.resize(factor); picture.resize(factor);
cell.setCellValue(""); cell.setCellValue("");
}
} }
} }
} }
@ -311,9 +324,8 @@ public class ExcelTemplateReportGenerationService {
} }
private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus, private String getPlaceholderValue(String placeholder, String dossierName, String filename,
Map<String, String> fileAttributePlaceholders, PlaceholderModel placeholderModel) {
Map<String, String> dossierAttributesPlaceholders) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) { if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
return OffsetDateTime.now().format(FORMAT_DATE_ISO); return OffsetDateTime.now().format(FORMAT_DATE_ISO);
@ -328,23 +340,19 @@ public class ExcelTemplateReportGenerationService {
return OffsetDateTime.now().format(FORMAT_TIME_ISO); return OffsetDateTime.now().format(FORMAT_TIME_ISO);
} }
if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) { if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) {
return project.getDossierName(); return dossierName;
} }
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) { if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return null; return filename;
}
if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) {
return placeholderModel.getFileAttributeValueByPlaceholder().get(placeholder);
} }
if (fileAttributePlaceholders.containsKey(placeholder)) {
String id = fileAttributePlaceholders.get(placeholder);
if (fileStatus.getFileAttributes().containsKey(id)) { if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
return fileStatus.getFileAttributes().get(id); return placeholderModel.getDossierAttributesValueByPlaceholder().get(placeholder);
} else {
return null;
}
}
if (dossierAttributesPlaceholders.containsKey(placeholder)) {
return dossierAttributesPlaceholders.get(placeholder);
} }
return null; return null;
} }

View File

@ -14,8 +14,10 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -23,6 +25,7 @@ import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.Do
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.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttribute; import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttribute;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttributeType; import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttributeType;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesClient; 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.DossierAttributesConfigClient;
import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient; import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient;
@ -42,15 +45,13 @@ public class GeneratePlaceholderService {
public PlaceholderModel buildPlaceholders(Dossier dossier) { public PlaceholderModel buildPlaceholders(Dossier dossier) {
var dossierAttributes = dossierAttributesClient.getDossierAttributes(dossier.getId()); var dossierAttributes = dossierAttributesClient.getDossierAttributes(dossier.getId());
var dossierAttributesConfig = dossierAttributesConfigClient.getDossierAttributes(dossier.getDossierTemplateId()); var dossierAttributesConfig = dossierAttributesConfigClient.getDossierAttributes(dossier.getDossierTemplateId());
var fileAttributePlaceHolders = getFileAttributePlaceholders(dossier.getDossierTemplateId()); var fileAttributePlaceHolders = getFileAttributePlaceholders(dossier.getDossierTemplateId());
List<String> placeholders = getDefaultPlaceholders(); Set<String> placeholders = getDefaultPlaceholders();
List<ImagePlaceholder> imagePlaceholders = new ArrayList<>(); List<ImagePlaceholder> imagePlaceholders = new ArrayList<>();
Map<String, String> dossierAttributesPlaceholder = new HashMap<>(); //KEY:placeholder, VALUE:value Map<String, String> dossierAttributesPlaceholder = new HashMap<>(); //KEY:placeholder, VALUE:value
for (DossierAttributeConfig attributeConfig : dossierAttributesConfig) { for (DossierAttributeConfig attributeConfig : dossierAttributesConfig) {
@ -63,7 +64,8 @@ public class GeneratePlaceholderService {
imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder() imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder()
.decode(dossierAttribute.getValue().split(",")[1]))); .decode(dossierAttribute.getValue().split(",")[1])));
} else { } else {
imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder().decode(dossierAttribute.getValue()))); imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder()
.decode(dossierAttribute.getValue())));
} }
} }
} else { } else {
@ -87,14 +89,28 @@ public class GeneratePlaceholderService {
} }
private List<String> getDefaultPlaceholders() { public void resolveFileAttributeValues(FileModel fileModel, PlaceholderModel placeholderModel) {
List<String> defPlaceholders = new ArrayList<>(); Map<String, String> fileAttributeValueByPlaceholder = new HashMap<>();
defPlaceholders.addAll(Arrays.asList(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)); for (Map.Entry<String, String> idEntry : placeholderModel.getFileAttributeIdByPlaceholder().entrySet()) {
return defPlaceholders; if (fileModel.getFileAttributes().containsKey(idEntry.getValue())) {
fileAttributeValueByPlaceholder.put(idEntry.getKey(), fileModel.getFileAttributes()
.get(idEntry.getValue()));
} else {
fileAttributeValueByPlaceholder.put(idEntry.getKey(), "");
}
}
placeholderModel.setFileAttributeValueByPlaceholder(fileAttributeValueByPlaceholder);
} }
public Set<String> getDefaultPlaceholders() {
Set<String> defPlaceholders = new HashSet<>();
defPlaceholders.addAll(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));
return defPlaceholders;
}
private Map<String, String> getFileAttributePlaceholders(String dossierTemplateId) { private Map<String, String> getFileAttributePlaceholders(String dossierTemplateId) {
@ -106,4 +122,5 @@ public class GeneratePlaceholderService {
}); });
return fileAttributePlaceholders; return fileAttributePlaceholders;
} }
} }

View File

@ -11,9 +11,12 @@ import org.springframework.stereotype.Service;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry; import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import io.micrometer.core.annotation.Timed;
@Service @Service
public class IuclidFunctionService { public class IuclidFunctionService {
@Timed("computeIuclidFunction")
public String computeIuclidFunction(List<ReportRedactionEntry> reportRedactionEntries, String filename) { public String computeIuclidFunction(List<ReportRedactionEntry> reportRedactionEntries, String filename) {
Map<String, List<ReportRedactionEntry>> entriesPerJustification = new HashMap<>(); Map<String, List<ReportRedactionEntry>> entriesPerJustification = new HashMap<>();

View File

@ -1,5 +1,18 @@
package com.iqser.red.service.redaction.report.v1.server.service; package com.iqser.red.service.redaction.report.v1.server.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
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.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.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.Dossier;
@ -22,28 +35,14 @@ import com.iqser.red.service.redaction.v1.model.RedactionLog;
import com.iqser.red.service.redaction.v1.model.RedactionLogEntry; import com.iqser.red.service.redaction.v1.model.RedactionLogEntry;
import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist; import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@SuppressWarnings("PMD")
public class ReportGenerationService { public class ReportGenerationService {
private final ReportStorageService reportStorageService; private final ReportStorageService reportStorageService;
@ -53,12 +52,13 @@ public class ReportGenerationService {
private final DossierClient dossierClient; private final DossierClient dossierClient;
private final ReportTemplateClient reportTemplateClient; private final ReportTemplateClient reportTemplateClient;
private final RedactionLogClient redactionLogClient; private final RedactionLogClient redactionLogClient;
private final ExcelTemplateReportGenerationService excelTemplateReportGenerationService; private final ExcelReportGenerationService excelTemplateReportGenerationService;
private final GeneratePlaceholderService generatePlaceholderService; private final GeneratePlaceholderService generatePlaceholderService;
@SneakyThrows @SneakyThrows
public List<StoredFileInformation> generateReport(ReportRequestMessage reportMessage) { @Timed("generateReports")
public List<StoredFileInformation> generateReports(ReportRequestMessage reportMessage) {
List<StoredFileInformation> storedFileInformation = Collections.synchronizedList(new ArrayList<>()); List<StoredFileInformation> storedFileInformation = Collections.synchronizedList(new ArrayList<>());
@ -108,10 +108,12 @@ public class ReportGenerationService {
int i = 1; int i = 1;
for (int j = 0; j < reportMessage.getFileIds().size(); j++) { for (int j = 0; j < reportMessage.getFileIds().size(); j++) {
long start = System.currentTimeMillis();
var fileStatus = fileStatusClient.getFileStatus(reportMessage.getDossierId(), reportMessage.getFileIds() var fileStatus = fileStatusClient.getFileStatus(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j)); .get(j));
long start = System.currentTimeMillis(); generatePlaceholderService.resolveFileAttributeValues(fileStatus, placeholderModel);
List<ReportRedactionEntry> reportEntries = getReportEntries(reportMessage.getDossierId(), reportMessage.getFileIds() List<ReportRedactionEntry> reportEntries = getReportEntries(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j), fileStatus.isExcluded()); .get(j), fileStatus.isExcluded());
@ -120,21 +122,20 @@ public class ReportGenerationService {
for (MultiFileWorkbook multiFileWorkbook : multiFileWorkbooks) { for (MultiFileWorkbook multiFileWorkbook : multiFileWorkbooks) {
if (excelModel == null) { if (excelModel == null) {
excelModel = excelTemplateReportGenerationService.caculateExcelModel(multiFileWorkbook.getReadWorkBook().getSheetAt(0)); excelModel = excelTemplateReportGenerationService.calculateExcelModel(multiFileWorkbook.getReadWorkBook()
.getSheetAt(0));
} }
excelModel = excelTemplateReportGenerationService.generateReport(reportEntries, placeholderModel, multiFileWorkbook.getTemplateName(), multiFileWorkbook.getWriteWorkbook(), fileStatus, dossier, isLastFile, excelModel); excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholderModel, multiFileWorkbook.getTemplateName(), multiFileWorkbook.getWriteWorkbook(), dossier.getDossierName(), fileStatus, excelModel, isLastFile);
} }
for (MultiFileDocument multiFileDocument : multiFileDocuments) { for (MultiFileDocument multiFileDocument : multiFileDocuments) {
wordReportGenerationService.generateReport(reportEntries, placeholderModel, multiFileDocument.getTemplateName(), multiFileDocument.getDocument(), fileStatus, dossier, isLastFile); wordReportGenerationService.generateWordReport(reportEntries, placeholderModel, multiFileDocument.getTemplateName(), multiFileDocument.getDocument(), fileStatus, dossier, isLastFile);
} }
for (ReportTemplate reportTemplate : singleFilesTemplates) { for (ReportTemplate reportTemplate : singleFilesTemplates) {
storedFileInformation.add(createReportFromTemplate(dossier, fileStatus, placeholderModel, reportTemplate.getFileName(), reportMessage.getDownloadId(), reportEntries, reportTemplate)); storedFileInformation.add(createReportFromTemplate(dossier, fileStatus, placeholderModel, reportTemplate.getFileName(), reportMessage.getDownloadId(), reportEntries, reportTemplate));
} }
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
@ -172,8 +173,8 @@ public class ReportGenerationService {
for (Sheet sheet : readWorkbook) { for (Sheet sheet : readWorkbook) {
writeWorkbook.createSheet(sheet.getSheetName()); writeWorkbook.createSheet(sheet.getSheetName());
} }
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(readWorkbook.getSheetAt(0)); var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
excelTemplateReportGenerationService.generateReport(reportEntries, placeholderModel, templateName, writeWorkbook, fileStatus, dossier, true, excelModel); excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholderModel, templateName, writeWorkbook, dossier.getDossierName(), fileStatus, excelModel, true);
byte[] template = excelTemplateReportGenerationService.toByteArray(writeWorkbook); byte[] template = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
String storageId = reportStorageService.storeObject(downloadId, template); String storageId = reportStorageService.storeObject(downloadId, template);
return new StoredFileInformation(fileStatus.getId(), storageId, ReportType.EXCEL_TEMPLATE_SINGLE_FILE, reportTemplate.getTemplateId()); return new StoredFileInformation(fileStatus.getId(), storageId, ReportType.EXCEL_TEMPLATE_SINGLE_FILE, reportTemplate.getTemplateId());
@ -184,7 +185,7 @@ public class ReportGenerationService {
byte[] wordTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId()); byte[] wordTemplate = reportStorageService.getReportTemplate(reportTemplate.getStorageId());
try (ByteArrayInputStream is = new ByteArrayInputStream(wordTemplate)) { try (ByteArrayInputStream is = new ByteArrayInputStream(wordTemplate)) {
XWPFDocument doc = new XWPFDocument(is); XWPFDocument doc = new XWPFDocument(is);
wordReportGenerationService.generateReport(reportEntries, placeholderModel, templateName, doc, fileStatus, dossier, true); wordReportGenerationService.generateWordReport(reportEntries, placeholderModel, templateName, doc, fileStatus, dossier, true);
byte[] template = wordReportGenerationService.toByteArray(doc); byte[] template = wordReportGenerationService.toByteArray(doc);
String storageId = reportStorageService.storeObject(downloadId, template); String storageId = reportStorageService.storeObject(downloadId, template);
return new StoredFileInformation(fileStatus.getId(), storageId, ReportType.WORD_SINGLE_FILE, reportTemplate.getTemplateId()); return new StoredFileInformation(fileStatus.getId(), storageId, ReportType.WORD_SINGLE_FILE, reportTemplate.getTemplateId());
@ -195,6 +196,7 @@ public class ReportGenerationService {
} }
@Timed("getReportEntries")
private List<ReportRedactionEntry> getReportEntries(String dossierId, String fileId, boolean isExcluded) { private List<ReportRedactionEntry> getReportEntries(String dossierId, String fileId, boolean isExcluded) {
if (isExcluded) { if (isExcluded) {

View File

@ -36,7 +36,7 @@ public class ReportMessageReceiver {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
log.info("Start generating reports for downloadId {}", reportMessage.getDownloadId()); log.info("Start generating reports for downloadId {}", reportMessage.getDownloadId());
List<StoredFileInformation> storedFileInformation = reportGenerationService.generateReport(reportMessage); List<StoredFileInformation> storedFileInformation = reportGenerationService.generateReports(reportMessage);
addToReportResultQueue(reportMessage.getUserId(), reportMessage.getDownloadId(), storedFileInformation, 1); addToReportResultQueue(reportMessage.getUserId(), reportMessage.getDownloadId(), storedFileInformation, 1);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();

View File

@ -3,9 +3,12 @@ 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.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.dossier.file.FileModel;
import com.iqser.red.service.redaction.report.v1.server.model.*; import com.iqser.red.service.redaction.report.v1.server.model.*;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Dimension2DDouble; import org.apache.poi.util.Dimension2DDouble;
@ -14,6 +17,7 @@ import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -30,17 +34,14 @@ import static com.iqser.red.service.redaction.report.v1.server.service.Placehold
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@SuppressWarnings("PMD")
public class WordReportGenerationService { public class WordReportGenerationService {
private final IuclidFunctionService iuclidFunctionService; private final IuclidFunctionService iuclidFunctionService;
public XWPFDocument generateReport(List<ReportRedactionEntry> reportEntries, @Timed("generateWordReport")
PlaceholderModel placeholderModel, public XWPFDocument generateWordReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, String reportTemplateName, XWPFDocument doc, FileModel fileModel,
XWPFDocument doc, FileModel fileStatus,
Dossier dossier, boolean isLastFile) { Dossier dossier, boolean isLastFile) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@ -51,39 +52,31 @@ public class WordReportGenerationService {
replaceImagePlaceholders(doc, imagePlaceholder); replaceImagePlaceholders(doc, imagePlaceholder);
} }
long t1 = System.currentTimeMillis();
XWPFTable table = getRedactionTable(doc); XWPFTable table = getRedactionTable(doc);
var placeholderFunctions = computePlaceholderPos(table);
addTableRows(table, reportEntries, fileStatus.getFilename(), fileStatus, placeholderModel.getFileAttributePlaceHolders(), placeholderFunctions);
long t2 = System.currentTimeMillis();
log.debug("Table time: {}", (t2 - t1));
t1 = System.currentTimeMillis(); var placeholderFunctions = computePlaceholderPos(table, placeholderModel);
replaceTextPlaceholders(doc, placeholderModel, dossier, fileStatus, table, reportEntries);
addRedactionEntryRows(table, reportEntries, fileModel.getFilename(), placeholderModel, placeholderFunctions);
replaceTextPlaceholders(doc, placeholderModel, dossier.getDossierName(), fileModel.getFilename(), table, reportEntries);
if (isLastFile) { if (isLastFile) {
removePlaceholdersRow(table); removePlaceholdersRow(table);
} }
t2 = System.currentTimeMillis(); log.info("Report Generation took: {} for file with id {}, pageCount: {}, redactionLogEntryCount: {}, reportName: {}, className: {}", System.currentTimeMillis() - start, fileModel.getId(), fileModel.getNumberOfPages(), reportEntries.size(), reportTemplateName, getClass().getSimpleName());
log.debug("Text time: {}", (t2 - t1));
long end = System.currentTimeMillis();
log.info("Report Generation took: {} for file with id {}, pageCount: {}, redactionLogEntryCount: {}, reportName: {}, className: {}", end - start,
fileStatus.getId(), fileStatus.getNumberOfPages(), reportEntries.size(), reportTemplateName, getClass().getSimpleName());
return doc; return doc;
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage() + " in file: " + fileStatus.getFilename()); log.error(e.getMessage() + " in file: " + fileModel.getFilename());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private void removePlaceholdersRow(XWPFTable table) { private void removePlaceholdersRow(XWPFTable table) {
if (table == null) { if (table == null) {
return; return;
} }
@ -112,8 +105,9 @@ public class WordReportGenerationService {
} }
private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus, Map<String, String> fileAttributePlaceholders, private String getPlaceholderValue(String placeholder, String dossierName, String filename,
Map<String, String> dossierAttributesPlaceholders, List<ReportRedactionEntry> reportRedactionEntries) { PlaceholderModel placeholderModel,
List<ReportRedactionEntry> reportRedactionEntries) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) { if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
return OffsetDateTime.now().format(FORMAT_DATE_ISO); return OffsetDateTime.now().format(FORMAT_DATE_ISO);
@ -128,31 +122,29 @@ public class WordReportGenerationService {
return OffsetDateTime.now().format(FORMAT_TIME_ISO); return OffsetDateTime.now().format(FORMAT_TIME_ISO);
} }
if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) { if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) {
return project.getDossierName(); return dossierName;
} }
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) { if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return fileStatus.getFilename(); return filename;
} }
if (placeholder.equals(IUCLID_FUNCTION_PLACEHOLDER)) { if (placeholder.equals(IUCLID_FUNCTION_PLACEHOLDER)) {
return iuclidFunctionService.computeIuclidFunction(reportRedactionEntries, fileStatus.getFilename()); return iuclidFunctionService.computeIuclidFunction(reportRedactionEntries, filename);
} }
if (fileAttributePlaceholders.containsKey(placeholder)) {
String id = fileAttributePlaceholders.get(placeholder);
if (fileStatus.getFileAttributes().containsKey(id)) { if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) {
return fileStatus.getFileAttributes().get(id); return placeholderModel.getFileAttributeValueByPlaceholder().get(placeholder);
} else {
return "";
} }
if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
return placeholderModel.getDossierAttributesValueByPlaceholder().get(placeholder);
} }
if (dossierAttributesPlaceholders.containsKey(placeholder)) {
return dossierAttributesPlaceholders.get(placeholder);
}
return ""; return "";
} }
private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs, ImagePlaceholder imagePlaceholder) { private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs,
ImagePlaceholder imagePlaceholder) {
for (XWPFParagraph p : paragraphs) { for (XWPFParagraph p : paragraphs) {
String paragraphText = p.getText(); String paragraphText = p.getText();
@ -175,11 +167,14 @@ public class WordReportGenerationService {
} }
public void replaceTextPlaceholders(XWPFDocument doc, PlaceholderModel placeholderModel, Dossier dossier, FileModel fileModel, XWPFTable tableToSkip, List<ReportRedactionEntry> reportRedactionEntries) { @Timed("word-replaceTextPlaceholders")
public void replaceTextPlaceholders(XWPFDocument doc, PlaceholderModel placeholderModel, String dossierName,
String fileName, XWPFTable tableToSkip,
List<ReportRedactionEntry> reportRedactionEntries) {
Map<String, String> placeHolderValueMap = new HashMap<>(); Map<String, String> placeHolderValueMap = new HashMap<>();
for (String placeholder : placeholderModel.getPlaceholders()) { for (String placeholder : placeholderModel.getPlaceholders()) {
String placeholderValue = getPlaceholderValue(placeholder, dossier, fileModel, placeholderModel.getFileAttributePlaceHolders(), placeholderModel.getDossierAttributesPlaceholder(), reportRedactionEntries); String placeholderValue = getPlaceholderValue(placeholder, dossierName, fileName, placeholderModel, reportRedactionEntries);
placeHolderValueMap.put(placeholder, placeholderValue); placeHolderValueMap.put(placeholder, placeholderValue);
} }
@ -198,7 +193,8 @@ 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) { for (XWPFParagraph p : paragraphs) {
String paragraphText = p.getText(); String paragraphText = p.getText();
@ -264,130 +260,134 @@ public class WordReportGenerationService {
} }
private Map<Integer, Function<TextPlaceholderInput, String>> computePlaceholderPos(XWPFTable table) { @Timed("word-computePlaceholderPos")
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos = new HashMap<>(); private PlaceHolderFunctions computePlaceholderPos(XWPFTable table, PlaceholderModel placeholderModel) {
Set<String> foundPlaceHolder = new HashSet<>();
Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos = new HashMap<>();
if (table != null) { if (table != null) {
for (int j = 0; j < table.getRows().size(); j++) { for (int j = 0; j < table.getRows().size(); j++) {
for (int i = 0; i < table.getRows().get(j).getTableCells().size(); i++) { for (int i = 0; i < table.getRows().get(j).getTableCells().size(); i++) {
XWPFTableCell cell = table.getRows().get(j).getTableCells().get(i); XWPFTableCell cell = table.getRows().get(j).getTableCells().get(i);
if (containsRedactionPlaceholder(cell.getText())) { if (containsRedactionPlaceholder(cell.getText())) {
placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getText())); placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getText(), foundPlaceHolder, placeholderModel));
} else if (cell.getText().isEmpty()) { } else if (cell.getText().isEmpty()) {
placeholderCellPos.put(i, (input) -> ""); placeholderCellPos.put(i, (input) -> "");
} }
} }
} }
} }
return placeholderCellPos; return new PlaceHolderFunctions(placeholderCellPos, foundPlaceHolder);
} }
private void addTableRows(XWPFTable table,
List<ReportRedactionEntry> reportEntries, @Timed("word-addRedactionEntryRows")
String filename, private void addRedactionEntryRows(XWPFTable table, List<ReportRedactionEntry> reportEntries, String filename,
FileModel fileStatus, PlaceholderModel placeholderModel, PlaceHolderFunctions placeHolderFunctions) {
Map<String, String> fileAttributePlaceholders,
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos) {
if (table == null) { if (table == null) {
return; return;
} }
if (placeholderCellPos.containsValue(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) { if (placeHolderFunctions.getFoundPlaceHolder()
.contains(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) {
var redactionsPerJustification = getRedactionsPerJustification(reportEntries); var redactionsPerJustification = getRedactionsPerJustification(reportEntries);
for (Map.Entry<String, List<ReportRedactionEntry>> entry : redactionsPerJustification.entrySet()) { for (Map.Entry<String, List<ReportRedactionEntry>> entry : redactionsPerJustification.entrySet()) {
XWPFTableRow row = table.createRow(); XWPFTableRow row = table.createRow();
// for (Map.Entry<Integer, Function<String, String>> entry1 : placeholderCellPos.entrySet()) { for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
// .entrySet()) {
// setText(row.getCell(entry1.getKey()), replaceSeedsPlaceholder(entry, filename, entry1.getValue(), fileStatus, fileAttributePlaceholders)); setText(row.getCell(entry1.getKey()), entry1.getValue()
// .apply(new PlaceholderInput(filename, null, placeholderModel, entry)));
// if (!entry1.getValue().isEmpty()) { }
//
// } else {
// setText(row.getCell(entry1.getKey()), "");
// }
// }
} }
} else { } else {
reportEntries.forEach(entry -> { reportEntries.forEach(entry -> {
XWPFTableRow row = table.createRow(); XWPFTableRow row = table.createRow();
for (Map.Entry<Integer, Function<TextPlaceholderInput, String>> entry1 : placeholderCellPos.entrySet()) { for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
setText(row.getCell(entry1.getKey()), entry1.getValue().apply(new TextPlaceholderInput(filename, entry))); .entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
} }
}); });
} }
} }
private void readdPlaceholders(XWPFTable table, Map<Integer, String> 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 Function<PlaceholderInput, String> getFunctionForPlaceHolder(String placeholder,
private Function<TextPlaceholderInput, String> getFunctionForPlaceHolder(String placeholder) { Set<String> foundPlaceholders,
PlaceholderModel placeholderModel) {
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) { if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return TextPlaceholderInput::getFilename; foundPlaceholders.add(FILE_NAME_PLACEHOLDER);
return PlaceholderInput::getFilename;
} }
if (placeholder.equals(PAGE_PLACEHOLDER)) { if (placeholder.equals(PAGE_PLACEHOLDER)) {
foundPlaceholders.add(PAGE_PLACEHOLDER);
return (input) -> String.valueOf(input.getEntry().getPage()); return (input) -> String.valueOf(input.getEntry().getPage());
} }
if (placeholder.equals(PARAGRAPH_PLACEHOLDER)) { if (placeholder.equals(PARAGRAPH_PLACEHOLDER)) {
foundPlaceholders.add(PARAGRAPH_PLACEHOLDER);
return (input) -> input.getEntry().getSection(); return (input) -> input.getEntry().getSection();
} }
if (placeholder.equals(JUSTIFICATION_PLACEHOLDER)) { if (placeholder.equals(JUSTIFICATION_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_PLACEHOLDER);
return (input) -> input.getEntry().getJustification(); return (input) -> input.getEntry().getJustification();
} }
if (placeholder.equals(JUSTIFICATION_PARAGRAPH_PLACEHOLDER)) { if (placeholder.equals(JUSTIFICATION_PARAGRAPH_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_PARAGRAPH_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationParagraph(); return (input) -> input.getEntry().getJustificationParagraph();
} }
if (placeholder.equals(JUSTIFICATION_REASON_PLACEHOLDER)) { if (placeholder.equals(JUSTIFICATION_REASON_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_REASON_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationReason(); return (input) -> input.getEntry().getJustificationReason();
} }
if (placeholder.equals(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER)) { if (placeholder.equals(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationParagraph(); return (input) -> input.getEntry().getJustificationParagraph();
} }
if (placeholder.equals(JUSTIFICATION_TEXT_PLACEHOLDER)) { if (placeholder.equals(JUSTIFICATION_TEXT_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_TEXT_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationReason(); return (input) -> input.getEntry().getJustificationReason();
} }
if (placeholder.equals(EXCERPT_PLACEHOLDER)) { if (placeholder.equals(EXCERPT_PLACEHOLDER)) {
foundPlaceholders.add(EXCERPT_PLACEHOLDER);
return (input) -> input.getEntry().getExcerpt(); return (input) -> input.getEntry().getExcerpt();
} }
if (placeholder.equals(REDACTION_VALUE_PLACEHOLDER)) { if (placeholder.equals(REDACTION_VALUE_PLACEHOLDER)) {
return (input) -> input.getEntry().getValue() != null ? input.getEntry().getValue().replaceAll("\n", " ").replaceAll(" ", " ") : ""; foundPlaceholders.add(REDACTION_VALUE_PLACEHOLDER);
return (input) -> input.getEntry().getValue() != null ? input.getEntry()
.getValue()
.replaceAll("\n", " ")
.replaceAll(" ", " ") : "";
} }
// TODO in case placeholder is invalid -> do not replace with empty string but throw previus exception
return (input) -> "";
}
private String replaceSeedsPlaceholder(Map.Entry<String, List<ReportRedactionEntry>> entry, String filename, String placeholder, FileModel fileStatus,
Map<String, String> fileAttributePlaceholders) {
if (placeholder.equals(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) { if (placeholder.equals(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER)) {
var pages = entry.getValue().stream().map(ReportRedactionEntry::getPage).collect(Collectors.toSet()); foundPlaceholders.add(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER);
return computePageRanges(pages); return (input) -> computePageRanges(input.getRedactionsPerJustificationEntry()
.getValue()
.stream()
.map(ReportRedactionEntry::getPage)
.collect(Collectors.toSet()));
} }
if (placeholder.equals(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER)) { if (placeholder.equals(SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER)) {
return entry.getKey(); return (input) -> input.getRedactionsPerJustificationEntry().getKey();
} }
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return filename;
}
if (fileAttributePlaceholders.containsKey(placeholder)) {
String id = fileAttributePlaceholders.get(placeholder);
if (fileStatus.getFileAttributes().containsKey(id)) { if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) {
return fileStatus.getFileAttributes().get(id); foundPlaceholders.add(placeholder);
} else { return (input) -> input.getPlaceholderModel().getFileAttributeValueByPlaceholder().get(placeholder);
return null;
} }
if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
foundPlaceholders.add(placeholder);
return (input) -> input.getPlaceholderModel().getDossierAttributesValueByPlaceholder().get(placeholder);
} }
return "";
return (input) -> "";
} }
@ -467,9 +467,12 @@ 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

@ -3,13 +3,7 @@ package com.iqser.red.service.redaction.report.v1.server;
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.DossierAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.ReportTemplate;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier; 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.DossierAttribute;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttributeType;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileAttributeType;
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.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.legalbasis.LegalBasis;
import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesClient; import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributesClient;
@ -17,8 +11,10 @@ import com.iqser.red.service.redaction.report.v1.server.client.DossierAttributes
import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient; import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient;
import com.iqser.red.service.redaction.report.v1.server.client.ReportTemplateClient; import com.iqser.red.service.redaction.report.v1.server.client.ReportTemplateClient;
import com.iqser.red.service.redaction.report.v1.server.configuration.MessagingConfiguration; import com.iqser.red.service.redaction.report.v1.server.configuration.MessagingConfiguration;
import com.iqser.red.service.redaction.report.v1.server.model.ImagePlaceholder;
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.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.service.ExcelTemplateReportGenerationService; import com.iqser.red.service.redaction.report.v1.server.service.ExcelReportGenerationService;
import com.iqser.red.service.redaction.report.v1.server.service.GeneratePlaceholderService; import com.iqser.red.service.redaction.report.v1.server.service.GeneratePlaceholderService;
import com.iqser.red.service.redaction.report.v1.server.service.RedactionLogConverterService; import com.iqser.red.service.redaction.report.v1.server.service.RedactionLogConverterService;
import com.iqser.red.service.redaction.report.v1.server.service.WordReportGenerationService; import com.iqser.red.service.redaction.report.v1.server.service.WordReportGenerationService;
@ -30,7 +26,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -40,12 +35,18 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.*; 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 com.iqser.red.service.redaction.report.v1.server.utils.OsUtils.getTemporaryDirectory;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -86,504 +87,289 @@ public class RedactionReportIntegrationTest {
private DossierAttributesClient dossierAttributesClient; private DossierAttributesClient dossierAttributesClient;
@Autowired @Autowired
private ExcelTemplateReportGenerationService excelTemplateReportGenerationService; private ExcelReportGenerationService excelTemplateReportGenerationService;
@Autowired @Autowired
private GeneratePlaceholderService generatePlaceholderService; private GeneratePlaceholderService generatePlaceholderService;
@Test @Test
public void testWordReportGeneration() throws IOException { @SneakyThrows
public void testWordJustificationAppendixA1(){
String dossierTemplateId = "dossierTemplateId";
ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog.json");
ClassPathResource imageResource = new ClassPathResource("files/exampleImage.jpg");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMapping.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
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<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
var dossierAttributeConfig1 = DossierAttributeConfig var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A1.docx");
.builder() var imageResource = new ClassPathResource("files/exampleImage.jpg");
.id("id")
.label("label")
.editable(true)
.type(DossierAttributeType.TEXT)
.placeholder("{{dossier.attribute.name}}")
.build();
var dossierAttributeConfig2 = DossierAttributeConfig Dossier dossier = Dossier.builder().dossierName("dossierName").build();
.builder() FileModel fileModel = FileModel.builder().filename("filename").build();
.id("id2")
.label("label2") var placeholders = buildPlaceHolderModel(
.editable(true) Map.of("{{dossier.attribute.ActiveSubstance}}","Aktive Substanz \n Test Return",
.type(DossierAttributeType.IMAGE) "{{dossier.attribute.RapporteurMemberState}}","Reporter Status",
.placeholder("{{dossier.attribute.Signature}}") "{{dossier.attribute.Name}}","Dossier Name",
.build(); "{{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);
byte[] wordReport = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Justification Appendix A1_justification.docx")) {
fileOutputStream.write(wordReport);
}
}
@Test
@SneakyThrows
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);
var dossierAttributesConfig = Arrays.asList(dossierAttributeConfig1, dossierAttributeConfig2); var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A2.docx");
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(dossierAttributesConfig); var imageResource = new ClassPathResource("files/exampleImage.jpg");
var dossierAttribute1 = DossierAttribute Dossier dossier = Dossier.builder().dossierName("dossierName").build();
.builder() FileModel fileModel = FileModel.builder().filename("filename").build();
.dossierAttributeConfigId("id")
.dossierId("dossierId") var placeholders = buildPlaceHolderModel(
.value("Michael") Map.of("{{dossier.attribute.ActiveSubstance}}","Aktive Substanz \n Test Return",
.build(); "{{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);
byte[] wordReport = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Justification Appendix A2_justification.docx")) {
fileOutputStream.write(wordReport);
}
}
@Test
@SneakyThrows
public void testWordIUCLIDFile() {
var dossierAttribute2 = DossierAttribute RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
.builder() List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
.dossierAttributeConfigId("id2") List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
.dossierId("dossierId")
.value("data:image/png;base64," + Base64.getEncoder()
.encodeToString(IOUtils.toByteArray(imageResource.getInputStream())))
.build();
var dossierAttributes = Arrays.asList(dossierAttribute1, dossierAttribute2); FileModel fileStatus = FileModel.builder().filename("filename").build();
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(dossierAttributes); ClassPathResource templateResource = new ClassPathResource("templates/IUCLID_Template.docx");
var fileAttributeConfig1 = FileAttributeConfig
.builder()
.id("3e9b9569-5d2e-4619-848b-dd0a3e96527f")
.label("Document Title")
.placeholder("{{file.attribute.Path}}")
.type(FileAttributeType.TEXT)
.build();
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig1));
FileModel fileStatus = FileModel.builder().filename("FileABCD").fileAttributes(Map.of("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test")).build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
String templateId = "templateId";
String storageId = "storageId";
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource templateResource = new ClassPathResource("templates/Seeds - New Justification Form.docx");
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream()); XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
var placeholders = buildPlaceHolderModel(new HashMap<>(), Map.of("{{file.attribute.Path}}", "Path"), List.of());
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc); byte[] report = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template_wrg.docx")) { try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/IUCLID_Template_justification.docx")) {
fileOutputStream.write(report); fileOutputStream.write(report);
} }
} }
@Test @Test
@Ignore @SneakyThrows
public void testExcelTemplateReportGeneration() throws IOException { public void testWordSeedReportSingleFile() {
ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog.json");
ClassPathResource excelRedactionLogResource = new ClassPathResource("files/excelReportRedactionLog.json");
ClassPathResource imageResource = new ClassPathResource("files/exampleImage.jpg");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class); RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
RedactionLog redactionLog2 = objectMapper.readValue(excelRedactionLogResource.getInputStream(), RedactionLog.class); List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
String dossierTemplateId = "dossierTemplateId";
String storageId = "storageId";
String templateId = "templateId";
String dossierId = "dossierId";
ClassPathResource templateResource = new ClassPathResource("templates/TestReport.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(wb.getSheetAt(0));
SXSSFWorkbook workbook = new SXSSFWorkbook();
workbook.createSheet("Test Row Peformance");
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMapping.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping); List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<ReportRedactionEntry> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping);
DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "label", true, "{{dossier.attribute.name}}", DossierAttributeType.TEXT, dossierTemplateId); FileModel fileStatus = FileModel.builder().filename("filename").build();
DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "label2", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId); Dossier dossier = Dossier.builder().dossierName("dossierName").build();
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2));
DossierAttribute dossierAttribute = new DossierAttribute(dossierId, dossierAttributeConfig.getId(), "Michael"); ClassPathResource templateResource = new ClassPathResource("templates/Seeds - New Justification Form.docx");
DossierAttribute dossierAttribute2 = new DossierAttribute(dossierId, dossierAttributeConfig2.getId(), Base64.getEncoder().encodeToString(IOUtils.toByteArray(imageResource.getInputStream()))); XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(List.of(dossierAttribute, dossierAttribute2));
FileAttributeConfig fileAttributeConfig = new FileAttributeConfig("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "", "Document Title", true, true, false, false, "{{file.attribute.placeholder}}", FileAttributeType.TEXT, dossierTemplateId); var placeholders = buildPlaceHolderModel(new HashMap<>(), Map.of("{{file.attribute.Path}}", "Path"), List.of());
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
Map<String, String> attributeIdToValue = new HashMap(); wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
attributeIdToValue.put("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test"); byte[] report = wordReportGenerationService.toByteArray(doc);
FileModel fileModel = FileModel.builder().filename("filename").fileAttributes(attributeIdToValue).build();
FileModel fileModel2 = FileModel.builder().filename("other file").fileAttributes(attributeIdToValue).build();
Dossier dossier = Dossier.builder().id(dossierId).dossierName("dossierName").build(); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/seeds-report.docx")) {
fileOutputStream.write(report);
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
excelTemplateReportGenerationService.generateReport(reportEntries, placeholders, "test", workbook, fileModel, dossier, false, excelModel);
excelTemplateReportGenerationService.generateReport(reportEntries2, placeholders, "test", workbook, fileModel2, dossier, true, excelModel);
byte[] excelTemplateReport = excelTemplateReportGenerationService.toByteArray(workbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/test_report_excel_template.xlsx")) {
fileOutputStream.write(excelTemplateReport);
} }
} }
@Test @Test
public void testReportGeneration() throws IOException { @SneakyThrows
public void testWordSeedReportMultiFile() {
ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog.json");
ClassPathResource redactionLogResource2 = new ClassPathResource("files/excelReportRedactionLog.json");
ClassPathResource imageResource = new ClassPathResource("files/exampleImage.jpg");
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMapping.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
String dossierTemplateId = "dossierTemplateId"; RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
String storageId = "storageId"; List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
String templateId = "templateId";
String dossierId = "dossierId";
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
RedactionLog redactionLog2 = objectMapper.readValue(redactionLogResource2.getInputStream(), RedactionLog.class);
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping); List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<ReportRedactionEntry> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping);
DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "Active Substance", true, "{{dossier.attribute.ActiveSubstance}}", DossierAttributeType.TEXT, dossierTemplateId); RedactionLog redactionLogSecondFile = objectMapper.readValue(new ClassPathResource("files/excelReportRedactionLog.json").getInputStream(), RedactionLog.class);
DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "Rapporteur Member State", false, "{{dossier.attribute.RapporteurMemberState}}", DossierAttributeType.TEXT, dossierTemplateId); List<ReportRedactionEntry> reportEntriesSecondFile = redactionLogConverterService.convertAndSort(redactionLogSecondFile, legalBasisMapping);
DossierAttributeConfig dossierAttributeConfig3 = new DossierAttributeConfig("id3", "Dossier Name", true, "{{dossier.attribute.Name}}", DossierAttributeType.TEXT, dossierTemplateId); FileModel fileModelSecondFile = FileModel.builder().filename("secondFile").build();
DossierAttributeConfig dossierAttributeConfig4 = new DossierAttributeConfig("id4", "Company", false, "{{dossier.attribute.Company}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig5 = new DossierAttributeConfig("id5", "Date", true, "{{dossier.attribute.Date}}", DossierAttributeType.DATE, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig6 = new DossierAttributeConfig("id6", "Signature", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId);
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2, dossierAttributeConfig3, dossierAttributeConfig4, dossierAttributeConfig5, dossierAttributeConfig6));
DossierAttribute dossierAttribute = new DossierAttribute(dossierId, "id", "Aktive Substanz \n Test Return"); FileModel fileStatus = FileModel.builder().filename("filename").build();
DossierAttribute dossierAttribute2 = new DossierAttribute(dossierId, "id2", "Reporter Status"); Dossier dossier = Dossier.builder().dossierName("dossierName").build();
DossierAttribute dossierAttribute3 = new DossierAttribute(dossierId, "id3", "Dossier Name");
DossierAttribute dossierAttribute4 = new DossierAttribute(dossierId, "id4", "Firma");
DossierAttribute dossierAttribute5 = new DossierAttribute(dossierId, "id5", "2021-11-09T23:00:00.000Z");
DossierAttribute dossierAttribute6 = new DossierAttribute(dossierId, "id6", "data:image/png;base64," + Base64.getEncoder().encodeToString(IOUtils.toByteArray(imageResource.getInputStream())));
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(List.of(dossierAttribute, dossierAttribute2, dossierAttribute3, dossierAttribute4, dossierAttribute5, dossierAttribute6)); ClassPathResource templateResource = new ClassPathResource("templates/Seeds-NewJustificationForm.docx");
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
FileAttributeConfig fileAttributeConfig = new FileAttributeConfig("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "", "Document Title", true, true, false, false, "{{file.attribute.placeholder}}", FileAttributeType.TEXT, dossierTemplateId); var placeholders = buildPlaceHolderModel(new HashMap<>(), Map.of("{{file.attribute.Path}}", "Path"), List.of());
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
Map<String, String> attributeIdToValue = new HashMap(); wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, false);
attributeIdToValue.put("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test"); wordReportGenerationService.generateWordReport(reportEntriesSecondFile, placeholders, "test", doc, fileModelSecondFile, dossier, true);
FileModel fileModel = FileModel.builder().filename("filename").fileAttributes(attributeIdToValue).build(); byte[] report = wordReportGenerationService.toByteArray(doc);
FileModel fileModel2 = FileModel.builder().filename("other file").fileAttributes(attributeIdToValue).build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build(); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/seeds-report-multifile.docx")) {
fileOutputStream.write(report);
}
}
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource wordTemplateResource = new ClassPathResource("templates/Justification Appendix A1.docx");
var placeholders = generatePlaceholderService.buildPlaceholders(dossier); @Test
@SneakyThrows
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);
var wordTemplateResource = new ClassPathResource("templates/6464 appendix_b EFSA dRAR justification.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
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()))));
XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream()); XWPFDocument doc = new XWPFDocument(wordTemplateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true); wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
byte[] wordReport = wordReportGenerationService.toByteArray(doc); byte[] wordReport = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template1.docx")) { try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/6464 appendix_b EFSA dRAR_justification.docx")) {
fileOutputStream.write(wordReport); fileOutputStream.write(wordReport);
} }
doc = new XWPFDocument(wordTemplateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries2, placeholders, "test", doc, fileModel2, dossier, true);
byte[] wordReport2 = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template2.docx")) {
fileOutputStream.write(wordReport2);
} }
ClassPathResource excelTemplateResource = new ClassPathResource("templates/Excel Report.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(excelTemplateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(wb.getSheetAt(0)); @Test
SXSSFWorkbook workbook = new SXSSFWorkbook(); @SneakyThrows
workbook.createSheet("Test Row Peformance"); public void testExcelTemplateReportGenerationSingleFile() {
excelTemplateReportGenerationService.generateReport(reportEntries, placeholders, "test", workbook, fileModel, dossier, false,excelModel); RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
excelTemplateReportGenerationService.generateReport(reportEntries2, placeholders, "test", workbook, fileModel2, dossier, true,excelModel); List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
byte[] excelTemplateReport = excelTemplateReportGenerationService.toByteArray(workbook); List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/report_excel_template.xlsx")) {
fileOutputStream.write(excelTemplateReport);
}
XSSFWorkbook wb2 = new XSSFWorkbook(excelTemplateResource.getInputStream());
var excelModel2 = excelTemplateReportGenerationService.caculateExcelModel(wb2.getSheetAt(0)); FileModel fileModel = FileModel.builder().filename("filename").build();
SXSSFWorkbook workbook2 = new SXSSFWorkbook();
workbook2.createSheet("Test Row Peformance");
excelTemplateReportGenerationService.generateReport(reportEntries, placeholders, "test", workbook2, fileModel, dossier, true,excelModel2); ClassPathResource templateResource = new ClassPathResource("templates/Excel Report.xlsx");
byte[] excelTemplateReport2 = excelTemplateReportGenerationService.toByteArray(workbook2);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/report_excel_template2.xlsx")) { var placeholders = buildPlaceHolderModel(new HashMap<>(), new HashMap<>(), List.of());
fileOutputStream.write(excelTemplateReport2);
} XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
XSSFWorkbook wb3 = new XSSFWorkbook(excelTemplateResource.getInputStream()); var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
var excelModel3 = excelTemplateReportGenerationService.caculateExcelModel(wb2.getSheetAt(0)); SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
SXSSFWorkbook workbook3 = new SXSSFWorkbook(); writeWorkbook.createSheet("Sheet1");
workbook3.createSheet("Test Row Peformance"); excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook, "dossierName" ,fileModel, excelModel, true);
excelTemplateReportGenerationService.generateReport(reportEntries2, placeholders, "test", workbook3, fileModel2, dossier, true,excelModel3);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(workbook3);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/report_excel_template3.xlsx")) { byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_justification.xlsx")) {
fileOutputStream.write(excelTemplateReport3); fileOutputStream.write(excelTemplateReport3);
} }
} }
@Test
public void testExcerptReportGeneration() throws IOException {
String dossierTemplateId = "dossierTemplateId";
String dossierId = "dossierId";
ClassPathResource redactionLogResource = new ClassPathResource("files/S11RedactionLog.json");
ClassPathResource imageResource = new ClassPathResource("files/exampleImage.jpg");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/S1116LegalBasis.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "Active Substance", true, "{{dossier.attribute.ActiveSubstance}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "Rapporteur Member State", false, "{{dossier.attribute.RapporteurMemberState}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig3 = new DossierAttributeConfig("id3", "Dossier Name", true, "{{dossier.attribute.Name}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig4 = new DossierAttributeConfig("id4", "Company", false, "{{dossier.attribute.Company}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig5 = new DossierAttributeConfig("id5", "Date", true, "{{dossier.attribute.Date}}", DossierAttributeType.DATE, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig6 = new DossierAttributeConfig("id6", "Signature", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId);
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2, dossierAttributeConfig3, dossierAttributeConfig4, dossierAttributeConfig5, dossierAttributeConfig6));
DossierAttribute dossierAttribute = new DossierAttribute(dossierId, "id", "Aktive Substanz");
DossierAttribute dossierAttribute2 = new DossierAttribute(dossierId, "id2", "Reporter Status");
DossierAttribute dossierAttribute3 = new DossierAttribute(dossierId, "id3", "Dossier Name");
DossierAttribute dossierAttribute4 = new DossierAttribute(dossierId, "id4", "Firma");
DossierAttribute dossierAttribute5 = new DossierAttribute(dossierId, "id5", "2021-11-09T23:00:00.000Z");
DossierAttribute dossierAttribute6 = new DossierAttribute(dossierId, "id6", "data:image/png;base64," + Base64.getEncoder().encodeToString(IOUtils.toByteArray(imageResource.getInputStream())));
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(List.of(dossierAttribute, dossierAttribute2, dossierAttribute3, dossierAttribute4, dossierAttribute5, dossierAttribute6));
FileAttributeConfig fileAttributeConfig = new FileAttributeConfig("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "", "Document Title", true, true, false, false, "{{file.attribute.placeholder}}", FileAttributeType.TEXT, dossierTemplateId);
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
Map<String, String> attributeIdToValue = new HashMap();
attributeIdToValue.put("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test");
FileModel fileModel = FileModel.builder().filename("filename").fileAttributes(attributeIdToValue).build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
String templateId = "templateId";
String storageId = "storageId";
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource templateResource = new ClassPathResource("templates/6464 appendix_b EFSA dRAR justification.docx");
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template13.docx")) {
fileOutputStream.write(report);
}
}
@Test @Test
@SneakyThrows @SneakyThrows
public void testIuclidReport() { public void testExcelTemplateReportGenerationMultiFile() {
String dossierTemplateId = "dossierTemplateId";
ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog2817.json");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMappingNew.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
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<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
FileModel fileModel = FileModel.builder().filename("filename").build();
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(new ArrayList<>()); ClassPathResource templateResource = new ClassPathResource("templates/Excel Report.xlsx");
var placeholders = buildPlaceHolderModel(new HashMap<>(), new HashMap<>(), List.of());
XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
writeWorkbook.createSheet("Sheet1");
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);
FileModel fileModelSecondFile = FileModel.builder().filename("secondFile").build();
excelTemplateReportGenerationService.generateExcelReport(reportEntriesSecondFile, placeholders, "test", writeWorkbook,"dossierName" , fileModelSecondFile, excelModel, true);
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(new ArrayList<>()); byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_multifile.xlsx")) {
fileOutputStream.write(excelTemplateReport3);
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(new ArrayList<>());
FileModel fileStatus = FileModel.builder().filename("VV123456").build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
String templateId = "templateId";
String storageId = "storageId";
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource templateResource = new ClassPathResource("templates/IUCLID_Template.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();
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/iuclid_report_2.docx")) {
fileOutputStream.write(report);
} }
} }
@Test
public void testExcelReportGeneration() throws IOException {
ClassPathResource redactionLogResource = new ClassPathResource("files/redactionLog.json");
ClassPathResource redactionLogResource2 = new ClassPathResource("files/excelReportRedactionLog.json");
ClassPathResource imageResource = new ClassPathResource("files/exampleImage.jpg");
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMapping.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
String dossierTemplateId = "dossierTemplateId";
String storageId = "storageId";
String templateId = "templateId";
String dossierId = "dossierId";
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
RedactionLog redactionLog2 = objectMapper.readValue(redactionLogResource2.getInputStream(), RedactionLog.class);
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<ReportRedactionEntry> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping);
List<ReportRedactionEntry> emptyReportEntries = new ArrayList<>();
DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "Active Substance", true, "{{dossier.attribute.ActiveSubstance}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "Rapporteur Member State", false, "{{dossier.attribute.RapporteurMemberState}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig3 = new DossierAttributeConfig("id3", "Dossier Name", true, "{{dossier.attribute.Name}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig4 = new DossierAttributeConfig("id4", "Company", false, "{{dossier.attribute.Company}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig5 = new DossierAttributeConfig("id5", "Date", true, "{{dossier.attribute.Date}}", DossierAttributeType.DATE, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig6 = new DossierAttributeConfig("id6", "Signature", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId);
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2, dossierAttributeConfig3, dossierAttributeConfig4, dossierAttributeConfig5, dossierAttributeConfig6));
DossierAttribute dossierAttribute = new DossierAttribute(dossierId, "id", "Aktive Substanz \n Test Return");
DossierAttribute dossierAttribute2 = new DossierAttribute(dossierId, "id2", "Reporter Status");
DossierAttribute dossierAttribute3 = new DossierAttribute(dossierId, "id3", "Dossier Name");
DossierAttribute dossierAttribute4 = new DossierAttribute(dossierId, "id4", "Firma");
DossierAttribute dossierAttribute5 = new DossierAttribute(dossierId, "id5", "2021-11-09T23:00:00.000Z");
DossierAttribute dossierAttribute6 = new DossierAttribute(dossierId, "id6", "data:image/png;base64," + Base64.getEncoder().encodeToString(IOUtils.toByteArray(imageResource.getInputStream())));
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(List.of(dossierAttribute, dossierAttribute2, dossierAttribute3, dossierAttribute4, dossierAttribute5, dossierAttribute6));
FileAttributeConfig fileAttributeConfig = new FileAttributeConfig("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "", "Document Title", true, true, false, false, "{{file.attribute.placeholder}}", FileAttributeType.TEXT, dossierTemplateId);
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
Map<String, String> attributeIdToValue = new HashMap();
attributeIdToValue.put("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test");
FileModel fileModel = FileModel.builder().filename("filename").fileAttributes(attributeIdToValue).build();
FileModel fileModel2 = FileModel.builder().filename("other file").fileAttributes(attributeIdToValue).build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource excelTemplateResource = new ClassPathResource("templates/Excel Report.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(excelTemplateResource.getInputStream());
SXSSFWorkbook workbook = new SXSSFWorkbook();
workbook.createSheet("Test Row Peformance");
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(wb.getSheetAt(0));
excelTemplateReportGenerationService.generateReport(emptyReportEntries, placeholders, "test", workbook, fileModel, dossier, false,excelModel);
excelTemplateReportGenerationService.generateReport(reportEntries2, placeholders, "test", workbook, fileModel2, dossier, true,excelModel);
byte[] excelTemplateReport = excelTemplateReportGenerationService.toByteArray(workbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/report_excel_templateAAA.xlsx")) {
fileOutputStream.write(excelTemplateReport);
}
}
@Test @Test
@SneakyThrows @SneakyThrows
public void testSeedsFunctionService() { public void testExcelPlaceholders() {
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<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
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<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<ReportRedactionEntry> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping); var imageResource = new ClassPathResource("files/exampleImage.jpg");
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(new ArrayList<>()); FileModel fileModel = FileModel.builder().filename("filename").build();
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(new ArrayList<>());
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(new ArrayList<>()); 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()))));
XSSFWorkbook readWorkbook = new XSSFWorkbook(templateResource.getInputStream());
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
writeWorkbook.createSheet("Sheet1");
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholders, "test", writeWorkbook,"dossierName" , fileModel, excelModel, true);
FileModel fileStatus = FileModel.builder().filename("VV123456").build(); byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
FileModel fileStatus2 = FileModel.builder().filename("second file").build(); try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/PlaceholderTest_justification.xlsx")) {
fileOutputStream.write(excelTemplateReport3);
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
String templateId = "templateId";
String storageId = "storageId";
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource templateResource = new ClassPathResource("templates/Seeds-NewJustificationForm.docx");
when(reportStorageService.getReportTemplate(storageId)).thenReturn(IOUtils.toByteArray(templateResource.getInputStream()));
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
doc = wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, false);
doc = wordReportGenerationService.generateReport(reportEntries2, placeholders, "test", doc, fileStatus2, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/seedsReport.docx")) {
fileOutputStream.write(report);
} }
} }
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);
}
} }

View File

@ -1,132 +0,0 @@
package com.iqser.red.service.redaction.report.v1.server.realdata;
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.DossierAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.ReportTemplate;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttribute;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.DossierAttributeType;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileAttributeConfig;
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileAttributeType;
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.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;
import com.iqser.red.service.redaction.report.v1.server.client.ReportTemplateClient;
import com.iqser.red.service.redaction.report.v1.server.configuration.MessagingConfiguration;
import com.iqser.red.service.redaction.report.v1.server.model.ReportRedactionEntry;
import com.iqser.red.service.redaction.report.v1.server.service.ExcelTemplateReportGenerationService;
import com.iqser.red.service.redaction.report.v1.server.service.GeneratePlaceholderService;
import com.iqser.red.service.redaction.report.v1.server.service.RedactionLogConverterService;
import com.iqser.red.service.redaction.report.v1.server.service.WordReportGenerationService;
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 org.apache.commons.io.IOUtils;
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.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import static com.iqser.red.service.redaction.report.v1.server.utils.OsUtils.getTemporaryDirectory;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class RealDataServiceTest {
@Autowired
private ObjectMapper objectMapper;
@MockBean
private StorageService storageService;
@MockBean
private ReportStorageService reportStorageService;
@MockBean
private ReportTemplateClient reportTemplateClient;
@MockBean
private FileAttributesConfigClient fileAttributesConfigClient;
@MockBean
private AmazonS3 s3Client;
@Autowired
private WordReportGenerationService wordReportGenerationService;
@MockBean
private MessagingConfiguration messagingConfiguration;
@Autowired
private RedactionLogConverterService redactionLogConverterService;
@MockBean
private DossierAttributesConfigClient dossierAttributesConfigClient;
@MockBean
private DossierAttributesClient dossierAttributesClient;
@Autowired
private ExcelTemplateReportGenerationService excelTemplateReportGenerationService;
@Autowired
private GeneratePlaceholderService generatePlaceholderService;
@Test
public void testWordReportGeneration() throws IOException {
String dossierTemplateId = "dossierTemplateId";
ClassPathResource redactionLogResource = new ClassPathResource("realdata/redaction-log.json");
RedactionLog redactionLog = objectMapper.readValue(redactionLogResource.getInputStream(), RedactionLog.class);
ClassPathResource legalBasisMappingResource = new ClassPathResource("files/legalBasisMapping.json");
List<LegalBasis> legalBasisMapping = objectMapper.readValue(legalBasisMappingResource.getInputStream(), new TypeReference<>() {
});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
FileModel fileStatus = FileModel.builder().filename("FileABCD").fileAttributes(Map.of("3e9b9569-5d2e-4619-848b-dd0a3e96527f", "Test")).build();
Dossier dossier = Dossier.builder().id("dossierId").dossierName("dossierName").build();
String templateId = "templateId";
String storageId = "storageId";
when(reportTemplateClient.getReportTemplate(dossierTemplateId, templateId)).thenReturn(ReportTemplate.builder()
.dossierTemplateId(dossierTemplateId)
.storageId(storageId)
.build());
ClassPathResource templateResource = new ClassPathResource("realdata/Justification Appendix A2.docx");
var placeholders = generatePlaceholderService.buildPlaceholders(dossier);
XWPFDocument doc = new XWPFDocument(templateResource.getInputStream());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template_wrg.docx")) {
fileOutputStream.write(report);
}
}
}