Skip to content

Commit

Permalink
[DUOS-2703][risk=no] Delete study API (#2187)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong authored Dec 5, 2023
1 parent ca77785 commit e001f48
Show file tree
Hide file tree
Showing 13 changed files with 573 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.broadinstitute.consent.http.resources.SamResource;
import org.broadinstitute.consent.http.resources.SchemaResource;
import org.broadinstitute.consent.http.resources.StatusResource;
import org.broadinstitute.consent.http.resources.StudyResource;
import org.broadinstitute.consent.http.resources.SwaggerResource;
import org.broadinstitute.consent.http.resources.TDRResource;
import org.broadinstitute.consent.http.resources.TosResource;
Expand Down Expand Up @@ -246,6 +247,7 @@ public void run(ConsentConfiguration config, Environment env) {
env.jersey().register(
new TDRResource(tdrService, datasetService, userService, dataAccessRequestService));
env.jersey().register(new MailResource(emailService));
env.jersey().register(injector.getInstance(StudyResource.class));

// Authentication filters
final UserRoleDAO userRoleDAO = injector.getProvider(UserRoleDAO.class).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ void updateStudyProperty(
);

@SqlUpdate("""
DELETE FROM study_property WHERE study_property_id = :studyPropertyId
WITH property_deletes AS (
DELETE from study_property where study_id = :studyId returning study_id
)
DELETE FROM study WHERE study_id in (select study_id from property_deletes)
""")
void deleteStudyPropertyById(@Bind("studyPropertyId") Integer studyPropertyId);
void deleteStudyByStudyId(@Bind("studyId") Integer studyId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.broadinstitute.consent.http.models.UserRole;
import org.broadinstitute.consent.http.models.dataset_registration_v1.ConsentGroup.AccessManagement;
import org.broadinstitute.consent.http.models.dataset_registration_v1.DatasetRegistrationSchemaV1;
import org.broadinstitute.consent.http.models.dataset_registration_v1.DatasetRegistrationSchemaV1UpdateValidator;
import org.broadinstitute.consent.http.models.dataset_registration_v1.builder.DatasetRegistrationSchemaV1Builder;
import org.broadinstitute.consent.http.models.dto.DatasetDTO;
import org.broadinstitute.consent.http.models.dto.DatasetPropertyDTO;
Expand Down Expand Up @@ -193,48 +192,6 @@ public Response createDatasetRegistration(
}
}

@PUT
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.APPLICATION_JSON})
@Path("/study/{studyId}")
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
/*
* This endpoint accepts a json instance of a dataset-registration-schema_v1.json schema.
* With that object, we can fully update the study/datasets from the provided values.
*/
public Response updateStudyByRegistration(
@Auth AuthUser authUser,
FormDataMultiPart multipart,
@PathParam("studyId") Integer studyId,
@FormDataParam("dataset") String json) {
try {
User user = userService.findUserByEmail(authUser.getEmail());
Study existingStudy = datasetRegistrationService.findStudyById(studyId);

// Manually validate the schema from an editing context. Validation with the schema tools
// enforces it in a creation context but doesn't work for editing purposes.
DatasetRegistrationSchemaV1UpdateValidator updateValidator = new DatasetRegistrationSchemaV1UpdateValidator();
Gson gson = GsonUtil.gsonBuilderWithAdapters().create();
DatasetRegistrationSchemaV1 registration = gson.fromJson(json,
DatasetRegistrationSchemaV1.class);

if (updateValidator.validate(existingStudy, registration)) {
// Update study from registration
Map<String, FormDataBodyPart> files = extractFilesFromMultiPart(multipart);
Study updatedStudy = datasetRegistrationService.updateStudyFromRegistration(
studyId,
registration,
user,
files);
return Response.ok(updatedStudy).build();
} else {
return Response.status(Status.BAD_REQUEST).build();
}
} catch (Exception e) {
return createExceptionResponse(e);
}
}

/**
* Finds and validates all the files uploaded to the multipart.
*
Expand Down Expand Up @@ -411,38 +368,6 @@ public Response getDataset(@PathParam("datasetId") Integer datasetId) {
}
}

@GET
@Path("/study/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
public Response getStudyById(@PathParam("studyId") Integer studyId) {
try {
Study study = datasetService.getStudyWithDatasetsById(studyId);
return Response.ok(study).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@GET
@Path("/study/registration/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
public Response getRegistrationFromStudy(@Auth AuthUser authUser,
@PathParam("studyId") Integer studyId) {
try {
Study study = datasetService.getStudyWithDatasetsById(studyId);
List<Dataset> datasets =
Objects.nonNull(study.getDatasets()) ? study.getDatasets().stream().toList() : List.of();
DatasetRegistrationSchemaV1 registration = new DatasetRegistrationSchemaV1Builder().build(
study, datasets);
String entity = GsonUtil.buildGsonNullSerializer().toJson(registration);
return Response.ok().entity(entity).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@GET
@Path("/registration/{datasetIdentifier}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package org.broadinstitute.consent.http.resources;

import com.google.gson.Gson;
import com.google.inject.Inject;
import io.dropwizard.auth.Auth;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.broadinstitute.consent.http.enumeration.UserRoles;
import org.broadinstitute.consent.http.models.AuthUser;
import org.broadinstitute.consent.http.models.Dataset;
import org.broadinstitute.consent.http.models.Study;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.dataset_registration_v1.DatasetRegistrationSchemaV1;
import org.broadinstitute.consent.http.models.dataset_registration_v1.DatasetRegistrationSchemaV1UpdateValidator;
import org.broadinstitute.consent.http.models.dataset_registration_v1.builder.DatasetRegistrationSchemaV1Builder;
import org.broadinstitute.consent.http.service.DatasetRegistrationService;
import org.broadinstitute.consent.http.service.DatasetService;
import org.broadinstitute.consent.http.service.ElasticSearchService;
import org.broadinstitute.consent.http.service.UserService;
import org.broadinstitute.consent.http.util.gson.GsonUtil;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Path("api/dataset/study")
public class StudyResource extends Resource {

private final DatasetService datasetService;
private final DatasetRegistrationService datasetRegistrationService;
private final UserService userService;
private final ElasticSearchService elasticSearchService;


@Inject
public StudyResource(DatasetService datasetService, UserService userService,
DatasetRegistrationService datasetRegistrationService,
ElasticSearchService elasticSearchService) {
this.datasetService = datasetService;
this.userService = userService;
this.datasetRegistrationService = datasetRegistrationService;
this.elasticSearchService = elasticSearchService;
}

@GET
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
public Response getStudyById(@PathParam("studyId") Integer studyId) {
try {
Study study = datasetService.getStudyWithDatasetsById(studyId);
return Response.ok(study).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@DELETE
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
public Response deleteStudyById(@Auth AuthUser authUser, @PathParam("studyId") Integer studyId) {
try {
User user = userService.findUserByEmail(authUser.getEmail());
Study study = datasetService.getStudyWithDatasetsById(studyId);

if (Objects.isNull(study)) {
throw new NotFoundException("Study not found");
}

// If the user is not an admin, ensure that they are the study/dataset creator
if (!user.hasUserRole(UserRoles.ADMIN) && (!Objects.equals(study.getCreateUserId(),
user.getUserId()))) {
throw new NotFoundException("Study not found");
}

boolean deletable = study.getDatasets()
.stream()
.allMatch(Dataset::getDeletable);
if (!deletable) {
throw new BadRequestException("Study has datasets that are in use and cannot be deleted.");
}
Set<Integer> studyDatasetIds = study.getDatasetIds();
datasetService.deleteStudy(study, user);
// Remove from ES index
studyDatasetIds.forEach(id -> {
try {
elasticSearchService.deleteIndex(id);
} catch (IOException e) {
logException(e);
}
});
return Response.ok().build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@GET
@Path("/registration/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
public Response getRegistrationFromStudy(@Auth AuthUser authUser,
@PathParam("studyId") Integer studyId) {
try {
Study study = datasetService.getStudyWithDatasetsById(studyId);
List<Dataset> datasets =
Objects.nonNull(study.getDatasets()) ? study.getDatasets().stream().toList() : List.of();
DatasetRegistrationSchemaV1 registration = new DatasetRegistrationSchemaV1Builder().build(
study, datasets);
String entity = GsonUtil.buildGsonNullSerializer().toJson(registration);
return Response.ok().entity(entity).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}

@PUT
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.APPLICATION_JSON})
@Path("/{studyId}")
@RolesAllowed({ADMIN, CHAIRPERSON, DATASUBMITTER})
/*
* This endpoint accepts a json instance of a dataset-registration-schema_v1.json schema.
* With that object, we can fully update the study/datasets from the provided values.
*/
public Response updateStudyByRegistration(
@Auth AuthUser authUser,
FormDataMultiPart multipart,
@PathParam("studyId") Integer studyId,
@FormDataParam("dataset") String json) {
try {
User user = userService.findUserByEmail(authUser.getEmail());
Study existingStudy = datasetRegistrationService.findStudyById(studyId);

// Manually validate the schema from an editing context. Validation with the schema tools
// enforces it in a creation context but doesn't work for editing purposes.
DatasetRegistrationSchemaV1UpdateValidator updateValidator = new DatasetRegistrationSchemaV1UpdateValidator();
Gson gson = GsonUtil.gsonBuilderWithAdapters().create();
DatasetRegistrationSchemaV1 registration = gson.fromJson(json,
DatasetRegistrationSchemaV1.class);

if (updateValidator.validate(existingStudy, registration)) {
// Update study from registration
Map<String, FormDataBodyPart> files = extractFilesFromMultiPart(multipart);
Study updatedStudy = datasetRegistrationService.updateStudyFromRegistration(
studyId,
registration,
user,
files);
return Response.ok(updatedStudy).build();
} else {
return Response.status(Status.BAD_REQUEST).build();
}
} catch (Exception e) {
return createExceptionResponse(e);
}
}

/**
* Finds and validates all the files uploaded to the multipart.
*
* @param multipart Form data
* @return Map of file body parts, where the key is the name of the field and the value is the
* body part including the file(s).
*/
private Map<String, FormDataBodyPart> extractFilesFromMultiPart(FormDataMultiPart multipart) {
if (Objects.isNull(multipart)) {
return Map.of();
}

Map<String, FormDataBodyPart> files = new HashMap<>();
for (List<FormDataBodyPart> parts : multipart.getFields().values()) {
for (FormDataBodyPart part : parts) {
if (Objects.nonNull(part.getContentDisposition().getFileName())) {
validateFileDetails(part.getContentDisposition());
files.put(part.getName(), part);
}
}
}

return files;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ public void deleteDataset(Integer datasetId, Integer userId) throws Exception {
}
}

public void deleteStudy(Study study, User user) throws Exception {
datasetServiceDAO.deleteStudy(study, user);
}

public List<Dataset> searchDatasets(String query, AccessManagement accessManagement, User user) {
List<Dataset> datasets = findAllDatasetsByUser(user);
return datasets.stream().filter(ds -> ds.isDatasetMatch(query, accessManagement)).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.broadinstitute.consent.http.service.dao;

public class DatasetDeletionException extends RuntimeException {

public DatasetDeletionException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.broadinstitute.consent.http.models.FileStorageObject;
import org.broadinstitute.consent.http.models.Study;
import org.broadinstitute.consent.http.models.StudyProperty;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.util.ConsentLogger;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
Expand Down Expand Up @@ -65,6 +66,29 @@ public void deleteDataset(Dataset dataset, Integer userId) throws Exception {
});
}

public void deleteStudy(Study study, User user) throws Exception {
jdbi.useHandle(handle -> {
handle.getConnection().setAutoCommit(false);
study.getDatasets().forEach(d -> {
try {
deleteDataset(d, user.getUserId());
} catch (Exception e) {
handle.rollback();
logException(e);
throw new DatasetDeletionException(e);
}
});
try {
studyDAO.deleteStudyByStudyId(study.getStudyId());
} catch (Exception e) {
handle.rollback();
logException(e);
throw e;
}
handle.commit();
});
}

public record StudyInsert(String name,
String description,
List<String> dataTypes,
Expand Down Expand Up @@ -217,9 +241,9 @@ private Integer executeInsertStudy(Handle handle, StudyInsert insert) {
public Study updateStudy(StudyUpdate studyUpdate, List<DatasetUpdate> datasetUpdates,
List<DatasetServiceDAO.DatasetInsert> datasetInserts) throws SQLException {
jdbi.useHandle(
handle -> {
handle.getConnection().setAutoCommit(false);
executeUpdateStudy(handle, studyUpdate);
handle -> {
handle.getConnection().setAutoCommit(false);
executeUpdateStudy(handle, studyUpdate);
for (DatasetUpdate datasetUpdate : datasetUpdates) {
executeUpdateDatasetWithFiles(
handle,
Expand Down
Loading

0 comments on commit e001f48

Please sign in to comment.