Skip to content

Commit

Permalink
Check if the cells contain valid values before validation (#337)
Browse files Browse the repository at this point in the history
* Check if the cells contain valid values before validation

* add bracket missing due to merge

---------

Co-authored-by: Tobias Koch <[email protected]>
Co-authored-by: wow-such-code <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2023
1 parent 021081f commit 7047cee
Showing 1 changed file with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class SampleRegistrationSpreadsheet extends Spreadsheet implements Serial
//Spreadsheet component only allows retrieval of strings, so we have to store the experimentalGroupId separately
private Map<String, ExperimentalGroup> experimentalGroupToConditionString;
private Map<String, List<BiologicalReplicate>> conditionsToReplicates;
LinkedHashMap<SamplesheetHeaderName, List<String>> cellValueOptionsForColumnMap = new LinkedHashMap<>();
private int numberOfSamples;

public SampleRegistrationSpreadsheet() {
Expand Down Expand Up @@ -364,11 +365,10 @@ private void generateLigandomicsSheet() {
* specifically defined for the {@link MetadataType} of the genomics facility
*/
private void generateGenomicsSheet() {
this.header = retrieveGenomics();
LinkedHashMap<SamplesheetHeaderName, List<String>> cellValueOptionsMap = cellValueOptionsForCellWithinColumn(
header);
generateColumnsHeaders(cellValueOptionsMap);
dropdownCellFactory.setColumnValues(cellValueOptionsMap);
header = retrieveGenomics();
generateCellValueOptionsMap(header);
generateColumnsHeaders(cellValueOptionsForColumnMap);
dropdownCellFactory.setColumnValues(cellValueOptionsForColumnMap);
}

/**
Expand All @@ -381,9 +381,8 @@ private void generateGenomicsSheet() {
* @return cellValueOptionsForColumnMap map grouping the selectable cell values within the cells
* of a column with the {@link SamplesheetHeaderName} of the colum
*/
private LinkedHashMap<SamplesheetHeaderName, List<String>> cellValueOptionsForCellWithinColumn(
private void generateCellValueOptionsMap(
List<SamplesheetHeaderName> headerNames) {
LinkedHashMap<SamplesheetHeaderName, List<String>> cellValueOptionsForColumnMap = new LinkedHashMap<>();
analysisTypes = generateGenomicsAnalysisTypes();
for (SamplesheetHeaderName head : headerNames) {
cellValueOptionsForColumnMap.put(head, new ArrayList<>());
Expand All @@ -396,7 +395,6 @@ private LinkedHashMap<SamplesheetHeaderName, List<String>> cellValueOptionsForCe
cellValueOptionsForColumnMap.put(SamplesheetHeaderName.SEQ_ANALYSIS_TYPE, analysisTypes);
cellValueOptionsForColumnMap.put(SamplesheetHeaderName.BIOLOGICAL_REPLICATE_ID,
getReplicateLabels());
return cellValueOptionsForColumnMap;
}

/**
Expand Down Expand Up @@ -477,11 +475,12 @@ public Result<Void, InvalidSpreadsheetInput> areInputsValid() {
// mandatory not filled in --> invalid
for (int colId : mandatoryInputCols) {
Cell cell = row.getCell(colId);
if (SpreadsheetMethods.cellToStringOrNull(cell).isBlank()) {

if (!isCellValueValid(cell)) {
invalidCells.add(cell);
} else {
// if a background color was set, but the cell is valid, we need to change the style
if(cell.getCellStyle().getFillBackgroundColorColor()!=null) {
if (cell.getCellStyle().getFillBackgroundColorColor() != null) {
validCells.add(cell);
}
}
Expand All @@ -499,6 +498,37 @@ public Result<Void, InvalidSpreadsheetInput> areInputsValid() {
return Result.fromValue(null);
}

/**
* Validates if the provided cell is in a column which requires mandatory information based on the
* {@link SamplesheetHeaderName} mandatory attribute. If that is the case, then the validation
* will trigger an error if the cell was left blank or if a value outside the possible selectable
* values was provided
*
* @param cell to be investigated for validity
* @return boolean showing if the provided cell is valid or not
*/
private boolean isCellValueValid(Cell cell) {
String cellValue = cell.getStringCellValue().trim();
//Check if there are columns which require mandatory information
List<SamplesheetHeaderName> mandatorySampleSheetHeaderNames = new ArrayList<>(
header.stream().filter(samplesheetHeaderName -> samplesheetHeaderName.isMandatory)
.toList());
Optional<SamplesheetHeaderName> mandatoryHeaderName = mandatorySampleSheetHeaderNames.stream().
filter(mandatorySampleSheetHeaderName -> mandatorySampleSheetHeaderName.ordinal()
== cell.getColumnIndex()).findFirst();
if (mandatoryHeaderName.isPresent()) {
List<String> cellValueOptions = cellValueOptionsForColumnMap.get(mandatoryHeaderName.get());
//If there are no options for the cell value the user has to provide some sort of information
if (cellValueOptions.isEmpty()) {
return !cellValue.isBlank();
} else {
//Otherwise the user should have selected one of the possible cell values
return cellValueOptions.contains(cellValue);
}
}
return true;
}

private CellStyle getDefaultStyle() {
CellStyle defaultStyle = getWorkbook().createCellStyle();
defaultStyle.setLocked(false);
Expand Down

0 comments on commit 7047cee

Please sign in to comment.