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
@@ -1,187 +1,31 @@
/*
* 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.brapps.importer.daos;

import io.micronaut.context.annotation.Property;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.brapi.client.v2.modules.core.TrialsApi;
import org.brapi.v2.model.core.BrAPITrial;
import org.brapi.v2.model.core.request.BrAPITrialSearchRequest;
import org.breedinginsight.brapps.importer.model.ImportUpload;
import org.breedinginsight.brapps.importer.services.ExternalReferenceSource;
import org.breedinginsight.daos.ProgramDAO;
import org.breedinginsight.model.Program;
import org.breedinginsight.services.ProgramService;
import org.breedinginsight.services.brapi.BrAPIEndpointProvider;
import org.breedinginsight.services.exceptions.DoesNotExistException;
import org.breedinginsight.utilities.BrAPIDAOUtil;
import org.breedinginsight.utilities.Utilities;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Singleton
public class BrAPITrialDAO {
@Property(name = "brapi.server.reference-source")
private String BRAPI_REFERENCE_SOURCE;
public interface BrAPITrialDAO {
List<BrAPITrial> getTrialsByName(List<String> trialNames, Program program) throws ApiException;

private final ProgramDAO programDAO;
private final ImportDAO importDAO;
private final BrAPIDAOUtil brAPIDAOUtil;
private final ProgramService programService;
private final BrAPIEndpointProvider brAPIEndpointProvider;
List<BrAPITrial> createBrAPITrials(List<BrAPITrial> brAPITrialList, UUID programId, ImportUpload upload)
throws ApiException;

private final String referenceSource;
BrAPITrial updateBrAPITrial(String trialDbId, BrAPITrial trial, UUID programId) throws ApiException;

@Inject
public BrAPITrialDAO(ProgramDAO programDAO, ImportDAO importDAO, BrAPIDAOUtil brAPIDAOUtil, ProgramService programService, @Property(name = "brapi.server.reference-source") String referenceSource, BrAPIEndpointProvider brAPIEndpointProvider) {
this.programDAO = programDAO;
this.importDAO = importDAO;
this.brAPIDAOUtil = brAPIDAOUtil;
this.programService = programService;
this.referenceSource = referenceSource;
this.brAPIEndpointProvider = brAPIEndpointProvider;
}
List<BrAPITrial> getTrials(UUID programId) throws ApiException;

public List<BrAPITrial> getTrialsByName(List<String> trialNames, Program program) throws ApiException {
if(trialNames.isEmpty()) {
return Collections.emptyList();
}
Optional<BrAPITrial> getTrialById(UUID programId, UUID trialId) throws ApiException, DoesNotExistException;

BrAPITrialSearchRequest trialSearch = new BrAPITrialSearchRequest();
trialSearch.programDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
trialSearch.trialNames(trialNames);
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), TrialsApi.class);
return brAPIDAOUtil.search(
api::searchTrialsPost,
api::searchTrialsSearchResultsDbIdGet,
trialSearch
);
}
Optional<BrAPITrial> getTrialByDbId(String trialDbId, Program program) throws ApiException;

private List<BrAPITrial> getTrialsByExRef(String referenceSource, String referenceId, Program program) throws ApiException {
BrAPITrialSearchRequest trialSearch = new BrAPITrialSearchRequest();
trialSearch.programDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
trialSearch.externalReferenceSources(List.of(referenceSource));
trialSearch.externalReferenceIDs(List.of(referenceId));
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), TrialsApi.class);
return brAPIDAOUtil.search(
api::searchTrialsPost,
api::searchTrialsSearchResultsDbIdGet,
trialSearch
);
}

public List<BrAPITrial> createBrAPITrials(List<BrAPITrial> brAPITrialList, UUID programId, ImportUpload upload) throws ApiException {
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), TrialsApi.class);
return brAPIDAOUtil.post(brAPITrialList, upload, api::trialsPost, importDAO::update);
}
public BrAPITrial updateBrAPITrial(String trialDbId, BrAPITrial trial, UUID programId) throws ApiException {
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), TrialsApi.class);
return brAPIDAOUtil.put(trialDbId, trial, api::trialsTrialDbIdPut);
}
/**
* Fetch formatted trials/experiments for this program
* @param programId
* @return List<formatted BrAPITrial>
* @throws ApiException
*/
public List<BrAPITrial> getTrials(UUID programId) throws ApiException, DoesNotExistException {
BrAPITrialSearchRequest trialSearch = new BrAPITrialSearchRequest();
//TODO check external references filter works once implemented in BI-1552
trialSearch.externalReferenceSources(List.of(Utilities.generateReferenceSource(referenceSource, ExternalReferenceSource.PROGRAMS)));
trialSearch.externalReferenceIDs(List.of(programId.toString()));

Program program = programService.getById(programId).orElseThrow(() -> new DoesNotExistException("Program id does not exist"));
trialSearch.programDbIds(List.of(program.getBrapiProgram().getProgramDbId()));

TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), TrialsApi.class);
return processExperimentsForDisplay(brAPIDAOUtil.search(
api::searchTrialsPost,
api::searchTrialsSearchResultsDbIdGet,
trialSearch), program.getKey(), programId, true);
}

//Removes program key from trial name and adds dataset information
private List<BrAPITrial> processExperimentsForDisplay(List<BrAPITrial> trials, String programKey, UUID programId, boolean metadata) throws ApiException {
List<BrAPITrial> displayExperiments = new ArrayList<>();
for (BrAPITrial trial: trials) {
trial.setTrialName(Utilities.removeProgramKey(trial.getTrialName(), programKey, ""));
List<String> datasets = new ArrayList<>();

if (metadata) {
//todo presumably BI-1193 replace dummy value with list of datasets once datasets implemented
datasets.add("Observation Dataset");
trial.putAdditionalInfoItem("datasets", datasets);
}

displayExperiments.add(trial);
}
return displayExperiments;
}

public Optional<BrAPITrial> getTrialById(UUID programId, UUID trialDbId) throws ApiException, DoesNotExistException {
Program program = programService.getById(programId).orElseThrow(() -> new DoesNotExistException("Program id does not exist"));
String refSoure = Utilities.generateReferenceSource(BRAPI_REFERENCE_SOURCE, ExternalReferenceSource.TRIALS);
List<BrAPITrial> trials = getTrialsByExRef(refSoure, trialDbId.toString(), program);

return Utilities.getSingleOptional(trials);
}

public Optional<BrAPITrial> getTrialByDbId(String trialDbId, Program program) throws ApiException {
List<BrAPITrial> trials = getTrialsByDbIds(List.of(trialDbId), program);
return Utilities.getSingleOptional(trials);
}
public List<BrAPITrial> getTrialsByDbIds(Collection<String> trialDbIds, Program program) throws ApiException {
if(trialDbIds.isEmpty()) {
return Collections.emptyList();
}

BrAPITrialSearchRequest trialSearch = new BrAPITrialSearchRequest();
trialSearch.programDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
trialSearch.trialDbIds(new ArrayList<>(trialDbIds));
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), TrialsApi.class);
return brAPIDAOUtil.search(
api::searchTrialsPost,
api::searchTrialsSearchResultsDbIdGet,
trialSearch
);
}

public List<BrAPITrial> getTrialsByExperimentIds(Collection<UUID> experimentIds, Program program) throws ApiException {
if(experimentIds.isEmpty()) {
return Collections.emptyList();
}

BrAPITrialSearchRequest trialSearch = new BrAPITrialSearchRequest();
trialSearch.programDbIds(List.of(program.getBrapiProgram().getProgramDbId()));
//trialSearch.trialDbIds(experimentIds.stream().map(id -> id.toString()).collect(Collectors.toList()));
trialSearch.externalReferenceSources(List.of(referenceSource + "/" + ExternalReferenceSource.TRIALS.getName()));
trialSearch.externalReferenceIDs(experimentIds.stream().map(id -> id.toString()).collect(Collectors.toList()));
TrialsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(program.getId()), TrialsApi.class);
return brAPIDAOUtil.search(
api::searchTrialsPost,
api::searchTrialsSearchResultsDbIdGet,
trialSearch
);
}
List<BrAPITrial> getTrialsByDbIds(Collection<String> trialDbIds, Program program) throws ApiException;

List<BrAPITrial> getTrialsByExperimentIds(Collection<UUID> experimentIds, Program program) throws ApiException;
}


Loading