From a652de293b9c884f94460bb79df467eb37e5c95c Mon Sep 17 00:00:00 2001 From: Kilian Schuettler Date: Mon, 16 Dec 2024 12:32:45 +0100 Subject: [PATCH] RED-10683: fix NPE for firstRow --- .../service/ExcelReportGenerationService.java | 113 ++++++++---------- .../v1/server/service/ScmReportService.java | 44 ++++--- 2 files changed, 76 insertions(+), 81 deletions(-) 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 b305d1e..1593df2 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 @@ -34,7 +34,6 @@ import static com.iqser.red.service.redaction.report.v1.server.service.Placehold import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.HashMap; @@ -127,69 +126,63 @@ public class ExcelReportGenerationService { .filter(entry -> !entry.isSkipped()) .collect(Collectors.toList()) : allReportEntries; - try { + for (Sheet sheet : workbook) { - for (Sheet sheet : workbook) { + if (!excelModel.isRowsBeforeRedactionEntryRowsAdded()) { - if (!excelModel.isRowsBeforeRedactionEntryRowsAdded()) { - - for (Map.Entry columnWidthEntry : excelModel.getCellWidths().entrySet()) { - sheet.setColumnWidth(columnWidthEntry.getKey(), columnWidthEntry.getValue()); - } - - addRows(workbook, - sheet, - excelModel.getCellsToCopyBeforeRedactionPlaceholderRow(), - excelModel.isRssPlaceholdersPresent() ? excelModel.getWrittenRows().size() : 0, - placeholderModel, - dossierName, - fileModel.getFilename(), - excelModel); - - if (!excelModel.isRssPlaceholdersPresent()) { - excelModel.setRowsBeforeRedactionEntryRowsAdded(true); - } + for (Map.Entry columnWidthEntry : excelModel.getCellWidths().entrySet()) { + sheet.setColumnWidth(columnWidthEntry.getKey(), columnWidthEntry.getValue()); } - if (!excelModel.isRssPlaceholdersPresent() && !excelModel.isFileAttributesPlaceholderPresent() && !excelModel.isScmFunctionPlaceholderPresent()) { - addRedactionEntryRows(sheet, reportEntries, fileModel.getFilename(), excelModel, placeholderModel); - } + addRows(workbook, + sheet, + excelModel.getCellsToCopyBeforeRedactionPlaceholderRow(), + excelModel.isRssPlaceholdersPresent() ? excelModel.getWrittenRows().size() : 0, + placeholderModel, + dossierName, + fileModel.getFilename(), + excelModel); - if (excelModel.isFileAttributesPlaceholderPresent()) { - componentReportService.addScmRows(sheet, fileModel, excelModel); - } - - if (excelModel.isScmFunctionPlaceholderPresent()) { - componentRowsReportService.addComponentRows(sheet, fileModel, excelModel); - } - - if (isLastFile) { - addRows(workbook, - sheet, - excelModel.getCellsToCopyAfterRedactionPlaceholderRow(), - excelModel.getWrittenRows().size(), - placeholderModel, - dossierName, - fileModel.getFilename(), - excelModel); + if (!excelModel.isRssPlaceholdersPresent()) { + excelModel.setRowsBeforeRedactionEntryRowsAdded(true); } } - log.info("Report Generation took: {} for file with id {}, pageCount: {}, entityLogEntryCount: {}, reportName: {}, className: {}", - System.currentTimeMillis() - start, - fileModel.getId(), - fileModel.getNumberOfPages(), - reportEntries.size(), - reportTemplateName, - getClass().getSimpleName()); + if (!excelModel.isRssPlaceholdersPresent() && !excelModel.isFileAttributesPlaceholderPresent() && !excelModel.isScmFunctionPlaceholderPresent()) { + addRedactionEntryRows(sheet, reportEntries, fileModel.getFilename(), excelModel, placeholderModel); + } - } catch (Exception e) { - throw new RuntimeException(e); + if (excelModel.isFileAttributesPlaceholderPresent()) { + componentReportService.addScmRows(sheet, fileModel, excelModel); + } + + if (excelModel.isScmFunctionPlaceholderPresent()) { + componentRowsReportService.addComponentRows(sheet, fileModel, excelModel); + } + + if (isLastFile) { + addRows(workbook, + sheet, + excelModel.getCellsToCopyAfterRedactionPlaceholderRow(), + excelModel.getWrittenRows().size(), + placeholderModel, + dossierName, + fileModel.getFilename(), + excelModel); + } } + + log.info("Report Generation took: {} for file with id {}, pageCount: {}, entityLogEntryCount: {}, reportName: {}, className: {}", + System.currentTimeMillis() - start, + fileModel.getId(), + fileModel.getNumberOfPages(), + reportEntries.size(), + reportTemplateName, + getClass().getSimpleName()); + } - @SneakyThrows private void addRows(SXSSFWorkbook workbook, Sheet sheet, Map copiedCells, @@ -268,7 +261,8 @@ public class ExcelReportGenerationService { } - private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell, PlaceholderModel placeholderModel) throws IOException { + @SneakyThrows + private void replacePlaceholdersForImagePlaceholder(SXSSFWorkbook workbook, Sheet sheet, Cell cell, PlaceholderModel placeholderModel) { for (ImagePlaceholder imagePlaceholder : placeholderModel.getImagePlaceholders()) { @@ -335,18 +329,15 @@ public class ExcelReportGenerationService { return filename; } if (placeholderModel.getFileAttributeValueByPlaceholder().containsKey(placeholder)) { - return placeholderModel.getFileAttributeValueByPlaceholder() - .get(placeholder); + return placeholderModel.getFileAttributeValueByPlaceholder().get(placeholder); } if (placeholderModel.getDossierAttributesValueByPlaceholder().containsKey(placeholder)) { - return placeholderModel.getDossierAttributesValueByPlaceholder() - .get(placeholder); + return placeholderModel.getDossierAttributesValueByPlaceholder().get(placeholder); } if (placeholderModel.getRssComponentPlaceholder() != null && placeholderModel.getRssComponentPlaceholder().containsKey(placeholder)) { - return placeholderModel.getRssComponentPlaceholder() - .get(placeholder); + return placeholderModel.getRssComponentPlaceholder().get(placeholder); } return null; @@ -516,12 +507,10 @@ public class ExcelReportGenerationService { private Function getFunctionForPlaceHolder(String placeholder) { if (placeholder.startsWith(FILE_ATTRIBUTE_PLACEHOLDER_BASE)) { - return placeholderInput -> placeholderInput.getPlaceholderModel().getFileAttributeValueByPlaceholder() - .get(placeholder); + return placeholderInput -> placeholderInput.getPlaceholderModel().getFileAttributeValueByPlaceholder().get(placeholder); } if (placeholder.startsWith(DOSSIER_ATTRIBUTE_PLACEHOLDER_BASE)) { - return placeholderInput -> placeholderInput.getPlaceholderModel().getDossierAttributesValueByPlaceholder() - .get(placeholder); + return placeholderInput -> placeholderInput.getPlaceholderModel().getDossierAttributesValueByPlaceholder().get(placeholder); } return switch (placeholder) { 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 2d376a6..f4bded2 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 @@ -30,7 +30,6 @@ import lombok.experimental.FieldDefaults; @SuppressWarnings("checkstyle:all") public class ScmReportService { - public static final int MAX_CELL_TEXT_LENGTH = 32767; final ComponentClient componentResource; final FileAttributesConfigClient fileAttributesClient; final CellTextChunkingService cellTextChunkingService; @@ -39,19 +38,7 @@ public class ScmReportService { public void addScmRows(Sheet sheet, FileModel fileModel, ExcelModel excelModel) { - var firstRow = sheet.getRow(0); - - int firstRowIndex = 1; - while (firstRow == null) { - firstRow = sheet.getRow(firstRowIndex); - firstRowIndex++; - } - - // Ensure we have the correct first row - Cell firstCell = firstRow.getCell(0); - if (!formatter.formatCellValue(firstCell).equals("File")) { - firstRow = sheet.getRow(0); - } + Row firstRow = findFirstRow(sheet); int fileColumnIndex = 0; int textColumnIndex = firstRow.getLastCellNum() - 1; @@ -66,8 +53,6 @@ public class ScmReportService { Map componentIndexMap = new HashMap<>(); - Row finalFirstRow = firstRow; - componentLog.getComponentLogEntries() .forEach(componentLogEntry -> { @@ -82,7 +67,7 @@ public class ScmReportService { componentIndexMap, componentLogEntry, fileModel, - finalFirstRow, + firstRow, fileColumnIndex, componentColumnIndex, valueIndexColumn, @@ -104,6 +89,28 @@ public class ScmReportService { } + private Row findFirstRow(Sheet sheet) { + + Row firstRow; + for (int firstRowIndex = 0; firstRowIndex < 100; firstRowIndex++) { + firstRow = sheet.getRow(firstRowIndex); + + if (firstRow == null) { + continue; + } + var firstCell = firstRow.getCell(0); + if (firstCell == null) { + continue; + } + if (!formatter.formatCellValue(firstCell).equals("File")) { + continue; + } + return firstRow; + } + throw new AssertionError("No row with 'File' placeholder in first column found"); + } + + private void addOtherCells(Sheet sheet, ExcelModel excelModel, AtomicInteger rowIndex, @@ -162,8 +169,7 @@ public class ScmReportService { for (FileAttributeModel fileAttributeModel : fileAttributeModels) { Cell fileAttributeCell = row.createCell(fileAttributeModel.getCell().getColumnIndex()); - String cellValue = fileModel.getFileAttributes() - .get(fileAttributeModel.getId()); + String cellValue = fileModel.getFileAttributes().get(fileAttributeModel.getId()); fileAttributeCell.setCellValue(cellValue == null ? "" : cellValue); CellUtil.setAlignment(fileAttributeCell, HorizontalAlignment.CENTER); CellUtil.setVerticalAlignment(fileAttributeCell, VerticalAlignment.TOP);