Skip to content

Commit

Permalink
updated XLSX support
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Oct 13, 2024
1 parent 0f4e62b commit 15e3e39
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ private void generateExcel() {
if (outputPath != null) {
Files.createDirectories(outputPath.getParent());

if (Files.isRegularFile(outputPath)) {
// TODO option for split files by module

if (Files.exists(outputPath) && !Files.isRegularFile(outputPath)) {
err.println("For Excel (xlsx) generation the output path must be a file");
}

Expand All @@ -303,7 +305,7 @@ private void generateExcel() {
} else {
err.println("For Excel (xlsx) generation the output file must be provided");
}
} catch (IOException exception) {
} catch (Exception exception) {
err.println(exception.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.brapi.schematools.core.brapischema.BrAPISchemaReader;
import org.brapi.schematools.core.model.BrAPIClass;
import org.brapi.schematools.core.model.BrAPIObjectProperty;
import org.brapi.schematools.core.model.BrAPIObjectType;
import org.brapi.schematools.core.ontmodel.options.OntModelGeneratorOptions;
import org.brapi.schematools.core.response.Response;
import org.brapi.schematools.core.utils.StringUtils;
Expand All @@ -20,17 +22,12 @@
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.function.Function.identity;
import static org.brapi.schematools.core.response.Response.fail;
import static org.brapi.schematools.core.response.Response.success;

/**
* Generates Excel (xlsx) file(s) for type and their field descriptions from a BrAPI Json Schema.
Expand Down Expand Up @@ -81,7 +78,8 @@ private class Generator {
private final List<BrAPIClass> brAPISchemas ;

public Generator(List<BrAPIClass> brAPISchemas) {
this.brAPISchemas = brAPISchemas.stream().filter(this::isGenerating).collect(Collectors.toList()) ;
this.brAPISchemas = brAPISchemas.stream().filter(this::isGenerating).
sorted(Comparator.comparing(BrAPIClass::getName)).collect(Collectors.toList()) ;
}

public Response<List<Path>> generate() {
Expand All @@ -104,16 +102,31 @@ private Response<List<Path>> generateDataClasses(List<BrAPIClass> brAPIClasses)

Sheet sheet = workbook.createSheet("Data Classes");

List<PropertyDescriptor> propertyDescriptors = propertyDescriptors(BrAPIClass.class, options.getDataClassProperties());
createHeaderRow(workbook, sheet, options.getDataClassProperties()) ;
createRows(sheet, 1, null, options.getDataClassProperties(), brAPIClasses);

createHeaderRow(workbook, sheet, propertyDescriptors) ;
createRows(sheet, 1, propertyDescriptors, brAPIClasses);
formatSheet(sheet, brAPIClasses.size()) ;

formatSheet(workbook, sheet, brAPIClasses.size()) ;
sheet = workbook.createSheet("Data Classes Fields");

createHeaderRow(workbook, sheet, options.getDataClassFieldProperties()) ;
createRows(sheet, 1, BrAPIClass::getName, options.getDataClassFieldProperties(), brAPIClasses, this::findFields);

formatSheet(sheet, brAPIClasses.size()) ;

return saveWorkbook(workbook, outputPath).mapResult(Collections::singletonList) ;
}

private List<BrAPIObjectProperty> findFields(BrAPIClass brAPIClass) {
List<BrAPIObjectProperty> properties = new ArrayList<>() ;

if (brAPIClass instanceof BrAPIObjectType brAPIObjectType) {
return brAPIObjectType.getProperties();
}

return properties ;
}

private Response<Path> saveWorkbook(Workbook workbook, Path path) {
try {
FileOutputStream outputStream = new FileOutputStream(path.toFile());
Expand All @@ -125,29 +138,19 @@ private Response<Path> saveWorkbook(Workbook workbook, Path path) {
}
}

private List<PropertyDescriptor> propertyDescriptors(Class<?> descriptorClass, List<String> propertyNames) {
Map<String, PropertyDescriptor> propertyDescriptors = Arrays.stream(PropertyUtils.getPropertyDescriptors(descriptorClass)).collect(Collectors.toMap(PropertyDescriptor::getName, identity()));

return propertyNames
.stream()
.map(propertyDescriptors::get)
.filter(Objects::nonNull).
toList();
}

private void formatSheet(Workbook workbook, Sheet sheet, int lastIndex) {
private void formatSheet(Sheet sheet, int lastIndex) {
CellRangeAddress ca =
new CellRangeAddress(0, lastIndex,
sheet.getRow(0).getFirstCellNum(),
sheet.getRow(0).getLastCellNum() - 1);
sheet.setAutoFilter(ca);
}

private void createHeaderRow(Workbook workbook, Sheet sheet, List<PropertyDescriptor> columns) {
createHeaderRow(workbook, sheet, columns, null);
private void createHeaderRow(Workbook workbook, Sheet sheet, List<String> properties) {
createHeaderRow(workbook, sheet, properties, null);
}

private void createHeaderRow(Workbook workbook, Sheet sheet, List<PropertyDescriptor> columns, String header) {
private void createHeaderRow(Workbook workbook, Sheet sheet, List<String> properties, String header) {
Row headerRow = sheet.createRow(0);

CellStyle headerStyle = createHeaderStyle(workbook);
Expand All @@ -162,10 +165,10 @@ private void createHeaderRow(Workbook workbook, Sheet sheet, List<PropertyDescri
++columnIndex ;
}

for (PropertyDescriptor column : columns) {
for (String property : properties) {
sheet.setColumnWidth(columnIndex, 6000);
Cell headerCell = headerRow.createCell(columnIndex);
headerCell.setCellValue(StringUtils.toLabel(column.getName()));
headerCell.setCellValue(StringUtils.toLabel(property));
headerCell.setCellStyle(headerStyle);
++columnIndex ;
}
Expand All @@ -185,27 +188,27 @@ private CellStyle createHeaderStyle(Workbook workbook) {
return headerStyle ;
}

private <T> void createRows(Sheet sheet, int startIndex, List<PropertyDescriptor> propertyDescriptors, List<T> values) {
private <T> int createRows(Sheet sheet, int startIndex, String header, List<String> properties, List<T> values) {
int rowIndex = startIndex ;

for (Object value : values) {
createRow(sheet, propertyDescriptors, rowIndex, null, value);
createRow(sheet, properties, rowIndex, header, value);

++rowIndex ;
}

return rowIndex ;
}

private <T> void createRows(Sheet sheet, int startIndex, List<PropertyDescriptor> columns, String header, List<T> values) {
private <T, V> void createRows(Sheet sheet, int startIndex, Function<T, String> headerFunction, List<String> properties, List<T> values, Function<T, List<V>> valuesFunction) {
int rowIndex = startIndex ;

for (Object value : values) {
createRow(sheet, columns, rowIndex, header, value);

++rowIndex ;
for (T value : values) {
rowIndex = createRows(sheet, rowIndex, headerFunction.apply(value), properties, valuesFunction.apply(value));
}
}

private <T> void createRow(Sheet sheet, List<PropertyDescriptor> propertyDescriptors, int rowIndex, String header, T bean) {
private <T> void createRow(Sheet sheet, List<String> properties, int rowIndex, String header, T bean) {
Row row = sheet.createRow(rowIndex);

int columnIndex = 0 ;
Expand All @@ -216,11 +219,11 @@ private <T> void createRow(Sheet sheet, List<PropertyDescriptor> propertyDescrip
++columnIndex ;
}

for (PropertyDescriptor column : propertyDescriptors) {
for (String property : properties) {
Cell cell = row.createCell(columnIndex);
Object value = null;
try {
value = PropertyUtils.getProperty(bean, column.getName());
value = PropertyUtils.getProperty(bean, property);
if (value instanceof Boolean booleanValue) {
cell.setCellValue(booleanValue);
} else if (value instanceof Integer integerValue) {
Expand All @@ -233,7 +236,7 @@ private <T> void createRow(Sheet sheet, List<PropertyDescriptor> propertyDescrip
cell.setCellValue(value.toString());
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
log.warn(String.format("Error parsing bean with column '%s', at index '%d' due to '%s'", column.getName(), rowIndex, e.getMessage())) ;
log.warn(String.format("Error parsing bean with property '%s', at index '%d' due to '%s'", property, rowIndex, e.getMessage())) ;
}

++columnIndex ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public class XSSFWorkbookGeneratorOptions implements Options {

List<String> dataClassProperties ;
List<String> dataClassFieldProperties ;

/**
* Load the options from an options file in YAML or Json. The options file may have missing
Expand Down
3 changes: 3 additions & 0 deletions java/core/src/main/resources/xlsx-options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dataClassProperties:
- name
- module
- description
dataClassFieldProperties:
- name
- description

0 comments on commit 15e3e39

Please sign in to comment.