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.security.servlet.SecurityAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
@EnableAsync
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
@Import({DefaultWebMvcConfiguration.class, MessagingConfiguration.class})
@ -27,4 +31,9 @@ public class Application {
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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@ -15,10 +17,13 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
public class ExcelModel {
private Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos;
private int placeholderRow;
private Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos;
private int redactionPlaceholderRow;
private Map<Integer, Integer> cellWidths = new HashMap<>();
private Map<CellIdentifier, Cell> cellsToCopy = new HashMap<>();
private boolean copyDone;
private Map<CellIdentifier, Cell> cellsToCopyBeforeRedactionPlaceholderRow = new HashMap<>();
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.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;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class PlaceholderModel {
private List<String> placeholders;
private List<ImagePlaceholder> imagePlaceholders;
private Map<String, String> dossierAttributesPlaceholder;
private Map<String, String> fileAttributePlaceHolders;
private final Set<String> placeholders;
private final List<ImagePlaceholder> imagePlaceholders;
private final Map<String, String> dossierAttributesValueByPlaceholder;
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.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -40,10 +38,6 @@ import java.util.regex.Pattern;
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.CellStyle;
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.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.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.model.CellIdentifier;
import com.iqser.red.service.redaction.report.v1.server.model.ExcelModel;
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.TextPlaceholderInput;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -75,71 +65,94 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@RequiredArgsConstructor
@SuppressWarnings("PMD")
public class ExcelTemplateReportGenerationService {
public class ExcelReportGenerationService {
@SuppressWarnings("checkstyle:ParameterAssignment")
public ExcelModel generateReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, SXSSFWorkbook workbook, FileModel fileStatus,
Dossier dossier, boolean isLastFile, ExcelModel excelModel) {
@Timed("generateExcelReport")
public void generateExcelReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, SXSSFWorkbook workbook, String dossierName,
FileModel fileModel, ExcelModel excelModel, boolean isLastFile) {
long start = System.currentTimeMillis();
try {
for (Sheet sheet : workbook) {
if(!excelModel.isCopyDone()) {
if (!excelModel.isRowsBeforeRedactionEntryRowsAdded()) {
for (Map.Entry<Integer, Integer> colummWidthEntry : excelModel.getCellWidths().entrySet()) {
sheet.setColumnWidth(colummWidthEntry.getKey(), colummWidthEntry.getValue());
}
Set<Integer> createdRows = new HashSet<>();
for (Map.Entry<CellIdentifier, Cell> cellsToCopyEntry : excelModel.getCellsToCopy().entrySet()) {
if (!createdRows.contains(cellsToCopyEntry.getKey().getRowIndex())) {
sheet.createRow(cellsToCopyEntry.getKey().getRowIndex());
createdRows.add(cellsToCopyEntry.getKey().getRowIndex());
}
var createdCell = sheet.getRow(cellsToCopyEntry.getKey().getRowIndex())
.createCell(cellsToCopyEntry.getKey().getColumnIndex());
createdCell.setCellValue(cellsToCopyEntry.getValue().getStringCellValue());
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.cloneStyleFrom(cellsToCopyEntry.getValue().getCellStyle());
createdCell.setCellStyle(newCellStyle);
}
excelModel.setCopyDone(true);
addRows(workbook, sheet, excelModel.getCellsToCopyBeforeRedactionPlaceholderRow(), 0, placeholderModel, dossierName, fileModel.getFilename());
excelModel.setRowsBeforeRedactionEntryRowsAdded(true);
}
excelModel = addEntryRows(sheet, reportEntries, fileStatus.getFilename(), isLastFile, excelModel);
addRedactionEntryRows(sheet, reportEntries, fileModel.getFilename(), excelModel, placeholderModel);
// 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);
// }
// }
if (isLastFile) {
addRows(workbook, sheet, excelModel.getCellsToCopyAfterRedactionPlaceholderRow(), excelModel.getWrittenRows()
.size(), placeholderModel, dossierName, fileModel.getFilename());
}
}
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());
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());
} catch (Exception 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();
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos = new HashMap<>();
Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos = 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;
for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {
Row actualRow = sheet.getRow(j);
@ -147,43 +160,51 @@ public class ExcelTemplateReportGenerationService {
for (int i = 0; i < actualRow.getLastCellNum(); i++) {
Cell cell = sheet.getRow(j).getCell(i);
if (cell != null) {
int columnWidth = sheet.getColumnWidth(i);
columnWidths.put(i, columnWidth);
if (containsRedactionPlaceholder(cell.getStringCellValue())) {
int columnWidth = sheet.getColumnWidth(i);
columnWidths.put(i, columnWidth);
placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getStringCellValue()));
placeholderRow = j;
} 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);
return new ExcelModel(placeholderCellPos, placeholderRow, columnWidths, cellsToCopy, false);
return new ExcelModel(placeholderCellPos, placeholderRow, columnWidths, cellsToCopyBeforePlaceholderRow, cellsToCopyAfterPlaceholderRow, new ArrayList<>(), false);
}
@SuppressWarnings("checkstyle:ParameterAssignment")
private ExcelModel addEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename,
boolean isLastFile, ExcelModel excelModel) {
@Timed("excel-addRedactionEntryRows")
private void addRedactionEntryRows(Sheet sheet, List<ReportRedactionEntry> reportEntries, String filename,
ExcelModel excelModel, PlaceholderModel placeholderModel) {
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 -> {
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.setCellValue(entry1.getValue().apply(new TextPlaceholderInput(filename, entry)));
cell.setCellValue(entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
}
rowIndex.getAndIncrement();
});
log.debug("Adding rows took: {}", System.currentTimeMillis() - start);
excelModel.setPlaceholderRow(rowIndex.getAndIncrement());
return excelModel;
excelModel.setRedactionPlaceholderRow(rowIndex.getAndIncrement());
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)) {
return TextPlaceholderInput::getFilename;
return PlaceholderInput::getFilename;
}
if (placeholder.equals(PAGE_PLACEHOLDER)) {
return (input) -> String.valueOf(input.getEntry().getPage());
@ -228,61 +249,53 @@ public class ExcelTemplateReportGenerationService {
.replaceAll("\n", " ")
.replaceAll(" ", " ") : "";
}
// TODO in case placeholder is invalid -> do not replace with empty string but throw previus exception
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)) {
String safeToUseInReplaceAllString = Pattern.quote(search);
String escapedReplace = Matcher.quoteReplacement(replace);
String str = cell.getStringCellValue().replaceAll(safeToUseInReplaceAllString, escapedReplace);
cell.setCellValue(str);
}
}
if (cell.getStringCellValue().contains(search)) {
String safeToUseInReplaceAllString = Pattern.quote(search);
String escapedReplace = Matcher.quoteReplacement(replace);
String str = cell.getStringCellValue().replaceAll(safeToUseInReplaceAllString, escapedReplace);
cell.setCellValue(str);
}
}
private void replacePlaceholdersForImagePlaceholder(XSSFWorkbook workbook, Sheet sheet,
ImagePlaceholder imagePlaceholder) throws IOException {
private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell,
PlaceholderModel placeholderModel) throws IOException {
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getStringCellValue().contains(imagePlaceholder.getPlaceholder())) {
try (ByteArrayInputStream is = new ByteArrayInputStream(imagePlaceholder.getImage())) {
for (ImagePlaceholder imagePlaceholder : placeholderModel.getImagePlaceholders()) {
// ByteArrayInputStream scaledImage = getScaledImage(is, PixelUtil.widthUnits2Pixel((short) sheet.getColumnWidth(cell.getColumnIndex())), PixelUtil.heightUnits2Pixel(cell.getRow().getHeight()));
// is.reset();
if (cell.getStringCellValue().contains(imagePlaceholder.getPlaceholder())) {
try (ByteArrayInputStream is = new ByteArrayInputStream(imagePlaceholder.getImage())) {
double factor = calculateScale(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()
.getHeight()));
is.reset();
int pictureIdx = workbook.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG);
is.reset();
int pictureIdx = workbook.addPicture(is.readAllBytes(), SXSSFWorkbook.PICTURE_TYPE_JPEG);
is.reset();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
anchor.setCol1(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());
anchor.setCol1(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize(factor);
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize(factor);
cell.setCellValue("");
cell.setCellValue("");
}
}
}
}
@ -311,9 +324,8 @@ public class ExcelTemplateReportGenerationService {
}
private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus,
Map<String, String> fileAttributePlaceholders,
Map<String, String> dossierAttributesPlaceholders) {
private String getPlaceholderValue(String placeholder, String dossierName, String filename,
PlaceholderModel placeholderModel) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
return OffsetDateTime.now().format(FORMAT_DATE_ISO);
@ -328,23 +340,19 @@ public class ExcelTemplateReportGenerationService {
return OffsetDateTime.now().format(FORMAT_TIME_ISO);
}
if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) {
return project.getDossierName();
return dossierName;
}
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)) {
return fileStatus.getFileAttributes().get(id);
} else {
return null;
}
}
if (dossierAttributesPlaceholders.containsKey(placeholder)) {
return dossierAttributesPlaceholders.get(placeholder);
if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
return placeholderModel.getDossierAttributesValueByPlaceholder().get(placeholder);
}
return null;
}

View File

@ -14,8 +14,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.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.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;
@ -40,17 +43,15 @@ public class GeneratePlaceholderService {
private final DossierAttributesConfigClient dossierAttributesConfigClient;
public PlaceholderModel buildPlaceholders(Dossier dossier){
public PlaceholderModel buildPlaceholders(Dossier dossier) {
var dossierAttributes = dossierAttributesClient.getDossierAttributes(dossier.getId());
var dossierAttributesConfig = dossierAttributesConfigClient.getDossierAttributes(dossier.getDossierTemplateId());
var fileAttributePlaceHolders = getFileAttributePlaceholders(dossier.getDossierTemplateId());
List<String> placeholders = getDefaultPlaceholders();
Set<String> placeholders = getDefaultPlaceholders();
List<ImagePlaceholder> imagePlaceholders = new ArrayList<>();
Map<String, String> dossierAttributesPlaceholder = new HashMap<>(); //KEY:placeholder, VALUE:value
for (DossierAttributeConfig attributeConfig : dossierAttributesConfig) {
@ -63,7 +64,8 @@ public class GeneratePlaceholderService {
imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder()
.decode(dossierAttribute.getValue().split(",")[1])));
} else {
imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder().decode(dossierAttribute.getValue())));
imagePlaceholders.add(new ImagePlaceholder(attributeConfig.getPlaceholder(), Base64.getDecoder()
.decode(dossierAttribute.getValue())));
}
}
} else {
@ -83,19 +85,33 @@ public class GeneratePlaceholderService {
placeholders.addAll(fileAttributePlaceHolders.keySet());
placeholders.addAll(dossierAttributesPlaceholder.keySet());
return new PlaceholderModel(placeholders,imagePlaceholders,dossierAttributesPlaceholder,fileAttributePlaceHolders);
return new PlaceholderModel(placeholders, imagePlaceholders, dossierAttributesPlaceholder, fileAttributePlaceHolders);
}
private List<String> getDefaultPlaceholders() {
public void resolveFileAttributeValues(FileModel fileModel, PlaceholderModel placeholderModel) {
List<String> defPlaceholders = new ArrayList<>();
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));
Map<String, String> fileAttributeValueByPlaceholder = new HashMap<>();
for (Map.Entry<String, String> idEntry : placeholderModel.getFileAttributeIdByPlaceholder().entrySet()) {
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) {
Map<String, String> fileAttributePlaceholders = new HashMap<>();
@ -106,4 +122,5 @@ public class GeneratePlaceholderService {
});
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 io.micrometer.core.annotation.Timed;
@Service
public class IuclidFunctionService {
@Timed("computeIuclidFunction")
public String computeIuclidFunction(List<ReportRedactionEntry> reportRedactionEntries, String filename) {
Map<String, List<ReportRedactionEntry>> entriesPerJustification = new HashMap<>();

View File

@ -1,5 +1,18 @@
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.dossiertemplate.ReportTemplate;
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.storage.commons.exception.StorageObjectDoesNotExist;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
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
@Service
@RequiredArgsConstructor
@SuppressWarnings("PMD")
public class ReportGenerationService {
private final ReportStorageService reportStorageService;
@ -53,12 +52,13 @@ public class ReportGenerationService {
private final DossierClient dossierClient;
private final ReportTemplateClient reportTemplateClient;
private final RedactionLogClient redactionLogClient;
private final ExcelTemplateReportGenerationService excelTemplateReportGenerationService;
private final ExcelReportGenerationService excelTemplateReportGenerationService;
private final GeneratePlaceholderService generatePlaceholderService;
@SneakyThrows
public List<StoredFileInformation> generateReport(ReportRequestMessage reportMessage) {
@Timed("generateReports")
public List<StoredFileInformation> generateReports(ReportRequestMessage reportMessage) {
List<StoredFileInformation> storedFileInformation = Collections.synchronizedList(new ArrayList<>());
@ -76,7 +76,7 @@ public class ReportGenerationService {
try (ByteArrayInputStream is = new ByteArrayInputStream(excelTemplate)) {
XSSFWorkbook readWorkbook = new XSSFWorkbook(is);
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
for(Sheet sheet: readWorkbook){
for (Sheet sheet : readWorkbook) {
writeWorkbook.createSheet(sheet.getSheetName());
}
MultiFileWorkbook multiFileWorkbook = new MultiFileWorkbook(readWorkbook, writeWorkbook, templateId, reportTemplate.getFileName());
@ -108,10 +108,12 @@ public class ReportGenerationService {
int i = 1;
for (int j = 0; j < reportMessage.getFileIds().size(); j++) {
long start = System.currentTimeMillis();
var fileStatus = fileStatusClient.getFileStatus(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j));
long start = System.currentTimeMillis();
generatePlaceholderService.resolveFileAttributeValues(fileStatus, placeholderModel);
List<ReportRedactionEntry> reportEntries = getReportEntries(reportMessage.getDossierId(), reportMessage.getFileIds()
.get(j), fileStatus.isExcluded());
@ -119,22 +121,21 @@ public class ReportGenerationService {
var isLastFile = j == reportMessage.getFileIds().size() - 1;
for (MultiFileWorkbook multiFileWorkbook : multiFileWorkbooks) {
if(excelModel == null){
excelModel = excelTemplateReportGenerationService.caculateExcelModel(multiFileWorkbook.getReadWorkBook().getSheetAt(0));
if (excelModel == null) {
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) {
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) {
storedFileInformation.add(createReportFromTemplate(dossier, fileStatus, placeholderModel, reportTemplate.getFileName(), reportMessage.getDownloadId(), reportEntries, reportTemplate));
}
long end = System.currentTimeMillis();
@ -169,11 +170,11 @@ public class ReportGenerationService {
try (ByteArrayInputStream is = new ByteArrayInputStream(excelTemplate)) {
XSSFWorkbook readWorkbook = new XSSFWorkbook(is);
SXSSFWorkbook writeWorkbook = new SXSSFWorkbook();
for(Sheet sheet: readWorkbook){
for (Sheet sheet : readWorkbook) {
writeWorkbook.createSheet(sheet.getSheetName());
}
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(readWorkbook.getSheetAt(0));
excelTemplateReportGenerationService.generateReport(reportEntries, placeholderModel, templateName, writeWorkbook, fileStatus, dossier, true, excelModel);
var excelModel = excelTemplateReportGenerationService.calculateExcelModel(readWorkbook.getSheetAt(0));
excelTemplateReportGenerationService.generateExcelReport(reportEntries, placeholderModel, templateName, writeWorkbook, dossier.getDossierName(), fileStatus, excelModel, true);
byte[] template = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
String storageId = reportStorageService.storeObject(downloadId, template);
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());
try (ByteArrayInputStream is = new ByteArrayInputStream(wordTemplate)) {
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);
String storageId = reportStorageService.storeObject(downloadId, template);
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) {
if (isExcluded) {

View File

@ -36,7 +36,7 @@ public class ReportMessageReceiver {
long start = System.currentTimeMillis();
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);
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.file.FileModel;
import com.iqser.red.service.redaction.report.v1.server.model.*;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Dimension2DDouble;
@ -14,6 +17,7 @@ import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -30,18 +34,15 @@ import static com.iqser.red.service.redaction.report.v1.server.service.Placehold
@Slf4j
@Service
@RequiredArgsConstructor
@SuppressWarnings("PMD")
public class WordReportGenerationService {
private final IuclidFunctionService iuclidFunctionService;
public XWPFDocument generateReport(List<ReportRedactionEntry> reportEntries,
PlaceholderModel placeholderModel,
String reportTemplateName,
XWPFDocument doc, FileModel fileStatus,
Dossier dossier, boolean isLastFile) {
@Timed("generateWordReport")
public XWPFDocument generateWordReport(List<ReportRedactionEntry> reportEntries, PlaceholderModel placeholderModel,
String reportTemplateName, XWPFDocument doc, FileModel fileModel,
Dossier dossier, boolean isLastFile) {
long start = System.currentTimeMillis();
@ -51,40 +52,32 @@ public class WordReportGenerationService {
replaceImagePlaceholders(doc, imagePlaceholder);
}
long t1 = System.currentTimeMillis();
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();
replaceTextPlaceholders(doc, placeholderModel, dossier, fileStatus, table, reportEntries);
var placeholderFunctions = computePlaceholderPos(table, placeholderModel);
addRedactionEntryRows(table, reportEntries, fileModel.getFilename(), placeholderModel, placeholderFunctions);
replaceTextPlaceholders(doc, placeholderModel, dossier.getDossierName(), fileModel.getFilename(), table, reportEntries);
if (isLastFile) {
removePlaceholdersRow(table);
}
t2 = System.currentTimeMillis();
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());
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());
return doc;
} catch (Exception e) {
log.error(e.getMessage() + " in file: " + fileStatus.getFilename());
log.error(e.getMessage() + " in file: " + fileModel.getFilename());
throw new RuntimeException(e);
}
}
private void removePlaceholdersRow(XWPFTable table) {
if (table == null){
if (table == null) {
return;
}
for (int j = 0; j < table.getRows().size(); j++) {
@ -112,8 +105,9 @@ public class WordReportGenerationService {
}
private String getPlaceholderValue(String placeholder, Dossier project, FileModel fileStatus, Map<String, String> fileAttributePlaceholders,
Map<String, String> dossierAttributesPlaceholders, List<ReportRedactionEntry> reportRedactionEntries) {
private String getPlaceholderValue(String placeholder, String dossierName, String filename,
PlaceholderModel placeholderModel,
List<ReportRedactionEntry> reportRedactionEntries) {
if (placeholder.equals(FORMAT_DATE_ISO_PLACEHOLDER)) {
return OffsetDateTime.now().format(FORMAT_DATE_ISO);
@ -128,31 +122,29 @@ public class WordReportGenerationService {
return OffsetDateTime.now().format(FORMAT_TIME_ISO);
}
if (placeholder.equals(DOSSIER_NAME_PLACEHOLDER)) {
return project.getDossierName();
return dossierName;
}
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return fileStatus.getFilename();
return filename;
}
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)) {
return fileStatus.getFileAttributes().get(id);
} else {
return "";
}
if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) {
return placeholderModel.getFileAttributeValueByPlaceholder().get(placeholder);
}
if (dossierAttributesPlaceholders.containsKey(placeholder)) {
return dossierAttributesPlaceholders.get(placeholder);
if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
return placeholderModel.getDossierAttributesValueByPlaceholder().get(placeholder);
}
return "";
}
private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs, ImagePlaceholder imagePlaceholder) {
private void replaceParagraphForImagePlaceholder(List<XWPFParagraph> paragraphs,
ImagePlaceholder imagePlaceholder) {
for (XWPFParagraph p : paragraphs) {
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<>();
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);
}
@ -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) {
String paragraphText = p.getText();
@ -264,130 +260,134 @@ public class WordReportGenerationService {
}
private Map<Integer, Function<TextPlaceholderInput, String>> computePlaceholderPos(XWPFTable table) {
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos = new HashMap<>();
@Timed("word-computePlaceholderPos")
private PlaceHolderFunctions computePlaceholderPos(XWPFTable table, PlaceholderModel placeholderModel) {
Set<String> foundPlaceHolder = new HashSet<>();
Map<Integer, Function<PlaceholderInput, String>> placeholderCellPos = new HashMap<>();
if (table != null) {
for (int j = 0; j < table.getRows().size(); j++) {
for (int i = 0; i < table.getRows().get(j).getTableCells().size(); i++) {
XWPFTableCell cell = table.getRows().get(j).getTableCells().get(i);
if (containsRedactionPlaceholder(cell.getText())) {
placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getText()));
placeholderCellPos.put(i, getFunctionForPlaceHolder(cell.getText(), foundPlaceHolder, placeholderModel));
} else if (cell.getText().isEmpty()) {
placeholderCellPos.put(i, (input) -> "");
}
}
}
}
return placeholderCellPos;
return new PlaceHolderFunctions(placeholderCellPos, foundPlaceHolder);
}
private void addTableRows(XWPFTable table,
List<ReportRedactionEntry> reportEntries,
String filename,
FileModel fileStatus,
Map<String, String> fileAttributePlaceholders,
Map<Integer, Function<TextPlaceholderInput, String>> placeholderCellPos) {
@Timed("word-addRedactionEntryRows")
private void addRedactionEntryRows(XWPFTable table, List<ReportRedactionEntry> reportEntries, String filename,
PlaceholderModel placeholderModel, PlaceHolderFunctions placeHolderFunctions) {
if (table == null) {
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);
for (Map.Entry<String, List<ReportRedactionEntry>> entry : redactionsPerJustification.entrySet()) {
XWPFTableRow row = table.createRow();
// for (Map.Entry<Integer, Function<String, String>> entry1 : placeholderCellPos.entrySet()) {
//
// setText(row.getCell(entry1.getKey()), replaceSeedsPlaceholder(entry, filename, entry1.getValue(), fileStatus, fileAttributePlaceholders));
//
// if (!entry1.getValue().isEmpty()) {
//
// } else {
// setText(row.getCell(entry1.getKey()), "");
// }
// }
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
.entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue()
.apply(new PlaceholderInput(filename, null, placeholderModel, entry)));
}
}
} else {
reportEntries.forEach(entry -> {
XWPFTableRow row = table.createRow();
for (Map.Entry<Integer, Function<TextPlaceholderInput, String>> entry1 : placeholderCellPos.entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue().apply(new TextPlaceholderInput(filename, entry)));
for (Map.Entry<Integer, Function<PlaceholderInput, String>> entry1 : placeHolderFunctions.getFunctionPerPlaceHolder()
.entrySet()) {
setText(row.getCell(entry1.getKey()), entry1.getValue()
.apply(new PlaceholderInput(filename, entry, placeholderModel, null)));
}
});
}
}
private 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<TextPlaceholderInput, String> getFunctionForPlaceHolder(String placeholder) {
private Function<PlaceholderInput, String> getFunctionForPlaceHolder(String placeholder,
Set<String> foundPlaceholders,
PlaceholderModel placeholderModel) {
if (placeholder.equals(FILE_NAME_PLACEHOLDER)) {
return TextPlaceholderInput::getFilename;
foundPlaceholders.add(FILE_NAME_PLACEHOLDER);
return PlaceholderInput::getFilename;
}
if (placeholder.equals(PAGE_PLACEHOLDER)) {
foundPlaceholders.add(PAGE_PLACEHOLDER);
return (input) -> String.valueOf(input.getEntry().getPage());
}
if (placeholder.equals(PARAGRAPH_PLACEHOLDER)) {
foundPlaceholders.add(PARAGRAPH_PLACEHOLDER);
return (input) -> input.getEntry().getSection();
}
if (placeholder.equals(JUSTIFICATION_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_PLACEHOLDER);
return (input) -> input.getEntry().getJustification();
}
if (placeholder.equals(JUSTIFICATION_PARAGRAPH_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_PARAGRAPH_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationParagraph();
}
if (placeholder.equals(JUSTIFICATION_REASON_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_REASON_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationReason();
}
if (placeholder.equals(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_LEGAL_BASIS_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationParagraph();
}
if (placeholder.equals(JUSTIFICATION_TEXT_PLACEHOLDER)) {
foundPlaceholders.add(JUSTIFICATION_TEXT_PLACEHOLDER);
return (input) -> input.getEntry().getJustificationReason();
}
if (placeholder.equals(EXCERPT_PLACEHOLDER)) {
foundPlaceholders.add(EXCERPT_PLACEHOLDER);
return (input) -> input.getEntry().getExcerpt();
}
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)) {
var pages = entry.getValue().stream().map(ReportRedactionEntry::getPage).collect(Collectors.toSet());
return computePageRanges(pages);
foundPlaceholders.add(SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER);
return (input) -> computePageRanges(input.getRedactionsPerJustificationEntry()
.getValue()
.stream()
.map(ReportRedactionEntry::getPage)
.collect(Collectors.toSet()));
}
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)) {
return fileStatus.getFileAttributes().get(id);
} else {
return null;
}
if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) {
foundPlaceholders.add(placeholder);
return (input) -> input.getPlaceholderModel().getFileAttributeValueByPlaceholder().get(placeholder);
}
return "";
if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) {
foundPlaceholders.add(placeholder);
return (input) -> input.getPlaceholderModel().getDossierAttributesValueByPlaceholder().get(placeholder);
}
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.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;
@ -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.ReportTemplateClient;
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.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.RedactionLogConverterService;
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.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
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 java.io.FileOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.*;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.DOSSIER_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FILE_NAME_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ENG_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_GER_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_DATE_ISO_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.FORMAT_TIME_ISO_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.IUCLID_FUNCTION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_JUSTIFICATION_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.service.PlaceholderService.SEEDS_FUNCTION_REDACTION_GROUPED_BY_JUSTIFICATION_PAGES_PLACEHOLDER;
import static com.iqser.red.service.redaction.report.v1.server.utils.OsUtils.getTemporaryDirectory;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@ -86,504 +87,289 @@ public class RedactionReportIntegrationTest {
private DossierAttributesClient dossierAttributesClient;
@Autowired
private ExcelTemplateReportGenerationService excelTemplateReportGenerationService;
private ExcelReportGenerationService excelTemplateReportGenerationService;
@Autowired
private GeneratePlaceholderService generatePlaceholderService;
@Test
public void testWordReportGeneration() throws IOException {
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<>() {
});
@SneakyThrows
public void testWordJustificationAppendixA1(){
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
var dossierAttributeConfig1 = DossierAttributeConfig
.builder()
.id("id")
.label("label")
.editable(true)
.type(DossierAttributeType.TEXT)
.placeholder("{{dossier.attribute.name}}")
.build();
var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A1.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
var dossierAttributeConfig2 = DossierAttributeConfig
.builder()
.id("id2")
.label("label2")
.editable(true)
.type(DossierAttributeType.IMAGE)
.placeholder("{{dossier.attribute.Signature}}")
.build();
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());
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);
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(dossierAttributesConfig);
var wordTemplateResource = new ClassPathResource("templates/Justification Appendix A2.docx");
var imageResource = new ClassPathResource("files/exampleImage.jpg");
var dossierAttribute1 = DossierAttribute
.builder()
.dossierAttributeConfigId("id")
.dossierId("dossierId")
.value("Michael")
.build();
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());
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
.builder()
.dossierAttributeConfigId("id2")
.dossierId("dossierId")
.value("data:image/png;base64," + Base64.getEncoder()
.encodeToString(IOUtils.toByteArray(imageResource.getInputStream())))
.build();
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 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);
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);
ClassPathResource templateResource = new ClassPathResource("templates/IUCLID_Template.docx");
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);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/efsa_template_wrg.docx")) {
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/IUCLID_Template_justification.docx")) {
fileOutputStream.write(report);
}
}
@Test
@Ignore
public void testExcelTemplateReportGeneration() throws IOException {
@SneakyThrows
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 redactionLog2 = objectMapper.readValue(excelRedactionLogResource.getInputStream(), RedactionLog.class);
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<>() {
});
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> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping);
DossierAttributeConfig dossierAttributeConfig = new DossierAttributeConfig("id", "label", true, "{{dossier.attribute.name}}", DossierAttributeType.TEXT, dossierTemplateId);
DossierAttributeConfig dossierAttributeConfig2 = new DossierAttributeConfig("id2", "label2", false, "{{dossier.attribute.Signature}}", DossierAttributeType.IMAGE, dossierTemplateId);
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(List.of(dossierAttributeConfig, dossierAttributeConfig2));
FileModel fileStatus = FileModel.builder().filename("filename").build();
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
DossierAttribute dossierAttribute = new DossierAttribute(dossierId, dossierAttributeConfig.getId(), "Michael");
DossierAttribute dossierAttribute2 = new DossierAttribute(dossierId, dossierAttributeConfig2.getId(), Base64.getEncoder().encodeToString(IOUtils.toByteArray(imageResource.getInputStream())));
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(List.of(dossierAttribute, dossierAttribute2));
ClassPathResource templateResource = new ClassPathResource("templates/Seeds - New Justification Form.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);
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
var placeholders = buildPlaceHolderModel(new HashMap<>(), Map.of("{{file.attribute.Path}}", "Path"), List.of());
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();
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
Dossier dossier = Dossier.builder().id(dossierId).dossierName("dossierName").build();
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);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/seeds-report.docx")) {
fileOutputStream.write(report);
}
}
@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";
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);
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> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, 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));
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();
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())));
FileModel fileStatus = FileModel.builder().filename("filename").build();
Dossier dossier = Dossier.builder().dossierName("dossierName").build();
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);
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(List.of(fileAttributeConfig));
var placeholders = buildPlaceHolderModel(new HashMap<>(), Map.of("{{file.attribute.Path}}", "Path"), List.of());
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();
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileStatus, dossier, false);
wordReportGenerationService.generateWordReport(reportEntriesSecondFile, placeholders, "test", doc, fileModelSecondFile, dossier, true);
byte[] report = wordReportGenerationService.toByteArray(doc);
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());
wordReportGenerationService.generateReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
wordReportGenerationService.generateWordReport(reportEntries, placeholders, "test", doc, fileModel, dossier, true);
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);
}
}
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());
@Test
@SneakyThrows
public void testExcelTemplateReportGenerationSingleFile() {
var excelModel = excelTemplateReportGenerationService.caculateExcelModel(wb.getSheetAt(0));
SXSSFWorkbook workbook = new SXSSFWorkbook();
workbook.createSheet("Test Row Peformance");
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);
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() + "/report_excel_template.xlsx")) {
fileOutputStream.write(excelTemplateReport);
}
XSSFWorkbook wb2 = new XSSFWorkbook(excelTemplateResource.getInputStream());
FileModel fileModel = FileModel.builder().filename("filename").build();
var excelModel2 = excelTemplateReportGenerationService.caculateExcelModel(wb2.getSheetAt(0));
SXSSFWorkbook workbook2 = new SXSSFWorkbook();
workbook2.createSheet("Test Row Peformance");
ClassPathResource templateResource = new ClassPathResource("templates/Excel Report.xlsx");
excelTemplateReportGenerationService.generateReport(reportEntries, placeholders, "test", workbook2, fileModel, dossier, true,excelModel2);
byte[] excelTemplateReport2 = excelTemplateReportGenerationService.toByteArray(workbook2);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/report_excel_template2.xlsx")) {
fileOutputStream.write(excelTemplateReport2);
}
XSSFWorkbook wb3 = new XSSFWorkbook(excelTemplateResource.getInputStream());
var excelModel3 = excelTemplateReportGenerationService.caculateExcelModel(wb2.getSheetAt(0));
SXSSFWorkbook workbook3 = new SXSSFWorkbook();
workbook3.createSheet("Test Row Peformance");
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")) {
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, true);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_justification.xlsx")) {
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
@SneakyThrows
public void testIuclidReport() {
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<>() {
});
public void testExcelTemplateReportGenerationMultiFile() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
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<>());
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);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/Excel Report_multifile.xlsx")) {
fileOutputStream.write(excelTemplateReport3);
}
}
@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
@SneakyThrows
public void testSeedsFunctionService() {
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<>() {
});
public void testExcelPlaceholders() {
RedactionLog redactionLog = objectMapper.readValue(new ClassPathResource("files/redactionLog.json").getInputStream(), RedactionLog.class);
List<LegalBasis> legalBasisMapping = objectMapper.readValue(new ClassPathResource("files/legalBasisMapping.json").getInputStream(), new TypeReference<>() {});
List<ReportRedactionEntry> reportEntries = redactionLogConverterService.convertAndSort(redactionLog, legalBasisMapping);
List<ReportRedactionEntry> reportEntries2 = redactionLogConverterService.convertAndSort(redactionLog2, legalBasisMapping);
var imageResource = new ClassPathResource("files/exampleImage.jpg");
when(dossierAttributesConfigClient.getDossierAttributes(dossierTemplateId)).thenReturn(new ArrayList<>());
when(dossierAttributesClient.getDossierAttributes("dossierId")).thenReturn(new ArrayList<>());
when(fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId)).thenReturn(new ArrayList<>());
FileModel fileModel = FileModel.builder().filename("filename").build();
ClassPathResource templateResource = new ClassPathResource("templates/Excel Report_PlaceholderTest.xlsx");
var placeholders = buildPlaceHolderModel(Map.of("{{dossier_attribute.test1}}", "replaced_dossier_test1"), Map.of("{{file_attribute.test1}}", "replaced_file_test1", "{{file_attribute.test2}}", "replaced_file_test2"), List.of(new ImagePlaceholder("{{dossier.attribute.Signature}}",IOUtils.toByteArray(imageResource.getInputStream()))));
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();
FileModel fileStatus2 = FileModel.builder().filename("second file").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-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);
byte[] excelTemplateReport3 = excelTemplateReportGenerationService.toByteArray(writeWorkbook);
try (FileOutputStream fileOutputStream = new FileOutputStream(getTemporaryDirectory() + "/PlaceholderTest_justification.xlsx")) {
fileOutputStream.write(excelTemplateReport3);
}
}
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);
}
}
}