From a525fe688755af832c1441c6a728f02d1eeddbd7 Mon Sep 17 00:00:00 2001 From: Kilian Schuettler Date: Mon, 16 Dec 2024 16:05:37 +0100 Subject: [PATCH] RED-10683: fix NPE for firstRow --- .../report/v1/server/model/ExcelModel.java | 7 +++- .../v1/server/service/ExcelModelFactory.java | 20 ++++++----- .../service/ExcelReportGenerationService.java | 2 +- .../v1/server/service/ScmReportService.java | 34 +++++++++++-------- .../src/test/resources/logback-spring.xml | 17 ++++++++++ 5 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/logback-spring.xml 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 02466ea..d42b786 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 @@ -28,8 +28,13 @@ public class ExcelModel { private boolean placeholderInFirstRow; private boolean firstRowWritten; private boolean skippedPlaceholderPresent; - private boolean fileAttributesPlaceholderPresent; private boolean scmFunctionPlaceholderPresent; private ComponentReportModel componentReport; + private List fileAttributeColumns; + + public boolean isFileAttributesPlaceholderPresent() { + + return !fileAttributeColumns.isEmpty(); + } } diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelModelFactory.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelModelFactory.java index ecdb021..a1ba01b 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelModelFactory.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ExcelModelFactory.java @@ -42,6 +42,7 @@ import com.iqser.red.service.redaction.report.v1.server.client.FileAttributesCon import com.iqser.red.service.redaction.report.v1.server.model.CellIdentifier; import com.iqser.red.service.redaction.report.v1.server.model.ComponentReportModel; import com.iqser.red.service.redaction.report.v1.server.model.ExcelModel; +import com.iqser.red.service.redaction.report.v1.server.model.FileAttributeModel; import com.iqser.red.service.redaction.report.v1.server.model.PlaceholderInput; import io.micrometer.core.annotation.Timed; @@ -93,12 +94,12 @@ public class ExcelModelFactory { Map cellsToCopyInPlaceholderRow = new HashMap<>(); Integer componentColumn = null; Integer componentValueIndexColumn = null; + List fileAttributeCols = new ArrayList<>(); boolean hasRssPlaceHolders = false; boolean hasSkippedPlaceholder = false; int placeholderRow = -1; boolean placeholderInFirstRow = false; - boolean fileAttributesPlaceholder = false; boolean hasScmFunctionPlaceholder = false; for (int row = 0; row < sheet.getLastRowNum() + 1; row++) { @@ -117,7 +118,7 @@ public class ExcelModelFactory { continue; } - boolean skipCopy = false; + boolean isFileAttributesPlaceholder = false; int columnWidth = sheet.getColumnWidth(col); columnWidths.put(col, columnWidth); String cellStringValue = cell.getStringCellValue(); @@ -143,18 +144,19 @@ public class ExcelModelFactory { } if (cellStringValue.contains(FILE_ATTRIBUTES_PLACEHOLDER)) { - fileAttributesPlaceholder = true; // For each file attribute we have to replace the column header with the corresponding file attribute. List fileAttributeConfigs = fileAttributesConfigClient.getFileAttributeConfigs(dossierTemplateId); + var iterator = fileAttributeConfigs.iterator(); while (iterator.hasNext()) { cell = getOrCreateCell(actualRow, col); - cell.setCellValue(iterator.next().getLabel()); + FileAttributeConfig fileAttributeConfig = iterator.next(); + cell.setCellValue(fileAttributeConfig.getLabel()); cellsToCopyBeforePlaceholderRow.put(new CellIdentifier(row, col), cell); - + fileAttributeCols.add(FileAttributeModel.builder().id(fileAttributeConfig.getId()).cell(cell).build()); // If there is more than one file attribute we have to insert a new column, so we shift all columns to the right by 1 // and increase the current and last cell index. if (iterator.hasNext()) { @@ -164,10 +166,10 @@ public class ExcelModelFactory { } } - skipCopy = true; + isFileAttributesPlaceholder = true; } - if (!skipCopy) { + if (!isFileAttributesPlaceholder) { if (isRedactionPlaceholder(cellStringValue)) { if (cellStringValue.equals(SKIPPED_PLACEHOLDER)) { hasSkippedPlaceholder = true; @@ -205,9 +207,9 @@ public class ExcelModelFactory { placeholderInFirstRow, false, hasSkippedPlaceholder, - fileAttributesPlaceholder, hasScmFunctionPlaceholder, - componentReport); + componentReport, + fileAttributeCols); } 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 b7fd2f1..d671c47 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 @@ -163,7 +163,7 @@ public class ExcelReportGenerationService { continue; } - log.info("Copying cell {},{} with content {} from template", + log.debug("Copying cell {},{} with content {} from template", cellsToCopyEntry.getKey().getRowIndex(), cellsToCopyEntry.getKey().getColumnIndex(), formatter.formatCellValue(cellsToCopyEntry.getValue())); diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ScmReportService.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ScmReportService.java index c58228e..87f5351 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ScmReportService.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/ScmReportService.java @@ -6,9 +6,14 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.util.CellUtil; -import org.apache.poi.xssf.streaming.SXSSFSheet; import org.springframework.stereotype.Service; import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.componentlog.ComponentLog; @@ -38,9 +43,9 @@ public class ScmReportService { public void addComponentRows(Sheet sheet, FileModel fileModel, ExcelModel excelModel) { - Row firstRow = findFirstRow(sheet); +// Row firstRow = findFirstRow(sheet); - CellUtil.setAlignment(getOrCreateCell(firstRow, excelModel.getComponentReport().getComponentValueColumn()), HorizontalAlignment.LEFT); +// CellUtil.setAlignment(getOrCreateCell(firstRow, excelModel.getComponentReport().getComponentValueColumn()), HorizontalAlignment.LEFT); ComponentLog componentLog = componentResource.getComponentLog(fileModel.getDossierId(), fileModel.getId(), true); @@ -56,17 +61,17 @@ public class ScmReportService { } // Process each ComponentLogEntryValue - addOtherCells(sheet, excelModel, rowIndex, componentIndexMap, componentLogEntry, fileModel, firstRow); + addOtherCells(sheet, excelModel, rowIndex, componentIndexMap, componentLogEntry, fileModel); }); - // Autosize all cells besides the last column because the extracted text should be multiline - if (sheet instanceof SXSSFSheet) { - ((SXSSFSheet) sheet).trackAllColumnsForAutoSizing(); - for (int i = 0; i < firstRow.getLastCellNum() - 1; i++) { - sheet.autoSizeColumn(i); - } - } +// // Autosize all cells besides the last column because the extracted text should be multiline +// if (sheet instanceof SXSSFSheet) { +// ((SXSSFSheet) sheet).trackAllColumnsForAutoSizing(); +// for (int i = 0; i < firstRow.getLastCellNum() - 1; i++) { +// sheet.autoSizeColumn(i); +// } +// } sheet.createRow(rowIndex.get()); excelModel.getWrittenRows().add(rowIndex.get()); @@ -112,14 +117,13 @@ public class ScmReportService { AtomicInteger rowIndex, Map componentIndexMap, ComponentLogEntry componentLogEntry, - FileModel fileModel, - Row firstRow) { + FileModel fileModel) { String componentValue = componentLogEntry.getName().replaceAll("_", " "); int componentIndex = componentIndexMap.getOrDefault(componentValue, 0); - List fileAttributeModels = buildFileAttributeModels(fileModel, firstRow); + List fileAttributeModels = excelModel.getFileAttributeColumns(); for (ComponentLogEntryValue componentLogEntryValue : componentLogEntry.getComponentValues()) { diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/logback-spring.xml b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/logback-spring.xml new file mode 100644 index 0000000..33b2cef --- /dev/null +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/test/resources/logback-spring.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file