|
1 | 1 | package org.labkey.studies.query; |
2 | 2 |
|
3 | 3 | import org.apache.commons.collections4.MultiValuedMap; |
4 | | -import org.apache.commons.collections4.SetValuedMap; |
5 | 4 | import org.apache.logging.log4j.Logger; |
6 | 5 | import org.labkey.api.collections.CaseInsensitiveHashMap; |
7 | | - |
8 | 6 | import org.labkey.api.collections.CaseInsensitiveKeyedHashSetValuedMap; |
9 | 7 | import org.labkey.api.data.AbstractTableInfo; |
| 8 | +import org.labkey.api.data.BaseColumnInfo; |
| 9 | +import org.labkey.api.data.ColumnInfo; |
| 10 | +import org.labkey.api.data.Container; |
| 11 | +import org.labkey.api.data.MutableColumnInfo; |
10 | 12 | import org.labkey.api.data.TableCustomizer; |
11 | 13 | import org.labkey.api.data.TableInfo; |
12 | 14 | import org.labkey.api.ldk.LDKService; |
| 15 | +import org.labkey.api.query.ExprColumn; |
| 16 | +import org.labkey.api.query.FieldKey; |
| 17 | +import org.labkey.api.query.LookupForeignKey; |
| 18 | +import org.labkey.api.query.QueryDefinition; |
| 19 | +import org.labkey.api.query.QueryException; |
| 20 | +import org.labkey.api.query.QueryService; |
| 21 | +import org.labkey.api.query.UserSchema; |
13 | 22 | import org.labkey.api.study.DatasetTable; |
| 23 | +import org.labkey.api.study.Study; |
| 24 | +import org.labkey.api.study.StudyService; |
14 | 25 | import org.labkey.api.util.logging.LogHelper; |
| 26 | +import org.labkey.studies.StudiesServiceImpl; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
15 | 30 |
|
16 | 31 | public class StudiesTableCustomizer implements TableCustomizer |
17 | 32 | { |
@@ -61,7 +76,148 @@ else if (tableInfo instanceof DatasetTable ds) |
61 | 76 | private void doCustomize(AbstractTableInfo ati) |
62 | 77 | { |
63 | 78 | // TODO: |
64 | | - // Overlapping studies/cohorts |
65 | 79 | // TimepointLabel |
| 80 | + |
| 81 | + addProjectAssignmentColumns(ati); |
| 82 | + } |
| 83 | + |
| 84 | + private String getSubjectColName(Container c) |
| 85 | + { |
| 86 | + Study s = StudyService.get().getStudy(c.isWorkbookOrTab() ? c.getParent() : c); |
| 87 | + if (s == null) |
| 88 | + { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return s.getSubjectColumnName(); |
| 93 | + } |
| 94 | + |
| 95 | + private void addProjectAssignmentColumns(AbstractTableInfo ati) |
| 96 | + { |
| 97 | + final String pivotColName = "allProjectsPivot"; |
| 98 | + if (ati.getColumn(pivotColName) != null) |
| 99 | + return; |
| 100 | + |
| 101 | + List<ColumnInfo> pks = ati.getPkColumns(); |
| 102 | + ColumnInfo pk; |
| 103 | + if (pks.size() == 1) |
| 104 | + { |
| 105 | + pk = pks.get(0); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + if (! (ati instanceof DatasetTable)) |
| 110 | + { |
| 111 | + _log.error("Table does not have a single PK column: " + ati.getName()); |
| 112 | + return; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + pk = pks.get(0); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (!StudiesServiceImpl.get().hasAssignmentDataset(ati.getUserSchema().getContainer())) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + final String subjectSelectName = getSubjectColName(ati.getUserSchema().getContainer()); |
| 126 | + if (subjectSelectName == null) |
| 127 | + { |
| 128 | + _log.error("Unable to find subjectSelectName in StudiesTableCustomizer"); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + final String pkColSelectName = pk.getFieldKey().toSQLString(); |
| 133 | + |
| 134 | + final String lookupName = ati.getName() + "_allProjectsPivot"; |
| 135 | + BaseColumnInfo col2 = new ExprColumn(ati, FieldKey.fromString(pivotColName), pk.getValueSql(ExprColumn.STR_TABLE_ALIAS), pk.getJdbcType(), pk); |
| 136 | + col2.setLabel("Assignment By Study"); |
| 137 | + col2.setName(pivotColName); |
| 138 | + col2.setCalculated(true); |
| 139 | + col2.setShownInInsertView(false); |
| 140 | + col2.setShownInUpdateView(false); |
| 141 | + col2.setDescription("Shows groups to which this subject belonged at any point in time."); |
| 142 | + col2.setHidden(true); |
| 143 | + col2.setReadOnly(true); |
| 144 | + col2.setIsUnselectable(true); |
| 145 | + col2.setUserEditable(false); |
| 146 | + col2.setKeyField(false); |
| 147 | + col2.setFk(new LookupForeignKey(){ |
| 148 | + @Override |
| 149 | + public TableInfo getLookupTableInfo() |
| 150 | + { |
| 151 | + final UserSchema us = ati.getUserSchema(); |
| 152 | + Container target = us.getContainer().isWorkbookOrTab() ? us.getContainer().getParent() : us.getContainer(); |
| 153 | + QueryDefinition qd = createQueryDef(us, lookupName); |
| 154 | + |
| 155 | + qd.setSql(getAssignmentPivotSql(target, ati, pkColSelectName, subjectSelectName)); |
| 156 | + qd.setIsTemporary(true); |
| 157 | + |
| 158 | + List<QueryException> errors = new ArrayList<>(); |
| 159 | + TableInfo ti = qd.getTable(errors, true); |
| 160 | + |
| 161 | + if (!errors.isEmpty()){ |
| 162 | + _log.error("Problem with table customizer: " + ati.getPublicName()); |
| 163 | + for (QueryException e : errors) |
| 164 | + { |
| 165 | + _log.error(e.getMessage()); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (ti != null) |
| 170 | + { |
| 171 | + MutableColumnInfo col = (MutableColumnInfo) ti.getColumn(pk.getName()); |
| 172 | + col.setKeyField(true); |
| 173 | + col.setHidden(true); |
| 174 | + |
| 175 | + ((MutableColumnInfo)ti.getColumn("lastStartDate")).setLabel("Most Recent Assignment Date"); |
| 176 | + } |
| 177 | + |
| 178 | + return ti; |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + ati.addColumn(col2); |
| 183 | + } |
| 184 | + |
| 185 | + private String getAssignmentPivotSql(Container source, final AbstractTableInfo ati, String pkColSelectName, String subjectSelectName) |
| 186 | + { |
| 187 | + return "SELECT\n" + |
| 188 | + "s." + pkColSelectName + ",\n" + |
| 189 | + "p.study,\n" + |
| 190 | + "max(p.date) as lastStartDate\n" + |
| 191 | + "\n" + |
| 192 | + "FROM " + ati.getPublicSchemaName() + "." + ati.getPublicName() + " s\n" + |
| 193 | + "JOIN \"" + source.getPath() + "\".study.assignment p\n" + |
| 194 | + "ON (s." + subjectSelectName + " = p." + subjectSelectName + ")\n" + |
| 195 | + "WHERE s." + subjectSelectName + " IS NOT NULL\n" + |
| 196 | + "\n" + |
| 197 | + "GROUP BY s." + pkColSelectName + ", p.study\n" + |
| 198 | + "PIVOT lastStartDate by study IN (select distinct studyName from studies.studies)"; |
| 199 | + } |
| 200 | + |
| 201 | + // TODO: move to parent class |
| 202 | + protected QueryDefinition createQueryDef(UserSchema us, String queryName) |
| 203 | + { |
| 204 | + if (!us.getContainer().isWorkbook()) |
| 205 | + { |
| 206 | + return QueryService.get().createQueryDef(us.getUser(), us.getContainer(), us, queryName); |
| 207 | + } |
| 208 | + |
| 209 | + // The rationale is that if we are querying from a workbook, preferentially translate to the parent US |
| 210 | + // However, there are situations like workbook-scoped lists, where that query might not exist on the parent |
| 211 | + UserSchema parentUserSchema = QueryService.get().getUserSchema(us.getUser(), us.getContainer().getParent(), us.getSchemaPath()); |
| 212 | + assert parentUserSchema != null; |
| 213 | + |
| 214 | + if (parentUserSchema.getTableNames().contains(queryName)) |
| 215 | + { |
| 216 | + return QueryService.get().createQueryDef(parentUserSchema.getUser(), parentUserSchema.getContainer(), parentUserSchema, queryName); |
| 217 | + } |
| 218 | + else |
| 219 | + { |
| 220 | + return QueryService.get().createQueryDef(us.getUser(), us.getContainer(), us, queryName); |
| 221 | + } |
66 | 222 | } |
67 | 223 | } |
0 commit comments