Skip to content

Commit 96b38d5

Browse files
committed
Read laboratory reports from the database as well
1 parent 8af7221 commit 96b38d5

File tree

6 files changed

+277
-1
lines changed

6 files changed

+277
-1
lines changed

laboratory/api-src/org/labkey/api/laboratory/LaboratoryService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ static public void setInstance(LaboratoryService instance)
123123

124124
abstract public @Nullable DemographicsProvider getDemographicsProviderByName(Container c, User u, String name);
125125

126+
abstract public void clearDataProviderCache();
127+
126128
public static enum NavItemCategory
127129
{
128130
samples(),

laboratory/api-src/org/labkey/api/laboratory/QueryTabbedReportItem.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class QueryTabbedReportItem extends TabbedReportItem
3030
{
3131
private String _schemaName;
3232
private String _queryName;
33+
private String _viewName;
3334

3435
public QueryTabbedReportItem(QueryCache cache, DataProvider provider, String schemaName, String queryName, String label, String reportCategory)
3536
{
@@ -67,6 +68,16 @@ public void setQueryName(String queryName)
6768
_queryName = queryName;
6869
}
6970

71+
public String getViewName()
72+
{
73+
return _viewName;
74+
}
75+
76+
public void setViewName(String viewName)
77+
{
78+
_viewName = viewName;
79+
}
80+
7081
@Override
7182
public JSONObject toJSON(Container c, User u)
7283
{
@@ -83,6 +94,13 @@ public JSONObject toJSON(Container c, User u)
8394
json.put("schemaName", getSchemaName());
8495
json.put("queryName", getQueryName());
8596
String viewName = getDefaultViewName(c, getOwnerKey());
97+
98+
// TODO: should we always override here?
99+
if (getViewName() != null)
100+
{
101+
viewName = getViewName();
102+
}
103+
86104
if (viewName != null)
87105
{
88106
json.put("viewName", viewName);

laboratory/resources/schemas/laboratory.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@
633633
<table tableName="reports" tableDbType="TABLE" useColumnOrder="true">
634634
<pkColumnName>rowid</pkColumnName>
635635
<javaCustomizer class="org.labkey.ldk.query.DefaultTableCustomizer" />
636+
<tableTitle>Extra Reports</tableTitle>
636637
<auditLogging>DETAILED</auditLogging>
637638
<columns>
638639
<column columnName="rowid">

laboratory/src/org/labkey/laboratory/LaboratoryDataProvider.java

Lines changed: 249 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,41 @@
1515
*/
1616
package org.labkey.laboratory;
1717

18+
import org.apache.logging.log4j.Logger;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.json.JSONObject;
21+
import org.labkey.api.cache.Cache;
22+
import org.labkey.api.cache.CacheManager;
2023
import org.labkey.api.data.ColumnInfo;
2124
import org.labkey.api.data.Container;
2225
import org.labkey.api.data.ContainerManager;
2326
import org.labkey.api.data.SimpleFilter;
2427
import org.labkey.api.data.TableInfo;
28+
import org.labkey.api.data.TableSelector;
2529
import org.labkey.api.laboratory.AbstractDataProvider;
30+
import org.labkey.api.laboratory.DataProvider;
2631
import org.labkey.api.laboratory.DetailsUrlWithoutLabelNavItem;
2732
import org.labkey.api.laboratory.JSTabbedReportItem;
2833
import org.labkey.api.laboratory.LaboratoryService;
34+
import org.labkey.api.laboratory.NavItem;
2935
import org.labkey.api.laboratory.QueryCountNavItem;
3036
import org.labkey.api.laboratory.QueryImportNavItem;
3137
import org.labkey.api.laboratory.QueryTabbedReportItem;
3238
import org.labkey.api.laboratory.ReportItem;
3339
import org.labkey.api.laboratory.SimpleSettingsItem;
3440
import org.labkey.api.laboratory.SummaryNavItem;
3541
import org.labkey.api.laboratory.TabbedReportItem;
36-
import org.labkey.api.laboratory.NavItem;
3742
import org.labkey.api.ldk.table.QueryCache;
3843
import org.labkey.api.module.Module;
3944
import org.labkey.api.query.DetailsURL;
45+
import org.labkey.api.query.FieldKey;
4046
import org.labkey.api.query.QueryAction;
4147
import org.labkey.api.query.QueryService;
4248
import org.labkey.api.query.UserSchema;
4349
import org.labkey.api.security.User;
4450
import org.labkey.api.study.Study;
4551
import org.labkey.api.study.StudyService;
52+
import org.labkey.api.util.logging.LogHelper;
4653
import org.labkey.api.view.ActionURL;
4754
import org.labkey.api.view.ViewContext;
4855
import 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
}

laboratory/src/org/labkey/laboratory/LaboratorySchema.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class LaboratorySchema
3939
public static final String TABLE_WORKBOOK_TAGS = "workbook_tags";
4040
public static final String TABLE_ASSAY_RUN_TEMPLATES = "assay_run_templates";
4141
public static final String TABLE_SPECIES = "species";
42+
public static final String TABLE_REPORTS = "reports";
4243

4344
public static LaboratorySchema getInstance()
4445
{

laboratory/src/org/labkey/laboratory/LaboratoryServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,10 @@ public List<DemographicsProvider> getDemographicsProviders(final Container c, fi
713713

714714
return null;
715715
}
716+
717+
@Override
718+
public void clearDataProviderCache()
719+
{
720+
LaboratoryDataProvider.clearCache();
721+
}
716722
}

0 commit comments

Comments
 (0)