|
| 1 | +package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 7 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 8 | +import org.springframework.stereotype.Repository; |
| 9 | + |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +@Repository |
| 13 | +public class DashboardDrawerRepository { |
| 14 | + |
| 15 | + private final NamedParameterJdbcTemplate template; |
| 16 | + |
| 17 | + private static final Logger log = LoggerFactory.getLogger(DashboardDrawerRepository.class); |
| 18 | + |
| 19 | + @Autowired |
| 20 | + public DashboardDrawerRepository(NamedParameterJdbcTemplate template) { |
| 21 | + this.template = template; |
| 22 | + } |
| 23 | + |
| 24 | + public List<DashboardDrawer> getDashboardDrawerRows() { |
| 25 | + String materializedViewSql = """ |
| 26 | + select * from dictionary_db.dict.dataset_meta_materialized_view dmmv; |
| 27 | + """; |
| 28 | + |
| 29 | + String fallbackSql = """ |
| 30 | + SELECT d.dataset_id, |
| 31 | + MAX(d.full_name) study_fullname, |
| 32 | + MAX(d.abbreviation) study_abbreviation, |
| 33 | + ARRAY_AGG(DISTINCT c.description) consent_groups, |
| 34 | + MAX(d.description) study_summary, |
| 35 | + ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus, |
| 36 | + MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design, |
| 37 | + MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor |
| 38 | + FROM dataset d |
| 39 | + JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id |
| 40 | + JOIN consent c ON d.dataset_id = c.dataset_id |
| 41 | + GROUP BY d.dataset_id |
| 42 | + """; |
| 43 | + |
| 44 | + try { |
| 45 | + return template.query(materializedViewSql, new DashboardDrawerRowMapper()); |
| 46 | + } catch (Exception e) { |
| 47 | + log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage()); |
| 48 | + return template.query(fallbackSql, new DashboardDrawerRowMapper()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public Optional<DashboardDrawer> getDashboardDrawerRows(Integer datasetId) { |
| 53 | + String materializedViewSql = """ |
| 54 | + select * from dictionary_db.dict.dataset_meta_materialized_view dmmv where dmmv.dataset_id = :datasetId; |
| 55 | + """; |
| 56 | + |
| 57 | + String fallbackSql = """ |
| 58 | + SELECT d.dataset_id dataset_id, |
| 59 | + MAX(d.full_name) study_fullname, |
| 60 | + MAX(d.abbreviation) study_abbreviation, |
| 61 | + ARRAY_AGG(DISTINCT c.description) consent_groups, |
| 62 | + MAX(d.description) study_summary, |
| 63 | + ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus, |
| 64 | + MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design, |
| 65 | + MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor |
| 66 | + FROM dataset d |
| 67 | + JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id |
| 68 | + JOIN consent c ON d.dataset_id = c.dataset_id |
| 69 | + where d.dataset_id = :datasetId |
| 70 | + GROUP BY d.dataset_id |
| 71 | + """; |
| 72 | + MapSqlParameterSource params = new MapSqlParameterSource(); |
| 73 | + params.addValue("datasetId", datasetId); |
| 74 | + |
| 75 | + try { |
| 76 | + return template.query(materializedViewSql, params, new DashboardDrawerRowMapper()).stream().findFirst(); |
| 77 | + } catch (Exception e) { |
| 78 | + log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage()); |
| 79 | + return template.query(fallbackSql, params, new DashboardDrawerRowMapper()).stream().findFirst(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments