Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.json.JSONArray;
import org.json.JSONObject;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.ConvertHelper;
import org.labkey.api.data.DbSchema;
Expand Down Expand Up @@ -462,13 +463,30 @@ protected void validateRows(List<Map<String, Object>> rows, ImportContext contex
errors.confirmNoErrors();
}

/**
* Returns the set of container ids in which an assay run template for a request in the given container may
* legitimately live: the container itself, plus its parent when the request runs in a workbook (mirroring the
* run-template picker, which sources templates from Laboratory.Utils.getQueryContainerPath). Used to scope
* assay_run_templates lookups to the request's folder tree without reaching an unrelated container.
*/
public static List<String> getTemplateContainerIds(Container c)
{
List<String> ids = new ArrayList<>();
ids.add(c.getId());
if (c.isWorkbook())
ids.add(c.getParent().getId());
return ids;
}

protected void saveTemplate(ViewContext ctx, int templateId, int runId) throws BatchValidationException
{
try
{
//validate the template exists
//validate the template exists in (or above) this container
TableInfo ti = DbSchema.get("laboratory").getTable("assay_run_templates");
TableSelector ts = new TableSelector(ti, new SimpleFilter(FieldKey.fromString("rowid"), templateId), null);
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("rowid"), templateId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the comment I just posted: this code is basically replicating ContainerFilter. I think we should switch this to use UserSchema, which looks like it might just handle the container aspects seamlessly for us

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I see the ContainerFilter.Type.WorkbookAndParent now. I can do a quick update.

@bbimber bbimber Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default behavior when querying the parent container is to include workbooks, so the module code shouldnt need to do much. See another solution to this PR here: #292

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll check it out

filter.addCondition(FieldKey.fromString("container"), getTemplateContainerIds(_container), CompareType.IN);
TableSelector ts = new TableSelector(ti, filter, null);
if (ts.getRowCount() == 0)
{
throw new BatchValidationException(Collections.singletonList(new ValidationException("Unknown template: " + templateId)), null);
Expand Down Expand Up @@ -586,7 +604,9 @@ protected Map<String, Map<String, Object>> getTemplateRowMap(ImportContext conte

TableInfo ti = DbSchema.get("laboratory").getTable("assay_run_templates");

TableSelector ts = new TableSelector(ti, new SimpleFilter(FieldKey.fromString("rowid"), templateId), null);
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("rowid"), templateId);
filter.addCondition(FieldKey.fromString("container"), getTemplateContainerIds(_container), CompareType.IN);
TableSelector ts = new TableSelector(ti, filter, null);
Map<String, Object>[] maps = ts.getMapArray();
if (maps.length == 0)
{
Expand Down
18 changes: 16 additions & 2 deletions laboratory/src/org/labkey/laboratory/assay/AssayHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.labkey.api.cache.CacheManager;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.collections.CollectionUtils;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.RuntimeSQLException;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TSVMapWriter;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
Expand All @@ -50,7 +52,9 @@
import org.labkey.api.laboratory.LaboratoryService;
import org.labkey.api.laboratory.assay.AssayDataProvider;
import org.labkey.api.laboratory.assay.AssayImportMethod;
import org.labkey.api.laboratory.assay.DefaultAssayParser;
import org.labkey.api.query.BatchValidationException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.User;
import org.labkey.api.util.FileUtil;
Expand Down Expand Up @@ -131,16 +135,26 @@ public Map<String, Object> saveTemplate(User u, Container c, ExpProtocol protoco
row.put("title", title);
row.put("importMethod", importMethod);
row.put("json", json.toString());
row.put("container", c.getId());

if (templateId == null)
{
row.put("container", c.getId());
row = Table.insert(u, ti, row);
}
else
{
// Table.update operates by global PK; verify the template is reachable from this container
// (here, or the parent when in a workbook) before updating, and leave its container unchanged.
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("rowid"), templateId);
filter.addCondition(FieldKey.fromString("container"), DefaultAssayParser.getTemplateContainerIds(c), CompareType.IN);
if (!new TableSelector(ti, filter, null).exists())
{
errors.addRowError(new ValidationException("Unknown template: " + templateId));
throw errors;
}

row.put("rowid", templateId);
row = Table.update(u, ti, row, templateId);
row = Table.update(u, ti, row, templateId); // "container" intentionally absent -> not moved
}

return row;
Expand Down