77import org .apache .logging .log4j .Logger ;
88import org .jetbrains .annotations .Nullable ;
99import org .labkey .api .data .AbstractTableInfo ;
10+ import org .labkey .api .data .BaseColumnInfo ;
1011import org .labkey .api .data .ColumnInfo ;
1112import org .labkey .api .data .Container ;
1213import org .labkey .api .data .ContainerFilter ;
1819import org .labkey .api .data .SQLFragment ;
1920import org .labkey .api .data .TableCustomizer ;
2021import org .labkey .api .data .TableInfo ;
21- import org .labkey .api .data .WrappedColumn ;
22+ import org .labkey .api .data .WrappedColumnInfo ;
2223import org .labkey .api .gwt .client .FacetingBehaviorType ;
2324import org .labkey .api .laboratory .LaboratoryService ;
2425import org .labkey .api .ldk .LDKService ;
@@ -183,13 +184,16 @@ public void customizeColumns(AbstractTableInfo ti)
183184 {
184185 container .setHidden (true );
185186
186- WrappedColumn wrappedContainer = new WrappedColumn (container , "workbook" );
187- wrappedContainer .setLabel ("Workbook" );
187+ BaseColumnInfo wrappedContainer = WrappedColumnInfo .wrapAsCopy (ti , FieldKey .fromString ("workbook" ), container , "Workbook" , null );
188+ wrappedContainer .setName ("workbook" );
189+ wrappedContainer .setCalculated (true );
190+ wrappedContainer .setShownInInsertView (false );
191+ wrappedContainer .setShownInUpdateView (false );
188192 wrappedContainer .setFk (QueryForeignKey
189193 .from (ti .getUserSchema (), ti .getContainerFilter ())
190194 .schema (us )
191195 .to ("workbooks" , LaboratoryWorkbooksTable .WORKBOOK_COL , "workbookId" ));
192- wrappedContainer .setURL (DetailsURL .fromString ("/project/start .view" ));
196+ wrappedContainer .setURL (DetailsURL .fromString ("/project/begin .view" ));
193197 wrappedContainer .setShownInDetailsView (true );
194198 wrappedContainer .setFacetingBehaviorType (FacetingBehaviorType .ALWAYS_OFF );
195199 wrappedContainer .setDisplayColumnFactory (new DisplayColumnFactory ()
@@ -290,11 +294,15 @@ private void appendDemographicsCols(final UserSchema us, AbstractTableInfo ti, C
290294 if (ti .getColumn (name ) != null )
291295 continue ;
292296
293- WrappedColumn col = new WrappedColumn (subjectCol , name );
294- col .setLabel (qd .getLabel ());
297+ BaseColumnInfo col = WrappedColumnInfo .wrapAsCopy (ti , FieldKey .fromString (name ), subjectCol , qd .getLabel (), null );
298+ col .setName (name );
299+ col .setCalculated (true );
300+ col .setShownInInsertView (false );
301+ col .setShownInUpdateView (false );
295302 col .setReadOnly (true );
296303 col .setIsUnselectable (true );
297304 col .setUserEditable (false );
305+ col .setKeyField (false );
298306
299307 UserSchema targetSchema = qd .getTableInfo (targetContainer , us .getUser ()).getUserSchema ();
300308 col .setFk (new QueryForeignKey (us , ti .getContainerFilter (), targetSchema , null , qd .getQueryName (), qd .getTargetColumn (), qd .getTargetColumn ())
@@ -366,12 +374,16 @@ private void appendMajorEventsCol(final UserSchema us, AbstractTableInfo ds, fin
366374 final String pkColSelectName = pk .getFieldKey ().toSQLString ();
367375 final String pkColRawName = pk .getName ();
368376
369- MutableColumnInfo col = new WrappedColumn (pk , name );
370- col .setLabel ("Major Events" );
377+ BaseColumnInfo col = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (name ), pk , "Major Events" , null );
371378 col .setDescription ("This column shows all major events recorded in this subject's history and will calculate the time elapsed between the current sample and these dates." );
379+ col .setName (name );
380+ col .setCalculated (true );
381+ col .setShownInInsertView (false );
382+ col .setShownInUpdateView (false );
372383 col .setReadOnly (true );
373384 col .setIsUnselectable (true );
374385 col .setUserEditable (false );
386+ col .setKeyField (false );
375387
376388 final String schemaName = ds .getUserSchema ().getSchemaPath ().toSQLString ();
377389 final String subjectSelectName = ds .getSqlDialect ().makeLegalIdentifier (subjectColName );
@@ -384,11 +396,10 @@ private void appendMajorEventsCol(final UserSchema us, AbstractTableInfo ds, fin
384396 @ Override
385397 public TableInfo getLookupTableInfo ()
386398 {
387- Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
388- UserSchema effectiveUs = us .getContainer ().isWorkbookOrTab () ? QueryService .get ().getUserSchema (us .getUser (), target , us .getSchemaPath ()) : us ;
389- QueryDefinition qd = QueryService .get ().createQueryDef (us .getUser (), target , effectiveUs , colName );
399+ Container parentContainer = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
400+ QueryDefinition qd = createQueryDef (us , colName );
390401
391- qd .setSql (getMajorEventsSql (target , schemaName , querySelectName , pkColSelectName , subjectSelectName , dateSelectName ));
402+ qd .setSql (getMajorEventsSql (parentContainer , schemaName , querySelectName , pkColSelectName , subjectSelectName , dateSelectName ));
392403 qd .setIsTemporary (true );
393404
394405 List <QueryException > errors = new ArrayList <>();
@@ -444,21 +455,24 @@ private void appendOverlapingProjectsCol(final UserSchema us, AbstractTableInfo
444455 final String subjectSelectName = ds .getSqlDialect ().makeLegalIdentifier (subjectColName );
445456 final String dateSelectName = dateColName == null ? null : ds .getSqlDialect ().makeLegalIdentifier (dateColName );
446457
447- WrappedColumn col = new WrappedColumn (pk , name );
448- col .setLabel ("Overlapping Groups" );
458+ BaseColumnInfo col = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (name ), pk , "Overlapping Groups" , null );
449459 col .setDescription ("This column shows all groups to which this subject belonged at the time of this sample." );
460+ col .setName (name );
461+ col .setCalculated (true );
462+ col .setShownInInsertView (false );
463+ col .setShownInUpdateView (false );
450464 col .setReadOnly (true );
451465 col .setIsUnselectable (true );
452466 col .setUserEditable (false );
467+ col .setKeyField (false );
453468 col .setFk (new LookupForeignKey (){
454469 @ Override
455470 public TableInfo getLookupTableInfo ()
456471 {
457- Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
458- UserSchema effectiveUs = us .getContainer ().isWorkbookOrTab () ? QueryService .get ().getUserSchema (us .getUser (), target , us .getSchemaPath ()) : us ;
459- QueryDefinition qd = QueryService .get ().createQueryDef (us .getUser (), target , effectiveUs , colName );
472+ Container parentContainer = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
473+ QueryDefinition qd = createQueryDef (us , colName );
460474
461- qd .setSql (getOverlapSql (target , schemaName , querySelectName , pkColSelectName , subjectSelectName , dateSelectName ));
475+ qd .setSql (getOverlapSql (parentContainer , schemaName , querySelectName , pkColSelectName , subjectSelectName , dateSelectName ));
462476 qd .setIsTemporary (true );
463477
464478 List <QueryException > errors = new ArrayList <>();
@@ -490,20 +504,24 @@ public TableInfo getLookupTableInfo()
490504
491505 //add pivot column
492506 String pivotColName = "overlappingProjectsPivot" ;
493- WrappedColumn col2 = new WrappedColumn (pk , pivotColName );
494- col2 .setLabel ("Overlapping Group List" );
507+ BaseColumnInfo col2 = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (pivotColName ), pk , "Overlapping Group List" , null );
508+ col2 .setName (pivotColName );
509+ col2 .setCalculated (true );
510+ col2 .setShownInInsertView (false );
511+ col2 .setShownInUpdateView (false );
495512 col2 .setDescription ("Shows groups to which this subject belonged at the time of this sample." );
496513 col2 .setHidden (true );
497514 col2 .setReadOnly (true );
498515 col2 .setIsUnselectable (true );
499516 col2 .setUserEditable (false );
517+ col2 .setKeyField (false );
500518 final String lookupColName = ds .getName () + "_overlappingProjectsPivot" ;
501519 col2 .setFk (new LookupForeignKey (){
502520 @ Override
503521 public TableInfo getLookupTableInfo ()
504522 {
505523 Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
506- QueryDefinition qd = QueryService . get (). createQueryDef (us . getUser (), target , us , lookupColName );
524+ QueryDefinition qd = createQueryDef (us , lookupColName );
507525
508526 qd .setSql (getOverlapPivotSql (target , schemaName , querySelectName , pkColSelectName , subjectColName , dateColName ));
509527 qd .setIsTemporary (true );
@@ -555,21 +573,24 @@ public void appendProjectsCol(final UserSchema us, AbstractTableInfo ds, final S
555573 final String publicTableName = ds .getPublicName ();
556574
557575 final String colName = ds .getName () + "_allProjects" ;
558- WrappedColumn col = new WrappedColumn (pk , name );
559- col .setLabel ("Groups" );
576+ BaseColumnInfo col = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (name ), pk , "Groups" , null );
577+ col .setName (name );
578+ col .setCalculated (true );
579+ col .setShownInInsertView (false );
580+ col .setShownInUpdateView (false );
560581 col .setDescription ("This column shows all groups to which this subject has ever been a member, regardless of whether that assignment overlaps with the current data point" );
561582 col .setReadOnly (true );
562583 col .setIsUnselectable (true );
563584 col .setUserEditable (false );
585+ col .setKeyField (false );
564586 col .setFk (new LookupForeignKey (){
565587 @ Override
566588 public TableInfo getLookupTableInfo ()
567589 {
568- Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
569- UserSchema effectiveUs = us .getContainer ().isWorkbookOrTab () ? QueryService .get ().getUserSchema (us .getUser (), target , us .getSchemaPath ()) : us ;
570- QueryDefinition qd = QueryService .get ().createQueryDef (us .getUser (), target , effectiveUs , colName );
590+ Container parentContainer = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
591+ QueryDefinition qd = createQueryDef (us , colName );
571592
572- qd .setSql (getOverlapSql (target , schemaName , querySelectName , pkColSelectName , subjectSelectName , null ));
593+ qd .setSql (getOverlapSql (parentContainer , schemaName , querySelectName , pkColSelectName , subjectSelectName , null ));
573594 qd .setIsTemporary (true );
574595
575596 List <QueryException > errors = new ArrayList <>();
@@ -602,20 +623,23 @@ public TableInfo getLookupTableInfo()
602623 //add pivot column
603624 String pivotColName = "allProjectsPivot" ;
604625 final String lookupName = ds .getName () + "_allProjectsPivot" ;
605- WrappedColumn col2 = new WrappedColumn (pk , pivotColName );
606- col2 .setLabel ("Group Summary List" );
626+ BaseColumnInfo col2 = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (pivotColName ), pk , "Group Summary List" , null );
627+ col2 .setName (pivotColName );
628+ col2 .setCalculated (true );
629+ col2 .setShownInInsertView (false );
630+ col2 .setShownInUpdateView (false );
607631 col2 .setDescription ("Shows groups to which this subject belonged at any point in time." );
608632 col2 .setHidden (true );
609633 col2 .setReadOnly (true );
610634 col2 .setIsUnselectable (true );
611635 col2 .setUserEditable (false );
636+ col2 .setKeyField (false );
612637 col2 .setFk (new LookupForeignKey (){
613638 @ Override
614639 public TableInfo getLookupTableInfo ()
615640 {
616641 Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
617- UserSchema effectiveUs = us .getContainer ().isWorkbookOrTab () ? QueryService .get ().getUserSchema (us .getUser (), target , us .getSchemaPath ()) : us ;
618- QueryDefinition qd = QueryService .get ().createQueryDef (us .getUser (), target , effectiveUs , lookupName );
642+ QueryDefinition qd = createQueryDef (us , lookupName );
619643
620644 qd .setSql (getOverlapPivotSql (target , schemaName , querySelectName , pkColSelectName , subjectSelectName , null ));
621645 qd .setIsTemporary (true );
@@ -749,11 +773,15 @@ private void appendRelativeDatesCol(final UserSchema us, AbstractTableInfo ds, f
749773 final String pkColSelectName = pk .getFieldKey ().toSQLString ();
750774 final String pkColRawName = pk .getName ();
751775
752- WrappedColumn col = new WrappedColumn (pk , name );
753- col .setLabel ("Relative Dates" );
776+ BaseColumnInfo col = WrappedColumnInfo .wrapAsCopy (ds , FieldKey .fromString (name ), pk , "Relative Dates" , null );
777+ col .setName (name );
778+ col .setCalculated (true );
779+ col .setShownInInsertView (false );
780+ col .setShownInUpdateView (false );
754781 col .setReadOnly (true );
755782 col .setIsUnselectable (true );
756783 col .setUserEditable (false );
784+ col .setKeyField (false );
757785
758786 final String colName = ds .getName () + "_relativeDates" ;
759787 final String schemaName = ds .getUserSchema ().getSchemaPath ().toSQLString ();
@@ -765,9 +793,8 @@ private void appendRelativeDatesCol(final UserSchema us, AbstractTableInfo ds, f
765793 @ Override
766794 public TableInfo getLookupTableInfo ()
767795 {
768- Container target = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
769- UserSchema effectiveUs = us .getContainer ().isWorkbookOrTab () ? QueryService .get ().getUserSchema (us .getUser (), target , us .getSchemaPath ()) : us ;
770- QueryDefinition qd = QueryService .get ().createQueryDef (us .getUser (), target , effectiveUs , colName );
796+ Container parentContainer = us .getContainer ().isWorkbookOrTab () ? us .getContainer ().getParent () : us .getContainer ();
797+ QueryDefinition qd = createQueryDef (us , colName );
771798
772799 qd .setSql ("SELECT\n " +
773800 "t." + pkColSelectName + ",\n " +
@@ -795,7 +822,7 @@ public TableInfo getLookupTableInfo()
795822 "ROUND(CONVERT(age_in_months(p.startdate, s." + dateSelectName + "), DOUBLE) / 12, 1) AS YearsPostStartDecimal,\n " +
796823 "\n " +
797824 "FROM " + schemaName + "." + publicTableName + " s\n " +
798- "JOIN \" " + target .getPath () + "\" .laboratory.project_usage p\n " +
825+ "JOIN \" " + parentContainer .getPath () + "\" .laboratory.project_usage p\n " +
799826 "ON (s." + subjectSelectName + " = p.subjectId AND CONVERT(p.startdate, DATE) <= CONVERT(s." + dateSelectName + ", DATE) AND CONVERT(s." + dateSelectName + ", DATE) <= CONVERT(COALESCE(p.enddate, {fn curdate()}), DATE))\n " +
800827 "WHERE s." + dateSelectName + " IS NOT NULL and s." + subjectSelectName + " IS NOT NULL\n " +
801828 "\n " +
@@ -830,6 +857,28 @@ public TableInfo getLookupTableInfo()
830857 ds .addColumn (col );
831858 }
832859
860+ private QueryDefinition createQueryDef (UserSchema us , String queryName )
861+ {
862+ if (!us .getContainer ().isWorkbook ())
863+ {
864+ return QueryService .get ().createQueryDef (us .getUser (), us .getContainer (), us , queryName );
865+ }
866+
867+ // The rationale is that if we are querying from a workbook, preferentially translate to the parent US
868+ // However, there are situations like workbook-scoped lists, where that query might not exist on the parent
869+ UserSchema parentUserSchema = QueryService .get ().getUserSchema (us .getUser (), us .getContainer ().getParent (), us .getSchemaPath ());
870+ assert parentUserSchema != null ;
871+
872+ if (parentUserSchema .getTableNames ().contains (queryName ))
873+ {
874+ return QueryService .get ().createQueryDef (parentUserSchema .getUser (), parentUserSchema .getContainer (), parentUserSchema , queryName );
875+ }
876+ else
877+ {
878+ return QueryService .get ().createQueryDef (us .getUser (), us .getContainer (), us , queryName );
879+ }
880+ }
881+
833882 public UserSchema getUserSchema (AbstractTableInfo ds , String name )
834883 {
835884 UserSchema us = ds .getUserSchema ();
0 commit comments