From 7047ceebbd9a2a19fbcb3617b4be59266e82055f Mon Sep 17 00:00:00 2001 From: Steffengreiner Date: Mon, 7 Aug 2023 12:25:28 +0200 Subject: [PATCH] Check if the cells contain valid values before validation (#337) * Check if the cells contain valid values before validation * add bracket missing due to merge --------- Co-authored-by: Tobias Koch Co-authored-by: wow-such-code --- .../batch/SampleRegistrationSpreadsheet.java | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/vaadinfrontend/src/main/java/life/qbic/datamanager/views/projects/project/samples/registration/batch/SampleRegistrationSpreadsheet.java b/vaadinfrontend/src/main/java/life/qbic/datamanager/views/projects/project/samples/registration/batch/SampleRegistrationSpreadsheet.java index 4f975d579..da01659b0 100644 --- a/vaadinfrontend/src/main/java/life/qbic/datamanager/views/projects/project/samples/registration/batch/SampleRegistrationSpreadsheet.java +++ b/vaadinfrontend/src/main/java/life/qbic/datamanager/views/projects/project/samples/registration/batch/SampleRegistrationSpreadsheet.java @@ -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 experimentalGroupToConditionString; private Map> conditionsToReplicates; + LinkedHashMap> cellValueOptionsForColumnMap = new LinkedHashMap<>(); private int numberOfSamples; public SampleRegistrationSpreadsheet() { @@ -364,11 +365,10 @@ private void generateLigandomicsSheet() { * specifically defined for the {@link MetadataType} of the genomics facility */ private void generateGenomicsSheet() { - this.header = retrieveGenomics(); - LinkedHashMap> cellValueOptionsMap = cellValueOptionsForCellWithinColumn( - header); - generateColumnsHeaders(cellValueOptionsMap); - dropdownCellFactory.setColumnValues(cellValueOptionsMap); + header = retrieveGenomics(); + generateCellValueOptionsMap(header); + generateColumnsHeaders(cellValueOptionsForColumnMap); + dropdownCellFactory.setColumnValues(cellValueOptionsForColumnMap); } /** @@ -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> cellValueOptionsForCellWithinColumn( + private void generateCellValueOptionsMap( List headerNames) { - LinkedHashMap> cellValueOptionsForColumnMap = new LinkedHashMap<>(); analysisTypes = generateGenomicsAnalysisTypes(); for (SamplesheetHeaderName head : headerNames) { cellValueOptionsForColumnMap.put(head, new ArrayList<>()); @@ -396,7 +395,6 @@ private LinkedHashMap> cellValueOptionsForCe cellValueOptionsForColumnMap.put(SamplesheetHeaderName.SEQ_ANALYSIS_TYPE, analysisTypes); cellValueOptionsForColumnMap.put(SamplesheetHeaderName.BIOLOGICAL_REPLICATE_ID, getReplicateLabels()); - return cellValueOptionsForColumnMap; } /** @@ -477,11 +475,12 @@ public Result 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); } } @@ -499,6 +498,37 @@ public Result 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 mandatorySampleSheetHeaderNames = new ArrayList<>( + header.stream().filter(samplesheetHeaderName -> samplesheetHeaderName.isMandatory) + .toList()); + Optional mandatoryHeaderName = mandatorySampleSheetHeaderNames.stream(). + filter(mandatorySampleSheetHeaderName -> mandatorySampleSheetHeaderName.ordinal() + == cell.getColumnIndex()).findFirst(); + if (mandatoryHeaderName.isPresent()) { + List 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);