Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
*/
package org.breedinginsight.api.v1.controller.geno;

import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.http.multipart.CompletedFileUpload;
import io.micronaut.http.server.types.files.StreamedFile;
import lombok.extern.slf4j.Slf4j;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.breedinginsight.api.auth.*;
Expand All @@ -30,6 +33,7 @@
import org.breedinginsight.api.model.v1.validators.QueryValid;
import org.breedinginsight.api.v1.controller.metadata.AddMetadata;
import org.breedinginsight.brapps.importer.model.response.ImportResponse;
import org.breedinginsight.model.DownloadFile;
import org.breedinginsight.model.GenotypeImportDetails;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
Expand All @@ -41,6 +45,7 @@

import javax.inject.Inject;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;

Expand Down Expand Up @@ -83,6 +88,30 @@ public HttpResponse<Response<DataResponse<GenotypeImportDetails>>> getGenotypeIm
);
}

@Get("programs/{programId}/geno/imports/{genotypeImportId}/download")
@ProgramSecured(roleGroups = ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES)
@Produces(value = {"application/octet-stream"})
public HttpResponse<StreamedFile> downloadGenotypeImport(@PathVariable UUID programId, @PathVariable UUID genotypeImportId) {
Optional<Program> program = programService.getById(programId);
if (program.isEmpty()) {
log.info("programId not found: {}", programId.toString());
return HttpResponse.notFound();
}

try {
Optional<DownloadFile> downloadFile = genoService.downloadGenotypeImport(programId, genotypeImportId);
if (downloadFile.isEmpty()) {
return HttpResponse.notFound();
}

return HttpResponse.ok(downloadFile.get().getStreamedFile())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + downloadFile.get().getFileName());
} catch (IOException e) {
log.error("Error downloading genotype import", e);
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR, "Error downloading genotype import");
}
}

@Post("programs/{programId}/submissions/{submissionId}/geno/import")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/breedinginsight/daos/GenotypeImportDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import org.breedinginsight.dao.db.tables.daos.GenotypeImportDao;
import org.breedinginsight.dao.db.tables.pojos.GenotypeImportEntity;
import org.breedinginsight.model.GenotypeImportDetails;
import org.breedinginsight.model.GenotypeImportDownloadDetails;
import org.jooq.Configuration;
import org.jooq.DSLContext;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.breedinginsight.dao.db.Tables.*;
Expand Down Expand Up @@ -62,6 +64,7 @@ public List<GenotypeImportDetails> getGenotypeImportsByProgramId(UUID programId)
BiUserTable genotypingImportByUser = BI_USER.as("genotypingImportByUser");

return dsl.select(
GENOTYPE_IMPORT.ID,
SAMPLE_SUBMISSION.ID,
SAMPLE_SUBMISSION.NAME,
sampleSubmissionCreatedByUser.NAME,
Expand All @@ -82,4 +85,19 @@ public List<GenotypeImportDetails> getGenotypeImportsByProgramId(UUID programId)
.parseSqlRecord(record, sampleSubmissionCreatedByUser, genotypingImportByUser));
}

public Optional<GenotypeImportDownloadDetails> getDownloadableGenotypeImportById(UUID programId, UUID genotypeImportId) {
return dsl.select(
GENOTYPE_IMPORT.SAMPLE_SUBMISSION_ID,
GENOTYPE_IMPORT.IMPORTER_IMPORT_ID,
IMPORTER_IMPORT.UPLOAD_FILE_NAME)
.from(GENOTYPE_IMPORT)
.join(SAMPLE_SUBMISSION).on(GENOTYPE_IMPORT.SAMPLE_SUBMISSION_ID.eq(SAMPLE_SUBMISSION.ID))
.join(IMPORTER_IMPORT).on(GENOTYPE_IMPORT.IMPORTER_IMPORT_ID.eq(IMPORTER_IMPORT.ID))
.join(IMPORTER_PROGRESS).on(IMPORTER_IMPORT.IMPORTER_PROGRESS_ID.eq(IMPORTER_PROGRESS.ID))
.where(GENOTYPE_IMPORT.ID.eq(genotypeImportId))
.and(SAMPLE_SUBMISSION.PROGRAM_ID.eq(programId))
.and(IMPORTER_IMPORT.PROGRAM_ID.eq(programId))
.and(IMPORTER_PROGRESS.STATUSCODE.eq((short) HttpStatus.OK.getCode()))
.fetchOptional(GenotypeImportDownloadDetails::parseSqlRecord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.breedinginsight.dao.db.Tables.IMPORTER_IMPORT;
import static org.breedinginsight.dao.db.Tables.SAMPLE_SUBMISSION;
import static org.breedinginsight.dao.db.Tables.GENOTYPE_IMPORT;

@Getter
@Setter
Expand All @@ -43,6 +44,7 @@
@Introspected
@Jacksonized
public class GenotypeImportDetails {
private UUID genotypeImportId;
private UUID sampleSubmissionId;
private String projectNameForSampleSubmission;
private String sampleSubmissionCreatedBy;
Expand All @@ -54,6 +56,7 @@ public static GenotypeImportDetails parseSqlRecord(Record record,
BiUserTable sampleSubmissionCreatedByUser,
BiUserTable genotypingImportByUser) {
return GenotypeImportDetails.builder()
.genotypeImportId(record.get(GENOTYPE_IMPORT.ID))
.sampleSubmissionId(record.get(SAMPLE_SUBMISSION.ID))
.projectNameForSampleSubmission(record.get(SAMPLE_SUBMISSION.NAME))
.sampleSubmissionCreatedBy(record.get(sampleSubmissionCreatedByUser.NAME))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.breedinginsight.model;

import io.micronaut.core.annotation.Introspected;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
import org.jooq.Record;

import java.util.UUID;

import static org.breedinginsight.dao.db.Tables.GENOTYPE_IMPORT;
import static org.breedinginsight.dao.db.Tables.IMPORTER_IMPORT;

@Getter
@Setter
@Accessors(chain = true)
@ToString
@SuperBuilder
@NoArgsConstructor
@Introspected
@Jacksonized
public class GenotypeImportDownloadDetails {
private UUID sampleSubmissionId;
private UUID importerImportId;
private String genotypeFileName;

public static GenotypeImportDownloadDetails parseSqlRecord(Record record) {
return GenotypeImportDownloadDetails.builder()
.sampleSubmissionId(record.get(GENOTYPE_IMPORT.SAMPLE_SUBMISSION_ID))
.importerImportId(record.get(GENOTYPE_IMPORT.IMPORTER_IMPORT_ID))
.genotypeFileName(record.get(IMPORTER_IMPORT.UPLOAD_FILE_NAME))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import io.micronaut.http.multipart.CompletedFileUpload;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.breedinginsight.brapps.importer.model.response.ImportResponse;
import org.breedinginsight.model.DownloadFile;
import org.breedinginsight.model.GermplasmGenotype;
import org.breedinginsight.services.exceptions.AuthorizationException;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.model.GenotypeImportDetails;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface GenotypeService {
Expand All @@ -33,4 +36,6 @@ public interface GenotypeService {
GermplasmGenotype retrieveGenotypeData(UUID programId, UUID germplasmId) throws DoesNotExistException, AuthorizationException, ApiException;

List<GenotypeImportDetails> getGenotypeImports(UUID programId);

Optional<DownloadFile> downloadGenotypeImport(UUID programId, UUID genotypeImportId)throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.micronaut.http.HttpStatus;
import io.micronaut.http.multipart.CompletedFileUpload;
import io.micronaut.http.server.exceptions.InternalServerException;
import io.micronaut.http.server.types.files.StreamedFile;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -46,10 +47,7 @@
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.daos.SampleSubmissionDAO;
import org.breedinginsight.daos.UserDAO;
import org.breedinginsight.model.GenotypeImportDetails;
import org.breedinginsight.model.GermplasmGenotype;
import org.breedinginsight.model.Program;
import org.breedinginsight.model.User;
import org.breedinginsight.model.*;
import org.breedinginsight.services.brapi.BrAPIEndpointProvider;
import org.breedinginsight.services.exceptions.AuthorizationException;
import org.breedinginsight.services.exceptions.DoesNotExistException;
Expand All @@ -66,6 +64,7 @@
import javax.inject.Singleton;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.*;
Expand Down Expand Up @@ -276,6 +275,52 @@ public List<GenotypeImportDetails> getGenotypeImports(UUID programId) {
return genotypeImportDAO.getGenotypeImportsByProgramId(programId);
}

@Override
public Optional<DownloadFile> downloadGenotypeImport(UUID programId, UUID genotypeImportId) {
Optional<GenotypeImportDownloadDetails> genotypeImportDownloadDetails = genotypeImportDAO
.getDownloadableGenotypeImportById(programId, genotypeImportId);

if (genotypeImportDownloadDetails.isEmpty()) {
return Optional.empty();
}

UUID submissionId = genotypeImportDownloadDetails.get().getSampleSubmissionId();
UUID importerImportId = genotypeImportDownloadDetails.get().getImporterImportId();
String originalFileName = genotypeImportDownloadDetails.get().getGenotypeFileName();

Optional<String> storedKey = findStoredGenotypeImportKey(programId, submissionId, importerImportId);
if (storedKey.isEmpty()) {
return Optional.empty();
}

InputStream inputStream = s3Client.getObject(GetObjectRequest.builder()
.bucket(storageService.getDefaultBucketName())
.key(storedKey.get())
.build());

return Optional.of(new DownloadFile(
originalFileName,
new StreamedFile(
inputStream,
new io.micronaut.http.MediaType(io.micronaut.http.MediaType.APPLICATION_OCTET_STREAM)
)
));
}

private Optional<String> findStoredGenotypeImportKey(UUID programId, UUID submissionId, UUID importerImportId) {
String prefix = programId + "/" + submissionId + "/" + importerImportId;

ListObjectsV2Response response = s3Client.listObjectsV2(ListObjectsV2Request.builder()
.bucket(storageService.getDefaultBucketName())
.prefix(prefix)
.maxKeys(1)
.build());

return response.contents().stream()
.map(S3Object::key)
.findFirst();
}

private boolean validateSamples(Program program, UUID submissionId, byte[] fileContents, ImportUpload upload) throws DoesNotExistException, ApiException {
log.debug("Validating samples in submitted VCF file for submission: " + submissionId);

Expand Down
Loading
Loading