From 1c3a0e90b4aa035e1620c4c5ea6ff1377342e4ba Mon Sep 17 00:00:00 2001 From: Andrei Isvoran Date: Wed, 22 Nov 2023 11:25:10 +0100 Subject: [PATCH] DM-578 - Add backwards compatibility to be able to use old report template --- .../report/v1/server/model/ExcelModel.java | 1 + .../service/ComponentRowsReportService.java | 81 +++++++++++++++++++ .../service/ExcelReportGenerationService.java | 16 +++- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ComponentRowsReportService.java diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/ExcelModel.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/ExcelModel.java index 0276b82..e7b7f72 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/ExcelModel.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/model/ExcelModel.java @@ -30,5 +30,6 @@ public class ExcelModel { private boolean firstRowWritten; private boolean skippedPlaceholderPresent; private boolean fileAttributesPlaceholderPresent; + private boolean scmFunctionPlaceholderPresent; } diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ComponentRowsReportService.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ComponentRowsReportService.java new file mode 100644 index 0000000..7ab8773 --- /dev/null +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ComponentRowsReportService.java @@ -0,0 +1,81 @@ +package com.iqser.red.service.redaction.report.v1.server.service; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.springframework.stereotype.Service; + +import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.componentlog.ComponentLog; +import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileAttributeConfig; +import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileModel; +import com.iqser.red.service.redaction.report.v1.server.client.ComponentClient; +import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesConfigClient; +import com.iqser.red.service.redaction.report.v1.server.model.ExcelModel; + +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; + +@Service +@RequiredArgsConstructor +@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) +public class ComponentRowsReportService { + + ComponentClient componentResource; + FileAttributesConfigClient fileAttributesClient; + + @SuppressWarnings("checkstyle:all") + static int COMPONENT_NAME_COL = 0; + static int COMPONENT_VALUE_STARTING_COL = 1; + + + public void addComponentRows(Sheet sheet, FileModel fileModel, ExcelModel excelModel) { + + ComponentLog componentLog = componentResource.getComponentLog(fileModel.getDossierId(), fileModel.getId(), true); + + AtomicInteger rowIndex = new AtomicInteger(excelModel.getRedactionPlaceholderRow()); + + String oecd = getOecdNumber(fileModel); + + componentLog.getComponentLogEntries().forEach(componentLogEntry -> { + + if (componentLogEntry.getComponentValues().isEmpty()) { + return; + } + int componentRowIdx = rowIndex.getAndIncrement(); + Row componentRow = sheet.createRow(componentRowIdx); + excelModel.getWrittenRows().add(componentRowIdx); + + Cell componentNameCell = componentRow.createCell(COMPONENT_NAME_COL); + componentNameCell.setCellValue(oecd + "-" + componentLogEntry.getName().replaceAll("_", " ")); + + for (int valueIdx = 0; valueIdx < componentLogEntry.getComponentValues().size(); valueIdx++) { + String value = componentLogEntry.getComponentValues().get(valueIdx).getValue(); + componentRow.createCell(COMPONENT_VALUE_STARTING_COL + valueIdx).setCellValue(value); + } + + }); + + sheet.createRow(rowIndex.get()); + excelModel.getWrittenRows().add(rowIndex.get()); + + excelModel.setRedactionPlaceholderRow(rowIndex.getAndIncrement()); + + } + + + private String getOecdNumber(FileModel file) { + + return fileAttributesClient.getFileAttributeConfigs(file.getDossierTemplateId()) + .stream() + .filter(f -> f.getLabel().equals("OECD Number")) + .map(FileAttributeConfig::getId) + .findFirst() + .map(oecd -> file.getFileAttributes().get(oecd)) + .orElse(null); + + } + +} diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelReportGenerationService.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelReportGenerationService.java index 2fbc84f..2202307 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelReportGenerationService.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelReportGenerationService.java @@ -105,6 +105,7 @@ public class ExcelReportGenerationService { private final ScmReportService componentReportService; private final FileAttributesConfigClient fileAttributesConfigClient; + private final ComponentRowsReportService componentRowsReportService; @Timed("redactmanager_generateExcelReport") @@ -147,7 +148,7 @@ public class ExcelReportGenerationService { } } - if (!excelModel.isRssPlaceholdersPresent() && !excelModel.isFileAttributesPlaceholderPresent()) { + if (!excelModel.isRssPlaceholdersPresent() && !excelModel.isFileAttributesPlaceholderPresent() && !excelModel.isScmFunctionPlaceholderPresent()) { addRedactionEntryRows(sheet, reportEntries, fileModel.getFilename(), excelModel, placeholderModel); } @@ -155,6 +156,11 @@ public class ExcelReportGenerationService { componentReportService.addScmRows(sheet, fileModel, excelModel); } + if (excelModel.isScmFunctionPlaceholderPresent()) { + componentRowsReportService.addComponentRows(sheet, fileModel, excelModel); + } + + if (isLastFile) { addRows(workbook, sheet, @@ -386,6 +392,7 @@ public class ExcelReportGenerationService { int placeholderRow = -1; boolean placeholderInFirstRow = false; boolean fileAttributesPlaceholder = false; + boolean hasScmFunctionPlaceholder = false; for (int j = 0; j < sheet.getLastRowNum() + 1; j++) { Row actualRow = sheet.getRow(j); @@ -408,6 +415,10 @@ public class ExcelReportGenerationService { hasRssPlaceHolders = true; } + if (cellStringValue.contains(SCM_FUNCTION_PLACEHOLDER)) { + hasScmFunctionPlaceholder = true; + } + if (cellStringValue.contains(FILE_ATTRIBUTES_PLACEHOLDER)) { fileAttributesPlaceholder = true; @@ -481,7 +492,8 @@ public class ExcelReportGenerationService { placeholderInFirstRow, false, hasSkippedPlaceholder, - fileAttributesPlaceholder); + fileAttributesPlaceholder, + hasScmFunctionPlaceholder); }