Repository navigation
Add shared project and protocol at time lookup columns #1248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
labkey-martyp
merged 2 commits into
release26.7-SNAPSHOT
from
26.7_fb_assignment_at_time
Nov 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
ehr/api-src/org/labkey/api/ehr/table/AssignmentAtTimeColumns.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * 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.api.ehr.table; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.labkey.api.data.AbstractTableInfo; | ||
| import org.labkey.api.collections.CaseInsensitiveHashSet; | ||
| import org.labkey.api.data.BaseColumnInfo; | ||
| import org.labkey.api.data.ColumnInfo; | ||
| import org.labkey.api.data.MultiValuedForeignKey; | ||
| import org.labkey.api.data.TableInfo; | ||
| import org.labkey.api.data.WrappedColumn; | ||
| import org.labkey.api.query.LookupForeignKey; | ||
| import org.labkey.api.query.QueryDefinition; | ||
| import org.labkey.api.query.QueryException; | ||
| import org.labkey.api.query.QueryService; | ||
| import org.labkey.api.query.UserSchema; | ||
| import org.labkey.api.util.logging.LogHelper; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Multi-valued lookups to the projects (study.assignment) and protocols (study.protocolAssignment) an animal held on | ||
| * each record's date. The protocol comes from protocolAssignment, not assignment.project.protocol, for centers that | ||
| * assign the two independently. | ||
| */ | ||
| public final class AssignmentAtTimeColumns | ||
| { | ||
| private static final Logger LOG = LogHelper.getLogger(AssignmentAtTimeColumns.class, "Project and protocol at time lookup columns"); | ||
|
|
||
| public enum AtTimeColumn | ||
| { | ||
| PROJECT("projectAtTime", "Project At Time", "assignment", "project"), | ||
| PROTOCOL("protocolAtTime", "Protocol At Time", "protocolAssignment", "protocol"); | ||
|
|
||
| private final String _name; | ||
| private final String _label; | ||
| private final String _sourceDataset; | ||
| private final String _valueCol; | ||
|
|
||
| AtTimeColumn(String name, String label, String sourceDataset, String valueCol) | ||
| { | ||
| _name = name; | ||
| _label = label; | ||
| _sourceDataset = sourceDataset; | ||
| _valueCol = valueCol; | ||
| } | ||
| } | ||
|
|
||
| private AssignmentAtTimeColumns() | ||
| { | ||
| } | ||
|
|
||
| public static void addAtTimeCols(@NotNull AbstractTableInfo ds, @NotNull UserSchema ehrStudySchema, @NotNull AtTimeColumn... cols) | ||
| { | ||
| if (ds.getColumn("Id") == null || ds.getColumn("date") == null || !"study".equalsIgnoreCase(ds.getPublicSchemaName())) | ||
| return; | ||
|
|
||
| UserSchema targetSchema = ds.getUserSchema(); | ||
| if ("demographics".equalsIgnoreCase(ds.getName()) || ds.getPkColumns().size() != 1 || targetSchema == null) | ||
| return; | ||
|
|
||
| Set<String> ehrStudyTables = null; | ||
| for (AtTimeColumn col : cols) | ||
| { | ||
| if (ds.getColumn(col._name, false) != null || col._sourceDataset.equalsIgnoreCase(ds.getName())) | ||
| continue; | ||
|
|
||
| if (ehrStudyTables == null) | ||
| ehrStudyTables = new CaseInsensitiveHashSet(ehrStudySchema.getTableNames()); | ||
|
|
||
| if (ehrStudyTables.contains(col._sourceDataset)) | ||
| addAtTimeLookupCol(ds, targetSchema, ehrStudySchema, col); | ||
| } | ||
| } | ||
|
|
||
| private static void addAtTimeLookupCol(AbstractTableInfo ds, UserSchema targetSchema, UserSchema ehrStudySchema, AtTimeColumn atTimeCol) | ||
| { | ||
| final ColumnInfo pkCol = ds.getPkColumns().getFirst(); | ||
| final String schemaName = ds.getPublicSchemaName(); | ||
| final String queryName = ds.getName(); | ||
| final String ehrPath = ehrStudySchema.getContainer().getPath(); | ||
| final String name = atTimeCol._name; | ||
| final String valueCol = atTimeCol._valueCol; | ||
|
|
||
| WrappedColumn col = new WrappedColumn(pkCol, name); | ||
| col.setLabel(atTimeCol._label); | ||
| col.setReadOnly(true); | ||
| col.setUserEditable(false); | ||
| // The junction's value column keeps the source dataset's lookup, which the multi-valued FK resolves through to | ||
| // the target's title column; values with no matching target row are dropped. | ||
| col.setFk(new MultiValuedForeignKey(new LookupForeignKey(pkCol.getName()) | ||
| { | ||
| @Override | ||
| public TableInfo getLookupTableInfo() | ||
| { | ||
| QueryDefinition qd = QueryService.get().createQueryDef(targetSchema.getUser(), targetSchema.getContainer(), targetSchema, queryName + "_" + name); | ||
| // Full timestamps with an exclusive end, like housingAtTime; AssignmentAtTimeForeignKey matches by date instead. | ||
| // The source is pinned to the EHR container so the dataset's container filter doesn't widen it. | ||
| qd.setSql("SELECT DISTINCT\n" + | ||
| "sd." + pkCol.getFieldKey().toSQLString() + ",\n" + | ||
| "src." + valueCol + "\n" + | ||
| "FROM \"" + schemaName + "\".\"" + queryName + "\" sd\n" + | ||
| "JOIN \"" + ehrPath + "\".study." + atTimeCol._sourceDataset + "[ContainerFilter='Current'] src\n" + | ||
| " ON (src.id = sd.id AND src.date <= sd.date\n" + | ||
| " AND (src.enddate IS NULL OR sd.date < src.enddate)\n" + | ||
| " AND src.qcstate.publicdata = true)"); | ||
| qd.setIsTemporary(true); | ||
| qd.setContainerFilter(ds.getContainerFilter()); | ||
|
|
||
| List<QueryException> errors = new ArrayList<>(); | ||
| TableInfo ti = qd.getTable(errors, true); | ||
| if (!errors.isEmpty() || ti == null) | ||
| { | ||
| LOG.warn("Error creating {} junction for: {}.{} in container: {}", name, schemaName, queryName, targetSchema.getContainer().getPath()); | ||
| errors.forEach(e -> LOG.error("Lookup table QueryException: {}", e.getMessage(), e)); | ||
| return null; | ||
| } | ||
|
|
||
| ((BaseColumnInfo) ti.getColumn(pkCol.getName())).setKeyField(true); | ||
| return ti; | ||
| } | ||
| }, valueCol)); | ||
|
|
||
| ds.addColumn(col); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the end date intended to be exclusive or inclusive? The PR's Changes section says, "Assignments match by date with the end date inclusive, so a transfer day shows both the old and new values." Depending on the intended behavior, may need to update the SQL below or clarify the PR description.