Skip to content
Merged
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
39 changes: 31 additions & 8 deletions experiment/src/org/labkey/experiment/api/ExpMaterialTableImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.util.Precision;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.assay.plate.AssayPlateMetadataService;
Expand Down Expand Up @@ -309,9 +310,8 @@ public StringExpression getURL(ColumnInfo parent)
case RawAmount ->
{
var columnInfo = wrapColumn(alias, _rootTable.getColumn(Column.StoredAmount.name()));
columnInfo.setDisplayColumnFactory(colInfo -> new SampleTypeAmountPrecisionDisplayColumn(colInfo, null));
columnInfo.setDescription("The amount of this sample, in the base unit for the sample type's display unit (if defined), currently on hand.");
if (columnInfo.getFormat() == null)
columnInfo.setFormat(Quantity.DEFAULT_FORMAT);
columnInfo.setUserEditable(false);
columnInfo.setReadOnly(true);
return columnInfo;
Expand All @@ -324,6 +324,7 @@ public StringExpression getURL(ColumnInfo parent)
if (typeUnit != null)
{
SampleTypeAmountDisplayColumn columnInfo = new SampleTypeAmountDisplayColumn(this, Column.StoredAmount.name(), Column.Units.name(), label, importAliases, typeUnit);
columnInfo.setDisplayColumnFactory(colInfo -> new SampleTypeAmountPrecisionDisplayColumn(colInfo, typeUnit));
columnInfo.setDescription("The amount of this sample, in the display unit for the sample type, currently on hand.");
columnInfo.setShownInUpdateView(true);
columnInfo.setShownInInsertView(true);
Expand All @@ -334,8 +335,7 @@ public StringExpression getURL(ColumnInfo parent)
else
{
var columnInfo = wrapColumn(alias, _rootTable.getColumn(Column.StoredAmount.name()));
if (columnInfo.getFormat() == null)
columnInfo.setFormat(Quantity.DEFAULT_FORMAT);
columnInfo.setDisplayColumnFactory(colInfo -> new SampleTypeAmountPrecisionDisplayColumn(colInfo, null));
columnInfo.setLabel(label);
columnInfo.setImportAliasesSet(importAliases);
columnInfo.setDescription("The amount of this sample currently on hand.");
Expand Down Expand Up @@ -1576,14 +1576,13 @@ public SampleTypeAmountDisplayColumn(TableInfo parent, String amountFieldName, S
super(parent, FieldKey.fromParts(amountFieldName), new SQLFragment(
"(CASE WHEN ").append(ExprColumn.STR_TABLE_ALIAS + ".").append(unitFieldName)
.append(" = ? AND ").append(ExprColumn.STR_TABLE_ALIAS + ".").append(amountFieldName)
.append(" IS NOT NULL THEN ROUND(CAST(").append(ExprColumn.STR_TABLE_ALIAS + ".").append(amountFieldName)
.append(" IS NOT NULL THEN CAST(").append(ExprColumn.STR_TABLE_ALIAS + ".").append(amountFieldName)
.append(" / ? AS ")
.append(parent.getSqlDialect().isPostgreSQL() ? "DECIMAL" : "DOUBLE PRECISION")
.append("), ?) ELSE ").append(ExprColumn.STR_TABLE_ALIAS + ".").append(amountFieldName)
.append(") ELSE ").append(ExprColumn.STR_TABLE_ALIAS + ".").append(amountFieldName)
.append(" END)")
.add(typeUnit.getBase().toString())
.add(typeUnit.getValue())
.add(typeUnit.getPrecisionScale()),
.add(typeUnit.getValue()),
JdbcType.DOUBLE);

setLabel(label);
Expand Down Expand Up @@ -1830,4 +1829,28 @@ public void overlayMetadata(String tableName, UserSchema schema, Collection<Quer
}
super.overlayMetadata(tableName, schema, errors);
}

static class SampleTypeAmountPrecisionDisplayColumn extends DataColumn
{
private Unit typeUnit;
private boolean applySampleTypePrecision = true;

public SampleTypeAmountPrecisionDisplayColumn(ColumnInfo col, Unit typeUnit) {
super(col, false);
this.typeUnit = typeUnit;
this.applySampleTypePrecision = col.getFormat() == null; // only apply if no custom format is set by user
}

@Override
public Object getDisplayValue(RenderContext ctx)
{
Object value = super.getDisplayValue(ctx);
if (this.applySampleTypePrecision && value != null)
{
int scale = this.typeUnit == null ? Quantity.DEFAULT_PRECISION_SCALE : this.typeUnit.getPrecisionScale();
value = Precision.round(Double.valueOf(value.toString()), scale);
}
return value;
}
}
}