Skip to content

Commit

Permalink
Generate Property Information sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
KochTobi committed Oct 25, 2024
1 parent a09ed29 commit 70b4acd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class XLSXTemplateHelper {
private static final byte[] LIGHT_GREY = {(byte) 220, (byte) 220, (byte) 220};
private static final byte[] LINK_BLUE = {(byte) 9, (byte) 105, (byte) 218};
private static final int COLUMN_MAX_WIDTH = 255;
private static final String PROPERTY_INFORMATION_SHEET_NAME = "Property Information";

protected XLSXTemplateHelper() {
//hide constructor as static methods only are used
Expand Down Expand Up @@ -310,6 +311,78 @@ public static void addDataValidation(Sheet sheet, int startColIdx, int startRowI
sheet.addValidationData(validation);
}

/**
* Adds a property information description to the workbook. Creates a sheet called
* {@link #PROPERTY_INFORMATION_SHEET_NAME} if it does not exist yet.
*
* @param workbook the workbook to take
* @param columnName the column name / property name to add information to
* @param isMandatory is filling the column mandatory?
* @param descriptionTitle allowed value type and example
* @param description the description of the input
* @param headerStyle the style used for headers in the property information sheet
*/
public static void addPropertyInformation(Workbook workbook,
String columnName,
boolean isMandatory,
String descriptionTitle,
String description,
CellStyle headerStyle) {
// add row with information
Sheet propertyInformationSheet = Optional
.ofNullable(workbook.getSheet(PROPERTY_INFORMATION_SHEET_NAME))
.orElseGet(() -> workbook.createSheet(PROPERTY_INFORMATION_SHEET_NAME));
int lastRowIdx = Math.max(propertyInformationSheet.getLastRowNum(), 0);
if (lastRowIdx == 0) {
//we do not have a header yet
Row row = getOrCreateRow(propertyInformationSheet, 0);
Cell propertyNameCell = getOrCreateCell(row, 0);
propertyNameCell.setCellStyle(headerStyle);
propertyNameCell.setCellValue("Property Name");

Cell provisionCell = getOrCreateCell(row, 1);
provisionCell.setCellStyle(headerStyle);
provisionCell.setCellValue("Provision");

Cell allowedValuesCell = getOrCreateCell(row, 2);
allowedValuesCell.setCellStyle(headerStyle);
allowedValuesCell.setCellValue("Allowed Values");

Cell descriptionCell = getOrCreateCell(row, 3);
descriptionCell.setCellStyle(headerStyle);
descriptionCell.setCellValue("Description");

}
lastRowIdx++;
Row row = getOrCreateRow(propertyInformationSheet, lastRowIdx);
Cell propertyNameCell = getOrCreateCell(row, 0);
propertyNameCell.setCellValue(columnName);

Cell provisionCell = getOrCreateCell(row, 1);
provisionCell.setCellValue(isMandatory ? "mandatory" : "optional");

Cell allowedValuesCell = getOrCreateCell(row, 2);
allowedValuesCell.setCellValue(descriptionTitle);

Cell descriptionCell = getOrCreateCell(row, 3);
descriptionCell.setCellValue(description);

setColumnAutoWidth(propertyInformationSheet, 0, 3);
}


/**
* Adds an input prompt box to cells within the selected range. If there is already a validation
* for exactly those cells, the prompt box of the existing validation is overwritten.
*
* @param sheet the sheet in which the cells are
* @param startColIdx the index of the first column
* @param startRowIdx the index of the first row
* @param stopColIdx the index of the last column
* @param stopRowIdx the index of the last row
* @param title the title of the message in the prompt box
* @param content the content of the prompt box
*/
public static void addInputHelper(Sheet sheet, int startColIdx, int startRowIdx,
int stopColIdx, int stopRowIdx, String title, String content) {
CellRangeAddressList validatedCells = new CellRangeAddressList(startRowIdx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ public byte[] getContent() {
column.columnIndex(),
0,
helper.exampleValue(),
helper.description())
);
helper.description()));
column.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addPropertyInformation(workbook,
column.headerName(),
column.isMandatory(),
helper.exampleValue(),
helper.description(),
boldStyle));
}

var startIndex = 1; // start in row number 2 with index 1 as the header row has number 1 index 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ public byte[] getContent() {
column.columnIndex(),
DEFAULT_GENERATED_ROW_COUNT - 1,
helper.exampleValue(),
helper.description())
);
helper.description()));
column.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addPropertyInformation(workbook,
column.headerName(),
column.isMandatory(),
helper.exampleValue(),
helper.description(),
boldStyle));
}

setAutoWidth(sheet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ public byte[] getContent() {
measurementColumn.columnIndex(),
0,
helper.exampleValue(),
helper.description())
);
helper.description()));
measurementColumn.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addPropertyInformation(workbook,
measurementColumn.headerName(),
measurementColumn.isMandatory(),
helper.exampleValue(),
helper.description(),
boldStyle));
}

var startIndex = 1; // start in row number 2 with index 1 skipping the header in the first row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ public byte[] getContent() {
measurementColumn.columnIndex(),
0,
helper.exampleValue(),
helper.description())
);
helper.description()));
measurementColumn.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addPropertyInformation(workbook,
measurementColumn.headerName(),
measurementColumn.isMandatory(),
helper.exampleValue(),
helper.description(),
boldStyle));
}

var startIndex = 1; // start in row number 2 with index 1 skipping the header in the first row
Expand Down

0 comments on commit 70b4acd

Please sign in to comment.