1515 */
1616package org .labkey .laboratory ;
1717
18+ import org .apache .logging .log4j .Logger ;
1819import org .jetbrains .annotations .NotNull ;
1920import org .json .JSONObject ;
21+ import org .labkey .api .cache .Cache ;
22+ import org .labkey .api .cache .CacheManager ;
2023import org .labkey .api .data .ColumnInfo ;
2124import org .labkey .api .data .Container ;
2225import org .labkey .api .data .ContainerManager ;
2326import org .labkey .api .data .SimpleFilter ;
2427import org .labkey .api .data .TableInfo ;
28+ import org .labkey .api .data .TableSelector ;
2529import org .labkey .api .laboratory .AbstractDataProvider ;
30+ import org .labkey .api .laboratory .DataProvider ;
2631import org .labkey .api .laboratory .DetailsUrlWithoutLabelNavItem ;
2732import org .labkey .api .laboratory .JSTabbedReportItem ;
2833import org .labkey .api .laboratory .LaboratoryService ;
34+ import org .labkey .api .laboratory .NavItem ;
2935import org .labkey .api .laboratory .QueryCountNavItem ;
3036import org .labkey .api .laboratory .QueryImportNavItem ;
3137import org .labkey .api .laboratory .QueryTabbedReportItem ;
3238import org .labkey .api .laboratory .ReportItem ;
3339import org .labkey .api .laboratory .SimpleSettingsItem ;
3440import org .labkey .api .laboratory .SummaryNavItem ;
3541import org .labkey .api .laboratory .TabbedReportItem ;
36- import org .labkey .api .laboratory .NavItem ;
3742import org .labkey .api .ldk .table .QueryCache ;
3843import org .labkey .api .module .Module ;
3944import org .labkey .api .query .DetailsURL ;
45+ import org .labkey .api .query .FieldKey ;
4046import org .labkey .api .query .QueryAction ;
4147import org .labkey .api .query .QueryService ;
4248import org .labkey .api .query .UserSchema ;
4349import org .labkey .api .security .User ;
4450import org .labkey .api .study .Study ;
4551import org .labkey .api .study .StudyService ;
52+ import org .labkey .api .util .logging .LogHelper ;
4653import org .labkey .api .view .ActionURL ;
4754import org .labkey .api .view .ViewContext ;
4855import org .labkey .api .view .template .ClientDependency ;
@@ -62,11 +69,23 @@ public class LaboratoryDataProvider extends AbstractDataProvider
6269 public static final String NAME = "Laboratory" ;
6370 private final Module _module ;
6471
72+ private static final Logger _log = LogHelper .getLogger (LaboratoryDataProvider .class , "Messages from LaboratoryDataProvider" );
73+
74+ private static Cache <String , List <TabbedReportModel >> _cache = null ;
75+
6576 public LaboratoryDataProvider (Module m )
6677 {
6778 _module = m ;
6879 }
6980
81+ public static void clearCache ()
82+ {
83+ if (_cache != null )
84+ {
85+ _cache .clear ();
86+ }
87+ }
88+
7089 @ Override
7190 public String getName ()
7291 {
@@ -332,6 +351,235 @@ public List<TabbedReportItem> getTabbedReportItems(Container c, User u)
332351 item2 .setOwnerKey (owner .getPropertyManagerKey ());
333352 items .add (item2 );
334353
354+ List <TabbedReportModel > models = getTabbedReports (c , u );
355+ if (!models .isEmpty ())
356+ {
357+ items .addAll (models .stream ().map (m -> {
358+ if (m .isValid ())
359+ {
360+ _log .error ("Invalid tabbed report in container: " + c .getPath () + ", rowId: " + m .getRowId ());
361+ }
362+
363+ return (m .toNavItem (c , u , cache , owner , this ));
364+ }).toList ());
365+ }
366+
335367 return Collections .unmodifiableList (items );
336368 }
369+
370+ private synchronized Cache <String , List <TabbedReportModel >> getCache ()
371+ {
372+ if (_cache == null )
373+ {
374+ _cache = CacheManager .getStringKeyCache (1000 , CacheManager .UNLIMITED , "LaboratoryDataProviderCache" );
375+ }
376+
377+ return _cache ;
378+ }
379+
380+ private synchronized List <TabbedReportModel > getTabbedReports (Container c , User u )
381+ {
382+ c = c .isWorkbookOrTab () ? c .getParent () : c ;
383+
384+ List <TabbedReportModel > models = getCache ().get (c .getId ());
385+ if (models != null )
386+ return models ;
387+
388+ UserSchema us = QueryService .get ().getUserSchema (u , c , LaboratoryModule .SCHEMA_NAME );
389+ if (us == null )
390+ {
391+ return Collections .emptyList ();
392+ }
393+
394+ models = new ArrayList <>(new TableSelector (us .getTable (LaboratorySchema .TABLE_REPORTS )).getArrayList (TabbedReportModel .class ));
395+ models = Collections .unmodifiableList (models );
396+ getCache ().put (c .getId (), models );
397+
398+ return models ;
399+ }
400+
401+ public static class TabbedReportModel
402+ {
403+ private int _rowId ;
404+ private String _category ;
405+ private String _reportType ;
406+ private String _reportTitle ;
407+ private String _sortOrder ;
408+ private String _containerPath ;
409+ private String _schemaName ;
410+ private String _queryName ;
411+ private String _viewName ;
412+ private String _reportPath ;
413+ private String _subjectFieldName ;
414+ private String _description ;
415+
416+ public boolean isValid ()
417+ {
418+ if (getCategory () == null || getSchemaName () == null || getQueryName () == null )
419+ {
420+ return false ;
421+ }
422+
423+ if (getContainerPath () != null )
424+ {
425+ Container target = ContainerManager .getForPath (getContainerPath ());
426+ if (target == null )
427+ {
428+ _log .error ("Unknown container for saved TabbedReportItem: " + getContainerPath () + ", rowId: " + getRowId ());
429+ return false ;
430+ }
431+ }
432+
433+ return true ;
434+ }
435+
436+ public TabbedReportItem toNavItem (Container c , User u , QueryCache cache , NavItem owner , DataProvider provider )
437+ {
438+ QueryTabbedReportItem report = new QueryTabbedReportItem (cache , provider , getSchemaName (), getQueryName (), getReportTitle () == null ? getQueryName () : getReportTitle (), getCategory ());
439+ report .setQueryCache (cache );
440+ if (getSubjectFieldName () != null )
441+ {
442+ report .setSubjectIdFieldKey (FieldKey .fromString (getSubjectFieldName ()));
443+ }
444+
445+ if (getContainerPath () != null )
446+ {
447+ Container target = ContainerManager .getForPath (getContainerPath ());
448+ if (target != null )
449+ {
450+ report .setTargetContainer (target );
451+ }
452+ }
453+
454+ if (getViewName () != null )
455+ {
456+ report .setViewName (getViewName ());
457+ }
458+
459+ report .setVisible (owner .isVisible (c , u ));
460+ report .setOwnerKey (owner .getPropertyManagerKey ());
461+
462+ return report ;
463+ }
464+
465+ public int getRowId ()
466+ {
467+ return _rowId ;
468+ }
469+
470+ public void setRowId (int rowId )
471+ {
472+ _rowId = rowId ;
473+ }
474+
475+ public String getCategory ()
476+ {
477+ return _category ;
478+ }
479+
480+ public void setCategory (String category )
481+ {
482+ _category = category ;
483+ }
484+
485+ public String getReportType ()
486+ {
487+ return _reportType ;
488+ }
489+
490+ public void setReportType (String reportType )
491+ {
492+ _reportType = reportType ;
493+ }
494+
495+ public String getReportTitle ()
496+ {
497+ return _reportTitle ;
498+ }
499+
500+ public void setReportTitle (String reportTitle )
501+ {
502+ _reportTitle = reportTitle ;
503+ }
504+
505+ public String getSortOrder ()
506+ {
507+ return _sortOrder ;
508+ }
509+
510+ public void setSortOrder (String sortOrder )
511+ {
512+ _sortOrder = sortOrder ;
513+ }
514+
515+ public String getContainerPath ()
516+ {
517+ return _containerPath ;
518+ }
519+
520+ public void setContainerPath (String containerPath )
521+ {
522+ _containerPath = containerPath ;
523+ }
524+
525+ public String getSchemaName ()
526+ {
527+ return _schemaName ;
528+ }
529+
530+ public void setSchemaName (String schemaName )
531+ {
532+ _schemaName = schemaName ;
533+ }
534+
535+ public String getQueryName ()
536+ {
537+ return _queryName ;
538+ }
539+
540+ public void setQueryName (String queryName )
541+ {
542+ _queryName = queryName ;
543+ }
544+
545+ public String getViewName ()
546+ {
547+ return _viewName ;
548+ }
549+
550+ public void setViewName (String viewName )
551+ {
552+ _viewName = viewName ;
553+ }
554+
555+ public String getReportPath ()
556+ {
557+ return _reportPath ;
558+ }
559+
560+ public void setReportPath (String reportPath )
561+ {
562+ _reportPath = reportPath ;
563+ }
564+
565+ public String getSubjectFieldName ()
566+ {
567+ return _subjectFieldName ;
568+ }
569+
570+ public void setSubjectFieldName (String subjectFieldName )
571+ {
572+ _subjectFieldName = subjectFieldName ;
573+ }
574+
575+ public String getDescription ()
576+ {
577+ return _description ;
578+ }
579+
580+ public void setDescription (String description )
581+ {
582+ _description = description ;
583+ }
584+ }
337585}
0 commit comments