Skip to content

Commit

Permalink
Add Dashboard Drawer feature
Browse files Browse the repository at this point in the history
- Added `DashboardDrawerController` for new endpoints
- Implemented `DashboardDrawerService` for business logic
- Created `DashboardDrawerRepository` for database access
- Defined new configuration properties
- Included new record classes: `DashboardDrawer`, `DashboardDrawerList`
- Updated SQL queries in `DashboardRepository.java` to include `dataset_id`
  • Loading branch information
TDeSain committed Nov 19, 2024
1 parent cd06343 commit 194f350
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public List<Map<String, String>> getHackyBDCRows() {
String sql =
"""
SELECT
dataset.dataset_id as dataset_id,
dataset.abbreviation AS abbreviation,
dataset.full_name AS name,
CASE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import java.util.List;

public record DashboardDrawer(
int datasetId,
String studyFullname,
String studyAbbreviation,
List<String> consentGroups,
String studySummary,
List<String> studyFocus,
String studyDesign,
String sponsor
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/dashboard-drawer")
public class DashboardDrawerController {

@Autowired
private DashboardDrawerService dashboardDrawerService;

@GetMapping
public ResponseEntity<DashboardDrawerList> findAll() {
return ResponseEntity.ok(dashboardDrawerService.findAll());
}

@GetMapping("/{id}")
public ResponseEntity<DashboardDrawerList> findByDatasetId(@PathVariable Integer id) {
return ResponseEntity.ok(dashboardDrawerService.findByDatasetId(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import java.util.List;

public record DashboardDrawerList(List<DashboardDrawer> dashboardDrawerList) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class DashboardDrawerRepository {

private final NamedParameterJdbcTemplate template;

private static final Logger log = LoggerFactory.getLogger(DashboardDrawerRepository.class);

@Autowired
public DashboardDrawerRepository(NamedParameterJdbcTemplate template) {
this.template = template;
}

public List<DashboardDrawer> getDashboardDrawerRows() {
String materializedViewSql = """
select * from dictionary_db.dict.dataset_meta_materialized_view dmmv;
""";

String fallbackSql = """
SELECT d.dataset_id,
MAX(d.full_name) study_fullname,
MAX(d.abbreviation) study_abbreviation,
ARRAY_AGG(DISTINCT c.description) consent_groups,
MAX(d.description) study_summary,
ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor
FROM dataset d
JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id
JOIN consent c ON d.dataset_id = c.dataset_id
GROUP BY d.dataset_id
""";

try {
return template.query(materializedViewSql, new DashboardDrawerRowMapper());
} catch (Exception e) {
log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage());
return template.query (fallbackSql, new DashboardDrawerRowMapper());
}
}

public List<DashboardDrawer> getDashboardDrawerRows(Integer datasetId) {
String materializedViewSql = """
select * from dictionary_db.dict.dataset_meta_materialized_view dmmv where dmmv.dataset_id = :datasetId;
""";

String fallbackSql = """
SELECT d.dataset_id dataset_id,
MAX(d.full_name) study_fullname,
MAX(d.abbreviation) study_abbreviation,
ARRAY_AGG(DISTINCT c.description) consent_groups,
MAX(d.description) study_summary,
ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design,
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor
FROM dataset d
JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id
JOIN consent c ON d.dataset_id = c.dataset_id
where d.dataset_id = :datasetId
GROUP BY d.dataset_id
""";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("datasetId", datasetId);

try {
return template.query(materializedViewSql, params, new DashboardDrawerRowMapper());
} catch (Exception e) {
log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage());
return template.query(fallbackSql, params, new DashboardDrawerRowMapper());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Array; // For handling SQL Array
import java.util.Arrays;
import java.util.List;

public class DashboardDrawerRowMapper implements RowMapper<DashboardDrawer> {

@Override
public DashboardDrawer mapRow(ResultSet rs, int rowNum) throws SQLException {
return new DashboardDrawer(
rs.getInt("dataset_id"),
rs.getString("study_fullname"),
rs.getString("study_abbreviation"),
convertSqlArrayToList(rs.getArray("consent_groups")),
rs.getString("study_summary"),
convertSqlArrayToList(rs.getArray("study_focus")),
rs.getString("study_design"),
rs.getString("sponsor")
);
}

private List<String> convertSqlArrayToList(Array sqlArray) throws SQLException {
if (sqlArray == null ) {
return List.of();
} else {
Object[] arrayContents = (Object[]) sqlArray.getArray();
// Check if the array contains a single empty value
if (arrayContents.length == 1 && "".equals(arrayContents[0])) {
return List.of();
} else {
return Arrays.asList((String[]) sqlArray.getArray());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DashboardDrawerService {

private final DashboardDrawerRepository repository;
private final String dashboardLayout;

@Autowired
public DashboardDrawerService(DashboardDrawerRepository repository, @Value("${dashboard.layout.type}") String dashboardLayout) {
this.repository = repository;
this.dashboardLayout = dashboardLayout;
}

/**
* Retrieves the Dashboard Drawer for all datasets.
*
* @return a Dashboard instance with drawer-specific columns and rows.
*/
public DashboardDrawerList findAll() {
if (dashboardLayout.equalsIgnoreCase("bdc")) {
List<DashboardDrawer> records = repository.getDashboardDrawerRows();
return new DashboardDrawerList(records);
}

return new DashboardDrawerList(new ArrayList<>());
}

/**
* Retrieves the Dashboard Drawer for a specific dataset.
*
* @param datasetId the ID of the dataset to fetch.
* @return a Dashboard instance with drawer-specific columns and rows.
*/
public DashboardDrawerList findByDatasetId(Integer datasetId) {
if (dashboardLayout.equalsIgnoreCase("bdc")) {
List<DashboardDrawer> records = repository.getDashboardDrawerRows(datasetId);
return new DashboardDrawerList(records);
}

return new DashboardDrawerList(new ArrayList<>());
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application-bdc.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ spring.datasource.username=${DATASOURCE_USERNAME}
server.port=80

dashboard.enable.extra_details=true
dashboard.enable.bdc_hack=true
dashboard.layout.type=bdc

filtering.unfilterable_concepts=stigmatized

filtering.unfilterable_concepts=stigmatized

0 comments on commit 194f350

Please sign in to comment.