use of org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO in project consent by DataBiosphere.
the class DataSetPropertiesMapper method map.
public DatasetDTO map(ResultSet r, StatementContext ctx) throws SQLException {
DatasetDTO dataSetDTO;
Integer dataSetId = r.getInt("dataSetId");
String consentId = r.getString("consentId");
Integer alias = r.getInt("alias");
if (!dataSets.containsKey(dataSetId)) {
dataSetDTO = new DatasetDTO(new ArrayList<>());
if (hasColumn(r, "dac_id")) {
int dacId = r.getInt("dac_id");
if (dacId > 0) {
dataSetDTO.setDacId(dacId);
}
}
dataSetDTO.setConsentId(consentId);
dataSetDTO.setAlias(alias);
dataSetDTO.setDataSetId(dataSetId);
dataSetDTO.setActive(r.getBoolean("active"));
dataSetDTO.setTranslatedUseRestriction(r.getString("translatedUseRestriction"));
if (hasColumn(r, "datause")) {
dataSetDTO.setDataUse(DataUse.parseDataUse(r.getString("datause")).orElse(null));
}
if (hasColumn(r, "createdate")) {
dataSetDTO.setCreateDate(r.getDate("createdate"));
}
if (hasColumn(r, "create_user_id")) {
int userId = r.getInt("create_user_id");
if (userId > 0) {
dataSetDTO.setCreateUserId(userId);
}
}
if (hasColumn(r, "update_date")) {
dataSetDTO.setUpdateDate(r.getTimestamp("update_date"));
}
if (hasColumn(r, "update_user_id")) {
int userId = r.getInt("update_user_id");
if (userId > 0) {
dataSetDTO.setUpdateUserId(userId);
}
}
DataSetPropertyDTO property = new DataSetPropertyDTO("Dataset Name", r.getString("name"));
dataSetDTO.addProperty(property);
property = new DataSetPropertyDTO(r.getString(PROPERTY_KEY), r.getString(PROPERTY_PROPERTYVALUE));
if (property.getPropertyName() != null) {
dataSetDTO.addProperty(property);
}
dataSetDTO.setNeedsApproval(r.getBoolean("needs_approval"));
dataSetDTO.setObjectId(r.getString("objectId"));
dataSets.put(dataSetId, dataSetDTO);
} else {
dataSetDTO = dataSets.get(dataSetId);
DataSetPropertyDTO property = new DataSetPropertyDTO(r.getString(PROPERTY_KEY), r.getString(PROPERTY_PROPERTYVALUE));
if (property.getPropertyName() != null) {
dataSetDTO.addProperty(property);
}
}
return dataSetDTO;
}
use of org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO in project consent by DataBiosphere.
the class DatasetResourceTest method testCreateDatasetDuplicateProperties.
@Test(expected = BadRequestException.class)
public void testCreateDatasetDuplicateProperties() {
List<DataSetPropertyDTO> duplicateProperties = new ArrayList<>();
duplicateProperties.add(new DataSetPropertyDTO("Dataset Name", "test"));
duplicateProperties.add(new DataSetPropertyDTO("Dataset Name", "test"));
when(datasetService.findDuplicateProperties(any())).thenReturn(duplicateProperties);
String json = createPropertiesJson(duplicateProperties);
initResource();
resource.createDataset(authUser, uriInfo, json);
}
use of org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO in project consent by DataBiosphere.
the class DatasetResource method downloadDataSets.
@POST
@Path("/download")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@PermitAll
public Response downloadDataSets(List<Integer> idList) {
try {
String msg = "GETing DataSets to download";
logger().debug(msg);
JSONObject json = new JSONObject();
Collection<Dictionary> headers = datasetService.describeDictionaryByReceiveOrder();
StringBuilder sb = new StringBuilder();
String TSV_DELIMITER = "\t";
for (Dictionary header : headers) {
if (sb.length() > 0)
sb.append(TSV_DELIMITER);
sb.append(header.getKey());
}
sb.append(END_OF_LINE);
if (CollectionUtils.isEmpty(idList)) {
json.put("datasets", sb.toString());
return Response.ok(json.toString(), MediaType.APPLICATION_JSON).build();
}
Collection<DatasetDTO> rows = datasetService.describeDataSetsByReceiveOrder(idList);
for (DatasetDTO row : rows) {
StringBuilder sbr = new StringBuilder();
DataSetPropertyDTO property = new DataSetPropertyDTO("Consent ID", row.getConsentId());
List<DataSetPropertyDTO> props = row.getProperties();
props.add(property);
for (DataSetPropertyDTO prop : props) {
if (sbr.length() > 0)
sbr.append(TSV_DELIMITER);
sbr.append(prop.getPropertyValue());
}
sbr.append(END_OF_LINE);
sb.append(sbr);
}
String tsv = sb.toString();
json.put("datasets", tsv);
return Response.ok(json.toString(), MediaType.APPLICATION_JSON).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}
use of org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO in project consent by DataBiosphere.
the class DatasetResource method createDataset.
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("/v2")
@RolesAllowed({ ADMIN, CHAIRPERSON })
public Response createDataset(@Auth AuthUser authUser, @Context UriInfo info, String json) {
DatasetDTO inputDataset = new Gson().fromJson(json, DatasetDTO.class);
if (Objects.isNull(inputDataset)) {
throw new BadRequestException("Dataset is required");
}
if (Objects.isNull(inputDataset.getProperties()) || inputDataset.getProperties().isEmpty()) {
throw new BadRequestException("Dataset must contain required properties");
}
List<DataSetPropertyDTO> invalidProperties = datasetService.findInvalidProperties(inputDataset.getProperties());
if (invalidProperties.size() > 0) {
List<String> invalidKeys = invalidProperties.stream().map(DataSetPropertyDTO::getPropertyName).collect(Collectors.toList());
throw new BadRequestException("Dataset contains invalid properties that could not be recognized or associated with a key: " + invalidKeys.toString());
}
List<DataSetPropertyDTO> duplicateProperties = datasetService.findDuplicateProperties(inputDataset.getProperties());
if (duplicateProperties.size() > 0) {
throw new BadRequestException("Dataset contains multiple values for the same property.");
}
String name = "";
try {
name = inputDataset.getPropertyValue("Dataset Name");
} catch (IndexOutOfBoundsException e) {
throw new BadRequestException("Dataset name is required");
}
if (Objects.isNull(name) || name.isBlank()) {
throw new BadRequestException("Dataset name is required");
}
DataSet datasetNameAlreadyUsed = datasetService.getDatasetByName(name);
if (Objects.nonNull(datasetNameAlreadyUsed)) {
throw new ClientErrorException("Dataset name: " + name + " is already in use", Status.CONFLICT);
}
User dacUser = userService.findUserByEmail(authUser.getGoogleUser().getEmail());
Integer userId = dacUser.getDacUserId();
try {
DatasetDTO createdDatasetWithConsent = datasetService.createDatasetWithConsent(inputDataset, name, userId);
URI uri = info.getRequestUriBuilder().replacePath("api/dataset/{datasetId}").build(createdDatasetWithConsent.getDataSetId());
return Response.created(uri).entity(createdDatasetWithConsent).build();
} catch (Exception e) {
return createExceptionResponse(e);
}
}
use of org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO in project consent by DataBiosphere.
the class DatasetResource method updateDataset.
@PUT
@Consumes("application/json")
@Produces("application/json")
@Path("/{datasetId}")
@RolesAllowed({ ADMIN, CHAIRPERSON })
public Response updateDataset(@Auth AuthUser authUser, @Context UriInfo info, @PathParam("datasetId") Integer datasetId, String json) {
try {
DatasetDTO inputDataset = new Gson().fromJson(json, DatasetDTO.class);
if (Objects.isNull(inputDataset)) {
throw new BadRequestException("Dataset is required");
}
if (Objects.isNull(inputDataset.getProperties()) || inputDataset.getProperties().isEmpty()) {
throw new BadRequestException("Dataset must contain required properties");
}
DataSet datasetExists = datasetService.findDatasetById(datasetId);
if (Objects.isNull(datasetExists)) {
throw new NotFoundException("Could not find the dataset with id: " + datasetId);
}
List<DataSetPropertyDTO> invalidProperties = datasetService.findInvalidProperties(inputDataset.getProperties());
if (invalidProperties.size() > 0) {
List<String> invalidKeys = invalidProperties.stream().map(DataSetPropertyDTO::getPropertyName).collect(Collectors.toList());
throw new BadRequestException("Dataset contains invalid properties that could not be recognized or associated with a key: " + invalidKeys.toString());
}
List<DataSetPropertyDTO> duplicateProperties = datasetService.findDuplicateProperties(inputDataset.getProperties());
if (duplicateProperties.size() > 0) {
throw new BadRequestException("Dataset contains multiple values for the same property.");
}
User user = userService.findUserByEmail(authUser.getGoogleUser().getEmail());
// Validate that the admin/chairperson has edit access to this dataset
validateDatasetDacAccess(user, datasetExists);
Integer userId = user.getDacUserId();
Optional<DataSet> updatedDataset = datasetService.updateDataset(inputDataset, datasetId, userId);
if (updatedDataset.isPresent()) {
URI uri = info.getRequestUriBuilder().replacePath("api/dataset/{datasetId}").build(updatedDataset.get().getDataSetId());
return Response.ok(uri).entity(updatedDataset.get()).build();
} else {
return Response.noContent().build();
}
} catch (Exception e) {
return createExceptionResponse(e);
}
}
Aggregations