Skip to content

Commit 194f350

Browse files
committed
Add Dashboard Drawer feature
- 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`
1 parent cd06343 commit 194f350

8 files changed

+222
-1
lines changed

src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboard/DashboardRepository.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public List<Map<String, String>> getHackyBDCRows() {
5252
String sql =
5353
"""
5454
SELECT
55+
dataset.dataset_id as dataset_id,
5556
dataset.abbreviation AS abbreviation,
5657
dataset.full_name AS name,
5758
CASE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import java.util.List;
4+
5+
public record DashboardDrawer(
6+
int datasetId,
7+
String studyFullname,
8+
String studyAbbreviation,
9+
List<String> consentGroups,
10+
String studySummary,
11+
List<String> studyFocus,
12+
String studyDesign,
13+
String sponsor
14+
) {
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@Controller
9+
@RequestMapping("/dashboard-drawer")
10+
public class DashboardDrawerController {
11+
12+
@Autowired
13+
private DashboardDrawerService dashboardDrawerService;
14+
15+
@GetMapping
16+
public ResponseEntity<DashboardDrawerList> findAll() {
17+
return ResponseEntity.ok(dashboardDrawerService.findAll());
18+
}
19+
20+
@GetMapping("/{id}")
21+
public ResponseEntity<DashboardDrawerList> findByDatasetId(@PathVariable Integer id) {
22+
return ResponseEntity.ok(dashboardDrawerService.findByDatasetId(id));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import java.util.List;
4+
5+
public record DashboardDrawerList(List<DashboardDrawer> dashboardDrawerList) {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 List<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());
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());
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Array; // For handling SQL Array
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
public class DashboardDrawerRowMapper implements RowMapper<DashboardDrawer> {
12+
13+
@Override
14+
public DashboardDrawer mapRow(ResultSet rs, int rowNum) throws SQLException {
15+
return new DashboardDrawer(
16+
rs.getInt("dataset_id"),
17+
rs.getString("study_fullname"),
18+
rs.getString("study_abbreviation"),
19+
convertSqlArrayToList(rs.getArray("consent_groups")),
20+
rs.getString("study_summary"),
21+
convertSqlArrayToList(rs.getArray("study_focus")),
22+
rs.getString("study_design"),
23+
rs.getString("sponsor")
24+
);
25+
}
26+
27+
private List<String> convertSqlArrayToList(Array sqlArray) throws SQLException {
28+
if (sqlArray == null ) {
29+
return List.of();
30+
} else {
31+
Object[] arrayContents = (Object[]) sqlArray.getArray();
32+
// Check if the array contains a single empty value
33+
if (arrayContents.length == 1 && "".equals(arrayContents[0])) {
34+
return List.of();
35+
} else {
36+
return Arrays.asList((String[]) sqlArray.getArray());
37+
}
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@Service
11+
public class DashboardDrawerService {
12+
13+
private final DashboardDrawerRepository repository;
14+
private final String dashboardLayout;
15+
16+
@Autowired
17+
public DashboardDrawerService(DashboardDrawerRepository repository, @Value("${dashboard.layout.type}") String dashboardLayout) {
18+
this.repository = repository;
19+
this.dashboardLayout = dashboardLayout;
20+
}
21+
22+
/**
23+
* Retrieves the Dashboard Drawer for all datasets.
24+
*
25+
* @return a Dashboard instance with drawer-specific columns and rows.
26+
*/
27+
public DashboardDrawerList findAll() {
28+
if (dashboardLayout.equalsIgnoreCase("bdc")) {
29+
List<DashboardDrawer> records = repository.getDashboardDrawerRows();
30+
return new DashboardDrawerList(records);
31+
}
32+
33+
return new DashboardDrawerList(new ArrayList<>());
34+
}
35+
36+
/**
37+
* Retrieves the Dashboard Drawer for a specific dataset.
38+
*
39+
* @param datasetId the ID of the dataset to fetch.
40+
* @return a Dashboard instance with drawer-specific columns and rows.
41+
*/
42+
public DashboardDrawerList findByDatasetId(Integer datasetId) {
43+
if (dashboardLayout.equalsIgnoreCase("bdc")) {
44+
List<DashboardDrawer> records = repository.getDashboardDrawerRows(datasetId);
45+
return new DashboardDrawerList(records);
46+
}
47+
48+
return new DashboardDrawerList(new ArrayList<>());
49+
}
50+
}

src/main/resources/application-bdc.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ spring.datasource.username=${DATASOURCE_USERNAME}
66
server.port=80
77

88
dashboard.enable.extra_details=true
9+
dashboard.enable.bdc_hack=true
10+
dashboard.layout.type=bdc
11+
12+
filtering.unfilterable_concepts=stigmatized
913

10-
filtering.unfilterable_concepts=stigmatized

0 commit comments

Comments
 (0)