@@ -64,6 +64,7 @@ public void performDatasetCustomization(DatasetTable ds)
6464 appendAgeAtTimeCol (ds .getUserSchema (), ati , ID_COL , DATE_COL );
6565 appendPvlColumns (ds , ID_COL , DATE_COL );
6666 appendSivChallengeColumns (ati , ID_COL , DATE_COL );
67+ appendArtColumns (ds , ID_COL , DATE_COL );
6768 }
6869
6970 appendDemographicsColumns (ati );
@@ -329,9 +330,9 @@ public TableInfo getLookupTableInfo()
329330 // NOTE: CAST() is used to ensure whole numbers
330331 "CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) as daysPostInfection,\n " +
331332 "CONVERT(age_in_months(CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), FLOAT) as monthsPostInfection,\n " +
332- pkCol .getFieldKey ().toString () + "\n " +
333+ "c." + pkCol .getFieldKey ().toString () + "\n " +
333334 "FROM \" " + schemaName + "\" .\" " + queryName + "\" c " +
334- "JOIN studies.subjectAnchorDates ad ON (ad.subjectId = c." + idCol .getFieldKey ().toSQLString () + " AND ad.date = c." + dateCol . getFieldKey (). toString () + " )\n " +
335+ "JOIN studies.subjectAnchorDates ad ON (ad.subjectId = c." + idCol .getFieldKey ().toSQLString () + ")\n " +
335336 "WHERE ad.eventLabel = 'SIV Infection'\n " +
336337 "GROUP BY c.date, c." + pkCol .getFieldKey ().toString () + "\n " +
337338 "HAVING count(*) = 1"
@@ -366,6 +367,105 @@ public TableInfo getLookupTableInfo()
366367 targetTable .addColumn (col );
367368 }
368369
370+ private void appendArtColumns (DatasetTable ds , String subjectColName , String dateColName )
371+ {
372+ String name = "artInformation" ;
373+ if (ds .getColumn (name ) != null )
374+ return ;
375+
376+ final ColumnInfo pkCol = getPkCol (ds );
377+ if (pkCol == null )
378+ return ;
379+
380+ final ColumnInfo idCol = ds .getColumn (subjectColName );
381+ if (idCol == null )
382+ return ;
383+
384+ final ColumnInfo dateCol = ds .getColumn (dateColName );
385+ if (dateCol == null )
386+ return ;
387+
388+ Dataset treatments = ds .getDataset ().getStudy ().getDatasetByName ("treatments" );
389+ if (treatments == null )
390+ {
391+ return ;
392+ }
393+
394+ final String targetSchemaName = ds .getUserSchema ().getName ();
395+ final Container targetSchemaContainer = ds .getUserSchema ().getContainer ();
396+ final User u = ds .getUserSchema ().getUser ();
397+ final String schemaName = ds .getPublicSchemaName ();
398+ final String queryName = ds .getName ();
399+
400+ WrappedColumn col = new WrappedColumn (pkCol , name );
401+ col .setLabel ("ART Information" );
402+ col .setReadOnly (true );
403+ col .setIsUnselectable (true );
404+ col .setUserEditable (false );
405+ col .setFk (new LookupForeignKey (){
406+ @ Override
407+ public TableInfo getLookupTableInfo ()
408+ {
409+ String name = queryName + "_artData" ;
410+ UserSchema targetSchema = ds .getUserSchema ().getDefaultSchema ().getUserSchema (targetSchemaName );
411+ QueryDefinition qd = QueryService .get ().createQueryDef (u , targetSchemaContainer , targetSchema , name );
412+ qd .setSql ("SELECT\n " +
413+ "max(tr.date) as artInitiation,\n " +
414+ "CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(tr.date) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) as daysPostArtInitiation,\n " +
415+ "CONVERT(age_in_months(CAST(max(tr.date) AS DATE), CAST(c." + dateColName + " AS DATE)), FLOAT) as monthsPostArtInitiation,\n " +
416+ "max(tr.enddate) as artRelease,\n " +
417+ "CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(tr.enddate) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) as daysPostArtRelease,\n " +
418+ "CONVERT(age_in_months(CAST(max(tr.enddate) AS DATE), CAST(c." + dateColName + " AS DATE)), FLOAT) as monthsPostArtRelease,\n " +
419+ "CAST(CASE WHEN max(tr.date) IS NULL THEN NULL ELSE 'Y' END as VARCHAR) as onArt,\n " +
420+ "GROUP_CONCAT(DISTINCT tr.treatment) AS artTreatment,\n " +
421+ "c." + pkCol .getFieldKey ().toString () + "\n " +
422+ "FROM \" " + schemaName + "\" .\" " + queryName + "\" c " +
423+ "JOIN study.treatments tr ON (tr.category = 'ART' AND CAST(tr.date AS DATE) <= CAST(c." + dateCol .getFieldKey ().toString () + " AS DATE) AND COALESCE(tr.enddate, now()) >= CAST(c." + dateCol .getFieldKey ().toString () + " AS DATE) AND tr.Id = c." + idCol .getFieldKey ().toSQLString () + ")\n " +
424+ "GROUP BY c.date, c." + pkCol .getFieldKey ().toString () + "\n " +
425+ "HAVING COUNT(*) = 1"
426+ );
427+ qd .setIsTemporary (true );
428+
429+ List <QueryException > errors = new ArrayList <>();
430+ TableInfo ti = qd .getTable (errors , true );
431+ if (!errors .isEmpty ())
432+ {
433+ _log .warn ("Error creating artData lookup table for: " + schemaName + "." + queryName + " in container: " + targetSchema .getContainer ().getPath ());
434+ for (QueryException e : errors )
435+ {
436+ _log .warn (e .getMessage (), e );
437+ }
438+ }
439+
440+ if (ti != null )
441+ {
442+ ((BaseColumnInfo )ti .getColumn (pkCol .getName ())).setHidden (true );
443+ ((BaseColumnInfo )ti .getColumn (pkCol .getName ())).setKeyField (true );
444+
445+ ((BaseColumnInfo )ti .getColumn ("artInitiation" )).setLabel ("ART Initiation" );
446+ ((BaseColumnInfo )ti .getColumn ("artRelease" )).setLabel ("ART Release" );
447+
448+ ((BaseColumnInfo )ti .getColumn ("daysPostArtInitiation" )).setLabel ("Days Post-ART Initiation" );
449+ ((BaseColumnInfo )ti .getColumn ("monthsPostArtInitiation" )).setLabel ("Months Post-ART Initiation" );
450+
451+ ((BaseColumnInfo )ti .getColumn ("daysPostArtRelease" )).setLabel ("Days Post-ART Release" );
452+ ((BaseColumnInfo )ti .getColumn ("monthsPostArtRelease" )).setLabel ("Months Post-ART Release" );
453+
454+ ((BaseColumnInfo )ti .getColumn ("artTreatment" )).setLabel ("ART Treatment(s)" );
455+ ((BaseColumnInfo )ti .getColumn ("onArt" )).setLabel ("Overlaps ART?" );
456+ }
457+
458+ return ti ;
459+ }
460+ });
461+
462+ if (ds instanceof AbstractTableInfo ati )
463+ {
464+ ati .addColumn (col );
465+ }
466+ }
467+
468+
369469 // TODO: was on ART or not??
370470
371471 private BaseColumnInfo getWrappedIdCol (UserSchema targetQueryUserSchema , String targetQueryName , AbstractTableInfo demographicsTable , String colName )
0 commit comments