Skip to content

Commit

Permalink
support for range in sheet for instance generation #1881
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveregger committed Jan 14, 2025
1 parent cf0d398 commit 7fe92b7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,17 +24,21 @@ public class ExcelDataProvider extends TableDataProvider {
private Map<String, Integer> 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) {
Expand All @@ -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();
}

Expand All @@ -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());
}
}
}
}
Expand All @@ -92,7 +105,10 @@ public List<String> 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;
}

Expand All @@ -103,10 +119,12 @@ public List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 7fe92b7

Please sign in to comment.