Skip to content

Commit

Permalink
AssayPlateTriggerFactory: query table provided to trigger (#6023)
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-nicka authored Nov 7, 2024
1 parent 5e982aa commit 049a01d
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions assay/src/org/labkey/assay/plate/AssayPlateTriggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.assay.AssayProtocolSchema;
import org.labkey.api.assay.AssayProvider;
import org.labkey.api.assay.AssayResultDomainKind;
import org.labkey.api.assay.AssayService;
import org.labkey.api.assay.plate.AssayPlateMetadataService;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableResultSet;
import org.labkey.api.data.TableSelector;
import org.labkey.api.data.triggers.Trigger;
import org.labkey.api.data.triggers.TriggerFactory;
Expand All @@ -22,6 +21,7 @@
import org.labkey.api.security.User;
import org.labkey.api.util.UnexpectedException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -46,7 +46,8 @@ public AssayPlateTriggerFactory(ExpProtocol protocol)
}

/**
* Trigger to handle updates, inserts are handled during assay run creation
* Recompute the stats for the changed replicate rows.
* Trigger to handle updates, inserts are handled during assay run creation.
*/
private class ReplicateStatsTrigger implements Trigger
{
Expand Down Expand Up @@ -87,30 +88,22 @@ public void complete(TableInfo table, Container c, User user, TableInfo.TriggerT
if (_replicateLsid.isEmpty() || errors.hasErrors())
return;

// recompute the stats for the changed replicate rows
AssayProvider provider = AssayService.get().getProvider(_protocol);
if (provider == null)
throw new IllegalStateException(String.format("Unable to find the provider for protocol : %s", _protocol.getName()));
var filter = new SimpleFilter(FieldKey.fromParts(AssayResultDomainKind.REPLICATE_LSID_COLUMN_NAME), _replicateLsid.keySet(), CompareType.IN);

AssayProtocolSchema schema = provider.createProtocolSchema(user, c, _protocol, null);
TableInfo dataTable = schema.createDataTable(null, false);
if (dataTable == null)
return;

try
try (TableResultSet rs = new TableSelector(table, filter, null).getResultSet())
{
SimpleFilter filter = new SimpleFilter().addInClause(FieldKey.fromParts(AssayResultDomainKind.REPLICATE_LSID_COLUMN_NAME), _replicateLsid.keySet());
Map<Lsid, List<Map<String, Object>>> replicates = new HashMap<>();

new TableSelector(dataTable, filter, null).getResults().forEach(row -> {
var lsid = row.get(AssayResultDomainKind.REPLICATE_LSID_COLUMN_NAME);
replicates.computeIfAbsent(Lsid.parse(String.valueOf(lsid)), m -> new ArrayList<>()).add(row);
_replicateLsid.remove(lsid.toString());
});

// if results are being deleted, check if all rows for the well group have been deleted
List<Map<String, Object>> deletedRows = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : _replicateLsid.entrySet())
var replicates = new HashMap<Lsid, List<Map<String, Object>>>();

while (rs.next())
{
var lsid = rs.getString(AssayResultDomainKind.REPLICATE_LSID_COLUMN_NAME);
replicates.computeIfAbsent(Lsid.parse(String.valueOf(lsid)), m -> new ArrayList<>()).add(rs.getRowMap());
_replicateLsid.remove(lsid);
}

// If results are being deleted, check if all rows for the well group have been deleted
var deletedRows = new ArrayList<Map<String, Object>>();
for (var entry : _replicateLsid.entrySet())
{
if (!entry.getValue())
deletedRows.add(Map.of(PlateReplicateStatsDomainKind.Column.Lsid.name(), entry.getKey()));
Expand All @@ -121,7 +114,7 @@ public void complete(TableInfo table, Container c, User user, TableInfo.TriggerT

AssayPlateMetadataService.get().updateReplicateStats(c, user, _protocol, replicates);
}
catch (ExperimentException e)
catch (ExperimentException | SQLException e)
{
throw UnexpectedException.wrap(e);
}
Expand Down

0 comments on commit 049a01d

Please sign in to comment.