From 7fe92b75bcb4486c78c4c44fded452ab6cbee4d8 Mon Sep 17 00:00:00 2001 From: oliveregger Date: Tue, 14 Jan 2025 22:47:26 +0100 Subject: [PATCH] support for range in sheet for instance generation #1881 --- .../dataprovider/ExcelDataProvider.java | 48 +++++++++++++------ .../dataprovider/TableDataProvider.java | 7 ++- .../tests/TestInstanceGenerationTester.java | 6 +-- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/ExcelDataProvider.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/ExcelDataProvider.java index a333c4891f..6f08ede1f5 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/ExcelDataProvider.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/ExcelDataProvider.java @@ -1,17 +1,14 @@ package org.hl7.fhir.r5.testfactory.dataprovider; import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellReference; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; -import org.apache.poi.EncryptedDocumentException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.text.SimpleDateFormat; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.HashMap; @@ -27,17 +24,21 @@ public class ExcelDataProvider extends TableDataProvider { private Map columnIndexMap = new HashMap<>(); private Row currentRow; private DataFormatter df = new DataFormatter(); - + private int startRow = 0; + private int startCol = 0; + private int endRow = -1; + private int endCol = -1; /** * Constructs an ExcelTableDataProvider. * * @param filename The path to the Excel file. * @param sheetName The name of the sheet to read. + * @param sheetName The range of the sheet to read. * @throws IOException If an I/O error occurs. * @throws InvalidFormatException If the file format is invalid. */ - public ExcelDataProvider(String filename, String sheetName) throws IOException, InvalidFormatException { + public ExcelDataProvider(String filename, String sheetName, String range) throws IOException, InvalidFormatException { FileInputStream fis = new FileInputStream(ManagedFileAccess.file(filename)); this.workbook = WorkbookFactory.create(fis); if (sheetName != null) { @@ -55,7 +56,17 @@ public ExcelDataProvider(String filename, String sheetName) throws IOException, throw new IllegalArgumentException("Sheet '" + sheetName + "' does not exist in the file. Sheet Names = "+CommaSeparatedStringBuilder.join(",", names)); } } - + if (range != null ) { + String[] parts = range.split(":"); + CellReference startCell = new CellReference(parts[0]); + startRow = startCell.getRow(); + startCol = startCell.getCol(); + if (parts.length==2) { + CellReference endCell = new CellReference(parts[1]); + endRow = endCell.getRow(); + endCol = endCell.getCol(); + } + } loadColumnHeaders(); } @@ -74,12 +85,14 @@ public ExcelDataProvider(String filename) throws InvalidFormatException, IOExcep private void loadColumnHeaders() { columnHeaders = new ArrayList<>(); columnHeaders.add("counter"); - Row headerRow = sheet.getRow(0); + Row headerRow = sheet.getRow(startRow); if (headerRow != null) { for (Cell cell : headerRow) { - String headerName = cell.getStringCellValue().trim(); - columnHeaders.add(headerName); - columnIndexMap.put(headerName, cell.getColumnIndex()); + if (cell.getColumnIndex()>= startCol && (endCol==-1 || cell.getColumnIndex()<= endCol )) { + String headerName = cell.getStringCellValue().trim(); + columnHeaders.add(headerName); + columnIndexMap.put(headerName, cell.getColumnIndex()); + } } } } @@ -92,7 +105,10 @@ public List columns() { @Override public boolean nextRow() { currentRowIndex++; - currentRow = sheet.getRow(currentRowIndex + 1); // Skip the header row + currentRow = sheet.getRow(startRow + currentRowIndex + 1); // Skip the header row + if (currentRow != null && endRow!=-1 && endRow == startRow + currentRowIndex) { + currentRow = null; + } return currentRow != null; } @@ -103,10 +119,12 @@ public List cells() { if (currentRow != null) { for (Cell cell : currentRow) { int i = cell.getColumnIndex(); - while (cellValues.size() <= i) { - cellValues.add(""); + if (i>= startCol && (endCol==-1 || i<= endCol )) { + while (cellValues.size() <= i-startCol) { + cellValues.add(""); + } + cellValues.add(getCellValue(cell).trim()); } - cellValues.add(getCellValue(cell).trim()); } } return cellValues; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/TableDataProvider.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/TableDataProvider.java index 3147adcc51..c27a4687f3 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/TableDataProvider.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/testfactory/dataprovider/TableDataProvider.java @@ -19,7 +19,12 @@ public static TableDataProvider forFile(String path) throws FHIRException { try { String filename = path; String sheetname = null; + String range = null; + if (path.contains("!")) { + range = path.substring(path.indexOf("!")+1); + path = path.substring(0, path.indexOf("!")); + } if (path.contains(";")) { filename = path.substring(0, path.indexOf(";")); sheetname = path.substring(path.indexOf(";")+1); @@ -28,7 +33,7 @@ public static TableDataProvider forFile(String path) throws FHIRException { if (Utilities.existsInList(extension, "csv", "txt")) { return new CSVDataProvider(filename); } else if (Utilities.existsInList(extension, "xlsx")) { - return new ExcelDataProvider(filename, sheetname); + return new ExcelDataProvider(filename, sheetname, range); } else if (Utilities.existsInList(extension, "db")) { return new SQLDataProvider(DriverManager.getConnection("jdbc:sqlite:"+filename), sheetname); } else { diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/generation/tests/TestInstanceGenerationTester.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/generation/tests/TestInstanceGenerationTester.java index 9c03bd915d..3d0973a12b 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/generation/tests/TestInstanceGenerationTester.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/generation/tests/TestInstanceGenerationTester.java @@ -76,7 +76,7 @@ public void testDataFactory() throws IOException, FHIRException, SQLException { byte[] fsrc = TestingUtilities.loadTestResourceBytes("rX", "instance-generation", "factories", name); TextFile.bytesToFile(fsrc, Utilities.path(path, name)); } - for (String name : Utilities.strings("Patient-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) { + for (String name : Utilities.strings("Patient-1.json","Patient2-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) { byte[] fsrc = TestingUtilities.loadTestResourceBytes("rX", "instance-generation", "expected", name); TextFile.bytesToFile(fsrc, Utilities.path(path, "expected", name)); } @@ -93,12 +93,12 @@ public void testDataFactory() throws IOException, FHIRException, SQLException { for (String name : Utilities.strings("Bundle-patients.json", "Encounter-1.json", "Encounter-2.json", "Encounter-3.json", "Encounter-4.json", "MedicationStatement-1.json", "MedicationStatement-2.json", "MedicationStatement-4.json", "Observation-bp-1.json", "Observation-bp-2.json", "Observation-bp-3.json", "Observation-bp-4.json", "Observation-weight-1.json", "Observation-weight-2.json", "Observation-weight-3.json", "Observation-weight-4.json", - "Patient-1.json", "Patient-2.json", "Patient-3.json", "Patient-4.json")) { + "Patient-1.json", "Patient-2.json", "Patient-3.json", "Patient-4.json", "Patient2-1.json", "Patient2-2.json", "Patient2-3.json")) { File f = new File(Utilities.path(output, name)); Assertions.assertTrue(f.exists()); } - for (String name : Utilities.strings("Patient-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) { + for (String name : Utilities.strings("Patient-1.json", "Patient2-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) { String diff = new CompareUtilities(null, null).checkJsonSrcIsSame(name, TextFile.fileToString(Utilities.path(expected, name)), TextFile.fileToString(Utilities.path(output, name)), false); Assertions.assertNull(diff, "unexpected difference for "+name); }