Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api/src/org/labkey/api/data/ArrayExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class ArrayExcelWriter extends ExcelWriter
* @param data The data rows, in which index position of a value corresponds to the desired respective column index
* @param cols The columns, in which ordering determines the left-to-right column ordering in the generated Excel
*/
public ArrayExcelWriter(List<Object[]> data, ColumnDescriptor[] cols)
public ArrayExcelWriter(List<Object[]> data, List<ColumnDescriptor> cols)
{
super(ExcelDocumentType.xlsx);
this.data = data;
List<DisplayColumn> xlcols = new ArrayList<>();
List<DisplayColumn> displayColumns = new ArrayList<>();

for (int i = 0; i < cols.length; i++)
for (int i = 0; i < cols.size(); i++)
{
ColumnDescriptor col = cols[i];
xlcols.add(new ArrayDisplayColumn(col.name, col.clazz, i));
ColumnDescriptor col = cols.get(i);
displayColumns.add(new ArrayDisplayColumn(col.name, col.clazz, i));
}

setDisplayColumns(xlcols);
setDisplayColumns(displayColumns);
}

@Override
Expand Down
11 changes: 5 additions & 6 deletions api/src/org/labkey/api/data/TSVArrayWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

// This class supports generating files with duplicate column names. Consider using TSVMapWriter if
// multiple identical column names is not an implementation concern.
Expand All @@ -15,17 +14,17 @@ public class TSVArrayWriter extends TSVWriter
private final List<List<String>> _rows;
private final String _fileName;

public TSVArrayWriter(String fileName, ColumnDescriptor[] columns, List<Object[]> rows)
public TSVArrayWriter(String fileName, List<ColumnDescriptor> columns, List<Object[]> rows)
{
_fileName = fileName;
_columns = Arrays.stream(columns)
_columns = columns.stream()
.map(ColumnDescriptor::getColumnName)
.collect(Collectors.toList());
.toList();
_rows = rows.stream()
.map(array -> Arrays.stream(array)
.map(obj -> (obj == null) ? "" : String.valueOf(obj))
.collect(Collectors.toList()))
.collect(Collectors.toList());
.toList())
.toList();
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions api/src/org/labkey/api/data/TSVColumnWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
* Extracted DisplayColumn handling out of TSVGridWriter so rendering DisplayColumns
* may be used without a ResultSet. You will still need to set up a RenderContext
* for DisplayColumn to render values.
*
* User: kevink
* Date: 9/9/11
*/
public abstract class TSVColumnWriter extends TSVWriter
{
Expand Down Expand Up @@ -72,11 +69,11 @@ protected Iterable<String> getColumnHeaders(RenderContext ctx, Iterable<DisplayC
String header = _columnHeaderType.getText(dc);
if (renameColumn.containsKey(colName))
header = renameColumn.get(colName);
else if (dc instanceof DataColumn)
else if (dc instanceof DataColumn dataColumn)
{
if (((DataColumn) dc).getBoundColumn() != null)
if (dataColumn.getBoundColumn() != null)
{
String fieldKey = ((DataColumn) dc).getBoundColumn().getFieldKey().toString();
String fieldKey = dataColumn.getBoundColumn().getFieldKey().toString();
if (renameColumn.containsKey(fieldKey))
header = renameColumn.get(fieldKey);

Expand All @@ -89,7 +86,6 @@ else if (dc instanceof DataColumn)
return headers;
}


/** Get the unquoted column values. */
protected Iterable<String> getValues(RenderContext ctx, Iterable<DisplayColumn> displayColumns)
{
Expand Down
4 changes: 0 additions & 4 deletions api/src/org/labkey/api/data/TSVMapWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
import java.util.List;
import java.util.Map;

/**
* User: kevink
* Date: 9/11/11
*/
public class TSVMapWriter extends TSVWriter
{
private final Collection<String> _columns;
Expand Down
8 changes: 4 additions & 4 deletions api/src/org/labkey/api/data/TSVWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public abstract class TSVWriter extends TextWriter
protected char _chQuote = '"';
protected String _rowSeparator = "\n";
public static final String BACKSLASH_CHAR_STRING = "\\";
private static final char COMMENT_CHAR = '#';

protected List<String> _fileHeader = null;
protected boolean _headerRowVisible = true;
Expand Down Expand Up @@ -96,7 +97,6 @@ public TSVWriter()
{
}


public String getFilenamePrefix()
{
return _filenamePrefix;
Expand Down Expand Up @@ -210,14 +210,15 @@ protected boolean shouldQuote(String value)
_escapedCharsString = "\r\n" + _rowSeparator + _chDelimiter + _chQuote;
}


int len = value.length();
if (len == 0)
return _preserveEmptyString;
char firstCh = value.charAt(0);
char lastCh = value.charAt(len-1);
if (Character.isSpaceChar(firstCh) || Character.isSpaceChar(lastCh))
return true;
if (firstCh == COMMENT_CHAR) // Issue 50719, Issue 53302
return true;
if (StringUtils.containsAny(value, _additionalQuotedChars))
return true;
return StringUtils.containsAny(value,_escapedCharsString);
Expand All @@ -236,7 +237,7 @@ protected String getContentType()
return delim.contentType;
}

return "text/tab-separated-values";
return DELIM.TAB.contentType;
}

/**
Expand Down Expand Up @@ -279,7 +280,6 @@ public boolean isHeaderRowVisible()
return _headerRowVisible;
}


public void setHeaderRowVisible(boolean headerRowVisible)
{
_headerRowVisible = headerRowVisible;
Expand Down
19 changes: 12 additions & 7 deletions api/src/org/labkey/api/reader/TabLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
* Parses rows of tab-delimited text, returning a CloseableIterator of Map<String, Object>. The iterator must be closed
* (typically via try-with-resources or a finally block) to close the underlying input source. The iterator can be wrapped
Expand Down Expand Up @@ -91,7 +90,10 @@ public DataLoader createLoader(InputStream is, boolean hasColumnHeaders, Contain
}

@NotNull @Override
public FileType getFileType() { return TSV_FILE_TYPE; }
public FileType getFileType()
{
return TSV_FILE_TYPE;
}
}

public static class CsvFactory extends AbstractDataLoaderFactory
Expand All @@ -114,7 +116,10 @@ public TabLoader createLoader(InputStream is, boolean hasColumnHeaders, Containe
}

@NotNull @Override
public FileType getFileType() { return CSV_FILE_TYPE; }
public FileType getFileType()
{
return CSV_FILE_TYPE;
}
}

public static class CsvFactoryNoConversions extends CsvFactory
Expand All @@ -141,9 +146,6 @@ public TabLoader createLoader(InputStream is, boolean hasColumnHeaders, Containe
TabLoader loader = super.createLoader(is, hasColumnHeaders, mvIndicatorContainer);
return configParsing(loader);
}

@NotNull @Override
public FileType getFileType() { return CSV_FILE_TYPE; }
}

public static class MysqlFactory extends AbstractDataLoaderFactory
Expand Down Expand Up @@ -172,7 +174,10 @@ public DataLoader createLoader(InputStream is, boolean hasColumnHeaders, Contain
}

@NotNull @Override
public FileType getFileType() { return CSV_FILE_TYPE; }
public FileType getFileType()
{
return CSV_FILE_TYPE;
}
}

protected static char COMMENT_CHAR = '#';
Expand Down
17 changes: 7 additions & 10 deletions assay/src/org/labkey/assay/PlateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1309,18 +1309,17 @@ public Object execute(WorklistForm form, BindException errors) throws Exception
List<FieldKey> sourceIncludedMetadataCols = PlateManager.get().getMetadataColumns(plateSetSource, getContainer(), getUser(), cf);
List<FieldKey> destinationIncludedMetadataCols = PlateManager.get().getMetadataColumns(plateSetDestination, getContainer(), getUser(), cf);

ColumnDescriptor[] sourceXlCols = PlateSetExport.getColumnDescriptors(PlateSetExport.SOURCE, sourceIncludedMetadataCols);
ColumnDescriptor[] destinationXlCols = PlateSetExport.getColumnDescriptors(PlateSetExport.DESTINATION, destinationIncludedMetadataCols);
ColumnDescriptor[] xlCols = ArrayUtils.addAll(sourceXlCols, destinationXlCols);
List<ColumnDescriptor> sourceColumns = PlateSetExport.getColumnDescriptors(PlateSetExport.SOURCE, sourceIncludedMetadataCols);
List<ColumnDescriptor> destinationColumns = PlateSetExport.getColumnDescriptors(PlateSetExport.DESTINATION, destinationIncludedMetadataCols);
List<ColumnDescriptor> exportColumns = new ArrayList<>(sourceColumns);
exportColumns.addAll(destinationColumns);

List<Object[]> plateDataRows = PlateManager.get().getWorklist(form.getSourcePlateSetId(), form.getDestinationPlateSetId(), sourceIncludedMetadataCols, destinationIncludedMetadataCols, getContainer(), getUser());

String fullFileName = plateSetSource.getName() + " - " + plateSetDestination.getName();

PlateManager.get().getPlateSetExportFile(fullFileName, xlCols, plateDataRows, form.getFileType(), getViewContext().getResponse());
PlateManager.get().getPlateSetExportFile(fullFileName, exportColumns, plateDataRows, form.getFileType(), getViewContext().getResponse());
SimpleMetricsService.get().increment(AssayModule.NAME, "plateSet", "exportWorklist");

return null; // Returning anything here will cause error as excel writer will close the response stream
}
catch (Exception e)
{
Expand Down Expand Up @@ -1387,12 +1386,10 @@ public Object execute(InstrumentInstructionForm form, BindException errors) thro
cf = form.getContainerFilter().create(getViewContext());

List<FieldKey> includedMetadataCols = PlateManager.get().getMetadataColumns(plateSet, getContainer(), getUser(), cf);
ColumnDescriptor[] xlCols = PlateSetExport.getColumnDescriptors("", includedMetadataCols);
List<ColumnDescriptor> exportColumns = PlateSetExport.getColumnDescriptors("", includedMetadataCols);
List<Object[]> plateDataRows = PlateManager.get().getInstrumentInstructions(form.getPlateSetId(), includedMetadataCols, getContainer(), getUser());

PlateManager.get().getPlateSetExportFile(plateSet.getName() + "-instructions", xlCols, plateDataRows, form.getFileType(), getViewContext().getResponse());

return null; // Returning anything here will cause error as excel writer will close the response stream
PlateManager.get().getPlateSetExportFile(plateSet.getName() + "-instructions", exportColumns, plateDataRows, form.getFileType(), getViewContext().getResponse());
}
catch (Exception e)
{
Expand Down
1 change: 0 additions & 1 deletion assay/src/org/labkey/assay/actions/ImportRunApiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ else if (rawData != null && !rawData.isEmpty())

try (TSVMapWriter tsvWriter = saveMatchingColumnDataOnly ? new TSVMapWriter(columns, rawData) : new TSVMapWriter(columns, rawData, true))
{
tsvWriter.setAdditionalQuotedChars("#"); //Issue 50719: If the first column name starts with a #, the data loader will treat the header row as a comment
tsvWriter.write(fileObject.toNioPathForWrite().toFile());
factory.setRawData(null);
factory.setUploadedData(Collections.singletonMap(PRIMARY_FILE, fileObject));
Expand Down
8 changes: 4 additions & 4 deletions assay/src/org/labkey/assay/plate/PlateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3654,10 +3654,10 @@ else if (isSampleOrReplicate)
return counter;
}

public void getPlateSetExportFile(String fileName, ColumnDescriptor[] cols, List<Object[]> rows, PlateController.FileType fileType, HttpServletResponse response) throws IOException
public void getPlateSetExportFile(String fileName, List<ColumnDescriptor> cols, List<Object[]> rows, PlateController.FileType fileType, HttpServletResponse response) throws IOException
{
boolean isCSV = fileType.equals(PlateController.FileType.CSV);
boolean isTSV = fileType.equals(PlateController.FileType.TSV);
boolean isCSV = PlateController.FileType.CSV.equals(fileType);
boolean isTSV = PlateController.FileType.TSV.equals(fileType);
if (isCSV || isTSV)
{
try (TSVArrayWriter writer = new TSVArrayWriter(fileName, cols, rows))
Expand Down Expand Up @@ -3754,7 +3754,7 @@ private List<DisplayColumn> getPlateDisplayColumns(QueryView queryView)
// Filter on isQueryColumn, so we don't get the details or update columns
return dataRegion.getDisplayColumns().stream()
.filter(DisplayColumn::isQueryColumn)
.filter(col -> !col.getName().equals("sampleID"))
.filter(col -> !col.getName().equalsIgnoreCase(WellTable.Column.SampleID.name()))
.toList();
}

Expand Down
15 changes: 8 additions & 7 deletions assay/src/org/labkey/assay/plate/PlateSetExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private Object[] getDataRow(String prefix, Results rs, List<FieldKey> includedMe
)
);

if (!prefix.equals(PlateSetExport.DESTINATION))
if (!PlateSetExport.DESTINATION.equals(prefix))
baseColumns.add(rs.getString(FKMap.get(SAMPLE_ID_COL)));

for (FieldKey col : includedMetaDataCols)
Expand All @@ -77,9 +77,9 @@ private Object[] getDataRow(String prefix, Results rs, List<FieldKey> includedMe
}

// Returns array of ColumnDescriptors used as column layout once fed to an ArrayExcelWriter
public static ColumnDescriptor[] getColumnDescriptors(String prefix, List<FieldKey> includedMetadataCols)
public static List<ColumnDescriptor> getColumnDescriptors(String prefix, List<FieldKey> includedMetadataCols)
{
List<ColumnDescriptor> baseColumns = new ArrayList<>(
List<ColumnDescriptor> columnDescriptors = new ArrayList<>(
Arrays.asList(
new ColumnDescriptor(prefix + "Plate ID"),
new ColumnDescriptor(prefix + "Barcode"),
Expand All @@ -89,21 +89,22 @@ public static ColumnDescriptor[] getColumnDescriptors(String prefix, List<FieldK
);

if (!PlateSetExport.DESTINATION.equals(prefix))
baseColumns.add(new ColumnDescriptor("Sample ID"));
columnDescriptors.add(new ColumnDescriptor("Sample ID"));

List<ColumnDescriptor> metadataColumns = includedMetadataCols
.stream()
.map(fk -> new ColumnDescriptor(fk.getParts().size() > 1 ? fk.getParent().getCaption() : fk.getCaption()))
.toList();

baseColumns.addAll(metadataColumns);
return baseColumns.toArray(new ColumnDescriptor[0]);
columnDescriptors.addAll(metadataColumns);
return columnDescriptors;
}

// Create sampleIdToRow of the following form:
// {<sample id>: [{dataRow1}, {dataRow2}, ... ], ... }
// Where the data rows contain the key's sample
private Map<String, List<Object[]>> getSampleIdToRows(TableInfo wellTable, List<FieldKey> includedMetadataCols, int plateSetId, String plateSetExport) {
private Map<String, List<Object[]>> getSampleIdToRows(TableInfo wellTable, List<FieldKey> includedMetadataCols, int plateSetId, String plateSetExport)
{
Map<String, List<Object[]>> sampleIdToRow = new LinkedHashMap<>();
try (Results rs = QueryService.get().select(wellTable, getWellColumns(wellTable, includedMetadataCols), new SimpleFilter(FKMap.get(PLATE_SET_ID_COL), plateSetId), new Sort(ROW_ID_COL)))
{
Expand Down
16 changes: 10 additions & 6 deletions assay/src/org/labkey/assay/plate/data/PlateMapExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.labkey.api.assay.plate.Plate;
import org.labkey.api.assay.plate.PlateCustomField;
import org.labkey.api.assay.plate.PositionImpl;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.collections.ResultSetRowMapFactory;
import org.labkey.api.collections.RowMap;
import org.labkey.api.data.ColumnInfo;
Expand All @@ -22,7 +23,7 @@
import org.labkey.api.query.QueryView;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.api.view.HttpView;
import org.labkey.assay.plate.query.WellTable;
import org.labkey.assay.plate.query.WellTable.Column;

import java.io.IOException;
import java.sql.SQLException;
Expand All @@ -34,7 +35,7 @@
public class PlateMapExcelWriter extends ExcelWriter
{
private static final Logger logger = LogHelper.getLogger(PlateMapExcelWriter.class, "Plate map export");
private static final Set<String> excludedFields = Set.of("sampleid", "type", "wellgroup");
private static final Set<String> excludedFields = CaseInsensitiveHashSet.of(Column.SampleID.name(), Column.Type.name(), Column.WellGroup.name());

private final Plate _plate;
private final QueryView _queryView;
Expand Down Expand Up @@ -66,8 +67,8 @@ private void initializeWellData() throws SQLException, IOException
while (results.next())
{
RowMap<Object> well = factory.getRowMap(results);
Integer row = (Integer) well.get(WellTable.Column.Row.name());
Integer col = (Integer) well.get(WellTable.Column.Col.name());
Integer row = (Integer) well.get(Column.Row.name());
Integer col = (Integer) well.get(Column.Col.name());

Map<Integer, RowMap<Object>> rowMap = _wellData.computeIfAbsent(row, k -> new HashMap<>());

Expand Down Expand Up @@ -190,7 +191,7 @@ protected void renderGrid(Sheet sheet, List<DisplayColumn> displayColumns) throw
// Removes fields explicitly excluded for Map export
protected List<DisplayColumn> getDisplayColumns()
{
return _displayColumns.stream().filter(col -> !excludedFields.contains(col.getName().toLowerCase())).toList();
return _displayColumns.stream().filter(col -> !excludedFields.contains(col.getName())).toList();
}

protected List<PlateCustomField> getCustomFields()
Expand All @@ -212,7 +213,10 @@ protected void renderSheet(Workbook workbook, int sheetNumber)
List<DisplayColumn> displayColumns;

if (sheetNumber == 0) // Summary view, render all values in each cell
displayColumns = displayCols.stream().filter(dc -> !dc.getName().equals("row") && !dc.getName().equals("col")).toList();
{
Set<FieldKey> excludeFromSummary = Set.of(Column.Col.fieldKey(), Column.Row.fieldKey());
displayColumns = displayCols.stream().filter(dc -> !excludeFromSummary.contains(dc.getColumnInfo().getFieldKey())).toList();
}
else if (sheetNumber == 1) // Sample ID view
displayColumns = List.of(displayCols.get(0));
else // CustomField view
Expand Down