Skip to content
2 changes: 2 additions & 0 deletions api/src/org/labkey/api/audit/AuditHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface AuditHandler
{
String PROVIDED_DATA_PREFIX = ":::provided:::";
Comment thread
cnathe marked this conversation as resolved.
String DELTA_PROVIDED_DATA_PREFIX = ":::delta_provided:::";
// GH Issue 1640: values under PROVIDED_DATA_PREFIX were written before the large-import fix and may be wrong, so user-entered amounts now use this prefix
String USER_PROVIDED_DATA_PREFIX = ":::user_provided:::";

void addSummaryAuditEvent(User user, Container c, TableInfo table, QueryService.AuditAction action, Integer dataRowCount, @Nullable AuditBehaviorType auditBehaviorType, @Nullable String userComment);

Expand Down
2 changes: 2 additions & 0 deletions experiment/src/org/labkey/experiment/ExperimentModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
import org.labkey.experiment.samples.DataClassFolderWriter;
import org.labkey.experiment.samples.SampleStatusFolderImporter;
import org.labkey.experiment.samples.SampleTimelineAuditProvider;
import org.labkey.experiment.samples.SampleTimelineMetadataDisplayColumn;
import org.labkey.experiment.samples.SampleTypeFolderImporter;
import org.labkey.experiment.samples.SampleTypeFolderWriter;
import org.labkey.experiment.security.DataClassDesignerRole;
Expand Down Expand Up @@ -1170,6 +1171,7 @@ public Collection<String> getSummary(Container c)
LsidUtils.TestCase.class,
PropertyController.TestCase.class,
Quantity.TestCase.class,
SampleTimelineMetadataDisplayColumn.TestCase.class,
Unit.TestCase.class
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import java.util.List;
import java.util.Map;

import static org.labkey.api.audit.AuditHandler.PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.AuditHandler.USER_PROVIDED_DATA_PREFIX;
import static org.labkey.api.exp.query.ExpMaterialTable.Column.StoredAmount;
import static org.labkey.api.exp.query.ExpMaterialTable.Column.Units;

Expand All @@ -60,7 +60,7 @@
public class SampleProvidedAmountAuditTestCase extends Assert
{
private static final int ROW_COUNT = 2500; // spans three audit batches
private static final String PROVIDED_AMOUNT_KEY = PROVIDED_DATA_PREFIX + StoredAmount.label();
private static final String PROVIDED_AMOUNT_KEY = USER_PROVIDED_DATA_PREFIX + StoredAmount.label();

private static Container _c;
private static User _user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
import static java.util.Collections.emptyMap;
import static org.labkey.api.audit.AuditHandler.DELTA_PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.AuditHandler.PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.AuditHandler.USER_PROVIDED_DATA_PREFIX;
import static org.labkey.api.data.TableSelector.ALL_COLUMNS;
import static org.labkey.api.dataiterator.DataIteratorUtil.DUPLICATE_COLUMN_IN_DATA_ERROR;
import static org.labkey.api.dataiterator.DetailedAuditLogDataIterator.AuditConfigs;
Expand Down Expand Up @@ -379,14 +380,19 @@ protected Map<String, Object> extractProvidedAmountsAndUnits(@NotNull Map<String
Map<String, Object> result = new HashMap<>();
String unitsStr = "";
String prefix;
String metadataPrefix;
if (dataRow.containsKey(DELTA_PROVIDED_DATA_PREFIX + StoredAmount.name()))
{
prefix = DELTA_PROVIDED_DATA_PREFIX;
metadataPrefix = DELTA_PROVIDED_DATA_PREFIX;
}
else
{
// with no sample type display unit, no conversion will happen
if (_sampleType == null || _sampleType.getMetricUnit() == null)
return null;
prefix = PROVIDED_DATA_PREFIX;
metadataPrefix = USER_PROVIDED_DATA_PREFIX;
}
Object amountVal = dataRow.get(prefix + StoredAmount.name());
if (amountVal == null)
Expand All @@ -395,7 +401,7 @@ protected Map<String, Object> extractProvidedAmountsAndUnits(@NotNull Map<String
if (dataRow.get(prefix + Units.name()) != null)
unitsStr = " " + dataRow.get(prefix + Units.name()).toString();

result.put(prefix + StoredAmount.label(), amountVal + unitsStr);
result.put(metadataPrefix + StoredAmount.label(), amountVal + unitsStr);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ else if (COLUMN_NAME_USER_COMMENT.equalsIgnoreCase(col.getName()))
{
col.setLabel("Reason");
}
else if (METADATA_COLUMN_NAME.equalsIgnoreCase(col.getName()))
{
col.setDisplayColumnFactory(SampleTimelineMetadataDisplayColumn::new);
}
}
};
table.setTitleColumn(SAMPLE_NAME_COLUMN_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2026 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.experiment.samples;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.audit.AbstractAuditTypeProvider;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.DataColumn;
import org.labkey.api.data.RenderContext;

import java.util.Map;

import static org.labkey.api.audit.AuditHandler.DELTA_PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.AuditHandler.PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.AuditHandler.USER_PROVIDED_DATA_PREFIX;
import static org.labkey.api.audit.SampleTimelineAuditEvent.SAMPLE_TIMELINE_EVENT_TYPE;
import static org.labkey.api.exp.query.ExpMaterialTable.Column.StoredAmount;

/** GH Issue 1640: flags metadata that has a provided amount recorded before the large-import fix, which may be wrong. */
public class SampleTimelineMetadataDisplayColumn extends DataColumn
Comment thread
cnathe marked this conversation as resolved.
{
private static final String LEGACY_PROVIDED_AMOUNT_KEY = PROVIDED_DATA_PREFIX + StoredAmount.label();
private static final String WARNING_STYLE = "background-color:#fcf8e3";
private static final String WARNING_TITLE = "Provided Amount May Be Inaccurate";
private static final String WARNING_MESSAGE = "This provided amount value may be inaccurate if recorded during an import or update of more than 1,000 samples. To confirm the provided amount, check the original import file or source data. The stored sample amount is not affected.";

public SampleTimelineMetadataDisplayColumn(ColumnInfo col)
{
super(col);
}

static boolean containsLegacyProvidedAmount(@Nullable Object metadata)
{
return metadata instanceof String str
&& AbstractAuditTypeProvider.decodeFromDataMap(str).containsKey(LEGACY_PROVIDED_AMOUNT_KEY);
}

private boolean hasLegacyProvidedAmount(RenderContext ctx)
{
return containsLegacyProvidedAmount(getValue(ctx));
}

@Override
public @NotNull String getCssStyle(RenderContext ctx)
{
String style = super.getCssStyle(ctx);
if (!hasLegacyProvidedAmount(ctx))
return style;
return StringUtils.isEmpty(style) ? WARNING_STYLE : style + ";" + WARNING_STYLE;
}

@Override
protected String getHoverContent(RenderContext ctx)
{
return hasLegacyProvidedAmount(ctx) ? WARNING_MESSAGE : super.getHoverContent(ctx);
}

@Override
protected String getHoverTitle(RenderContext ctx)
{
return hasLegacyProvidedAmount(ctx) ? WARNING_TITLE : super.getHoverTitle(ctx);
}

public static class TestCase extends Assert
{
private static String metadata(String key, String value)
{
return AbstractAuditTypeProvider.encodeForDataMap(Map.of(key, value, SAMPLE_TIMELINE_EVENT_TYPE, "INSERT"));
}

@Test
public void testLegacyProvidedAmountIsFlagged()
{
assertTrue(containsLegacyProvidedAmount(metadata(PROVIDED_DATA_PREFIX + StoredAmount.label(), "2 L")));
}

@Test
public void testOtherMetadataIsNotFlagged()
{
assertFalse(containsLegacyProvidedAmount(metadata(USER_PROVIDED_DATA_PREFIX + StoredAmount.label(), "2 L")));
assertFalse(containsLegacyProvidedAmount(metadata(DELTA_PROVIDED_DATA_PREFIX + StoredAmount.label(), "-1 mL")));
assertFalse(containsLegacyProvidedAmount(AbstractAuditTypeProvider.encodeForDataMap(Map.of(SAMPLE_TIMELINE_EVENT_TYPE, "INSERT"))));
assertFalse(containsLegacyProvidedAmount(""));
assertFalse(containsLegacyProvidedAmount(null));
}
}
}
Loading