Skip to content

Commit

Permalink
adjust sample sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
KochTobi committed Oct 25, 2024
1 parent adcee66 commit 6234b9d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package life.qbic.datamanager.templates.sample;

import static life.qbic.datamanager.templates.XLSXTemplateHelper.*;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.addDataValidation;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createBoldCellStyle;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createDefaultCellStyle;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createOptionArea;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createReadOnlyHeaderCellStyle;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.getOrCreateCell;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.getOrCreateRow;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.hideSheet;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.lockSheet;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.setColumnAutoWidth;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.setColumnWidth;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import life.qbic.datamanager.parser.ExampleProvider.Helper;
import life.qbic.datamanager.parser.sample.RegisterColumn;
import life.qbic.datamanager.templates.XLSXTemplateHelper;
import org.apache.poi.ss.usermodel.Name;
Expand Down Expand Up @@ -66,6 +73,7 @@ public static XSSFWorkbook createRegistrationTemplate(List<String> conditions,
XSSFWorkbook workbook = new XSSFWorkbook();
var readOnlyHeaderStyle = createReadOnlyHeaderCellStyle(workbook);
var boldCellStyle = createBoldCellStyle(workbook);
var defaultStyle = createDefaultCellStyle(workbook);

var sheet = workbook.createSheet("Sample Metadata");

Expand All @@ -82,7 +90,34 @@ public static XSSFWorkbook createRegistrationTemplate(List<String> conditions,
if (column.isReadOnly()) {
cell.setCellStyle(readOnlyHeaderStyle);
}

//add helper to header
column.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addInputHelper(sheet,
column.columnIndex(),
0,
column.columnIndex(),
0,
helper.exampleValue(),
helper.description()));
}

// add property information order of columns matters!!
for (RegisterColumn column : Arrays.stream(
RegisterColumn.values())
.sorted(Comparator.comparing(RegisterColumn::columnIndex)).toList()) {
// add property information
var exampleValue = column.getFillHelp().map(Helper::exampleValue).orElse("");
var description = column.getFillHelp().map(Helper::description).orElse("");
XLSXTemplateHelper.addPropertyInformation(workbook,
column.headerName(),
column.isMandatory(),
exampleValue,
description,
defaultStyle,
boldCellStyle);
}

var startIndex = 1; //start in the second row with index 1.

var hiddenSheet = workbook.createSheet("hidden");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package life.qbic.datamanager.templates.sample;

import static life.qbic.datamanager.templates.XLSXTemplateHelper.addDataValidation;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createDefaultCellStyle;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createOptionArea;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.createReadOnlyCellStyle;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.getOrCreateCell;
Expand All @@ -9,7 +10,10 @@
import static life.qbic.datamanager.templates.XLSXTemplateHelper.lockSheet;
import static life.qbic.datamanager.templates.XLSXTemplateHelper.setColumnAutoWidth;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import life.qbic.datamanager.parser.ExampleProvider.Helper;
import life.qbic.datamanager.parser.sample.EditColumn;
import life.qbic.datamanager.templates.XLSXTemplateHelper;
import life.qbic.projectmanagement.application.sample.PropertyConversion;
Expand Down Expand Up @@ -57,6 +61,7 @@ public static XSSFWorkbook createUpdateTemplate(List<Sample> samples, List<Strin
var readOnlyCellStyle = createReadOnlyCellStyle(workbook);
var readOnlyHeaderStyle = XLSXTemplateHelper.createReadOnlyHeaderCellStyle(workbook);
var boldCellStyle = XLSXTemplateHelper.createBoldCellStyle(workbook);
var defaultStyle = createDefaultCellStyle(workbook);

var sheet = workbook.createSheet("Sample Metadata");

Expand All @@ -72,17 +77,45 @@ public static XSSFWorkbook createUpdateTemplate(List<Sample> samples, List<Strin
if (column.isReadOnly()) {
cell.setCellStyle(readOnlyHeaderStyle);
}
//add helper to header
column.getFillHelp().ifPresent(
helper -> XLSXTemplateHelper.addInputHelper(sheet,
column.columnIndex(),
0,
column.columnIndex(),
0,
helper.exampleValue(),
helper.description()));
}

// add property information order of columns matters!!
for (EditColumn column : Arrays.stream(
EditColumn.values())
.sorted(Comparator.comparing(EditColumn::columnIndex)).toList()) {
// add property information
var exampleValue = column.getFillHelp().map(Helper::exampleValue).orElse("");
var description = column.getFillHelp().map(Helper::description).orElse("");
XLSXTemplateHelper.addPropertyInformation(workbook,
column.headerName(),
column.isMandatory(),
exampleValue,
description,
defaultStyle,
boldCellStyle);
}

var startIndex = 1; //start in the second row with index 1.
int rowIndex = startIndex;
for (Sample sample : samples) {
Row row = getOrCreateRow(sheet, rowIndex);
var experimentalGroup = experimentalGroups.stream()
.filter(group -> group.id() == sample.experimentalGroupId()).findFirst().orElseThrow();
fillRowWithSampleMetadata(row, sample, experimentalGroup.condition(), readOnlyCellStyle);
fillRowWithSampleMetadata(row, sample, experimentalGroup.condition(), defaultStyle,
readOnlyCellStyle);
rowIndex++;
}


var hiddenSheet = workbook.createSheet("hidden");
Name analysisToBePerformedOptions = createOptionArea(hiddenSheet, "Analysis to be performed",
analysisToPerform);
Expand Down Expand Up @@ -131,7 +164,7 @@ public static XSSFWorkbook createUpdateTemplate(List<Sample> samples, List<Strin
}

private static void fillRowWithSampleMetadata(Row row, Sample sample,
Condition condition, CellStyle readOnlyCellStyle) {
Condition condition, CellStyle defaultStyle, CellStyle readOnlyCellStyle) {
for (EditColumn column : EditColumn.values()) {
var value = switch (column) {
case SAMPLE_ID -> sample.sampleCode().code();
Expand All @@ -146,6 +179,7 @@ private static void fillRowWithSampleMetadata(Row row, Sample sample,
};
var cell = getOrCreateCell(row, column.columnIndex());
cell.setCellValue(value);
cell.setCellStyle(defaultStyle);
if (column.isReadOnly()) {
cell.setCellStyle(readOnlyCellStyle);
}
Expand Down

0 comments on commit 6234b9d

Please sign in to comment.