From 698c1e8877242bbc722029de79f6df25754ddba3 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Sat, 21 Nov 2026 12:52:57 -0800 Subject: [PATCH 1/3] Move specimen tables and sample type domain kind to the specimen module Adds AbstractStudyTable and AbstractStudyQuerySchema to the study API so the specimen module can provide study schema tables. Also deletes the unused SpecimenTable and ignores the viewSpecimenTables flag when the specimen module is deployed. --- .gitattributes | 7 - .../labkey/api/specimen/SpecimenManager.java | 11 +- .../specimen/SpecimenMigrationService.java | 12 +- .../SpecimenSampleTypeDomainKind.java | 92 ------ .../study/query/AbstractStudyQuerySchema.java | 55 ++++ .../api/study/query/AbstractStudyTable.java | 280 ++++++++++++++++++ .../query/PtidObfuscatingDisplayColumn.java | 7 +- study/src/org/labkey/study/StudyModule.java | 2 - .../study/query/AbstractSpecimenTable.java | 2 - .../labkey/study/query/BaseStudyTable.java | 255 +--------------- .../labkey/study/query/DatasetTableImpl.java | 1 + .../study/query/SpecimenCommentTable.java | 69 ----- .../study/query/SpecimenEventTable.java | 89 ------ .../query/SpecimenRequestStatusTable.java | 38 --- .../study/query/SpecimenRequestTable.java | 73 ----- .../org/labkey/study/query/SpecimenTable.java | 121 -------- .../study/query/SpecimenVialCountTable.java | 117 -------- .../labkey/study/query/StudyQuerySchema.java | 88 +++--- .../labkey/study/query/VialRequestTable.java | 97 ------ 19 files changed, 393 insertions(+), 1023 deletions(-) delete mode 100644 study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java create mode 100644 study/api-src/org/labkey/api/study/query/AbstractStudyQuerySchema.java create mode 100644 study/api-src/org/labkey/api/study/query/AbstractStudyTable.java rename study/{src/org/labkey => api-src/org/labkey/api}/study/query/PtidObfuscatingDisplayColumn.java (91%) delete mode 100644 study/src/org/labkey/study/query/SpecimenCommentTable.java delete mode 100644 study/src/org/labkey/study/query/SpecimenEventTable.java delete mode 100644 study/src/org/labkey/study/query/SpecimenRequestStatusTable.java delete mode 100644 study/src/org/labkey/study/query/SpecimenRequestTable.java delete mode 100644 study/src/org/labkey/study/query/SpecimenTable.java delete mode 100644 study/src/org/labkey/study/query/SpecimenVialCountTable.java delete mode 100644 study/src/org/labkey/study/query/VialRequestTable.java diff --git a/.gitattributes b/.gitattributes index 50c64436c0d..9587d16a634 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2582,20 +2582,13 @@ study/src/org/labkey/study/query/ParticipantTable.java -text study/src/org/labkey/study/query/ParticipantVisitDatasetTable.java -text study/src/org/labkey/study/query/ParticipantVisitTable.java -text study/src/org/labkey/study/query/PrimaryTypeTable.java -text -study/src/org/labkey/study/query/PtidObfuscatingDisplayColumn.java -text study/src/org/labkey/study/query/PublishedRecordQueryView.java -text study/src/org/labkey/study/query/QCStateTable.java -text study/src/org/labkey/study/query/SimpleSpecimenTable.java -text -study/src/org/labkey/study/query/SpecimenCommentTable.java -text study/src/org/labkey/study/query/SpecimenDetailTable.java -text -study/src/org/labkey/study/query/SpecimenEventTable.java -text -study/src/org/labkey/study/query/SpecimenRequestStatusTable.java -text -study/src/org/labkey/study/query/SpecimenRequestTable.java -text study/src/org/labkey/study/query/SpecimenSummaryTable.java -text -study/src/org/labkey/study/query/SpecimenVialCountTable.java -text study/src/org/labkey/study/query/StudyQuerySchema.java -text study/src/org/labkey/study/query/StudySchemaProvider.java -text -study/src/org/labkey/study/query/VialRequestTable.java -text study/src/org/labkey/study/query/VialTable.java -text study/src/org/labkey/study/query/VisitAliasesTable.java -text study/src/org/labkey/study/query/VisitTable.java -text diff --git a/study/api-src/org/labkey/api/specimen/SpecimenManager.java b/study/api-src/org/labkey/api/specimen/SpecimenManager.java index 9901067998d..8e80d7f6caa 100644 --- a/study/api-src/org/labkey/api/specimen/SpecimenManager.java +++ b/study/api-src/org/labkey/api/specimen/SpecimenManager.java @@ -63,11 +63,16 @@ public boolean isSpecimenModuleActive(Container c) return null != specimenModule && c.getActiveModules().contains(specimenModule); } - // Include specimen queries in the study schema if the specimen module is active or an admin has chosen to expose - // them via an optional feature + // Include specimen queries in the study schema if the specimen module is active or, when it isn't deployed, an admin + // has chosen to expose them via an optional feature public boolean areSpecimenTablesViewable(Container c) { - return isSpecimenModuleActive(c) || OptionalFeatureService.get().isFeatureEnabled(VIEW_SPECIMEN_TABLES_FLAG); + Module specimenModule = ModuleLoader.getInstance().getModule("Specimen"); + if (null != specimenModule) + return c.getActiveModules().contains(specimenModule); + + // The flag is registered only when the specimen module is absent, so a value saved before deploying it would otherwise linger with no UI to clear it + return OptionalFeatureService.get().isFeatureEnabled(VIEW_SPECIMEN_TABLES_FLAG); } public int getSpecimenCountForVisit(Visit visit) diff --git a/study/api-src/org/labkey/api/specimen/SpecimenMigrationService.java b/study/api-src/org/labkey/api/specimen/SpecimenMigrationService.java index 24ffdbe47e6..fdd3467853b 100644 --- a/study/api-src/org/labkey/api/specimen/SpecimenMigrationService.java +++ b/study/api-src/org/labkey/api/specimen/SpecimenMigrationService.java @@ -25,12 +25,11 @@ import org.labkey.api.query.QuerySettings; import org.labkey.api.query.QueryUpdateService; import org.labkey.api.query.QueryView; -import org.labkey.api.query.UserSchema; import org.labkey.api.security.User; import org.labkey.api.security.permissions.Permission; import org.labkey.api.services.ServiceRegistry; -import org.labkey.api.study.Study; import org.labkey.api.study.importer.SimpleStudyImportContext; +import org.labkey.api.study.query.AbstractStudyQuerySchema; import org.labkey.api.util.SafeToRender; import org.labkey.api.view.ActionURL; import org.labkey.api.view.ViewContext; @@ -71,12 +70,15 @@ void importSpecimenArchive(@Nullable FileLike inputFile, PipelineJob job, Simple boolean isEnableRequests(Container c); - void addSpecimenPivotTableNames(Set names); + /** Adds the names of the study schema tables that the specimen module provides */ + void addSpecimenTableNames(Set names); /** - * Returns a specimen pivot TableInfo, if that's what was requested + * Returns the requested study schema table if the specimen module provides it, otherwise null */ - @Nullable TableInfo getSpecimenPivotTable(UserSchema schema, String name, Study study, ContainerFilter cf); + @Nullable TableInfo getSpecimenTable(AbstractStudyQuerySchema schema, String name, ContainerFilter cf); + + TableInfo createSpecimenCommentTable(AbstractStudyQuerySchema schema, ContainerFilter cf, boolean joinBackToSpecimens); @Nullable SafeToRender getSpecimenSettingsLink(Container c, User user); diff --git a/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java b/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java deleted file mode 100644 index 9cc035c817a..00000000000 --- a/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2019-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.specimen; - -import org.labkey.api.data.Container; -import org.labkey.api.exp.Lsid; -import org.labkey.api.exp.property.Domain; -import org.labkey.api.security.User; -import org.labkey.api.study.SpecimenService; -import org.labkey.api.view.ActionURL; -import org.labkey.api.writer.ContainerUser; -import org.labkey.api.exp.api.SampleTypeDomainKind; - -public class SpecimenSampleTypeDomainKind extends SampleTypeDomainKind -{ - public SpecimenSampleTypeDomainKind() - { - } - - @Override - public Priority getPriority(String domainURI) - { - Lsid lsid = new Lsid(domainURI); - String prefix = lsid.getNamespacePrefix(); - String name = lsid.getObjectId(); - if ("SampleSet".equals(prefix) && SpecimenService.SAMPLE_TYPE_NAME.equals(name)) - return Priority.HIGH; - return null; - } - - @Override - public String getStorageSchemaName() - { - // Don't provision table with no properties - return null; - } - - @Override - public ActionURL urlEditDefinition(Domain domain, ContainerUser containerUser) - { - return null; - } - - @Override - public boolean canEditDefinition(User user, Domain domain) - { - return false; - } - - @Override - public boolean canCreateDefinition(User user, Container container) - { - return false; - } - - @Override - public boolean canDeleteDefinition(User user, Domain domain) - { - return false; - } - - @Override - public ActionURL urlCreateDefinition(String schemaName, String queryName, Container container, User user) - { - return null; - } - - @Override - public boolean allowUniqueConstraintProperties() - { - return false; - } - - @Override - public boolean allowCalculatedFields() - { - return false; - } -} diff --git a/study/api-src/org/labkey/api/study/query/AbstractStudyQuerySchema.java b/study/api-src/org/labkey/api/study/query/AbstractStudyQuerySchema.java new file mode 100644 index 00000000000..ef6eb61d8ea --- /dev/null +++ b/study/api-src/org/labkey/api/study/query/AbstractStudyQuerySchema.java @@ -0,0 +1,55 @@ +/* + * 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.study.query; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.labkey.api.data.Container; +import org.labkey.api.data.ContainerFilter; +import org.labkey.api.data.DbSchema; +import org.labkey.api.data.TableInfo; +import org.labkey.api.query.SchemaKey; +import org.labkey.api.query.UserSchema; +import org.labkey.api.security.User; +import org.labkey.api.study.Study; + +/** + * The study schema as seen by tables implemented outside the study module (e.g., specimen tables) + */ +public abstract class AbstractStudyQuerySchema extends UserSchema implements UserSchema.HasContextualRoles +{ + protected AbstractStudyQuerySchema(@NotNull SchemaKey path, @Nullable String description, @NotNull User user, @NotNull Container c, DbSchema dbSchema) + { + super(path, description, user, c, dbSchema, null); + } + + public abstract @Nullable Study getStudy(); + + /** for tables that support container filter, should they turn on support or not */ + public boolean allowSetContainerFilter() + { + return true; + } + + public abstract TableInfo createLocationLookupTable(@Nullable ContainerFilter cf); + + /** + * @param rootTableColumnName PrimaryTypeId, DerivativeTypeId, DerivativeTypeId2, or AdditiveTypeId + */ + public abstract TableInfo createSpecimenTypeLookupTable(String rootTableColumnName, ContainerFilter cf); + + public abstract TableInfo createSpecimenDetailLookupTable(@Nullable ContainerFilter cf); +} diff --git a/study/api-src/org/labkey/api/study/query/AbstractStudyTable.java b/study/api-src/org/labkey/api/study/query/AbstractStudyTable.java new file mode 100644 index 00000000000..1b48bfe9d06 --- /dev/null +++ b/study/api-src/org/labkey/api/study/query/AbstractStudyTable.java @@ -0,0 +1,280 @@ +/* + * Copyright (c) 2008-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.study.query; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.labkey.api.data.BaseColumnInfo; +import org.labkey.api.data.ColumnInfo; +import org.labkey.api.data.ContainerFilter; +import org.labkey.api.data.JdbcType; +import org.labkey.api.data.MutableColumnInfo; +import org.labkey.api.data.SQLFragment; +import org.labkey.api.data.TableInfo; +import org.labkey.api.data.dialect.SqlDialect; +import org.labkey.api.exp.PropertyColumn; +import org.labkey.api.exp.PropertyDescriptor; +import org.labkey.api.exp.property.DomainProperty; +import org.labkey.api.query.AliasedColumn; +import org.labkey.api.query.ExprColumn; +import org.labkey.api.query.FieldKey; +import org.labkey.api.query.FilteredTable; +import org.labkey.api.query.LookupForeignKey; +import org.labkey.api.query.QueryUpdateService; +import org.labkey.api.query.column.BuiltInColumnTypes; +import org.labkey.api.security.UserPrincipal; +import org.labkey.api.security.permissions.AdminPermission; +import org.labkey.api.security.permissions.Permission; +import org.labkey.api.security.permissions.ReadPermission; +import org.labkey.api.security.roles.ReaderRole; +import org.labkey.api.security.roles.Role; +import org.labkey.api.security.roles.RoleManager; +import org.labkey.api.specimen.SpecimenMigrationService; +import org.labkey.api.study.Study; +import org.labkey.api.study.StudyService; +import org.labkey.api.util.DemoMode; +import org.labkey.api.util.StringExpression; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +public abstract class AbstractStudyTable extends FilteredTable +{ + public AbstractStudyTable(SchemaType schema, TableInfo realTable, ContainerFilter cf) + { + this(schema, realTable, cf, false); + } + + public AbstractStudyTable(SchemaType schema, TableInfo realTable, ContainerFilter cf, boolean includeSourceStudyData) + { + super(realTable, schema); + + Study study = schema.getStudy(); + if (includeSourceStudyData && null != study && !study.isDataspaceStudy()) + { + boolean hasReaderRole = getContextualRoles().contains(RoleManager.getRole(ReaderRole.class)); + _setContainerFilter(new ContainerFilter.StudyAndSourceStudy(schema.getContainer(), schema.getUser(), hasReaderRole)); + } + else if (null != cf && supportsContainerFilter()) + _setContainerFilter(cf); + else + _setContainerFilter(getDefaultContainerFilter()); + } + + protected MutableColumnInfo addWrapParticipantColumn(String rootTableColumnName) + { + final String subjectColName = StudyService.get().getSubjectColumnName(getContainer()); + final String subjectTableName = StudyService.get().getSubjectTableName(getContainer()); + + var participantColumn = + new AliasedColumn(this, subjectColName, _rootTable.getColumn(rootTableColumnName)); + LookupForeignKey lfk = new LookupForeignKey(subjectTableName, subjectColName, null) + { + @Override + public TableInfo getLookupTableInfo() + { + return _userSchema.getTable(subjectTableName); + } + + @Override + public StringExpression getURL(ColumnInfo parent) + { + TableInfo table = getLookupTableInfo(); + if (table == null) + return null; + return LookupForeignKey.getDetailsURL(parent, table, _columnName); + } + }; + lfk.addJoin(FieldKey.fromParts("Container"), "Container", false); + participantColumn.setFk(lfk); + + // Don't setKeyField. Use addQueryFieldKeys where needed + + if (DemoMode.isDemoMode(_userSchema.getContainer(), _userSchema.getUser())) + { + participantColumn.setDisplayColumnFactory(PtidObfuscatingDisplayColumn::new); + } + + return addColumn(participantColumn); + } + + + @Override + protected ColumnInfo resolveColumn(String name) + { + // The name of the subject ID column can now be customized by study. Since there are some joins + // from the assay side which join to cross-study data, we need a single column name that works + // in all containers. This is 'ParticipantId', since this was the column name before + // customization was possible: + if ("ParticipantId".equalsIgnoreCase(name)) + { + String alt = StudyService.get().getSubjectColumnName(getContainer()); + if (!"ParticipantId".equalsIgnoreCase(alt)) + return getColumn(alt); + } + return super.resolveColumn(name); + } + + + protected MutableColumnInfo addWrapLocationColumn(String wrappedName, String rootTableColumnName) + { + var locationColumn = new AliasedColumn(this, wrappedName, _rootTable.getColumn(rootTableColumnName)); + locationColumn.setFk(new LookupForeignKey("RowId") + { + @Override + public TableInfo getLookupTableInfo() + { + return _userSchema.createLocationLookupTable(_userSchema.allowSetContainerFilter() ? getContainerFilter() : null); + } + }); + return addColumn(locationColumn); + } + + protected MutableColumnInfo addContainerColumn() + { + return addContainerColumn(false); + } + + protected MutableColumnInfo addContainerColumn(boolean isProvisioned) + { + BaseColumnInfo containerCol; + if (isProvisioned) + { + SQLFragment sql = SQLFragment.unsafe("CAST ("); + sql.appendValue(getContainer()); + sql.unsafeAppend(" AS ").append(getSchema().getSqlDialect().getGuidTypeAsSql()).unsafeAppend(")"); + containerCol = new ExprColumn(this, "Container", sql, JdbcType.GUID); + } + else + { + containerCol = new AliasedColumn(this, "Container", _rootTable.getColumn("Container")); + } + containerCol.setConceptURI(BuiltInColumnTypes.CONTAINERID_CONCEPT_URI); + return addColumn(containerCol); + } + + protected ColumnInfo addWrapTypeColumn(String wrappedName, final String rootTableColumnName) + { + var typeColumn = new AliasedColumn(this, wrappedName, _rootTable.getColumn(rootTableColumnName)); + LookupForeignKey fk = new LookupForeignKey(getContainerFilter(), "RowId", null) + { + @Override + public TableInfo getLookupTableInfo() + { + return _userSchema.createSpecimenTypeLookupTable(rootTableColumnName, getLookupContainerFilter()); + } + }; + typeColumn.setFk(fk); + + return addColumn(typeColumn); + } + + /* Subclass can override to support per-table roles */ + protected @NotNull Set getContextualRoles() + { + return getUserSchema().getContextualRoles(); + } + + /* NOTE getUpdateService() and hasPermission() should usually be overridden together */ + + @Override + public QueryUpdateService getUpdateService() + { + return null; + } + + /** + * ONLY OVERRIDE THIS IF TABLE SHOULD BE EDITABLE IN DATASPACE PROJECT-LEVEL CONTAINER + * CONSIDER: use a "allowEditInDataspace" field to avoid need to override + */ + @Override + public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class perm) + { + checkedPermissions.add(perm); + // Most tables should not be editable in Dataspace + if (!perm.equals(ReadPermission.class) && StudyService.get().isDataspace(getContainer())) + return false; + return hasPermissionOverridable(user, perm); + } + + /** + * Override this method if + * a) You need special per-table read behavior (e.g. datasets) + * b) You implement getUpdateService() + */ + protected boolean hasPermissionOverridable(UserPrincipal user, Class perm) + { + return checkHasReadPermission(user, perm); + } + + protected boolean checkReadOrIsAdminPermission(UserPrincipal user, Class perm) + { + return ReadPermission.class == perm && _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()) || + _userSchema.getContainer().hasPermission(user, AdminPermission.class, getContextualRoles()); + } + + protected boolean checkHasReadPermission(UserPrincipal user, Class perm) + { + return ReadPermission.class == perm && _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()); + } + + protected boolean checkContainerPermission(UserPrincipal user, Class perm) + { + return _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()); + } + + protected boolean checkSpecimenEditPermissions(UserPrincipal user, Class perm) + { + if (perm.equals(ReadPermission.class)) + return getContainer().hasPermission(user, perm, getContextualRoles()); + + SpecimenMigrationService sms = SpecimenMigrationService.get(); + return sms != null && getContainer().hasPermissions(user, Set.of(perm, sms.getEditSpecimenDataPermission()), getContextualRoles()); + } + + protected void addOptionalColumns(List optionalProperties, boolean editable, @Nullable List readOnlyColumnNames) + { + SqlDialect dialect = getSqlDialect(); + Supplier> defaultsSupplier = null; + for (DomainProperty domainProperty : optionalProperties) + { + PropertyDescriptor property = domainProperty.getPropertyDescriptor(); + SQLFragment sql = SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS); + var legalName = property.getLegalSelectName(dialect); + sql.unsafeAppend(".").appendIdentifier(legalName); + var column = new ExprColumn(this, legalName.getId(), sql, property.getJdbcType()); + defaultsSupplier = PropertyColumn.copyAttributes(getUserSchema().getUser(), column, domainProperty, getContainer(), null, defaultsSupplier); + if (editable) + { + // Make editable, but some should be read only + column.setUserEditable(true); + if (null != readOnlyColumnNames && readOnlyColumnNames.contains(column.getName().toLowerCase())) + column.setReadOnly(true); + } + addColumn(column); + } + } + + @Override + public boolean supportsContainerFilter() + { + if (!_userSchema.allowSetContainerFilter()) + return false; + return super.supportsContainerFilter(); + } +} diff --git a/study/src/org/labkey/study/query/PtidObfuscatingDisplayColumn.java b/study/api-src/org/labkey/api/study/query/PtidObfuscatingDisplayColumn.java similarity index 91% rename from study/src/org/labkey/study/query/PtidObfuscatingDisplayColumn.java rename to study/api-src/org/labkey/api/study/query/PtidObfuscatingDisplayColumn.java index 56721195831..c57abbd10bf 100644 --- a/study/src/org/labkey/study/query/PtidObfuscatingDisplayColumn.java +++ b/study/api-src/org/labkey/api/study/query/PtidObfuscatingDisplayColumn.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.labkey.study.query; +package org.labkey.api.study.query; import org.jetbrains.annotations.NotNull; import org.labkey.api.collections.NamedObject; @@ -24,11 +24,6 @@ import org.labkey.api.util.DemoMode; import org.labkey.api.util.HtmlString; -/* -* User: adam -* Date: Apr 26, 2011 -* Time: 6:44:28 PM -*/ public class PtidObfuscatingDisplayColumn extends DataColumn { public PtidObfuscatingDisplayColumn(ColumnInfo col) diff --git a/study/src/org/labkey/study/StudyModule.java b/study/src/org/labkey/study/StudyModule.java index 1d867ff70ad..9cb14b18d0d 100644 --- a/study/src/org/labkey/study/StudyModule.java +++ b/study/src/org/labkey/study/StudyModule.java @@ -74,7 +74,6 @@ import org.labkey.api.settings.OptionalFeatureFlag; import org.labkey.api.settings.OptionalFeatureService; import org.labkey.api.specimen.SpecimenManager; -import org.labkey.api.specimen.SpecimenSampleTypeDomainKind; import org.labkey.api.specimen.model.AdditiveTypeDomainKind; import org.labkey.api.specimen.model.DerivativeTypeDomainKind; import org.labkey.api.specimen.model.LocationDomainKind; @@ -268,7 +267,6 @@ protected void init() PropertyService.get().registerDomainKind(new SpecimenEventDomainKind()); PropertyService.get().registerDomainKind(new VialDomainKind()); PropertyService.get().registerDomainKind(new LocationDomainKind()); - PropertyService.get().registerDomainKind(new SpecimenSampleTypeDomainKind()); // study design domains PropertyService.get().registerDomainKind(new StudyProductDomainKind()); diff --git a/study/src/org/labkey/study/query/AbstractSpecimenTable.java b/study/src/org/labkey/study/query/AbstractSpecimenTable.java index fc5f7654ec1..8c80845570b 100644 --- a/study/src/org/labkey/study/query/AbstractSpecimenTable.java +++ b/study/src/org/labkey/study/query/AbstractSpecimenTable.java @@ -20,8 +20,6 @@ /** * Superclass for specimen tables that adds and configures all the common columns. - * User: jeckels - * Date: May 8, 2009 */ public abstract class AbstractSpecimenTable extends BaseStudyTable { diff --git a/study/src/org/labkey/study/query/BaseStudyTable.java b/study/src/org/labkey/study/query/BaseStudyTable.java index 54260ca53ed..54bc7028a3b 100644 --- a/study/src/org/labkey/study/query/BaseStudyTable.java +++ b/study/src/org/labkey/study/query/BaseStudyTable.java @@ -18,7 +18,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.labkey.api.data.BaseColumnInfo; import org.labkey.api.data.ColumnInfo; import org.labkey.api.data.Container; import org.labkey.api.data.ContainerFilter; @@ -28,36 +27,21 @@ import org.labkey.api.data.RenderContext; import org.labkey.api.data.SQLFragment; import org.labkey.api.data.TableInfo; -import org.labkey.api.data.dialect.SqlDialect; -import org.labkey.api.exp.PropertyColumn; -import org.labkey.api.exp.PropertyDescriptor; -import org.labkey.api.exp.property.DomainProperty; import org.labkey.api.gwt.client.FacetingBehaviorType; import org.labkey.api.query.AliasedColumn; import org.labkey.api.query.ExprColumn; import org.labkey.api.query.FieldKey; -import org.labkey.api.query.FilteredTable; import org.labkey.api.query.LookupForeignKey; -import org.labkey.api.query.QueryUpdateService; -import org.labkey.api.query.column.BuiltInColumnTypes; -import org.labkey.api.security.UserPrincipal; -import org.labkey.api.security.permissions.AdminPermission; -import org.labkey.api.security.permissions.Permission; -import org.labkey.api.security.permissions.ReadPermission; -import org.labkey.api.security.roles.ReaderRole; -import org.labkey.api.security.roles.Role; -import org.labkey.api.security.roles.RoleManager; import org.labkey.api.specimen.SpecimenMigrationService; import org.labkey.api.specimen.SpecimenSchema; import org.labkey.api.study.Study; import org.labkey.api.study.StudyService; import org.labkey.api.study.TimepointType; import org.labkey.api.study.Visit; -import org.labkey.api.util.DemoMode; +import org.labkey.api.study.query.AbstractStudyTable; import org.labkey.api.util.HtmlString; import org.labkey.api.util.PageFlowUtil; import org.labkey.api.util.Pair; -import org.labkey.api.util.StringExpression; import org.labkey.api.writer.HtmlWriter; import org.labkey.study.StudySchema; import org.labkey.study.model.DatasetDefinition; @@ -73,147 +57,17 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.function.Supplier; -public abstract class BaseStudyTable extends FilteredTable +public abstract class BaseStudyTable extends AbstractStudyTable { public BaseStudyTable(StudyQuerySchema schema, TableInfo realTable, ContainerFilter cf) { - this(schema, realTable, cf, false); + super(schema, realTable, cf); } public BaseStudyTable(StudyQuerySchema schema, TableInfo realTable, ContainerFilter cf, boolean includeSourceStudyData) { - super(realTable, schema); - - if (includeSourceStudyData && null != schema._study && !schema._study.isDataspaceStudy()) - { - boolean hasReaderRole = getContextualRoles().contains(RoleManager.getRole(ReaderRole.class)); - _setContainerFilter(new ContainerFilter.StudyAndSourceStudy(schema.getContainer(), schema.getUser(), hasReaderRole)); - } - else if (null != cf && supportsContainerFilter()) - _setContainerFilter(cf); - else - _setContainerFilter(getDefaultContainerFilter()); - } - - protected MutableColumnInfo addWrapParticipantColumn(String rootTableColumnName) - { - final String subjectColName = StudyService.get().getSubjectColumnName(getContainer()); - final String subjectTableName = StudyService.get().getSubjectTableName(getContainer()); - - var participantColumn = - new AliasedColumn(this, subjectColName, _rootTable.getColumn(rootTableColumnName)); - LookupForeignKey lfk = new LookupForeignKey(subjectTableName, subjectColName, null) - { - @Override - public TableInfo getLookupTableInfo() - { - return _userSchema.getTable(subjectTableName); - } - - @Override - public StringExpression getURL(ColumnInfo parent) - { - TableInfo table = getLookupTableInfo(); - if (table == null) - return null; - return LookupForeignKey.getDetailsURL(parent, table, _columnName); - } - }; - lfk.addJoin(FieldKey.fromParts("Container"), "Container", false); - participantColumn.setFk(lfk); - - // Don't setKeyField. Use addQueryFieldKeys where needed - - if (DemoMode.isDemoMode(_userSchema.getContainer(), _userSchema.getUser())) - { - participantColumn.setDisplayColumnFactory(PtidObfuscatingDisplayColumn::new); - } - - return addColumn(participantColumn); - } - - - @Override - protected ColumnInfo resolveColumn(String name) - { - // The name of the subject ID column can now be customized by study. Since there are some joins - // from the assay side which join to cross-study data, we need a single column name that works - // in all containers. This is 'ParticipantId', since this was the column name before - // customization was possible: - if ("ParticipantId".equalsIgnoreCase(name)) - { - String alt = StudyService.get().getSubjectColumnName(getContainer()); - if (!"ParticipantId".equalsIgnoreCase(alt)) - return getColumn(alt); - } - return super.resolveColumn(name); - } - - - protected MutableColumnInfo addWrapLocationColumn(String wrappedName, String rootTableColumnName) - { - var locationColumn = new AliasedColumn(this, wrappedName, _rootTable.getColumn(rootTableColumnName)); - locationColumn.setFk(new LookupForeignKey("RowId") - { - @Override - public TableInfo getLookupTableInfo() - { - return new LocationTable(_userSchema, _userSchema.allowSetContainerFilter() ? getContainerFilter() : null); - } - }); - return addColumn(locationColumn); - } - - protected MutableColumnInfo addContainerColumn() - { - return addContainerColumn(false); - } - - protected MutableColumnInfo addContainerColumn(boolean isProvisioned) - { - BaseColumnInfo containerCol; - if (isProvisioned) - { - SQLFragment sql = SQLFragment.unsafe("CAST ("); - sql.appendValue(getContainer()); - sql.unsafeAppend(" AS ").append(getSchema().getSqlDialect().getGuidTypeAsSql()).unsafeAppend(")"); - containerCol = new ExprColumn(this, "Container", sql, JdbcType.GUID); - } - else - { - containerCol = new AliasedColumn(this, "Container", _rootTable.getColumn("Container")); - } - containerCol.setConceptURI(BuiltInColumnTypes.CONTAINERID_CONCEPT_URI); - return addColumn(containerCol); - } - - protected ColumnInfo addWrapTypeColumn(String wrappedName, final String rootTableColumnName) - { - var typeColumn = new AliasedColumn(this, wrappedName, _rootTable.getColumn(rootTableColumnName)); - LookupForeignKey fk = new LookupForeignKey(getContainerFilter(), "RowId", null) - { - @Override - public TableInfo getLookupTableInfo() - { - BaseStudyTable result = switch (rootTableColumnName) - { - case "PrimaryTypeId" -> new PrimaryTypeTable(_userSchema, getLookupContainerFilter()); - case "DerivativeTypeId", "DerivativeTypeId2" -> - new DerivativeTypeTable(_userSchema, getLookupContainerFilter()); - case "AdditiveTypeId" -> new AdditiveTypeTable(_userSchema, getLookupContainerFilter()); - default -> - throw new IllegalStateException(rootTableColumnName + " is not recognized as a valid specimen type column."); - }; - if (_userSchema.allowSetContainerFilter()) - result.setContainerFilter(getLookupContainerFilter()); - return result; - } - }; - typeColumn.setFk(fk); - - return addColumn(typeColumn); + super(schema, realTable, cf, includeSourceStudyData); } @@ -385,9 +239,8 @@ protected void addVialCommentsColumn(final boolean joinBackToSpecimens) @Override public TableInfo getLookupTableInfo() { - SpecimenCommentTable result = new SpecimenCommentTable(_userSchema, getLookupContainerFilter(), joinBackToSpecimens); - result.setContainerFilter(getLookupContainerFilter()); - return result; + SpecimenMigrationService sms = SpecimenMigrationService.get(); + return sms != null ? sms.createSpecimenCommentTable(_userSchema, getLookupContainerFilter(), joinBackToSpecimens) : null; } }; commentsFK.addJoin(FieldKey.fromParts("Container"), "Folder", false); @@ -433,13 +286,6 @@ protected void addSpecimenCommentColumns(StudyQuerySchema schema, boolean isSpec participantVisitCommentTable, study.getParticipantVisitCommentProperty(), alias, includeVialComments, hidden)); } - - /* Subclass can override to support per-table roles */ - protected @NotNull Set getContextualRoles() - { - return getUserSchema().getContextualRoles(); - } - public static class SpecimenCommentColumn extends ExprColumn { public static final String COLUMN_NAME = "SpecimenComment"; @@ -738,86 +584,6 @@ protected static String formatParticipantComments(RenderContext ctx, boolean ren return sb.toString(); } - /* NOTE getUpdateService() and hasPermission() should usually be overridden together */ - - @Override - public QueryUpdateService getUpdateService() - { - return null; - } - - /** - * ONLY OVERRIDE THIS IF TABLE SHOULD BE EDITABLE IN DATASPACE PROJECT-LEVEL CONTAINER - * CONSIDER: use a "allowEditInDataspace" field to avoid need to override - */ - @Override - public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class perm) - { - checkedPermissions.add(perm); - // Most tables should not be editable in Dataspace - if (!perm.equals(ReadPermission.class) && StudyService.get().isDataspace(getContainer())) - return false; - return hasPermissionOverridable(user, perm); - } - - /** - * Override this method if - * a) You need special per-table read behavior (e.g. datasets) - * b) You implement getUpdateService() - */ - protected boolean hasPermissionOverridable(UserPrincipal user, Class perm) - { - return checkHasReadPermission(user, perm); - } - - protected boolean checkReadOrIsAdminPermission(UserPrincipal user, Class perm) - { - return ReadPermission.class == perm && _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()) || - _userSchema.getContainer().hasPermission(user, AdminPermission.class, getContextualRoles()); - } - - protected boolean checkHasReadPermission(UserPrincipal user, Class perm) - { - return ReadPermission.class == perm && _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()); - } - - protected boolean checkContainerPermission(UserPrincipal user, Class perm) - { - return _userSchema.getContainer().hasPermission(user, perm, getContextualRoles()); - } - - protected boolean checkSpecimenEditPermissions(UserPrincipal user, Class perm) - { - if (perm.equals(ReadPermission.class)) - return getContainer().hasPermission(user, perm, getContextualRoles()); - - SpecimenMigrationService sms = SpecimenMigrationService.get(); - return sms != null && getContainer().hasPermissions(user, Set.of(perm, sms.getEditSpecimenDataPermission()), getContextualRoles()); - } - - protected void addOptionalColumns(List optionalProperties, boolean editable, @Nullable List readOnlyColumnNames) - { - SqlDialect dialect = getSqlDialect(); - Supplier> defaultsSupplier = null; - for (DomainProperty domainProperty : optionalProperties) - { - PropertyDescriptor property = domainProperty.getPropertyDescriptor(); - SQLFragment sql = SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS); - var legalName = property.getLegalSelectName(dialect); - sql.unsafeAppend(".").appendIdentifier(legalName); - var column = new ExprColumn(this, legalName.getId(), sql, property.getJdbcType()); - defaultsSupplier = PropertyColumn.copyAttributes(getUserSchema().getUser(), column, domainProperty, getContainer(), null, defaultsSupplier); - if (editable) - { - // Make editable, but some should be read only - column.setUserEditable(true); - if (null != readOnlyColumnNames && readOnlyColumnNames.contains(column.getName().toLowerCase())) - column.setReadOnly(true); - } - addColumn(column); - } - } - // UNDONE: a lot of study tables already use addContainerColumns() @@ -862,15 +628,6 @@ public TableInfo getLookupTableInfo() return study; } - - @Override - public boolean supportsContainerFilter() - { - if (!_userSchema.allowSetContainerFilter()) - return false; - return super.supportsContainerFilter(); - } - // for testing/breakpoints @Override diff --git a/study/src/org/labkey/study/query/DatasetTableImpl.java b/study/src/org/labkey/study/query/DatasetTableImpl.java index c60ca83fe95..5c645b25a17 100644 --- a/study/src/org/labkey/study/query/DatasetTableImpl.java +++ b/study/src/org/labkey/study/query/DatasetTableImpl.java @@ -88,6 +88,7 @@ import org.labkey.api.study.assay.FileLinkDisplayColumn; import org.labkey.api.study.assay.SpecimenForeignKey; import org.labkey.api.study.model.ParticipantGroup; +import org.labkey.api.study.query.PtidObfuscatingDisplayColumn; import org.labkey.api.util.ContainerContext; import org.labkey.api.util.DemoMode; import org.labkey.api.util.HtmlString; diff --git a/study/src/org/labkey/study/query/SpecimenCommentTable.java b/study/src/org/labkey/study/query/SpecimenCommentTable.java deleted file mode 100644 index 9757c76ba79..00000000000 --- a/study/src/org/labkey/study/query/SpecimenCommentTable.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2008-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.study.query; - -import org.labkey.api.data.ColumnInfo; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.data.TableInfo; -import org.labkey.api.query.AliasedColumn; -import org.labkey.api.query.FilteredTable; -import org.labkey.api.query.LookupForeignKey; -import org.labkey.api.specimen.SpecimenSchema; - -/** - * User: brittp - * Created: July 15, 2008 11:13:43 AM - */ -public class SpecimenCommentTable extends FilteredTable -{ - public SpecimenCommentTable(final StudyQuerySchema schema, ContainerFilter cf) - { - this(schema, cf, true); - } - - public SpecimenCommentTable(final StudyQuerySchema schema, ContainerFilter cf, boolean joinBackToSpecimens) - { - super(SpecimenSchema.get().getTableInfoSpecimenComment(), schema); - for (ColumnInfo baseColumn : _rootTable.getColumns()) - { - String name = baseColumn.getName(); - if (joinBackToSpecimens && "GlobalUniqueId".equalsIgnoreCase(name)) - { - AliasedColumn globalUniqueIdColumn = new AliasedColumn(this, "GlobalUniqueId", _rootTable.getColumn("GlobalUniqueId")); - globalUniqueIdColumn.setFk(new LookupForeignKey(null, (String) null, "GlobalUniqueId", "GlobalUniqueId") - { - @Override - public TableInfo getLookupTableInfo() - { - return new SpecimenDetailTable(schema, cf); - } - }); - globalUniqueIdColumn.setKeyField(true); - addColumn(globalUniqueIdColumn); - } - else if (!"Container".equalsIgnoreCase(name)) - { - var wrappedColumn = addWrapColumn(baseColumn); - if ("RowId".equalsIgnoreCase(name) || "QualityControlFlagForced".equalsIgnoreCase(name)) - wrappedColumn.setHidden(true); - } - } - - addWrapColumn("Folder", _rootTable.getColumn("Container")); - - setTitleColumn("Comment"); - } -} \ No newline at end of file diff --git a/study/src/org/labkey/study/query/SpecimenEventTable.java b/study/src/org/labkey/study/query/SpecimenEventTable.java deleted file mode 100644 index f2c3d3ee731..00000000000 --- a/study/src/org/labkey/study/query/SpecimenEventTable.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2008-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.study.query; - -import org.labkey.api.data.ColumnInfo; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.exp.property.Domain; -import org.labkey.api.specimen.SpecimenSchema; -import org.labkey.api.specimen.model.SpecimenTablesProvider; - -public class SpecimenEventTable extends BaseStudyTable -{ - public SpecimenEventTable(StudyQuerySchema schema, ContainerFilter cf) - { - super(schema, SpecimenSchema.get().getTableInfoSpecimenEvent(schema.getContainer()), cf, true); - setName("SpecimenEvent"); - - var vid = addWrapColumn(_rootTable.getColumn("VialId")); - vid.setDisplayColumnFactory(ColumnInfo.NOWRAP_FACTORY); - addWrapColumn(_rootTable.getColumn("SpecimenNumber")); - addWrapLocationColumn("LabId", "LabId").setLabel("Location"); - addWrapColumn(_rootTable.getColumn("Stored")); - addWrapColumn(_rootTable.getColumn("StorageFlag")); - addWrapColumn(_rootTable.getColumn("StorageDate")); - addWrapColumn(_rootTable.getColumn("ShipFlag")); - addWrapColumn(_rootTable.getColumn("ShipBatchNumber")); - addWrapColumn(_rootTable.getColumn("ShipDate")); - addWrapColumn(_rootTable.getColumn("LabReceiptDate")); - addWrapColumn(_rootTable.getColumn("SpecimenCondition")); - addWrapColumn(_rootTable.getColumn("Comments")); - addWrapParticipantColumn("PTID").setKeyField(true); - addWrapColumn(_rootTable.getColumn("DrawTimestamp")); - addWrapColumn(_rootTable.getColumn("SalReceiptDate")); - addWrapTypeColumn("PrimaryType", "PrimaryTypeId"); - addWrapTypeColumn("DerivativeType", "DerivativeTypeId"); - addWrapTypeColumn("AdditiveType", "AdditiveTypeId"); - addWrapTypeColumn("DerivativeType2", "DerivativeTypeId2"); - addWrapColumn(_rootTable.getColumn("VisitValue")); - addWrapColumn(_rootTable.getColumn("VisitDescription")); - addWrapColumn(_rootTable.getColumn("ClassId")); - addWrapColumn(_rootTable.getColumn("ProtocolNumber")); - addWrapColumn(_rootTable.getColumn("Volume")); - addWrapColumn(_rootTable.getColumn("VolumeUnits")); - addWrapColumn(_rootTable.getColumn("SubAdditiveDerivative")); - addWrapLocationColumn("Clinic", "OriginatingLocationId"); - addWrapColumn(_rootTable.getColumn("FrozenTime")); - addWrapColumn(_rootTable.getColumn("ProcessingTime")); - addWrapColumn(_rootTable.getColumn("PrimaryVolume")); - addWrapColumn(_rootTable.getColumn("PrimaryVolumeUnits")); - addWrapColumn(_rootTable.getColumn("ProcessingDate")); - addWrapColumn(_rootTable.getColumn("ProcessedByInitials")); - addWrapColumn(_rootTable.getColumn("ShippedFromLab")); - addWrapColumn(_rootTable.getColumn("ShippedToLab")); - addWrapColumn(_rootTable.getColumn("ExpectedTimeValue")); - addWrapColumn(_rootTable.getColumn("ExpectedTimeUnit")); - addWrapColumn(_rootTable.getColumn("UpdateTimestamp")); - addWrapColumn(_rootTable.getColumn("TotalCellCount")); - addWrapColumn(_rootTable.getColumn("TubeType")); - addWrapColumn(_rootTable.getColumn("QualityComments")); - addWrapColumn(_rootTable.getColumn("ExternalId")).setHidden(true); - addWrapColumn(_rootTable.getColumn("OtherSpecimenId")); - addWrapColumn(_rootTable.getColumn("ParentSpecimenId")); - var obsoleteColumn = addWrapColumn(_rootTable.getColumn("Obsolete")); - obsoleteColumn.setHidden(true); - addContainerColumn(true); - - // Add optional fields - SpecimenTablesProvider specimenTablesProvider = new SpecimenTablesProvider(schema.getContainer(), null, null); - Domain specimenEventDomain = specimenTablesProvider.getDomain("SpecimenEvent", false, false); - if (null == specimenEventDomain) - throw new IllegalStateException("Expected SpecimenEvent table to already be created."); - - addOptionalColumns(specimenEventDomain.getNonBaseProperties(), false, null); - } -} diff --git a/study/src/org/labkey/study/query/SpecimenRequestStatusTable.java b/study/src/org/labkey/study/query/SpecimenRequestStatusTable.java deleted file mode 100644 index 2bd4bc469b2..00000000000 --- a/study/src/org/labkey/study/query/SpecimenRequestStatusTable.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2008-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.study.query; - -import org.labkey.api.data.ColumnInfo; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.specimen.SpecimenSchema; -import org.labkey.api.specimen.model.SpecimenTablesProvider; - -public class SpecimenRequestStatusTable extends BaseStudyTable -{ - public SpecimenRequestStatusTable(StudyQuerySchema schema, ContainerFilter cf) - { - super(schema, SpecimenSchema.get().getTableInfoSampleRequestStatus(), cf); - setName(SpecimenTablesProvider.SPECIMEN_REQUEST_STATUSES_TABLE_NAME); - for (ColumnInfo baseColumn : _rootTable.getColumns()) - { - String name = baseColumn.getName(); - if ("Container".equalsIgnoreCase(name) || "EntityId".equalsIgnoreCase(name)) - continue; - addWrapColumn(baseColumn); - } - } -} diff --git a/study/src/org/labkey/study/query/SpecimenRequestTable.java b/study/src/org/labkey/study/query/SpecimenRequestTable.java deleted file mode 100644 index b3cec057c4f..00000000000 --- a/study/src/org/labkey/study/query/SpecimenRequestTable.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2008-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.study.query; - -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.data.TableInfo; -import org.labkey.api.query.AliasedColumn; -import org.labkey.api.query.FieldKey; -import org.labkey.api.query.LookupForeignKey; -import org.labkey.api.specimen.SpecimenSchema; - -import java.util.ArrayList; -import java.util.List; - -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_REQUEST_TABLE_NAME; - -public class SpecimenRequestTable extends BaseStudyTable -{ - public SpecimenRequestTable(StudyQuerySchema schema, ContainerFilter cf) - { - super(schema, SpecimenSchema.get().getTableInfoSampleRequest(), cf); - setName(SPECIMEN_REQUEST_TABLE_NAME); - - AliasedColumn rowIdColumn = new AliasedColumn(this, "RequestId", _rootTable.getColumn("RowId")); - rowIdColumn.setKeyField(true); - addColumn(rowIdColumn); - AliasedColumn statusColumn = new AliasedColumn(this, "Status", _rootTable.getColumn("StatusId")); - statusColumn.setFk(new LookupForeignKey(null, (String) null, "RowId", "Label") - { - @Override - public TableInfo getLookupTableInfo() - { - return new SpecimenRequestStatusTable(_userSchema, cf); - } - }); - statusColumn.setKeyField(true); - addColumn(statusColumn); - - addWrapLocationColumn("Destination", "DestinationSiteId").setKeyField(true); - addWrapColumn(_rootTable.getColumn("Comments")); - // there are links to filter by 'createdby' in the UI; it's necessary that this column always - // be available, so we set it as a key field. - addWrapColumn(_rootTable.getColumn("CreatedBy")).setKeyField(true); - addWrapColumn(_rootTable.getColumn("Created")); - addWrapColumn(_rootTable.getColumn("ModifiedBy")); - addWrapColumn(_rootTable.getColumn("Modified")); - var hiddenColumn = addWrapColumn(_rootTable.getColumn("Hidden")); - hiddenColumn.setHidden(true); - hiddenColumn.setIsUnselectable(true); - - List fieldKeys = new ArrayList<>(); - fieldKeys.add(FieldKey.fromParts("RequestId")); - fieldKeys.add(FieldKey.fromParts("Status")); - fieldKeys.add(FieldKey.fromParts("Destination")); - fieldKeys.add(FieldKey.fromParts("CreatedBy")); - fieldKeys.add(FieldKey.fromParts("Created")); - setDefaultVisibleColumns(fieldKeys); - } -} diff --git a/study/src/org/labkey/study/query/SpecimenTable.java b/study/src/org/labkey/study/query/SpecimenTable.java deleted file mode 100644 index 2da34ef60bc..00000000000 --- a/study/src/org/labkey/study/query/SpecimenTable.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2013-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.study.query; - -import org.jetbrains.annotations.NotNull; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.data.ContainerManager; -import org.labkey.api.data.SQLFragment; -import org.labkey.api.query.AliasedColumn; -import org.labkey.api.security.User; -import org.labkey.api.security.permissions.ReadPermission; -import org.labkey.api.specimen.SpecimenSchema; -import org.labkey.api.study.Study; -import org.labkey.api.study.StudyService; -import org.labkey.api.study.TimepointType; -import org.labkey.study.model.StudyImpl; - -import java.util.ArrayList; -import java.util.List; - -public class SpecimenTable extends AbstractSpecimenTable -{ - private List _studySpecimenTables = null; - - public SpecimenTable(StudyQuerySchema schema, ContainerFilter cf, boolean allStudies) - { - super(schema, SpecimenSchema.get().getTableInfoSpecimen(schema.getContainer()), cf, true); - - var ptidColumn = getMutableColumn(StudyService.get().getSubjectColumnName(getContainer())); -// addWrapColumn(getRealTable().getColumn("RowId")); -// addContainerColumn(true); - if (false) // If we generate more like VialTable, then we need this - { - removeColumn(ptidColumn); - ptidColumn = new OuterAliasedColumn(this, "ParticipantId", getRealTable().getColumn("PTID")); - addColumn(ptidColumn); - addColumn(new OuterAliasedColumn(this, "Date", getRealTable().getColumn("DrawTimeStamp"))); - var aliasVisitColumn = new OuterAliasedColumn(this, "SequenceNum", _rootTable.getColumn("VisitValue")); - addSpecimenVisitColumn(TimepointType.DATE, aliasVisitColumn, true); - } - else - { -// addColumn(new AliasedColumn(this, "ParticipantId", getRealTable().getColumn("PTID"))); // Base class already does this. - addColumn(new AliasedColumn(this, "Date", getRealTable().getColumn("DrawTimeStamp"))); - addSpecimenVisitColumn(TimepointType.DATE, true); - } - ptidColumn.clearFk(); - - addSpecimenTypeColumns(); - - if (allStudies) - { - _studySpecimenTables = new ArrayList<>(); - assert null != getUserSchema(); - User user = getUserSchema().getUser(); - for (Study study : StudyService.get().getAllStudies(ContainerManager.getRoot(), user)) - { - // Commented out lines are for the lie VialTable alternative -// if (study.getContainer().hasPermission(user, ReadPermission.class)) - if (study.getContainer().hasPermission(user, ReadPermission.class) && study.getContainer().getId() != getContainer().getId()) - { - StudyQuerySchema studyQuerySchema = StudyQuerySchema.createSchema((StudyImpl)study, user); - SpecimenTable table = new SpecimenTable(studyQuerySchema, null, false); - _studySpecimenTables.add(table); - } - } -// _unionColumns = getUnionColumns(_studySpecimenTables); - } - } - - @NotNull - @Override - public SQLFragment getFromSQL(String alias) - { - checkReadBeforeExecute(); - SQLFragment sql = new SQLFragment(); - if (null != _studySpecimenTables) - { - for (SpecimenTable table : _studySpecimenTables) - sql.unsafeAppend("(").append(table.getFromSQL("")).unsafeAppend(" UNION "); - } - - sql.unsafeAppend("\n SELECT participantsequencenum, additivetypeid, firstprocessedbyinitials, \n") - .unsafeAppend(" volumeunits, visitdescription, specimenhash, expectedavailablecount, \n") - .unsafeAppend(" visitvalue, derivativetypeid, rowid, totalvolume, derivativetypeid2, \n") - .unsafeAppend(" drawtimestamp, vialcount, availablevolume, ptid, primarytypeid, \n") - .unsafeAppend(" originatinglocationid, atrepositorycount, processinglocation, \n") - .unsafeAppend(" lockedinrequestcount, subadditivederivative, salreceiptdate, \n") - .unsafeAppend(" availablecount, classid, protocolnumber,\n") - .unsafeAppend(" ").unsafeAppend("CAST (").appendValue(getContainer()).unsafeAppend(" AS ") - .append(getSchema().getSqlDialect().getGuidTypeAsSql()).unsafeAppend(") AS Container"); - - sql.unsafeAppend("\n FROM ").append(getRealTable()); - if (null != _studySpecimenTables) - { - for (SpecimenTable table : _studySpecimenTables) - sql.unsafeAppend(") "); - } - sql.appendIdentifier(alias); - return sql; - } - - @Override - public boolean hasUnionTable() - { - return true; - } -} diff --git a/study/src/org/labkey/study/query/SpecimenVialCountTable.java b/study/src/org/labkey/study/query/SpecimenVialCountTable.java deleted file mode 100644 index 339a510fdf5..00000000000 --- a/study/src/org/labkey/study/query/SpecimenVialCountTable.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2009-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.study.query; - -import org.jetbrains.annotations.NotNull; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.data.JdbcType; -import org.labkey.api.data.SQLFragment; -import org.labkey.api.data.TableInfo; -import org.labkey.api.query.ExprColumn; -import org.labkey.api.specimen.SpecimenMigrationService; -import org.labkey.api.specimen.SpecimenSchema; - -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_VIAL_COUNT_TABLE_NAME; - -public class SpecimenVialCountTable extends BaseStudyTable -{ - public SpecimenVialCountTable(StudyQuerySchema schema, ContainerFilter cf) - { -/* super(schema, StudySchema.getInstance().getTableInfoSpecimenVialCount()); - - addWrapColumn(_rootTable.getColumn("Container")).setHidden(true); - addWrapColumn(_rootTable.getColumn("SpecimenHash")).setHidden(true); - - addColumn(new AliasedColumn(this, "TotalCount", _rootTable.getColumn("VialCount"))); - - boolean enableSpecimenRequest = SampleManager.getInstance().getRepositorySettings(getContainer()).isEnableRequests(); - - addColumn(new AliasedColumn(this, "LockedInRequest", _rootTable.getColumn("LockedInRequestCount"))).setHidden(!enableSpecimenRequest); - addColumn(new AliasedColumn(this, "AtRepository", _rootTable.getColumn("AtRepositoryCount"))); - addColumn(new AliasedColumn(this, "Available", _rootTable.getColumn("AvailableCount"))).setHidden(!enableSpecimenRequest); - addColumn(new AliasedColumn(this, "ExpectedAvailable", _rootTable.getColumn("ExpectedAvailableCount"))).setHidden(!enableSpecimenRequest); -*/ - super(schema, SpecimenSchema.get().getTableInfoVial(schema.getContainer()), cf); - setName(SPECIMEN_VIAL_COUNT_TABLE_NAME); - setTitle("VialCounts"); - - addContainerColumn(true).setHidden(true); - addWrapColumn(_rootTable.getColumn("SpecimenHash")).setHidden(true); - - addColumn(new ExprColumn(this, "TotalCount", SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS + ".VialCount"), JdbcType.INTEGER)); - - SpecimenMigrationService sms = SpecimenMigrationService.get(); - boolean enableSpecimenRequest = sms != null && sms.isEnableRequests(getContainer()); - - addColumn(new ExprColumn(this, "LockedInRequest", SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS + ".LockedInRequestCount"), JdbcType.INTEGER)).setHidden(!enableSpecimenRequest); - addColumn(new ExprColumn(this, "AtRepository", SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS + ".AtRepositoryCount"), JdbcType.INTEGER)); - addColumn(new ExprColumn(this, "Available", SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS + ".AvailableCount"), JdbcType.INTEGER)).setHidden(!enableSpecimenRequest); - addColumn(new ExprColumn(this, "ExpectedAvailable", SQLFragment.unsafe(ExprColumn.STR_TABLE_ALIAS + ".ExpectedAvailableCount"), JdbcType.INTEGER)).setHidden(!enableSpecimenRequest); - } - - @NotNull - @Override - public SQLFragment getFromSQL(String alias) - { - checkReadBeforeExecute(); - TableInfo tableInfoVial = SpecimenSchema.get().getTableInfoVial(getContainer()); - if (null == tableInfoVial) - throw new IllegalStateException("Vial table not found."); - - SQLFragment sql = SQLFragment.unsafe("(SELECT ? As Container, Vial.specimenhash, sum(Vial.volume) AS totalvolume,\n" + - " sum(CASE Vial.available\n" + - " WHEN ? THEN Vial.volume\n" + - " ELSE 0\n" + - " END) AS availablevolume, count(Vial.globaluniqueid) AS vialcount, sum(\n" + - " CASE Vial.lockedinrequest\n" + - " WHEN ? THEN 1\n" + - " ELSE 0\n" + - " END) AS lockedinrequestcount, sum(\n" + - " CASE Vial.atrepository\n" + - " WHEN ? THEN 1\n" + - " ELSE 0\n" + - " END) AS atrepositorycount, sum(\n" + - " CASE Vial.available\n" + - " WHEN ? THEN 1\n" + - " ELSE 0\n" + - " END) AS availablecount, count(Vial.globaluniqueid) - sum(\n" + - " CASE \n" + - " CASE Vial.lockedinrequest\n" + - " WHEN ? THEN 1\n" + - " ELSE 0\n" + - " END | \n" + - " CASE Vial.requestable\n" + - " WHEN ? THEN 1\n" + - " ELSE 0\n" + - " END\n" + - " WHEN 1 THEN 1\n" + - " ELSE 0\n" + - " END) AS expectedavailablecount\n" + - " FROM "); - sql.add(getContainer()); - sql.add(Boolean.TRUE); - sql.add(Boolean.TRUE); - sql.add(Boolean.TRUE); - sql.add(Boolean.TRUE); - sql.add(Boolean.TRUE); - sql.add(Boolean.FALSE); - sql.append(tableInfoVial.getFromSQL("Vial")) - .unsafeAppend("\n GROUP BY Vial.specimenhash) ") - .appendIdentifier(alias); - return sql; - } -} diff --git a/study/src/org/labkey/study/query/StudyQuerySchema.java b/study/src/org/labkey/study/query/StudyQuerySchema.java index 3a8eca7ba04..28a202ef6e7 100644 --- a/study/src/org/labkey/study/query/StudyQuerySchema.java +++ b/study/src/org/labkey/study/query/StudyQuerySchema.java @@ -51,6 +51,7 @@ import org.labkey.api.study.StudyService; import org.labkey.api.study.TimepointType; import org.labkey.api.study.model.ParticipantGroup; +import org.labkey.api.study.query.AbstractStudyQuerySchema; import org.labkey.api.study.writer.AbstractStudyContext; import org.labkey.api.studydesign.query.StudyDesignQuerySchema; import org.labkey.api.view.ActionURL; @@ -92,18 +93,12 @@ import static org.labkey.api.specimen.model.SpecimenTablesProvider.LOCATION_SPECIMEN_LIST_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.PRIMARY_TYPE_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.SIMPLE_SPECIMEN_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_COMMENTS_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_DETAIL_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_EVENT_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_REQUEST_STATUSES_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_REQUEST_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_SUMMARY_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_VIAL_COUNT_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.SPECIMEN_WRAP_TABLE_NAME; -import static org.labkey.api.specimen.model.SpecimenTablesProvider.VIAL_REQUEST_TABLE_NAME; import static org.labkey.api.specimen.model.SpecimenTablesProvider.VIAL_TABLE_NAME; -public class StudyQuerySchema extends UserSchema implements UserSchema.HasContextualRoles, VisualizationSchema +public class StudyQuerySchema extends AbstractStudyQuerySchema implements VisualizationSchema { public static final String EXPERIMENTAL_STUDY_SUBSCHEMAS = "StudySubSchemas"; @@ -171,7 +166,7 @@ private StudyQuerySchema(@Nullable StudyImpl study, @NotNull Container c, @NotNu */ protected StudyQuerySchema(SchemaKey path, String description, @Nullable StudyImpl study, @NotNull Container c, @NotNull User user, @Nullable Role contextualRole) { - super(path, description, user, c, StudySchema.getInstance().getSchema(), null); + super(path, description, user, c, StudySchema.getInstance().getSchema()); _study = study; if (null != contextualRole && ReaderRole.class != contextualRole.getClass()) throw new IllegalStateException("Only ReaderRole is currently supported"); @@ -335,23 +330,16 @@ protected void addSpecimenTables(Set names) { if (SpecimenManager.get().areSpecimenTablesViewable(getContainer())) { - names.add(SPECIMEN_EVENT_TABLE_NAME); names.add(SPECIMEN_DETAIL_TABLE_NAME); names.add(SPECIMEN_SUMMARY_TABLE_NAME); - names.add(SPECIMEN_VIAL_COUNT_TABLE_NAME); names.add(SIMPLE_SPECIMEN_TABLE_NAME); - names.add(SPECIMEN_REQUEST_TABLE_NAME); - names.add(SPECIMEN_REQUEST_STATUSES_TABLE_NAME); - names.add(VIAL_REQUEST_TABLE_NAME); names.add(ADDITIVE_TYPE_TABLE_NAME); names.add(DERIVATIVE_TYPE_TABLE_NAME); names.add(PRIMARY_TYPE_TABLE_NAME); - names.add(SPECIMEN_COMMENTS_TABLE_NAME); - // specimen report pivots SpecimenMigrationService sms = SpecimenMigrationService.get(); if (sms != null) - sms.addSpecimenPivotTableNames(names); + sms.addSpecimenTableNames(names); names.add(LOCATION_SPECIMEN_LIST_TABLE_NAME); } @@ -543,28 +531,10 @@ public TableInfo createTable(String name, ContainerFilter cf) return new SpecimenWrapTable(this, cf); } - if (SPECIMEN_VIAL_COUNT_TABLE_NAME.equalsIgnoreCase(name)) - { - return new SpecimenVialCountTable(this, cf); - } - if (SPECIMEN_EVENT_TABLE_NAME.equalsIgnoreCase(name)) - { - if (getContainer().isRoot()) - return null; - return new SpecimenEventTable(this, cf); - } if ((studyService.getSubjectVisitTableName(getContainer()).equalsIgnoreCase(name) || "ParticipantVisit".equalsIgnoreCase(name)) && _study.getTimepointType() != TimepointType.CONTINUOUS) { return new ParticipantVisitTable(this, cf, false); } - if (SPECIMEN_REQUEST_TABLE_NAME.equalsIgnoreCase(name)) - { - return new SpecimenRequestTable(this, cf); - } - if (SPECIMEN_REQUEST_STATUSES_TABLE_NAME.equalsIgnoreCase(name)) - { - return new SpecimenRequestStatusTable(this, cf); - } if (VISIT_TABLE_NAME.equalsIgnoreCase(name) && _study.getTimepointType() != TimepointType.CONTINUOUS) { return new VisitTable(this, cf); @@ -607,14 +577,6 @@ public TableInfo createTable(String name, ContainerFilter cf) return new PrimaryTypeTable(this, cf); } - if (SPECIMEN_COMMENTS_TABLE_NAME.equalsIgnoreCase(name)) - { - return new SpecimenCommentTable(this, cf); - } - if (VIAL_REQUEST_TABLE_NAME.equalsIgnoreCase(name)) - { - return new VialRequestTable(this, cf); - } if (STUDY_SNAPSHOT_TABLE_NAME.equalsIgnoreCase(name)) { return new StudySnapshotTable(this, cf); @@ -622,9 +584,9 @@ public TableInfo createTable(String name, ContainerFilter cf) SpecimenMigrationService sms = SpecimenMigrationService.get(); if (sms != null) { - TableInfo pivot = sms.getSpecimenPivotTable(this, name, _study, cf); - if (pivot != null) - return pivot; + TableInfo specimenTable = sms.getSpecimenTable(this, name, cf); + if (specimenTable != null) + return specimenTable; } if (LOCATION_SPECIMEN_LIST_TABLE_NAME.equalsIgnoreCase(name)) { @@ -670,8 +632,6 @@ public TableInfo getUnionTable(TableInfo tableInfo, Set containers) if (1 == containers.size() && getContainer() == containers.toArray()[0]) return tableInfo; // No need to union 1 table - if (SpecimenTable.class == tableInfo.getClass()) - return studyService.getSpecimenTableUnion(this, containers, new HashMap<>(), _dontAliasColumns, false); if (VialTable.class == tableInfo.getClass()) return studyService.getVialTableUnion(this, containers); if (SpecimenDetailTable.class == tableInfo.getClass()) @@ -820,12 +780,40 @@ public QueryView createView(ViewContext context, @NotNull QuerySettings settings return super.createView(context, settings, errors); } + @Override @Nullable public StudyImpl getStudy() { return _study; } + @Override + public TableInfo createLocationLookupTable(@Nullable ContainerFilter cf) + { + return new LocationTable(this, cf); + } + + @Override + public TableInfo createSpecimenTypeLookupTable(String rootTableColumnName, ContainerFilter cf) + { + BaseStudyTable result = switch (rootTableColumnName) + { + case "PrimaryTypeId" -> new PrimaryTypeTable(this, cf); + case "DerivativeTypeId", "DerivativeTypeId2" -> new DerivativeTypeTable(this, cf); + case "AdditiveTypeId" -> new AdditiveTypeTable(this, cf); + default -> throw new IllegalStateException(rootTableColumnName + " is not recognized as a valid specimen type column."); + }; + if (allowSetContainerFilter()) + result.setContainerFilter(cf); + return result; + } + + @Override + public TableInfo createSpecimenDetailLookupTable(@Nullable ContainerFilter cf) + { + return new SpecimenDetailTable(this, cf); + } + public String decideTableName(DatasetDefinition dsd) { return dsd.getName(); @@ -903,12 +891,6 @@ public void setDontAliasColumns(boolean dontAliasColumns) } - /** for tables that support container filter, should they turn on support or not */ - public boolean allowSetContainerFilter() - { - return true; - } - protected void initSessionParticipantGroup(Study study, User user) { if (study == null) diff --git a/study/src/org/labkey/study/query/VialRequestTable.java b/study/src/org/labkey/study/query/VialRequestTable.java deleted file mode 100644 index ab301a8da7d..00000000000 --- a/study/src/org/labkey/study/query/VialRequestTable.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2009-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.study.query; - -import org.labkey.api.data.ColumnInfo; -import org.labkey.api.data.ContainerFilter; -import org.labkey.api.data.DataColumn; -import org.labkey.api.data.DisplayColumn; -import org.labkey.api.data.DisplayColumnFactory; -import org.labkey.api.data.TableInfo; -import org.labkey.api.query.AliasedColumn; -import org.labkey.api.query.FilteredTable; -import org.labkey.api.query.LookupForeignKey; -import org.labkey.api.specimen.SpecimenSchema; - -import static org.labkey.api.specimen.model.SpecimenTablesProvider.VIAL_REQUEST_TABLE_NAME; - -public class VialRequestTable extends FilteredTable -{ - public VialRequestTable(final StudyQuerySchema schema, ContainerFilter cf) - { - super(SpecimenSchema.get().getTableInfoSampleRequestSpecimen(), schema, cf); - setName(VIAL_REQUEST_TABLE_NAME); - - for (ColumnInfo baseColumn : _rootTable.getColumns()) - { - String name = baseColumn.getName(); - if ("SpecimenGlobalUniqueId".equalsIgnoreCase(name)) - { - AliasedColumn globalUniqueIdColumn = new AliasedColumn(this, "Vial", _rootTable.getColumn("SpecimenGlobalUniqueId")) - { - @Override - public ColumnInfo getDisplayField() - { - return null; - } - }; - LookupForeignKey fk = new LookupForeignKey(null, (String) null, "GlobalUniqueId", "GlobalUniqueId") - { - @Override - public TableInfo getLookupTableInfo() - { - return new SpecimenDetailTable(schema, cf); - } - }; - fk.setPrefixColumnCaption(false); - globalUniqueIdColumn.setFk(fk); - globalUniqueIdColumn.setKeyField(true); - globalUniqueIdColumn.setDisplayColumnFactory(new DisplayColumnFactory() - { - @Override - public DisplayColumn createRenderer(ColumnInfo colInfo) - { - return new DataColumn(colInfo, false); - } - }); - - addColumn(globalUniqueIdColumn); - } - else if ("SampleRequestId".equalsIgnoreCase(name)) - { - AliasedColumn requestIdColumn = new AliasedColumn(this, "Request", _rootTable.getColumn("SampleRequestId")); - LookupForeignKey fk = new LookupForeignKey(null, (String) null, "RequestId", "RequestId") - { - @Override - public TableInfo getLookupTableInfo() - { - return new SpecimenRequestTable(schema, cf); - } - }; - fk.setPrefixColumnCaption(false); - requestIdColumn.setFk(fk); - requestIdColumn.setKeyField(true); - addColumn(requestIdColumn); - } - else if (!"Container".equalsIgnoreCase(name)) - { - var wrappedColumn = addWrapColumn(baseColumn); - if ("RowId".equalsIgnoreCase(name)) - wrappedColumn.setHidden(true); - } - } - } -} From f6108f1a5b8d0c224533a27ceebc716257907827 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Mon, 23 Nov 2026 13:24:59 -0800 Subject: [PATCH 2/3] Remove the view specimen tables optional feature --- .../org/labkey/api/specimen/SpecimenManager.java | 15 --------------- study/src/org/labkey/study/StudyModule.java | 11 ----------- .../org/labkey/study/query/StudyQuerySchema.java | 7 +++---- 3 files changed, 3 insertions(+), 30 deletions(-) diff --git a/study/api-src/org/labkey/api/specimen/SpecimenManager.java b/study/api-src/org/labkey/api/specimen/SpecimenManager.java index 8e80d7f6caa..0f418d41682 100644 --- a/study/api-src/org/labkey/api/specimen/SpecimenManager.java +++ b/study/api-src/org/labkey/api/specimen/SpecimenManager.java @@ -30,7 +30,6 @@ import org.labkey.api.module.Module; import org.labkey.api.module.ModuleLoader; import org.labkey.api.query.FieldKey; -import org.labkey.api.settings.OptionalFeatureService; import org.labkey.api.specimen.model.SpecimenComment; import org.labkey.api.study.Study; import org.labkey.api.study.StudyService; @@ -46,8 +45,6 @@ public class SpecimenManager { private final static SpecimenManager INSTANCE = new SpecimenManager(); - public static final String VIEW_SPECIMEN_TABLES_FLAG = "viewSpecimenTables"; - private SpecimenManager() { } @@ -63,18 +60,6 @@ public boolean isSpecimenModuleActive(Container c) return null != specimenModule && c.getActiveModules().contains(specimenModule); } - // Include specimen queries in the study schema if the specimen module is active or, when it isn't deployed, an admin - // has chosen to expose them via an optional feature - public boolean areSpecimenTablesViewable(Container c) - { - Module specimenModule = ModuleLoader.getInstance().getModule("Specimen"); - if (null != specimenModule) - return c.getActiveModules().contains(specimenModule); - - // The flag is registered only when the specimen module is absent, so a value saved before deploying it would otherwise linger with no UI to clear it - return OptionalFeatureService.get().isFeatureEnabled(VIEW_SPECIMEN_TABLES_FLAG); - } - public int getSpecimenCountForVisit(Visit visit) { Container container = visit.getContainer(); diff --git a/study/src/org/labkey/study/StudyModule.java b/study/src/org/labkey/study/StudyModule.java index 9cb14b18d0d..a45733b930d 100644 --- a/study/src/org/labkey/study/StudyModule.java +++ b/study/src/org/labkey/study/StudyModule.java @@ -71,9 +71,7 @@ import org.labkey.api.security.roles.ReaderRole; import org.labkey.api.security.roles.RoleManager; import org.labkey.api.services.ServiceRegistry; -import org.labkey.api.settings.OptionalFeatureFlag; import org.labkey.api.settings.OptionalFeatureService; -import org.labkey.api.specimen.SpecimenManager; import org.labkey.api.specimen.model.AdditiveTypeDomainKind; import org.labkey.api.specimen.model.DerivativeTypeDomainKind; import org.labkey.api.specimen.model.LocationDomainKind; @@ -403,15 +401,6 @@ protected void startupAfterSpringConfig(ModuleContext moduleContext) "Allow unprovisioned, query-based dataset snapshots to be created.", false); - if (SpecimenService.get() == null) - { - OptionalFeatureService.get().addFeatureFlag(new OptionalFeatureFlag(SpecimenManager.VIEW_SPECIMEN_TABLES_FLAG, - "Show read-only specimen tables in the study schema", - "Provides a read-only view of specimen data when the specimen module is not present.", - false, false, OptionalFeatureService.FeatureType.Optional - )); - } - ReportAndDatasetChangeDigestProvider.get().addNotificationInfoProvider(new DatasetNotificationInfoProvider()); AdminLinkManager.getInstance().addListener((adminNavTree, container, user) -> { diff --git a/study/src/org/labkey/study/query/StudyQuerySchema.java b/study/src/org/labkey/study/query/StudyQuerySchema.java index 28a202ef6e7..8492b62f6f9 100644 --- a/study/src/org/labkey/study/query/StudyQuerySchema.java +++ b/study/src/org/labkey/study/query/StudyQuerySchema.java @@ -34,7 +34,6 @@ import org.labkey.api.query.QuerySettings; import org.labkey.api.query.QueryView; import org.labkey.api.query.SchemaKey; -import org.labkey.api.query.UserSchema; import org.labkey.api.security.SecurityLogger; import org.labkey.api.security.User; import org.labkey.api.security.permissions.AdminPermission; @@ -209,7 +208,7 @@ protected Set getSubSchemaNames() if (!OptionalFeatureService.get().isFeatureEnabled(EXPERIMENTAL_STUDY_SUBSCHEMAS)) return Set.of(); var ret = new LinkedHashSet<>(Arrays.asList(DATASETS_SCHEMA_NAME, DESIGN_SCHEMA_NAME)); - if (SpecimenManager.get().areSpecimenTablesViewable(getContainer())) + if (SpecimenManager.get().isSpecimenModuleActive(getContainer())) ret.add(SPECIMENS_SCHEMA_NAME); return ret; } @@ -221,7 +220,7 @@ public QuerySchema getSchema(String name) return new DatasetSchema(this); if (Strings.CI.equals(DESIGN_SCHEMA_NAME, name)) return new DesignSchema(this); - if (Strings.CI.equals(SPECIMENS_SCHEMA_NAME, name) && SpecimenManager.get().areSpecimenTablesViewable(getContainer())) + if (Strings.CI.equals(SPECIMENS_SCHEMA_NAME, name) && SpecimenManager.get().isSpecimenModuleActive(getContainer())) return new SpecimenSchema(this); return super.getSchema(name); } @@ -328,7 +327,7 @@ public Set getTableNames() protected void addSpecimenTables(Set names) { - if (SpecimenManager.get().areSpecimenTablesViewable(getContainer())) + if (SpecimenManager.get().isSpecimenModuleActive(getContainer())) { names.add(SPECIMEN_DETAIL_TABLE_NAME); names.add(SPECIMEN_SUMMARY_TABLE_NAME); From 7175ecd95372a4f1f75bb5221c43c46f67fbe5ba Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Mon, 23 Nov 2026 19:46:15 -0800 Subject: [PATCH 3/3] Keep the specimen sample type domain kind in study and add specimen data metrics Servers without the specimen module still hold specimen data that the domain kind protects; the metrics identify those servers. --- .../SpecimenSampleTypeDomainKind.java | 92 +++++++++++++++++++ study/src/org/labkey/study/StudyModule.java | 13 ++- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java diff --git a/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java b/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java new file mode 100644 index 00000000000..9cc035c817a --- /dev/null +++ b/study/api-src/org/labkey/api/specimen/SpecimenSampleTypeDomainKind.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2019-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.specimen; + +import org.labkey.api.data.Container; +import org.labkey.api.exp.Lsid; +import org.labkey.api.exp.property.Domain; +import org.labkey.api.security.User; +import org.labkey.api.study.SpecimenService; +import org.labkey.api.view.ActionURL; +import org.labkey.api.writer.ContainerUser; +import org.labkey.api.exp.api.SampleTypeDomainKind; + +public class SpecimenSampleTypeDomainKind extends SampleTypeDomainKind +{ + public SpecimenSampleTypeDomainKind() + { + } + + @Override + public Priority getPriority(String domainURI) + { + Lsid lsid = new Lsid(domainURI); + String prefix = lsid.getNamespacePrefix(); + String name = lsid.getObjectId(); + if ("SampleSet".equals(prefix) && SpecimenService.SAMPLE_TYPE_NAME.equals(name)) + return Priority.HIGH; + return null; + } + + @Override + public String getStorageSchemaName() + { + // Don't provision table with no properties + return null; + } + + @Override + public ActionURL urlEditDefinition(Domain domain, ContainerUser containerUser) + { + return null; + } + + @Override + public boolean canEditDefinition(User user, Domain domain) + { + return false; + } + + @Override + public boolean canCreateDefinition(User user, Container container) + { + return false; + } + + @Override + public boolean canDeleteDefinition(User user, Domain domain) + { + return false; + } + + @Override + public ActionURL urlCreateDefinition(String schemaName, String queryName, Container container, User user) + { + return null; + } + + @Override + public boolean allowUniqueConstraintProperties() + { + return false; + } + + @Override + public boolean allowCalculatedFields() + { + return false; + } +} diff --git a/study/src/org/labkey/study/StudyModule.java b/study/src/org/labkey/study/StudyModule.java index a45733b930d..e3512b1d071 100644 --- a/study/src/org/labkey/study/StudyModule.java +++ b/study/src/org/labkey/study/StudyModule.java @@ -34,6 +34,7 @@ import org.labkey.api.data.DbSchemaType; import org.labkey.api.data.PropertySchema; import org.labkey.api.data.SqlExecutor; +import org.labkey.api.data.SQLFragment; import org.labkey.api.data.SqlSelector; import org.labkey.api.data.TableInfo; import org.labkey.api.data.TableSelector; @@ -72,6 +73,7 @@ import org.labkey.api.security.roles.RoleManager; import org.labkey.api.services.ServiceRegistry; import org.labkey.api.settings.OptionalFeatureService; +import org.labkey.api.specimen.SpecimenSampleTypeDomainKind; import org.labkey.api.specimen.model.AdditiveTypeDomainKind; import org.labkey.api.specimen.model.DerivativeTypeDomainKind; import org.labkey.api.specimen.model.LocationDomainKind; @@ -265,6 +267,7 @@ protected void init() PropertyService.get().registerDomainKind(new SpecimenEventDomainKind()); PropertyService.get().registerDomainKind(new VialDomainKind()); PropertyService.get().registerDomainKind(new LocationDomainKind()); + PropertyService.get().registerDomainKind(new SpecimenSampleTypeDomainKind()); // study design domains PropertyService.get().registerDomainKind(new StudyProductDomainKind()); @@ -439,6 +442,12 @@ protected void startupAfterSpringConfig(ModuleContext moduleContext) metrics.put("datasetsLinkedFromAssays", () -> new SqlSelector(StudySchema.getInstance().getSchema(), "SELECT COUNT(*) FROM study.Dataset WHERE PublishSourceType = 'Assay'").getObject(Long.class), "Datasets linked from assay results; same value as linkedAssayDatasetCount"); metrics.put("datasetsLinkedFromSamples", () -> new SqlSelector(StudySchema.getInstance().getSchema(), "SELECT COUNT(*) FROM study.Dataset WHERE PublishSourceType = 'SampleType'").getObject(Long.class), "Datasets linked from sample types; same value as linkedSampleTypeDatasetCount"); + // Counted here, not in the specimen module, to find servers that hold specimen data without deploying that module + SQLFragment specimenVials = new SQLFragment("FROM exp.Material WHERE CpasType = ? OR CpasType IN (SELECT LSID FROM exp.MaterialSource WHERE Name = ?)", + StudyService.SPECIMEN_NAMESPACE_PREFIX, SpecimenService.SAMPLE_TYPE_NAME); + metrics.put("specimenContainerCount", () -> new SqlSelector(StudySchema.getInstance().getSchema(), new SQLFragment("SELECT COUNT(DISTINCT Container) ").append(specimenVials)).getObject(Long.class), "Containers holding imported specimen vials, whether or not the specimen module is deployed"); + metrics.put("specimenVialCount", () -> new SqlSelector(StudySchema.getInstance().getSchema(), new SQLFragment("SELECT COUNT(*) ").append(specimenVials)).getObject(Long.class), "Imported specimen vials across the server, whether or not the specimen module is deployed"); + metrics.put("assayScheduleCount", () -> new SqlSelector(StudySchema.getInstance().getSchema(), "SELECT COUNT(DISTINCT container) FROM study.AssaySpecimen").getObject(Long.class), "Containers with at least one assay schedule entry"); metrics.put("alternateParticipantIdCount", () -> new SqlSelector(StudySchema.getInstance().getSchema(), "SELECT COUNT(*) FROM study.Study WHERE AlternateIdPrefix IS NOT NULL").getObject(Long.class), "Studies with an alternate participant ID prefix configured"); @@ -827,7 +836,9 @@ public void testUsageMetrics() List metricNames = List.of( "studyCount", "datasetCount", - "timepointType" + "timepointType", + "specimenContainerCount", + "specimenVialCount" ); assertTrue("Mothership report missing expected metrics", UsageReportingLevel.MothershipReportTestHelper.getModuleMetrics(UsageReportingLevel.ON, MODULE_NAME)