Skip to content

Commit

Permalink
Fixes to BrAPI v1 services
Browse files Browse the repository at this point in the history
  • Loading branch information
BESOMBES Gabriel committed Dec 6, 2023
1 parent fded1f4 commit cfc5a50
Show file tree
Hide file tree
Showing 17 changed files with 646 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package org.opensilex.brapi.api;

import io.swagger.annotations.*;
import org.opensilex.brapi.responses.BrAPIv1GermplasmListResponse;
import org.opensilex.brapi.model.BrAPIv1GermplasmDTO;
import org.opensilex.brapi.responses.BrAPIv1AccessionWarning;
import org.opensilex.brapi.responses.BrAPIv1GermplasmListResponse;
import org.opensilex.core.germplasm.dal.GermplasmDAO;
import org.opensilex.core.germplasm.dal.GermplasmModel;
import org.opensilex.nosql.mongodb.MongoDBService;
import org.opensilex.security.account.dal.AccountModel;
import org.opensilex.security.authentication.ApiProtected;
import org.opensilex.security.authentication.NotFoundURIException;
import org.opensilex.security.authentication.injection.CurrentUser;
import org.opensilex.server.exceptions.BadRequestException;
import org.opensilex.server.response.ErrorResponse;
import org.opensilex.sparql.SPARQLModule;
import org.opensilex.sparql.ontology.store.OntologyStore;
import org.opensilex.sparql.service.SPARQLService;
import org.opensilex.utils.ListWithPagination;

Expand Down Expand Up @@ -47,7 +52,7 @@ public class GermplasmAPI extends BrapiCall {
@GET
@Path("v1/germplasm")
@BrapiVersion("1.3")
@ApiOperation("Submit a search request for germplasm")
@ApiOperation("Submit a search request for germplasm (type accession in OpenSILEX")
@ApiProtected
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
Expand All @@ -56,13 +61,23 @@ public class GermplasmAPI extends BrapiCall {
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorResponse.class)})

public Response getGermplasmBySearch(
@ApiParam(value = "Search by germplasmDbId") @QueryParam("germplasmDbId") URI uri,
@ApiParam(value = "Search by germplasmPUI") @QueryParam("germplasmPUI") URI germplasmPUI,
@ApiParam(value = "Search by germplasmName") @QueryParam("germplasmName") String germplasmName,
@ApiParam(value = "Search by commonCropName") @QueryParam("commonCropName") String commonCropName,
@ApiParam(value = "Search by germplasmDbId (URI of an OpenSilex accession)") @QueryParam("germplasmDbId") URI uri,
@ApiParam(value = "Search by germplasmPUI (URI of an OpenSilex accession)") @QueryParam("germplasmPUI") URI germplasmPUI,
@ApiParam(value = "Search by germplasmName (name of an OpenSilex accession)") @QueryParam("germplasmName") String germplasmName,
@ApiParam(value = "Search by commonCropName (name of the species of an OpenSilex accession)") @QueryParam("commonCropName") String commonCropName,
@ApiParam(value = "Page number", example = "0") @QueryParam("page") @DefaultValue("0") @Min(0) int page,
@ApiParam(value = "Page size", example = "20") @QueryParam("page_size") @DefaultValue("20") @Min(0) int pageSize
) throws Exception {

OntologyStore ontologyStore = SPARQLModule.getOntologyStoreInstance();
if (!ontologyStore.classExist(
BrAPIv1AccessionWarning.ACCESSION_URI,
BrAPIv1AccessionWarning.GERMPLASM_URI
)) {
throw new BadRequestException(
"The 'Accession' notion doesn't exist in your ontology so this service is unavailable"
);
}
GermplasmDAO germplasmDAO = new GermplasmDAO(sparql, nosql);

if (germplasmPUI != null && uri == null) {
Expand All @@ -77,7 +92,10 @@ public Response getGermplasmBySearch(
page,
pageSize
);

if (resultList.getList().isEmpty()){
throw new NotFoundURIException("No accession could be found with this URI ", uri);
}

// Convert paginated list to DTO
ListWithPagination<BrAPIv1GermplasmDTO> resultDTOList = resultList.convert(
BrAPIv1GermplasmDTO.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.opensilex.brapi.responses.*;
import org.opensilex.brapi.model.*;
import org.opensilex.brapi.responses.*;
import org.opensilex.core.data.dal.DataDAO;
import org.opensilex.core.data.dal.DataModel;
import org.opensilex.core.event.dal.move.MoveEventDAO;
Expand All @@ -25,6 +25,8 @@
import org.opensilex.core.scientificObject.dal.ScientificObjectDAO;
import org.opensilex.core.scientificObject.dal.ScientificObjectModel;
import org.opensilex.core.scientificObject.dal.ScientificObjectSearchFilter;
import org.opensilex.core.variable.dal.BaseVariableDAO;
import org.opensilex.core.variable.dal.MethodModel;
import org.opensilex.core.variable.dal.VariableModel;
import org.opensilex.fs.service.FileStorageService;
import org.opensilex.nosql.mongodb.MongoDBService;
Expand All @@ -49,6 +51,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* @see <a href="https://app.swaggerhub.com/apis/PlantBreedingAPI/BrAPI/1.3">BrAPI documentation 1.3</a>
Expand All @@ -74,6 +77,7 @@ public class StudiesAPI extends BrapiCall {
protected Response standardGetStudies(URI studyDbId, String active, String sortBy, String sortOrder, int page, int pageSize) throws Exception {

ExperimentDAO xpDao = new ExperimentDAO(sparql, nosql);
GermplasmDAO germplasmDAO = new GermplasmDAO(sparql, nosql);

ArrayList<OrderBy> orderByList = new ArrayList<>();

Expand Down Expand Up @@ -101,8 +105,15 @@ protected Response standardGetStudies(URI studyDbId, String active, String sortB

Boolean isEnded = !StringUtils.isEmpty(active) ? !Boolean.parseBoolean(active) : null;

if (studyDbId != null && xpDao.get(studyDbId, currentUser) == null) {
throw new NotFoundURIException(studyDbId);
ListWithPagination<ExperimentModel> resultList;

if (studyDbId != null) {
ExperimentModel expeModel = xpDao.get(studyDbId, currentUser);
if (Objects.nonNull(expeModel)){
resultList = new ListWithPagination<>(Collections.singletonList(expeModel));
} else {
throw new NotFoundURIException(studyDbId);
}
} else {
ExperimentSearchFilter filter = new ExperimentSearchFilter()
.setEnded(isEnded)
Expand All @@ -111,10 +122,18 @@ protected Response standardGetStudies(URI studyDbId, String active, String sortB
.setPage(page)
.setPageSize(pageSize);

ListWithPagination<ExperimentModel> resultList = xpDao.search(filter);
ListWithPagination<BrAPIv1StudyDTO> resultDTOList = resultList.convert(BrAPIv1StudyDTO.class, BrAPIv1StudyDTO::fromModel);
return new BrAPIv1StudyListResponse(resultDTOList).getResponse();
resultList = xpDao.search(filter);
}
ListWithPagination<BrAPIv1StudyDTO> resultDTOList = resultList.convert(BrAPIv1StudyDTO.class, experimentModel -> {
try {
return BrAPIv1StudyDTO.fromModel(experimentModel, germplasmDAO, currentUser);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
BrAPIv1StudyListResponse responseClass = new BrAPIv1StudyListResponse(resultDTOList);
BrAPIv1AccessionWarning.setAccessionWarningIfNeeded(responseClass);
return responseClass.getResponse();
}


Expand Down Expand Up @@ -187,9 +206,14 @@ public Response getStudyDetails(
OrganizationDAO organisationDAO = new OrganizationDAO(sparql, nosql);
FacilityDAO facilityDAO = new FacilityDAO(sparql, nosql, organisationDAO);
ExperimentModel model = xpDao.get(studyDbId, currentUser);
GermplasmDAO germplasmDAO = new GermplasmDAO(sparql, nosql);

if (model != null) {
return new BrAPIv1SingleStudyResponse(BrAPIv1StudyDetailsDTO.fromModel(model, facilityDAO, organisationDAO, currentUser)).getResponse();
BrAPIv1SingleStudyResponse responseClass = new BrAPIv1SingleStudyResponse(
BrAPIv1StudyDetailsDTO.fromModel(model, facilityDAO, organisationDAO, currentUser, germplasmDAO)
);
BrAPIv1AccessionWarning.setAccessionWarningIfNeeded(responseClass);
return responseClass.getResponse();
} else {
throw new NotFoundURIException(studyDbId);
}
Expand Down Expand Up @@ -229,7 +253,9 @@ public Response getObservations(
throw new RuntimeException(e);
}
});
return new BrAPIv1ObservationListResponse(observations).getResponse();
BrAPIv1ObservationListResponse responseClass = new BrAPIv1ObservationListResponse(observations);
BrAPIv1AccessionWarning.setAccessionWarningIfNeeded(responseClass);
return responseClass.getResponse();

}

Expand All @@ -252,8 +278,18 @@ public Response getObservationVariables(
DataDAO dataDAO = new DataDAO(nosql, sparql, fs);
List<VariableModel> variables = dataDAO.getUsedVariables(currentUser, Collections.singletonList(studyDbId), null, null, null);

BaseVariableDAO<MethodModel> baseVariableDAO = new BaseVariableDAO<>(MethodModel.class, sparql);
ListWithPagination<VariableModel> variablesPaginated = new ListWithPagination<>(variables, page, pageSize, variables.size());
ListWithPagination<BrAPIv1ObservationVariableDTO> resultDTOList = variablesPaginated.convert(BrAPIv1ObservationVariableDTO.class, BrAPIv1ObservationVariableDTO::fromModel);
ListWithPagination<BrAPIv1ObservationVariableDTO> resultDTOList = variablesPaginated.convert(
BrAPIv1ObservationVariableDTO.class,
variableModel -> {
try {
return BrAPIv1ObservationVariableDTO.fromModel(variableModel, baseVariableDAO);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
);
return new BrAPIv1ObservationVariableListResponse(resultDTOList).getResponse();
}

Expand Down Expand Up @@ -287,6 +323,7 @@ public Response getObservationUnits(
OntologyDAO ontologyDAO = new OntologyDAO(sparql);
MoveEventDAO moveEventDAO = new MoveEventDAO(sparql, nosql);
GeospatialDAO geospatialDAO = new GeospatialDAO(nosql);
GermplasmDAO germplasmDAO = new GermplasmDAO(sparql, nosql);

ScientificObjectSearchFilter searchFilter = new ScientificObjectSearchFilter()
.setExperiment(studyDbId)
Expand All @@ -307,13 +344,15 @@ public Response getObservationUnits(
dataDAO,
xpDao.get(studyDbId, currentUser),
ontologyDAO,
sparql,
moveEventDAO,
geospatialDAO);
geospatialDAO,
germplasmDAO);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return new BrAPIv1ObservationUnitListResponse(observations).getResponse();
BrAPIv1ObservationUnitListResponse responseClass = new BrAPIv1ObservationUnitListResponse(observations);
BrAPIv1AccessionWarning.setAccessionWarningIfNeeded(responseClass);
return responseClass.getResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.opensilex.brapi.responses.BrAPIv1ObservationVariableListResponse;
import org.opensilex.brapi.responses.BrAPIv1SingleObservationVariableResponse;
import org.opensilex.brapi.model.BrAPIv1ObservationVariableDTO;
import org.opensilex.core.variable.dal.BaseVariableDAO;
import org.opensilex.core.variable.dal.MethodModel;
import org.opensilex.core.variable.dal.VariableDAO;
import org.opensilex.core.variable.dal.VariableModel;
import org.opensilex.fs.service.FileStorageService;
Expand Down Expand Up @@ -77,7 +79,17 @@ public Response getVariablesList(
variables = varDAO.search(null, null, page, pageSize,currentUser.getLanguage());
}

ListWithPagination<BrAPIv1ObservationVariableDTO> resultDTOList = variables.convert(BrAPIv1ObservationVariableDTO.class, BrAPIv1ObservationVariableDTO::fromModel);
BaseVariableDAO<MethodModel> baseVariableDAO = new BaseVariableDAO<>(MethodModel.class, sparql);
ListWithPagination<BrAPIv1ObservationVariableDTO> resultDTOList = variables.convert(
BrAPIv1ObservationVariableDTO.class,
variableModel -> {
try {
return BrAPIv1ObservationVariableDTO.fromModel(variableModel, baseVariableDAO);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
);
return new BrAPIv1ObservationVariableListResponse(resultDTOList).getResponse();
}

Expand All @@ -98,7 +110,10 @@ public Response getVariableDetails(

VariableModel variable = variableDAO.get(observationVariableDbId);
if (variable != null) {
return new BrAPIv1SingleObservationVariableResponse(BrAPIv1ObservationVariableDTO.fromModel(variable)).getResponse();
BaseVariableDAO<MethodModel> baseVariableDAO = new BaseVariableDAO<>(MethodModel.class, sparql);
return new BrAPIv1SingleObservationVariableResponse(
BrAPIv1ObservationVariableDTO.fromModel(variable, baseVariableDAO)
).getResponse();
} else {
throw new NotFoundURIException(observationVariableDbId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ public BrAPIv1ContactDTO extractFromModel(PersonModel model, String role) {
this.setEmail(model.getEmail().toString());
}
this.setContactDbId(model.getUri().toString());
this.setName(model.getLastName().toUpperCase() + model.getFirstName().substring(0,1).toUpperCase() + model.getFirstName().substring(1));
this.setName(
model.getLastName().toUpperCase()
+ " "
+ model.getFirstName().substring(0,1).toUpperCase()
+ model.getFirstName().substring(1)
);
if (model.getOrcid() != null && !model.getOrcid().toString().isEmpty()){
this.setOrcid(model.getOrcid().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensilex.core.organisation.dal.facility.FacilityModel;
import org.opensilex.core.organisation.dal.site.SiteAddressModel;
import org.opensilex.security.account.dal.AccountModel;
import org.opensilex.server.rest.validation.model.OpenSilexLocale;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -188,7 +189,7 @@ public BrAPIv1LocationDTO extractFromModel(FacilityModel model, FacilityDAO faci
this.setInstituteName(institute.getName());
String countryName = parentAddress.getCountryName();
this.setCountryName(countryName);
this.setCountryCode(new Locale(countryName).getISO3Country());
this.setCountryCode(new OpenSilexLocale(countryName).getISO3Country());
directParentOrganizations.remove(parentsWithOneAddress.get(0));
} else if (parentsWithOneAddress.size()>1) { // If more than one, go an Organisation level above
Set<OrganizationModel> newParents = new HashSet<>();
Expand All @@ -206,7 +207,7 @@ public BrAPIv1LocationDTO extractFromModel(FacilityModel model, FacilityDAO faci
if (model.getAddress() != null && !model.getAddress().toString().isEmpty()){
String countryName = model.getAddress().getCountryName();
this.setCountryName(countryName);
this.setCountryCode(new Locale(countryName).getISO3Country());
this.setCountryCode(new OpenSilexLocale(countryName).getISO3Country());
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.opensilex.brapi.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.opensilex.core.variable.dal.BaseVariableDAO;
import org.opensilex.core.variable.dal.MethodModel;

/**
Expand Down Expand Up @@ -79,7 +80,8 @@ public void setReference(String reference) {
this.reference = reference;
}

public BrAPIv1MethodDTO extractFromModel(MethodModel methodModel){
public BrAPIv1MethodDTO extractFromModel(MethodModel methodModel, BaseVariableDAO<MethodModel> baseVariableDAO) throws Exception {
methodModel = baseVariableDAO.get(methodModel.getUri());

if (methodModel.getUri() != null){
this.setMethodDbId(methodModel.getUri().toString());
Expand All @@ -96,8 +98,8 @@ public BrAPIv1MethodDTO extractFromModel(MethodModel methodModel){
return this;
}

public static BrAPIv1MethodDTO fromModel(MethodModel methodModel){
public static BrAPIv1MethodDTO fromModel(MethodModel methodModel, BaseVariableDAO<MethodModel> baseVariableDAO) throws Exception {
BrAPIv1MethodDTO method = new BrAPIv1MethodDTO();
return method.extractFromModel(methodModel);
return method.extractFromModel(methodModel, baseVariableDAO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.vocabulary.RDFS;
import org.opensilex.brapi.responses.BrAPIv1AccessionWarning;
import org.opensilex.core.data.dal.DataModel;
import org.opensilex.core.experiment.dal.ExperimentModel;
import org.opensilex.core.germplasm.dal.GermplasmDAO;
Expand All @@ -26,6 +27,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -212,8 +214,10 @@ public BrAPIv1ObservationDTO extractFromModel(DataModel dataModel, ExperimentMod
List<SPARQLModelRelation> germplasms = objectModel.getRelations(Oeso.hasGermplasm).distinct().collect(Collectors.toList());
if (germplasms.size() >= 1){
GermplasmModel germplasmModel = germplasmDAO.get(new URI(germplasms.get(0).getValue()), currentUser, false);
this.setGermplasmDbId(germplasmModel.getUri().toString());
this.setGermplasmName(germplasmModel.getName());
if (Objects.equals(germplasmModel.getType(), BrAPIv1AccessionWarning.ACCESSION_URI)) {
this.setGermplasmDbId(germplasmModel.getUri().toString());
this.setGermplasmName(germplasmModel.getName());
}
}
}

Expand Down
Loading

0 comments on commit cfc5a50

Please sign in to comment.