Skip to content

Commit

Permalink
[DUOS-2887][risk=no] New api to search for dataset summaries (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Jan 22, 2024
1 parent 872ecc3 commit 43017c5
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/broadinstitute/consent/http/db/DatasetDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.broadinstitute.consent.http.db.mapper.DatasetMapper;
import org.broadinstitute.consent.http.db.mapper.DatasetPropertyMapper;
import org.broadinstitute.consent.http.db.mapper.DatasetReducer;
import org.broadinstitute.consent.http.db.mapper.DatasetSummaryMapper;
import org.broadinstitute.consent.http.db.mapper.DictionaryMapper;
import org.broadinstitute.consent.http.db.mapper.FileStorageObjectMapperWithFSOPrefix;
import org.broadinstitute.consent.http.models.ApprovedDataset;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetAudit;
import org.broadinstitute.consent.http.models.DatasetProperty;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.broadinstitute.consent.http.models.Dictionary;
import org.broadinstitute.consent.http.models.FileStorageObject;
import org.broadinstitute.consent.http.models.Study;
Expand Down Expand Up @@ -780,4 +782,17 @@ SELECT voteid, MAX(updatedate) update_date
OR (dp.schema_property = 'dataCustodianEmail' AND LOWER(dp.property_value) = LOWER(:email))
""")
List<Dataset> findDatasetsByCustodian(@Bind("userId") Integer userId, @Bind("email") String email);

@RegisterRowMapper(DatasetSummaryMapper.class)
@SqlQuery("""
SELECT DISTINCT d.dataset_id, d.alias, d.name
FROM dataset d
LEFT JOIN dataset_property p ON p.dataset_id = d.dataset_id
WHERE d.dac_approval = TRUE
AND (
LOWER(d.name) LIKE concat('%', LOWER(:query), '%') OR
LOWER(p.property_value) LIKE concat('%', LOWER(:query), '%')
)
""")
List<DatasetSummary> findDatasetSummariesByQuery(@Bind("query") String query);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.broadinstitute.consent.http.db.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;

public class DatasetSummaryMapper implements RowMapper<DatasetSummary>, RowMapperHelper {

@Override
public DatasetSummary map(ResultSet rs, StatementContext ctx) throws SQLException {
if (hasColumn(rs, "dataset_id") && hasColumn(rs, "name") && hasColumn(rs, "alias")) {
String identifier = Dataset.parseAliasToIdentifier(rs.getInt("alias"));
return new DatasetSummary(rs.getInt("dataset_id"), identifier, rs.getString("name"));
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.broadinstitute.consent.http.models;

public record DatasetSummary(Integer id, String identifier, String name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.broadinstitute.consent.http.models.AuthUser;
import org.broadinstitute.consent.http.models.DataUse;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.broadinstitute.consent.http.models.DatasetUpdate;
import org.broadinstitute.consent.http.models.Dictionary;
import org.broadinstitute.consent.http.models.Study;
Expand Down Expand Up @@ -606,6 +607,22 @@ public Response searchDatasets(
}
}

@GET
@Produces("application/json")
@Path("/autocomplete")
@PermitAll
public Response autocompleteDatasets(
@Auth AuthUser authUser,
@QueryParam("query") String query) {
try {
userService.findUserByEmail(authUser.getEmail());
List<DatasetSummary> datasets = datasetService.searchDatasetSummaries(query);
return Response.ok(datasets).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@POST
@Path("/search/index")
@Consumes("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.broadinstitute.consent.http.models.DataUse;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetProperty;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.broadinstitute.consent.http.models.Dictionary;
import org.broadinstitute.consent.http.models.Study;
import org.broadinstitute.consent.http.models.StudyConversion;
Expand Down Expand Up @@ -310,6 +311,10 @@ public List<Dataset> searchDatasets(String query, AccessManagement accessManagem
return datasets.stream().filter(ds -> ds.isDatasetMatch(query, accessManagement)).toList();
}

public List<DatasetSummary> searchDatasetSummaries(String query) {
return datasetDAO.findDatasetSummariesByQuery(query);
}

public Dataset approveDataset(Dataset dataset, User user, Boolean approval) {
Boolean currentApprovalState = dataset.getDacApproval();
Integer datasetId = dataset.getDataSetId();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/api-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ paths:
$ref: './paths/datasetIndexById.yaml'
/api/dataset/search:
$ref: './paths/datasetSearch.yaml'
/api/dataset/autocomplete:
$ref: './paths/datasetSummaryAutocomplete.yaml'
/api/dataset/search/index:
$ref: './paths/datasetSearchIndex.yaml'
/api/datasetAssociation/{datasetId}:
Expand Down
21 changes: 21 additions & 0 deletions src/main/resources/assets/paths/datasetSummaryAutocomplete.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
get:
summary: Autocomplete Datasets
description: Returns all DAC approved dataset summaries matching a search query.
parameters:
- name: query
in: query
description: Search Query
required: true
schema:
type: string
tags:
- Dataset
responses:
200:
description: A list of Dataset Summary objects
content:
application/json:
schema:
type: array
items:
$ref: '../schemas/DatasetSummary.yaml'
11 changes: 11 additions & 0 deletions src/main/resources/assets/schemas/DatasetSummary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
properties:
id:
type: integer
description: The unique identifier for a dataset
identifier:
type: string
description: The public DUOS identifier for a dataset
name:
type: string
description: The dataset name
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetAudit;
import org.broadinstitute.consent.http.models.DatasetProperty;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.broadinstitute.consent.http.models.Dictionary;
import org.broadinstitute.consent.http.models.Election;
import org.broadinstitute.consent.http.models.FileStorageObject;
Expand Down Expand Up @@ -1148,6 +1149,48 @@ void testFindDatasetsByCustodian() {
assertNotEquals(dataset2.getDataSetId(), datasets.stream().map(Dataset::getDataSetId).toList().get(0));
}

@Test
void testFindDatasetSummariesByQuery() {
Dataset dataset = createDataset();
Dataset dataset2 = createDataset();
User user = createUser();
datasetDAO.updateDatasetApproval(true, Instant.now(), user.getUserId(), dataset.getDataSetId());
datasetDAO.updateDatasetApproval(true, Instant.now(), user.getUserId(), dataset2.getDataSetId());

List<DatasetSummary> summaries = datasetDAO.findDatasetSummariesByQuery(dataset.getName());
assertNotNull(summaries);
assertFalse(summaries.isEmpty());
assertEquals(dataset.getDataSetId(), summaries.stream().map(DatasetSummary::id).toList().get(0));
assertNotEquals(dataset2.getDataSetId(), summaries.stream().map(DatasetSummary::id).toList().get(0));
}

@Test
void testFindDatasetSummariesByQuery_NotApproved() {
Dataset dataset = createDataset();

List<DatasetSummary> summaries = datasetDAO.findDatasetSummariesByQuery(dataset.getName());
assertNotNull(summaries);
assertTrue(summaries.isEmpty());
}

@Test
void testFindDatasetSummariesByQuery_NullQuery() {
createDataset();

List<DatasetSummary> summaries = datasetDAO.findDatasetSummariesByQuery(null);
assertNotNull(summaries);
assertTrue(summaries.isEmpty());
}

@Test
void testFindDatasetSummariesByQuery_EmptyQuery() {
createDataset();

List<DatasetSummary> summaries = datasetDAO.findDatasetSummariesByQuery("");
assertNotNull(summaries);
assertTrue(summaries.isEmpty());
}

private DarCollection createDarCollectionWithDatasets(int dacId, User user,
List<Dataset> datasets) {
String darCode = "DAR-" + RandomUtils.nextInt(1, 999999);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.broadinstitute.consent.http.models.DataUseBuilder;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.DatasetProperty;
import org.broadinstitute.consent.http.models.DatasetSummary;
import org.broadinstitute.consent.http.models.Error;
import org.broadinstitute.consent.http.models.Study;
import org.broadinstitute.consent.http.models.StudyProperty;
Expand Down Expand Up @@ -586,6 +587,18 @@ void testSearchDatasetsOpenAccess() {
assertEquals(GsonUtil.buildGson().toJson(List.of(ds)), response.getEntity());
}

@Test
void testAutocompleteDatasets() {
when(authUser.getEmail()).thenReturn("[email protected]");
when(userService.findUserByEmail("[email protected]")).thenReturn(user);
when(datasetService.searchDatasetSummaries(any())).thenReturn(List.of(new DatasetSummary(1, "ID", "Name")));

initResource();
try (Response response = resource.autocompleteDatasets(authUser, "test")) {
assertTrue(HttpStatusCodes.isSuccess(response.getStatus()));
}
}

@Test
void testSearchDatasetIndex() throws IOException {
String query = "{ \"dataUse\": [\"HMB\"] }";
Expand Down

0 comments on commit 43017c5

Please sign in to comment.