88import org .labkey .api .data .BaseColumnInfo ;
99import org .labkey .api .data .ColumnInfo ;
1010import org .labkey .api .data .Container ;
11- import org .labkey .api .data .MutableColumnInfo ;
1211import org .labkey .api .data .TableCustomizer ;
1312import org .labkey .api .data .TableInfo ;
1413import org .labkey .api .ldk .LDKService ;
1514import org .labkey .api .query .ExprColumn ;
1615import 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 ;
16+ import org .labkey .api .query .QueryForeignKey ;
2017import org .labkey .api .query .QueryService ;
2118import org .labkey .api .query .UserSchema ;
2219import org .labkey .api .study .DatasetTable ;
23- import org .labkey .api .study .Study ;
2420import org .labkey .api .study .StudyService ;
2521import org .labkey .api .util .logging .LogHelper ;
22+ import org .labkey .studies .StudiesSchema ;
2623import org .labkey .studies .StudiesServiceImpl ;
2724
28- import java .util .ArrayList ;
29- import java .util .List ;
25+ import java .util .Objects ;
3026
3127public class StudiesTableCustomizer implements TableCustomizer
3228{
@@ -81,58 +77,37 @@ private void doCustomize(AbstractTableInfo ati)
8177 addProjectAssignmentColumns (ati );
8278 }
8379
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-
9580 private void addProjectAssignmentColumns (AbstractTableInfo ati )
9681 {
9782 final String pivotColName = "allProjectsPivot" ;
9883 if (ati .getColumn (pivotColName ) != null )
99- return ;
100-
101- List <ColumnInfo > pks = ati .getPkColumns ();
102- ColumnInfo pk ;
103- if (pks .size () == 1 )
10484 {
105- pk = pks . get ( 0 ) ;
85+ return ;
10686 }
107- else
87+
88+ if (!StudiesServiceImpl .get ().hasAssignmentDataset (ati .getUserSchema ().getContainer ()))
10889 {
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- }
90+ return ;
11891 }
11992
120- if (!StudiesServiceImpl .get ().hasAssignmentDataset (ati .getUserSchema ().getContainer ()))
93+ final String subjectColumnName = Objects .requireNonNull (StudyService .get ()).getSubjectColumnName (ati .getUserSchema ().getContainer ().isWorkbookOrTab () ? ati .getUserSchema ().getContainer ().getParent () : ati .getUserSchema ().getContainer ());
94+ if (subjectColumnName == null )
12195 {
96+ _log .error ("Unable to find the study's subjectColumn in StudiesTableCustomizer" );
12297 return ;
12398 }
12499
125- final String subjectSelectName = getSubjectColName ( ati .getUserSchema (). getContainer () );
126- if (subjectSelectName == null )
100+ ColumnInfo subjectCol = ati .getColumn ( subjectColumnName );
101+ if (subjectCol == null )
127102 {
128- _log .error ("Unable to find subjectSelectName in StudiesTableCustomizer" );
103+ _log .error ("Table lacks the column " + subjectColumnName + ", " + ati . getName () );
129104 return ;
130105 }
131106
132- final String pkColSelectName = pk . getFieldKey ().toSQLString ();
107+ Container target = ati . getUserSchema ().getContainer (). isWorkbookOrTab () ? ati . getUserSchema (). getContainer (). getParent () : ati . getUserSchema (). getContainer ();
133108
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 );
109+ UserSchema studiesUs = QueryService . get (). getUserSchema ( ati .getUserSchema (). getUser (), target , StudiesSchema . NAME ) ;
110+ BaseColumnInfo col2 = new ExprColumn (ati , FieldKey .fromString (pivotColName ), subjectCol .getValueSql (ExprColumn .STR_TABLE_ALIAS ), subjectCol .getJdbcType (), subjectCol );
136111 col2 .setLabel ("Assignment By Study" );
137112 col2 .setName (pivotColName );
138113 col2 .setCalculated (true );
@@ -144,80 +119,8 @@ private void addProjectAssignmentColumns(AbstractTableInfo ati)
144119 col2 .setIsUnselectable (true );
145120 col2 .setUserEditable (false );
146121 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- });
122+ col2 .setFk (new QueryForeignKey (studiesUs , null , studiesUs , target , StudiesUserSchema .TABLE_ASSIGNMENT_BY_STUDY , subjectColumnName , subjectColumnName ));
181123
182124 ati .addColumn (col2 );
183125 }
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- }
222- }
223126}
0 commit comments