Search in sources :

Example 1 with DataSetPropertyDTO

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;
}
Also used : DatasetDTO(org.broadinstitute.consent.http.models.dto.DatasetDTO) DataSetPropertyDTO(org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO) ArrayList(java.util.ArrayList)

Example 2 with DataSetPropertyDTO

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);
}
Also used : DataSetPropertyDTO(org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 3 with DataSetPropertyDTO

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);
    }
}
Also used : DatasetDTO(org.broadinstitute.consent.http.models.dto.DatasetDTO) Dictionary(org.broadinstitute.consent.http.models.Dictionary) JSONObject(org.json.JSONObject) DataSetPropertyDTO(org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO) ClientErrorException(javax.ws.rs.ClientErrorException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PermitAll(javax.annotation.security.PermitAll)

Example 4 with DataSetPropertyDTO

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);
    }
}
Also used : User(org.broadinstitute.consent.http.models.User) AuthUser(org.broadinstitute.consent.http.models.AuthUser) DataSet(org.broadinstitute.consent.http.models.DataSet) Gson(com.google.gson.Gson) URI(java.net.URI) ClientErrorException(javax.ws.rs.ClientErrorException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) DatasetDTO(org.broadinstitute.consent.http.models.dto.DatasetDTO) DataSetPropertyDTO(org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO) BadRequestException(javax.ws.rs.BadRequestException) ClientErrorException(javax.ws.rs.ClientErrorException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 5 with DataSetPropertyDTO

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);
    }
}
Also used : User(org.broadinstitute.consent.http.models.User) AuthUser(org.broadinstitute.consent.http.models.AuthUser) DataSet(org.broadinstitute.consent.http.models.DataSet) Gson(com.google.gson.Gson) NotFoundException(javax.ws.rs.NotFoundException) URI(java.net.URI) ClientErrorException(javax.ws.rs.ClientErrorException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) DatasetDTO(org.broadinstitute.consent.http.models.dto.DatasetDTO) DataSetPropertyDTO(org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Aggregations

DataSetPropertyDTO (org.broadinstitute.consent.http.models.dto.DataSetPropertyDTO)17 DatasetDTO (org.broadinstitute.consent.http.models.dto.DatasetDTO)11 ArrayList (java.util.ArrayList)10 DataSet (org.broadinstitute.consent.http.models.DataSet)10 Test (org.junit.Test)8 NotFoundException (javax.ws.rs.NotFoundException)6 DataSetProperty (org.broadinstitute.consent.http.models.DataSetProperty)4 Dictionary (org.broadinstitute.consent.http.models.Dictionary)4 Timestamp (java.sql.Timestamp)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3 UUID (java.util.UUID)3