diff --git a/src/main/java/org/brapi/test/BrAPITestServer/controller/core/BrAPIController.java b/src/main/java/org/brapi/test/BrAPITestServer/controller/core/BrAPIController.java index e7ec4d54..1b4a5ddf 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/controller/core/BrAPIController.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/controller/core/BrAPIController.java @@ -11,7 +11,7 @@ import io.swagger.model.core.BatchDeletesListResponseResult; import org.brapi.test.BrAPITestServer.auth.AuthDetails; import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; -import org.brapi.test.BrAPITestServer.exceptions.InvalidPagingException; +import org.brapi.test.BrAPITestServer.service.PagingUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -33,21 +33,6 @@ public class BrAPIController { private static final Logger log = LoggerFactory.getLogger(ServerInfoApiController.class); - - protected Metadata generateMetaDataTemplateForSearch(Integer originalRequestedPage, Integer newRequestedPage, - Integer originalRequestedPageSize, Integer newRequestedPageSize) throws BrAPIServerException { - Integer page = newRequestedPage; - Integer pageSize = newRequestedPageSize; - - if (page == null) { - page = originalRequestedPage; - } - if (pageSize == null) { - pageSize = originalRequestedPageSize; - } - - return generateMetaDataTemplate(page, pageSize); - } protected Metadata generateMetaDataTemplate(SearchRequest request) throws BrAPIServerException { return generateMetaDataTemplate(request.getPage(), request.getPageSize()); @@ -55,7 +40,7 @@ protected Metadata generateMetaDataTemplate(SearchRequest request) throws BrAPIS protected Metadata generateMetaDataTemplate(String pageToken, Integer pageSize) { if (pageSize == null) { - pageSize = 1000; + pageSize = PagingUtility.getDefaultPageSize(); } Metadata metaData = generateEmptyMetadataToken(); @@ -65,14 +50,14 @@ protected Metadata generateMetaDataTemplate(String pageToken, Integer pageSize) } protected Metadata generateMetaDataTemplate(Integer page, Integer pageSize) throws BrAPIServerException { - validatePaging(page, pageSize); + PagingUtility.validatePaging(page, pageSize); // defaults if (page == null) { page = 0; } if (pageSize == null) { - pageSize = 1000; + pageSize = PagingUtility.getDefaultPageSize(); } Metadata metaData = generateEmptyMetadata(); @@ -81,16 +66,6 @@ protected Metadata generateMetaDataTemplate(Integer page, Integer pageSize) thro return metaData; } - private void validatePaging(Integer page, Integer pageSize) throws BrAPIServerException { - boolean pageValid = (page == null) || (page >= 0); - if (!pageValid) - throw new InvalidPagingException("page"); - boolean pageSizeValid = (pageSize == null) || (pageSize >= 1); - if (!pageSizeValid) - throw new InvalidPagingException("pageSize"); - - } - protected Metadata generateEmptyMetadata() { Metadata metaData = new Metadata(); metaData.setDatafiles(new ArrayList<>()); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/controller/germ/GermplasmApiController.java b/src/main/java/org/brapi/test/BrAPITestServer/controller/germ/GermplasmApiController.java index e13fa9ac..9f1ed1ae 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/controller/germ/GermplasmApiController.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/controller/germ/GermplasmApiController.java @@ -213,12 +213,37 @@ public ResponseEntity searchGermplasmPost(@RequestBody log.debug("Request: " + request.getRequestURI()); validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER"); validateAcceptHeader(request); - Metadata metadata = generateMetaDataTemplate(body); String searchReqDbId = searchService.saveSearchRequest(body, SearchRequestTypes.GERMPLASM); if (searchReqDbId != null) { return responseAccepted(searchReqDbId); + } + + // WARN: This code was introduced to deal with a specific use case from BI which requires all data associated with + // a particular program to be retreived at once. This method of data retreival is highly unadvised and can come + // with serious performance deficits, such as slow response times and exhausted memory allocation. + // Benchmarking suggests that at around 245-275k germplasm records returned 8GB of allocated memory will fail to + // be enough to return a result. + + // This code is a stop-gap to allow BI to continue to do this improper retreival in a way that will be efficient + // for their specific use case. + + // To get the data in this ill-advised way, forgo sending a page or pageSize attribute in the germplasm search request + // to this endpoint. This will trigger the findGermplasmWithoutPaging code, which will grab all of the data without regard + // to data size. + + // To use the endpoint the right way, ensure one or both of the aforementioned attributes are set and the germplasm + // records will be retrieved and returned paginated to limit resource consumption. This way is much more fine tuned + // and will result in fast retrieval times with minimal memory allocation. + if (body.getPage() == null && body.getPageSize() == null) { + log.debug("Retrieving germs without pagination"); + List data = germplasmService.findGermplasmWithoutPaging(body); + Metadata metadata = generateEmptyMetadata(); + metadata.getPagination().setTotalCount(data.size()); + return responseOK(new GermplasmListResponse(), new GermplasmListResponseResult(), data, metadata); } else { + log.debug("Retrieving germs with pagination"); + Metadata metadata = generateMetaDataTemplate(body); List data = germplasmService.findGermplasm(body, metadata); return responseOK(new GermplasmListResponse(), new GermplasmListResponseResult(), data, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/exceptions/InvalidPagingException.java b/src/main/java/org/brapi/test/BrAPITestServer/exceptions/InvalidPagingException.java index cee5b80f..819b23d6 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/exceptions/InvalidPagingException.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/exceptions/InvalidPagingException.java @@ -4,9 +4,18 @@ public class InvalidPagingException extends BrAPIServerException { private static final long serialVersionUID = 6250184179200451757L; + + private static final String pageNumExceedsTotalPages = + "A page was requested which exceeds total amount of data available. Please make a RQ with page < totalPages - 1. " + + "Page numbers start at 0, and you can find out totalPages by making a RQ with \"page\": 0"; public InvalidPagingException(String field) { super(HttpStatus.BAD_REQUEST, "\'" + field + "\' value is invalid"); } + + // This constructor should only be used when the pageNum of the RQ exceeds the total number of pages available. + public InvalidPagingException() { + super(HttpStatus.BAD_REQUEST, pageNumExceedsTotalPages); + } } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/BrAPIComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/BrAPIComponent.java index 7744633c..207d5404 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/BrAPIComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/BrAPIComponent.java @@ -4,11 +4,12 @@ import io.swagger.model.SearchRequest; import io.swagger.model.core.BatchDeleteTypes; import jakarta.validation.Valid; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import java.util.List; public interface BrAPIComponent { - List findEntities(@Valid R request, Metadata metadata); + List findEntities(@Valid R request, Metadata metadata) throws BrAPIServerException; BatchDeleteTypes getBatchDeleteType(); List collectDbIds(List entities); void deleteBatchDeleteData(List dbIds); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/core/ListComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/core/ListComponent.java index 25b3a1e9..134e08cd 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/core/ListComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/core/ListComponent.java @@ -5,6 +5,7 @@ import io.swagger.model.core.ListSearchRequest; import io.swagger.model.core.ListSummary; import jakarta.validation.Valid; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.factory.BrAPIComponent; import org.brapi.test.BrAPITestServer.service.core.ListService; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +24,8 @@ public ListComponent(ListService listService) { } @Override - public List findEntities(@Valid ListSearchRequest request, Metadata metadata) { + public List findEntities(@Valid ListSearchRequest request, Metadata metadata) + throws BrAPIServerException { return listService.findLists(request, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/core/TrialComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/core/TrialComponent.java index fc8ccbb9..0efbbaf7 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/core/TrialComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/core/TrialComponent.java @@ -4,6 +4,7 @@ import io.swagger.model.core.BatchDeleteTypes; import io.swagger.model.core.Trial; import io.swagger.model.core.TrialSearchRequest; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.factory.BrAPIComponent; import org.brapi.test.BrAPITestServer.service.core.TrialService; import org.springframework.beans.factory.annotation.Autowired; @@ -22,7 +23,8 @@ public TrialComponent(TrialService trialService) { } @Override - public List findEntities(TrialSearchRequest request, Metadata metadata) { + public List findEntities(TrialSearchRequest request, Metadata metadata) + throws BrAPIServerException { return trialService.findTrials(request, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/PlateComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/PlateComponent.java index 5bfaf1f4..52d7ee50 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/PlateComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/PlateComponent.java @@ -5,6 +5,7 @@ import io.swagger.model.geno.Plate; import io.swagger.model.geno.PlateSearchRequest; import org.apache.commons.lang3.NotImplementedException; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.factory.BrAPIComponent; import org.brapi.test.BrAPITestServer.service.geno.PlateService; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +24,8 @@ public PlateComponent(PlateService plateService) { } @Override - public List findEntities(PlateSearchRequest request, Metadata metadata) { + public List findEntities(PlateSearchRequest request, Metadata metadata) + throws BrAPIServerException { return plateService.findPlates(request, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/SampleComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/SampleComponent.java index 05209351..26679f58 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/SampleComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/geno/SampleComponent.java @@ -4,6 +4,7 @@ import io.swagger.model.core.BatchDeleteTypes; import io.swagger.model.geno.Sample; import io.swagger.model.geno.SampleSearchRequest; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.factory.BrAPIComponent; import org.brapi.test.BrAPITestServer.service.geno.SampleService; import org.springframework.beans.factory.annotation.Autowired; @@ -22,7 +23,8 @@ public SampleComponent(SampleService sampleService) { } @Override - public List findEntities(SampleSearchRequest request, Metadata metadata) { + public List findEntities(SampleSearchRequest request, Metadata metadata) + throws BrAPIServerException { return sampleService.findSamples(request, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/factory/germ/GermplasmComponent.java b/src/main/java/org/brapi/test/BrAPITestServer/factory/germ/GermplasmComponent.java index 953b4ec4..fea81f5b 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/factory/germ/GermplasmComponent.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/factory/germ/GermplasmComponent.java @@ -5,6 +5,7 @@ import io.swagger.model.germ.Germplasm; import io.swagger.model.germ.GermplasmSearchRequest; import jakarta.validation.Valid; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.factory.BrAPIComponent; import org.brapi.test.BrAPITestServer.service.germ.GermplasmService; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +24,8 @@ public GermplasmComponent(GermplasmService germplasmService) { } @Override - public List findEntities(@Valid GermplasmSearchRequest request, Metadata metadata) { + public List findEntities(@Valid GermplasmSearchRequest request, Metadata metadata) + throws BrAPIServerException { return germplasmService.findGermplasm(request, metadata); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepository.java b/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepository.java index 255cd5bd..e038360c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepository.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepository.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Optional; +import org.brapi.test.BrAPITestServer.exceptions.InvalidPagingException; import org.brapi.test.BrAPITestServer.model.entity.BrAPIPrimaryEntity; import org.brapi.test.BrAPITestServer.service.SearchQueryBuilder; import org.springframework.data.domain.Page; @@ -14,7 +15,11 @@ @NoRepositoryBean public interface BrAPIRepository extends JpaRepository { - public Page findAllBySearch(SearchQueryBuilder searchQuery, Pageable pageReq); + public Page findAllBySearchAndPaginate(SearchQueryBuilder searchQuery, Pageable pageReq) throws InvalidPagingException; + + public Page findAllBySearchPaginatingWithFetches(SearchQueryBuilder searchQuery, Pageable pageReq) throws InvalidPagingException; + + public List findAllBySearch(SearchQueryBuilder searchQuery); public Optional findById(ID id); @@ -24,5 +29,5 @@ public interface BrAPIRepository void refresh(S entity); - public void fetchXrefs(Page page, Class searchClass); + public void fetchXrefs(Page page, Class searchClass) throws InvalidPagingException; } \ No newline at end of file diff --git a/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepositoryImpl.java b/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepositoryImpl.java index bbad6ea2..9bfa577c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepositoryImpl.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/repository/BrAPIRepositoryImpl.java @@ -1,9 +1,21 @@ package org.brapi.test.BrAPITestServer.repository; +import java.io.Serializable; +import java.util.*; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import io.swagger.model.Metadata; +import io.swagger.model.germ.GermplasmSearchRequest; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import org.brapi.test.BrAPITestServer.exceptions.InvalidPagingException; import org.brapi.test.BrAPITestServer.model.entity.BrAPIBaseEntity; import org.brapi.test.BrAPITestServer.model.entity.BrAPIPrimaryEntity; import org.brapi.test.BrAPITestServer.model.entity.ExternalReferenceEntity; import org.brapi.test.BrAPITestServer.service.SearchQueryBuilder; +import org.brapi.test.BrAPITestServer.service.SecurityUtils; import org.hibernate.jpa.QueryHints; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -14,36 +26,90 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; -import javax.persistence.EntityManager; -import javax.persistence.TypedQuery; -import java.io.Serializable; -import java.util.*; -import java.util.Map.Entry; -import java.util.stream.Collectors; - public class BrAPIRepositoryImpl extends SimpleJpaRepository implements BrAPIRepository { - private EntityManager entityManager; + private final EntityManager entityManager; public BrAPIRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); this.entityManager = entityManager; } - public Page findAllBySearch(SearchQueryBuilder searchQuery, Pageable pageReq) { - searchQuery = applyUserId(searchQuery); + /** + * Use this method to page simple entities with no lazily loaded collections or attributes. + * WARN: Failure to do so can easily exhaust memory at scale and will cause hibernate warnings. + */ + public Page findAllBySearchAndPaginate(SearchQueryBuilder searchQuery, Pageable pageReq) + throws InvalidPagingException { + applyUserId(searchQuery); + + Long totalCount = getTotalCount(searchQuery); + + validatePageNumber(pageReq, totalCount); + + if (totalCount == 0) { + // Short-circuit to avoid running possible long-running query + return new PageImpl<>(Collections.emptyList(), pageReq, totalCount); + } + List content = getPagedContent(searchQuery, pageReq); + + return new PageImpl<>(content, pageReq, totalCount); + } + + /** + * This method should be used when there is a need to page entities that are complex and have lots of lazily loaded, + * one-to-many collection attributes. Call this method with the query you've built. + * WARN: To avoid multiple bag fetch hibernate errors, only one collection that is one-many can be fetched at a time. + * Ensure only one of these types of entity members is being fetched at time. + * See {@link org.brapi.test.BrAPITestServer.service.germ.GermplasmService#findGermplasmEntities(GermplasmSearchRequest, Metadata)} for example usage. + * + * Once you fetch the first lazy loaded collection, if there are others you need to fetch for your entity it is + * recommended to utilize the non-paging findAllBySearch() method, creating a searchQuery that inserts all the ids + * of the entities found in the results from this method. Again, follow code path of above example usage to see how this is done. + * + * If the need is to fetch according to an entirely different base criteria, feel free to call this method again. + * + * The results will be paged and ordered based on the submitted searchQuery and pageReq. + */ + public Page findAllBySearchPaginatingWithFetches(SearchQueryBuilder searchQuery, Pageable pageReq) + throws InvalidPagingException { + applyUserId(searchQuery); + Long totalCount = getTotalCount(searchQuery); - Page page = new PageImpl<>(content, pageReq, totalCount); + validatePageNumber(pageReq, totalCount); + + // First grab all the ids of the entities according to the criteria of the searchQuery, paging as specified in the pageReq. + List content = getPagedContentIdsOnly(searchQuery, pageReq); - return page; + Page pagedIds = new PageImpl<>(content, pageReq, totalCount); + + // Now execute another query to fetch all the entities requested in the searchQuery, passing the pagedIds found + // in the previous query. We will fetch them utilizing the ids from the paged query, avoiding the hibernate + // warning of paging while fetching and consuming considerably less memory. + return findAllBySearchUsingIds(searchQuery, pagedIds, pageReq); + } + + private Page findAllBySearchUsingIds(SearchQueryBuilder searchQuery, Page pagedIds, Pageable pageReq) { + List entities = searchEntitiesWithIds(searchQuery, pagedIds.toList()); + + return new PageImpl<>(entities, pageReq, pagedIds.getTotalElements()); + } + + /** + * Use this method to run the searchQuery without pagination. Useful for use cases where calls need to grab + * every result at once, but use sparingly for use cases returning large result sets. + */ + public List findAllBySearch(SearchQueryBuilder searchQuery) { + applyUserId(searchQuery); + return searchEntities(searchQuery); } public Optional findById(ID id) { Optional response = super.findById(id); if (response.isPresent()) { - String userId = getCurrentUserId(); + String userId = SecurityUtils.getCurrentUserId(); if (!(null == response.get().getAuthUserId() || userId.equals(response.get().getAuthUserId()) || "anonymousUser".equals(response.get().getAuthUserId()))) { @@ -54,13 +120,13 @@ public Optional findById(ID id) { } public S save(S entity) { - entity.setAuthUserId(getCurrentUserId()); + entity.setAuthUserId(SecurityUtils.getCurrentUserId()); return super.save(entity); } public List saveAll(Iterable entities) { for (S entity : entities) { - entity.setAuthUserId(getCurrentUserId()); + entity.setAuthUserId(SecurityUtils.getCurrentUserId()); } return super.saveAll(entities); } @@ -69,12 +135,12 @@ public void refresh(S entity) { this.entityManager.refresh(entity); } - public void fetchXrefs(Page page, Class searchClass) { + public void fetchXrefs(Page page, Class searchClass) throws InvalidPagingException { SearchQueryBuilder searchQuery = new SearchQueryBuilder(searchClass); searchQuery.leftJoinFetch("externalReferences", "externalReferences") .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); - Page xrefs = findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + Page xrefs = findAllBySearchAndPaginate(searchQuery, PageRequest.of(0, page.getSize())); Map> xrefByEntity = new HashMap<>(); xrefs.forEach(entity -> xrefByEntity.put(entity.getId(), entity.getExternalReferences())); @@ -82,38 +148,63 @@ public void fetchXrefs(Page page, Class searchClass) { page.forEach(entity -> entity.setExternalReferences(xrefByEntity.get(entity.getId()))); } - private String getCurrentUserId() { - SecurityContext context = SecurityContextHolder.getContext(); - String userId = ""; - if (context.getAuthentication().getPrincipal() != null) { - userId = context.getAuthentication().getPrincipal().toString(); - } - return userId; - } - - private SearchQueryBuilder applyUserId(SearchQueryBuilder searchQuery) { + private void applyUserId(SearchQueryBuilder searchQuery) { SecurityContext context = SecurityContextHolder.getContext(); Set userRolesSet = context.getAuthentication().getAuthorities().stream() .map(auth -> auth.getAuthority()).collect(Collectors.toSet()); List userIds = new ArrayList<>(); - userIds.add(getCurrentUserId()); + userIds.add(SecurityUtils.getCurrentUserId()); if (userRolesSet.contains("ROLE_ADMIN")) { - return searchQuery; + return; } else if (userRolesSet.contains("ROLE_USER")) { userIds.add("anonymousUser"); } searchQuery.appendList(userIds, "authUserId"); - - return searchQuery; } private List getPagedContent(SearchQueryBuilder searchQuery, Pageable pageReq) { TypedQuery query = entityManager.createQuery(searchQuery.getQuery(), searchQuery.getClazz()); query.setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, false); + setQueryParams(query, searchQuery); + + query.setFirstResult((int) pageReq.getOffset()); + query.setMaxResults(pageReq.getPageSize()); + + return query.getResultList(); + } + + private List searchEntitiesWithIds(SearchQueryBuilder searchQuery, List ids) { + searchQuery.appendList(ids.stream().map(Object::toString).collect(Collectors.toList()), "id"); + + TypedQuery query = entityManager.createQuery(searchQuery.getQuery(), searchQuery.getClazz()); + + setQueryParams(query, searchQuery); + + return query.getResultList(); + } + + private List searchEntities(SearchQueryBuilder searchQuery) { + TypedQuery query = entityManager.createQuery(searchQuery.getQuery(), searchQuery.getClazz()); + + setQueryParams(query, searchQuery); + + return query.getResultList(); + } + + private void setQueryParams(TypedQuery query, SearchQueryBuilder searchQuery) { + for (Entry entry : searchQuery.getParams().entrySet()) { + query.setParameter(entry.getKey(), entry.getValue()); + } + } + + private List getPagedContentIdsOnly(SearchQueryBuilder searchQuery, Pageable pageReq) { + + TypedQuery query = entityManager.createQuery(searchQuery.getIdQuery(), String.class); + for (Entry entry : searchQuery.getParams().entrySet()) { query.setParameter(entry.getKey(), entry.getValue()); } @@ -121,10 +212,12 @@ private List getPagedContent(SearchQueryBuilder searchQuery, Pageable page query.setFirstResult((int) pageReq.getOffset()); query.setMaxResults(pageReq.getPageSize()); - List content = query.getResultList(); - return content; + return query.getResultList(); } + + + private Long getTotalCount(SearchQueryBuilder searchQuery) { String countQueryStr = searchQuery.getQuery() .replaceFirst("(select|Select|SELECT)( distinct)? ([^\\s]*) ", "select count($2 $3) ") @@ -142,4 +235,13 @@ private Long getTotalCount(SearchQueryBuilder searchQuery) { } return 0L; } + + private void validatePageNumber(Pageable pageReq, Long totalCount) throws InvalidPagingException { + int reqPageNum = pageReq.getPageNumber(); + if (reqPageNum != 0 && reqPageNum > Math.floor(((double) totalCount) / pageReq.getPageSize())) { + // Condition indicates a request was sent where the page requested exceeds the total number of pages. + // Instruct requester to send the correct request. + throw new InvalidPagingException(); + } + } } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/PagingUtility.java b/src/main/java/org/brapi/test/BrAPITestServer/service/PagingUtility.java index 57c39c6b..8b2c250c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/PagingUtility.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/PagingUtility.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.List; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; +import org.brapi.test.BrAPITestServer.exceptions.InvalidPagingException; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -10,14 +13,49 @@ import io.swagger.model.IndexPagination; import io.swagger.model.Metadata; +import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; + +@Component public class PagingUtility { + @Value("${paging.page-size.default:1000}") + private int defaultPageSize; + @Value("${paging.page-size.max-allowed:65000}") + private int maxAllowablePageSize; + + private static int defaultPageSizeStatic; + private static int maxAllowablePageSizeStatic; + + @PostConstruct + private void init() { + // Java made me do this - JL + defaultPageSizeStatic = defaultPageSize; + maxAllowablePageSizeStatic = maxAllowablePageSize; + } + public static void calculateMetaData(Metadata metaData) { int totalCount = metaData.getPagination().getTotalCount(); int pageSize = metaData.getPagination().getPageSize(); metaData.getPagination().setTotalPages((totalCount / pageSize) + Integer.signum( totalCount % pageSize)); } + public static void validatePaging(Integer page, Integer pageSize) throws BrAPIServerException { + boolean pageValid = (page == null) || (page >= 0); + if (!pageValid) + throw new InvalidPagingException("page"); + boolean pageSizeValid = (pageSize == null) || (pageSize >= 1); + if (!pageSizeValid) + throw new InvalidPagingException("pageSize"); + if (pageSize != null && pageSize > maxAllowablePageSizeStatic) { + throw new InvalidPagingException("pageSize [Over maximum allowable page size of [" + maxAllowablePageSizeStatic + "]] "); + } + } + + public static int getDefaultPageSize() { + return defaultPageSizeStatic; + } + public static Pageable getPageRequest(Metadata metaData) { if (metaData == null) { metaData = new Metadata(); @@ -34,7 +72,7 @@ public static Pageable getPageRequest(Metadata metaData, Sort sort) { page = metaData.getPagination().getCurrentPage(); } - int pageSize = 1000; + int pageSize = defaultPageSizeStatic; if (metaData.getPagination().getPageSize() != null && metaData.getPagination().getPageSize() > 0) { pageSize = metaData.getPagination().getPageSize(); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/SearchQueryBuilder.java b/src/main/java/org/brapi/test/BrAPITestServer/service/SearchQueryBuilder.java index f96023f2..45226bdb 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/SearchQueryBuilder.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/SearchQueryBuilder.java @@ -1,11 +1,7 @@ package org.brapi.test.BrAPITestServer.service; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.time.LocalDate; import java.time.OffsetDateTime; @@ -16,23 +12,41 @@ public class SearchQueryBuilder { private String selectClause; + private String selectOnlyIds; private String whereClause; + private String defaultSort; private String sortClause; private Map params; private Class clazz; public SearchQueryBuilder(Class clazz) { this.selectClause = "SELECT distinct entity FROM " + clazz.getSimpleName() + " entity "; + this.selectOnlyIds = "SELECT entity.id FROM " + clazz.getSimpleName() + " entity "; this.whereClause = "WHERE 1=1 "; + this.defaultSort = " ORDER BY entity.id ASC "; this.sortClause = ""; this.params = new HashMap<>(); this.clazz = clazz; } public String getQuery() { + if (sortClause.isEmpty()) { + // By default, sort on entity id to have query result remain idempotent + sortClause = defaultSort; + } + return selectClause + whereClause + sortClause; } + public String getIdQuery() { + if (sortClause.isEmpty()) { + // By default, sort on entity id to have query result remain idempotent + sortClause = defaultSort; + } + + return selectOnlyIds + whereClause + sortClause; + } + public Map getParams() { return params; } @@ -50,6 +64,15 @@ public SearchQueryBuilder appendList(List list, String columnName) { return this; } + public SearchQueryBuilder appendIds(List ids) { + String paramName = paramFilter("id"); + if (ids != null && !ids.isEmpty()) { + this.whereClause += "AND " + entityPrefix("id") + " in :" + paramName + " "; + this.params.put(paramName, ids); + } + return this; + } + public SearchQueryBuilder appendIntList(List list, String columnName) { String paramName = paramFilter(columnName); if (list != null && !list.isEmpty()) { @@ -221,14 +244,44 @@ public SearchQueryBuilder withExRefs(List exRefIds, List exRe public SearchQueryBuilder join(String join, String name) { this.selectClause += "JOIN " + entityPrefix(join) + " " + paramFilter(name) + " "; + this.selectOnlyIds += "JOIN " + entityPrefix(join) + " " + paramFilter(name) + " "; return this; } public SearchQueryBuilder leftJoinFetch(String join, String name) { - this.selectClause += "LEFT JOIN FETCH " + entityPrefix(join) + " " + paramFilter(name) + " "; + this.selectClause += generateLeftJoinFetch(join, name); return this; } + /** + * Use this method to remove left join fetches from specific collection attributes so you can leverage the same query to + * iterate through other lazily loaded collections on an entity you need to fetch. + */ + public SearchQueryBuilder removeAndReplaceLeftJoinFetch(String join, + String name, + String existingJoin, + String existingName) { + + this.selectClause = + this.selectClause.replace(generateLeftJoinFetch(existingJoin, existingName), generateLeftJoinFetch(join, name)); + + return this; + } + + /** + * Use this method to remove left join fetches from specific collection attributes so you can leverage the same + * base query criteria to add another join fetch with leftJoinFetch() + */ + public SearchQueryBuilder removeLeftJoinFetch(String join, String name) { + this.selectClause = + this.selectClause.replace(generateLeftJoinFetch(join, name), ""); + return this; + } + + private String generateLeftJoinFetch(String join, String paramName) { + return "LEFT JOIN FETCH " + entityPrefix(join) + " " + paramFilter(paramName) + " "; + } + private String entityPrefix(String field) { if (field.startsWith("*")) { return field.substring(1); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/SecurityUtils.java b/src/main/java/org/brapi/test/BrAPITestServer/service/SecurityUtils.java new file mode 100644 index 00000000..6784a6f1 --- /dev/null +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/SecurityUtils.java @@ -0,0 +1,15 @@ +package org.brapi.test.BrAPITestServer.service; + +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +public class SecurityUtils { + public static String getCurrentUserId() { + SecurityContext context = SecurityContextHolder.getContext(); + String userId = ""; + if (context.getAuthentication().getPrincipal() != null) { + userId = context.getAuthentication().getPrincipal().toString(); + } + return userId; + } +} \ No newline at end of file diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/ListService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/ListService.java index b4ecc4b4..854559a2 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/ListService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/ListService.java @@ -59,7 +59,7 @@ public List findBatchDeleteLists(String batchDeleteDbId, Metadata m public List findLists(ListTypes listType, String listName, String listDbId, String listSource, String programDbId, String commonCropName, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) throws BrAPIServerException { ListSearchRequest request = new ListSearchRequest(); if (listType != null) { request.setListType(listType); @@ -80,15 +80,15 @@ public List findLists(ListTypes listType, String listName, String l request.addCommonCropNamesItem(commonCropName); } request.addExternalReferenceItem(externalReferenceId, externalReferenceID, externalReferenceSource); - + return findLists(request, metadata); } - public List findLists(ListSearchRequest request, Metadata metadata) { + public List findLists(ListSearchRequest request, Metadata metadata) throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = buildQueryString(request); - Page entityPage = listRepository.findAllBySearch(searchQuery, pageReq); + Page entityPage = listRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List data = entityPage.map(this::convertToSummary).getContent(); PagingUtility.calculateMetaData(metadata, entityPage); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/LocationService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/LocationService.java index 71bc8943..857d5187 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/LocationService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/LocationService.java @@ -37,7 +37,8 @@ public LocationService(LocationRepository locationRepository) { public List findLocations(String locationDbId, String locationType, String locationName, String parentLocationDbId, String parentLocationName, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { LocationSearchRequest request = new LocationSearchRequest(); if (locationDbId != null) request.addLocationDbIdsItem(locationDbId); @@ -59,7 +60,8 @@ public List findLocations(String locationDbId, String locationType, St return findLocations(request, metadata); } - public List findLocations(LocationSearchRequest request, Metadata metadata) { + public List findLocations(LocationSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(LocationEntity.class); @@ -83,7 +85,7 @@ public List findLocations(LocationSearchRequest request, Metadata meta .appendList(request.getParentLocationDbIds(), "parentLocation.id") .appendList(request.getParentLocationNames(), "parentLocation.name"); - Page entityPage = locationRepository.findAllBySearch(searchQuery, pageReq); + Page entityPage = locationRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List data = entityPage.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, entityPage); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/PeopleService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/PeopleService.java index 0a7ec737..8fc905fc 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/PeopleService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/PeopleService.java @@ -32,7 +32,8 @@ public PeopleService(PeopleRepository peopleRepository) { public List findPeople(String firstName, String lastName, String personDbId, String userID, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { PersonSearchRequest request = new PersonSearchRequest(); if (firstName != null) @@ -52,7 +53,8 @@ public List findPeople(String firstName, String lastName, String personD return findPeople(request, metadata); } - public List findPeople(PersonSearchRequest request, Metadata metadata) { + public List findPeople(PersonSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(PersonEntity.class) .withExRefs(request.getExternalReferenceIDs(), request.getExternalReferenceSources()) @@ -62,7 +64,7 @@ public List findPeople(PersonSearchRequest request, Metadata metadata) { .appendList(request.getMiddleNames(), "middleName").appendList(request.getPersonDbIds(), "id") .appendList(request.getPhoneNumbers(), "phoneNumber").appendList(request.getUserIDs(), "userID"); - Page entityPage = peopleRepository.findAllBySearch(searchQuery, pageReq); + Page entityPage = peopleRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List data = entityPage.map(this::convertToPerson).getContent(); PagingUtility.calculateMetaData(metadata, entityPage); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/ProgramService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/ProgramService.java index e160b190..0ef25c0c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/ProgramService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/ProgramService.java @@ -69,7 +69,7 @@ public List findPrograms(ProgramSearchRequest request, Metadata metadat .appendList(request.getObjectives(), "objective").appendList(request.getProgramDbIds(), "id") .appendList(request.getProgramNames(), "name").appendEnumList(request.getProgramTypes(), "programType"); - Page page = programRepository.findAllBySearch(searchQuery, pageReq); + Page page = programRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List programs = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return programs; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/SeasonService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/SeasonService.java index ec8b4f89..eaf9e706 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/SeasonService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/SeasonService.java @@ -32,7 +32,8 @@ public SeasonService(SeasonRepository seasonRepository) { } public List findSeasons(String seasonDbId, String season, String seasonName, Integer year, - Metadata metadata) { + Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(SeasonEntity.class); if (seasonDbId != null) @@ -44,7 +45,7 @@ public List findSeasons(String seasonDbId, String season, String seasonN if (year != null) searchQuery = searchQuery.appendSingle(year, "year"); - Page entityPage = seasonRepository.findAllBySearch(searchQuery, pageReq); + Page entityPage = seasonRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List data = entityPage.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, entityPage); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/StudyService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/StudyService.java index 0c6c78bf..b8e995b0 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/StudyService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/StudyService.java @@ -78,7 +78,7 @@ public List findStudies(String commonCropName, String studyType, String p String seasonDbId, String trialDbId, String studyDbId, String studyName, String studyCode, String studyPUI, String germplasmDbId, String observationVariableDbId, String externalReferenceId, String externalReferenceID, String externalReferenceSource, Boolean active, String sortBy, String sortOrder, - Metadata metadata) { + Metadata metadata) throws BrAPIServerException { StudySearchRequest request = new StudySearchRequest(); if (commonCropName != null) @@ -117,7 +117,8 @@ public List findStudies(String commonCropName, String studyType, String p return findStudies(request, metadata); } - public List findStudies(StudySearchRequest request, Metadata metaData) { + public List findStudies(StudySearchRequest request, Metadata metaData) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metaData); SearchQueryBuilder searchQuery = new SearchQueryBuilder(StudyEntity.class); @@ -149,7 +150,7 @@ public List findStudies(StudySearchRequest request, Metadata metaData) { .appendList(request.getTrialDbIds(), "trial.id").appendList(request.getTrialNames(), "trial.trialName") .withSort(getSortByField(request.getSortBy()), request.getSortOrder()); - Page studiesPage = studyRepository.findAllBySearch(searchQuery, pageReq); + Page studiesPage = studyRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metaData, studiesPage); List studies = studiesPage.map(this::convertFromEntity).getContent(); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/core/TrialService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/core/TrialService.java index 472d61c8..776906da 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/core/TrialService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/core/TrialService.java @@ -71,7 +71,8 @@ public List findTrials(@Valid String commonCropName, @Valid String contac @Valid String locationDbId, @Valid LocalDate searchDateRangeStart, @Valid LocalDate searchDateRangeEnd, @Valid String studyDbId, @Valid String trialDbId, @Valid String trialName, @Valid String trialPUI, @Valid String externalReferenceId, @Valid String externalReferenceID, @Valid String externalReferenceSource, - @Valid Boolean active, @Valid String sortBy, @Valid String sortOrder, Metadata metadata) { + @Valid Boolean active, @Valid String sortBy, @Valid String sortOrder, Metadata metadata) + throws BrAPIServerException { TrialSearchRequest request = new TrialSearchRequest(); if (active != null) @@ -105,7 +106,8 @@ public List findTrials(@Valid String commonCropName, @Valid String contac return findTrials(request, metadata); } - public List findTrials(@Valid TrialSearchRequest request, Metadata metadata) { + public List findTrials(@Valid TrialSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(TrialEntity.class); @@ -129,7 +131,7 @@ public List findTrials(@Valid TrialSearchRequest request, Metadata metada .appendDateRange(request.getSearchDateRangeStart(), request.getSearchDateRangeEnd(), "startDate") .withSort(getSortByField(request.getSortBy()), request.getSortOrder()); - Page trialsPage = trialRepository.findAllBySearch(searchQuery, pageReq); + Page trialsPage = trialRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, trialsPage); List trials = trialsPage.map(this::convertFromEntity).getContent(); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/AlleleMatrixService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/AlleleMatrixService.java index c0ba0c76..74e3ee14 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/AlleleMatrixService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/AlleleMatrixService.java @@ -8,6 +8,7 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.math.NumberUtils; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.model.entity.geno.CallEntity; import org.brapi.test.BrAPITestServer.model.entity.geno.CallSetEntity; import org.brapi.test.BrAPITestServer.model.entity.geno.VariantEntity; @@ -43,7 +44,8 @@ public AlleleMatrix findAlleleMatrix(Integer dimensionVariantPage, Integer dimen Integer dimensionCallSetPage, Integer dimensionCallSetPageSize, Boolean preview, String dataMatrixNames, String dataMatrixAbbreviations, String positionRange, String germplasmDbId, String germplasmName, String germplasmPUI, String callSetDbId, String variantDbId, String variantSetDbId, - Boolean expandHomozygotes, String unknownString, String sepPhased, String sepUnphased, Metadata metadata) { + Boolean expandHomozygotes, String unknownString, String sepPhased, String sepUnphased, Metadata metadata) + throws BrAPIServerException { AlleleMatrixSearchRequestPagination variantPage = new AlleleMatrixSearchRequestPagination() .dimension(DimensionEnum.VARIANTS); @@ -111,7 +113,8 @@ public AlleleMatrix findAlleleMatrix(Integer dimensionVariantPage, Integer dimen return findAlleleMatrix(request, metadata); } - public AlleleMatrix findAlleleMatrix(AlleleMatrixSearchRequest request, Metadata metadata) { + public AlleleMatrix findAlleleMatrix(AlleleMatrixSearchRequest request, Metadata metadata) + throws BrAPIServerException { AlleleMatrix matrixResponse = new AlleleMatrix(); AlleleMatrixPagination variantPage = new AlleleMatrixPagination().dimension(DimensionEnum.VARIANTS); AlleleMatrixPagination callSetPage = new AlleleMatrixPagination().dimension(DimensionEnum.CALLSETS); @@ -221,7 +224,8 @@ private void prepareMatrices(AlleleMatrixSearchRequest request, AlleleMatrix mat } } - private List findCalls(AlleleMatrix matrixResponse, AlleleMatrixSearchRequest request) { + private List findCalls(AlleleMatrix matrixResponse, AlleleMatrixSearchRequest request) + throws BrAPIServerException { CallsSearchRequest callSearchReq = new CallsSearchRequest(); callSearchReq.setCallSetDbIds(matrixResponse.getCallSetDbIds()); callSearchReq.setVariantDbIds(matrixResponse.getVariantDbIds()); @@ -241,7 +245,8 @@ private List findCalls(AlleleMatrix matrixResponse, AlleleMatrixSear return callService.findCallEntities(callSearchReq, metadata); } - private List findVariants(AlleleMatrixSearchRequest request, AlleleMatrixPagination page) { + private List findVariants(AlleleMatrixSearchRequest request, AlleleMatrixPagination page) + throws BrAPIServerException { VariantsSearchRequest variantSearchReq = new VariantsSearchRequest(); variantSearchReq.setCallSetDbIds(request.getCallSetDbIds()); variantSearchReq.setVariantDbIds(request.getVariantDbIds()); @@ -269,7 +274,8 @@ private List findVariants(AlleleMatrixSearchRequest request, Alle return variants; } - private List findCallSets(AlleleMatrixSearchRequest request, AlleleMatrixPagination page) { + private List findCallSets(AlleleMatrixSearchRequest request, AlleleMatrixPagination page) + throws BrAPIServerException { CallSetsSearchRequest callSetSearchReq = new CallSetsSearchRequest(); callSetSearchReq.setCallSetDbIds(request.getCallSetDbIds()); callSetSearchReq.setGermplasmDbIds(request.getGermplasmDbIds()); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallService.java index 757722f4..ab9be78f 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallService.java @@ -41,7 +41,8 @@ public CallService(CallRepository callRepository) { } public CallsListResponseResult findCalls(String callSetDbId, String variantDbId, String variantSetDbId, - Boolean expandHomozygotes, String unknownString, String sepPhased, String sepUnphased, Metadata metadata) { + Boolean expandHomozygotes, String unknownString, String sepPhased, String sepUnphased, Metadata metadata) + throws BrAPIServerException { CallsSearchRequest request = new CallsSearchRequest(); if (callSetDbId != null) request.addCallSetDbIdsItem(callSetDbId); @@ -57,7 +58,8 @@ public CallsListResponseResult findCalls(String callSetDbId, String variantDbId, return findCalls(request, metadata); } - public CallsListResponseResult findCalls(CallsSearchRequest request, Metadata metadata) { + public CallsListResponseResult findCalls(CallsSearchRequest request, Metadata metadata) + throws BrAPIServerException { List calls = findCallEntities(request, metadata).stream().map(e -> { return convertFromEntityWithFormatting(e, request); }).collect(Collectors.toList()); @@ -65,13 +67,14 @@ public CallsListResponseResult findCalls(CallsSearchRequest request, Metadata me return result; } - public List findCallEntities(CallsSearchRequest request, Metadata metadata) { + public List findCallEntities(CallsSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(CallEntity.class) .appendList(request.getCallSetDbIds(), "callSet.id").appendList(request.getVariantDbIds(), "variant.id") .appendList(request.getVariantSetDbIds(), "variant.variantSet.id"); - Page page = callRepository.findAllBySearch(searchQuery, pageReq); + Page page = callRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, page); return page.getContent(); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallSetService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallSetService.java index 1fe253bd..906f19b4 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallSetService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/CallSetService.java @@ -30,7 +30,8 @@ public CallSetService(CallSetRepository callSetRepository) { } public List findCallSets(String callSetDbId, String callSetName, String variantSetDbId, String sampleDbId, - String germplasmDbId, String externalReferenceId, String externalReferenceSource, Metadata metadata) { + String germplasmDbId, String externalReferenceId, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { CallSetsSearchRequest request = new CallSetsSearchRequest(); if (callSetDbId != null) request.addCallSetDbIdsItem(callSetDbId); @@ -47,11 +48,13 @@ public List findCallSets(String callSetDbId, String callSetName, String return findCallSets(request, metadata); } - public List findCallSets(CallSetsSearchRequest request, Metadata metadata) { + public List findCallSets(CallSetsSearchRequest request, Metadata metadata) + throws BrAPIServerException { return findCallSetEntities(request, metadata).stream().map(this::convertFromEntity).collect(Collectors.toList()); } - public List findCallSetEntities(CallSetsSearchRequest request, Metadata metadata) { + public List findCallSetEntities(CallSetsSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(CallSetEntity.class); if(request.getVariantSetDbIds() != null) { @@ -66,7 +69,7 @@ public List findCallSetEntities(CallSetsSearchRequest request, Me .appendList(request.getSampleDbIds(), "sample.id") .appendList(request.getSampleNames(), "sample.sampleName"); - Page page = callSetRepository.findAllBySearch(searchQuery, pageReq); + Page page = callSetRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, page); return page.getContent(); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/GenomeMapService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/GenomeMapService.java index 8393a0fb..654c87c0 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/GenomeMapService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/GenomeMapService.java @@ -35,7 +35,8 @@ public GenomeMapService(GenomeMapRepository genomeMapRepository, LinkageGroupRep } public List findMaps(String commonCropName, String mapPUI, String scientificName, String type, - String programDbId, String trialDbId, String studyDbId, Metadata metadata) { + String programDbId, String trialDbId, String studyDbId, Metadata metadata) + throws BrAPIServerException { SearchQueryBuilder searchQuery = new SearchQueryBuilder(GenomeMapEntity.class); if (programDbId != null || trialDbId != null || studyDbId != null) { @@ -51,7 +52,7 @@ public List findMaps(String commonCropName, String mapPUI, String sci .appendSingle(studyDbId, "*study.id"); Pageable pageReq = PagingUtility.getPageRequest(metadata); - Page page = genomeMapRepository.findAllBySearch(searchQuery, pageReq); + Page page = genomeMapRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List maps = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return maps; @@ -68,12 +69,13 @@ public GenomeMap getMap(String mapDbId) throws BrAPIServerException { return map; } - public List findLinkageGroups(String mapDbId, Metadata metadata) { + public List findLinkageGroups(String mapDbId, Metadata metadata) + throws BrAPIServerException { SearchQueryBuilder searchQuery = new SearchQueryBuilder( LinkageGroupEntity.class).appendSingle(mapDbId, "genomeMap.id"); Pageable pageReq = PagingUtility.getPageRequest(metadata); - Page page = linkageGroupRepository.findAllBySearch(searchQuery, pageReq); + Page page = linkageGroupRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List linkageGroups = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return linkageGroups; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/MarkerPositionService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/MarkerPositionService.java index 09e7f6f2..65c7391f 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/MarkerPositionService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/MarkerPositionService.java @@ -2,6 +2,7 @@ import java.util.List; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.model.entity.geno.MarkerPositionEntity; import org.brapi.test.BrAPITestServer.repository.geno.MarkerPositionRepository; import org.brapi.test.BrAPITestServer.service.PagingUtility; @@ -24,7 +25,8 @@ public MarkerPositionService(MarkerPositionRepository markerPositionRepository) } public List findMarkerPositions(String mapDbId, String linkageGroupName, String variantDbId, - Integer maxPosition, Integer minPosition, Metadata metadata) { + Integer maxPosition, Integer minPosition, Metadata metadata) + throws BrAPIServerException { MarkerPositionSearchRequest request = new MarkerPositionSearchRequest(); if (mapDbId != null) request.addMapDbIdsItem(mapDbId); @@ -38,7 +40,8 @@ public List findMarkerPositions(String mapDbId, String linkageGr return findMarkerPositions(request, metadata); } - public List findMarkerPositions(MarkerPositionSearchRequest request, Metadata metadata) { + public List findMarkerPositions(MarkerPositionSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( MarkerPositionEntity.class).appendList(request.getLinkageGroupNames(), "linkageGroup.linkageGroupName") @@ -46,7 +49,7 @@ public List findMarkerPositions(MarkerPositionSearchRequest requ .appendList(request.getMapDbIds(), "linkageGroup.genomeMap.id") .appendNumberRange(request.getMinPosition(), request.getMaxPosition(), "position"); - Page page = markerPositionRepository.findAllBySearch(searchQuery, pageReq); + Page page = markerPositionRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List markerPositions = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return markerPositions; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/PlateService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/PlateService.java index 64f4777b..1509a4d0 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/PlateService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/PlateService.java @@ -48,7 +48,8 @@ public PlateService(PlateRepository plateRepository, ProgramService programServi public List findPlates(String sampleDbId, String sampleName, String sampleGroupDbId, String observationUnitDbId, String plateDbId, String plateName, String germplasmDbId, String studyDbId, String trialDbId, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { PlateSearchRequest request = new PlateSearchRequest(); if (sampleDbId != null) request.addSampleDbIdsItem(sampleDbId); @@ -78,7 +79,8 @@ public List findPlates(String sampleDbId, String sampleName, String sampl return findPlates(request, metadata); } - public List findPlates(PlateSearchRequest request, Metadata metadata) { + public List findPlates(PlateSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(PlateEntity.class); if (request.getSampleDbIds() != null || request.getSampleNames() != null @@ -101,7 +103,7 @@ public List findPlates(PlateSearchRequest request, Metadata metadata) { .appendList(request.getTrialNames(), "trial.trialName").appendList(request.getStudyDbIds(), "study.id") .appendList(request.getStudyNames(), "study.studyName"); - Page page = plateRepository.findAllBySearch(searchQuery, pageReq); + Page page = plateRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List plates = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return plates; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceService.java index f8da61fb..0dd23e3c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceService.java @@ -47,7 +47,8 @@ public ReferenceService(ReferenceRepository referenceRepository, ReferenceBaseRe public List findReferences(String referenceDbId, String referenceSetDbId, String accession, String md5checksum, Boolean isDerived, Integer minLength, Integer maxLength, String trialDbId, String studyDbId, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { ReferencesSearchRequest request = new ReferencesSearchRequest(); if (referenceDbId != null) request.addReferenceDbIdsItem(referenceDbId); @@ -74,7 +75,8 @@ public List findReferences(String referenceDbId, String referenceSetD return findReferences(request, metadata); } - public List findReferences(@Valid ReferencesSearchRequest request, Metadata metadata) { + public List findReferences(@Valid ReferencesSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(ReferenceEntity.class) .appendList(request.getAccessions(), "referenceSet.sourceGermplasm.accessionNumber") @@ -83,7 +85,7 @@ public List findReferences(@Valid ReferencesSearchRequest request, Me .appendNumberRange(request.getMinLength(), request.getMaxLength(), "length") .appendSingle(request.isIsDerived(), "referenceSet.isDerived"); - Page page = referenceRepository.findAllBySearch(searchQuery, pageReq); + Page page = referenceRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List references = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return references; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceSetService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceSetService.java index 89b889b9..1ad58b53 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceSetService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/ReferenceSetService.java @@ -33,7 +33,8 @@ public ReferenceSetService(ReferenceSetRepository referenceSetRepository) { public List findReferenceSets(String referenceSetDbId, String accession, String assemblyPUI, String md5checksum, String trialDbId, String studyDbId, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { ReferenceSetsSearchRequest request = new ReferenceSetsSearchRequest(); if (referenceSetDbId != null) request.addReferenceSetDbIdsItem(referenceSetDbId); @@ -57,7 +58,8 @@ public List findReferenceSets(String referenceSetDbId, String acce return findReferenceSets(request, metadata); } - public List findReferenceSets(ReferenceSetsSearchRequest request, Metadata metadata) { + public List findReferenceSets(ReferenceSetsSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( ReferenceSetEntity.class).appendList(request.getAccessions(), "sourceGermplasm.accessionNumber") @@ -65,7 +67,7 @@ public List findReferenceSets(ReferenceSetsSearchRequest request, .appendList(request.getMd5checksums(), "md5checksum") .appendList(request.getReferenceSetDbIds(), "id"); - Page page = referenceSetRepository.findAllBySearch(searchQuery, pageReq); + Page page = referenceSetRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List referenceSets = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return referenceSets; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/SampleService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/SampleService.java index 9b612ebb..4f5b3680 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/SampleService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/SampleService.java @@ -87,7 +87,8 @@ public List findBatchDeleteSamples(String batchDeleteDbId, Metadata meta public List findSamples(String sampleDbId, String sampleName, String sampleGroupDbId, String observationUnitDbId, String plateDbId, String plateName, String germplasmDbId, String studyDbId, String trialDbId, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { SampleSearchRequest request = new SampleSearchRequest(); if (sampleDbId != null) request.addSampleDbIdsItem(sampleDbId); @@ -117,7 +118,8 @@ public List findSamples(String sampleDbId, String sampleName, String sam return findSamples(request, metadata); } - public List findSamples(SampleSearchRequest request, Metadata metadata) { + public List findSamples(SampleSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(SampleEntity.class) .withExRefs(request.getExternalReferenceIDs(), request.getExternalReferenceSources()) @@ -135,7 +137,7 @@ public List findSamples(SampleSearchRequest request, Metadata metadata) .appendList(request.getStudyDbIds(), "plate.study.id") .appendList(request.getStudyNames(), "plate.study.studyName"); - Page page = sampleRepository.findAllBySearch(searchQuery, pageReq); + Page page = sampleRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List samples = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return samples; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantService.java index 6a046e3a..c4612fc3 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantService.java @@ -31,7 +31,8 @@ public VariantService(VariantRepository variantRepository) { } public List findVariants(String variantDbId, String variantSetDbId, String referenceDbId, - String referenceSetDbId, String externalReferenceId, String externalReferenceSource, Metadata metadata) { + String referenceSetDbId, String externalReferenceId, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException{ VariantsSearchRequest request = new VariantsSearchRequest(); if (variantSetDbId != null) @@ -48,12 +49,14 @@ public List findVariants(String variantDbId, String variantSetDbId, Str return findVariants(request, metadata); } - public List findVariants(VariantsSearchRequest request, Metadata metadata) { + public List findVariants(VariantsSearchRequest request, Metadata metadata) + throws BrAPIServerException { return findVariantEntities(request, metadata).stream().map(this::convertFromEntity) .collect(Collectors.toList()); } - public List findVariantEntities(VariantsSearchRequest request, Metadata metadata) { + public List findVariantEntities(VariantsSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(VariantEntity.class); if (request.getCallSetDbIds() != null && !request.getCallSetDbIds().isEmpty()) { @@ -63,7 +66,7 @@ public List findVariantEntities(VariantsSearchRequest request, Me searchQuery = searchQuery.appendList(request.getVariantSetDbIds(), "variantSet.id") .appendList(request.getVariantDbIds(), "id"); - Page page = variantRepository.findAllBySearch(searchQuery, pageReq); + Page page = variantRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, page); return page.getContent(); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantSetService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantSetService.java index 507b1a18..a1572a41 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantSetService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/geno/VariantSetService.java @@ -55,7 +55,8 @@ public VariantSetService(VariantSetRepository variantSetRepository, VariantServi public List findVariantSets(String variantSetDbId, String variantDbId, String callSetDbId, String studyDbId, String studyName, String referenceSetDbId, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { VariantSetsSearchRequest request = new VariantSetsSearchRequest(); if (variantSetDbId != null) request.addVariantSetDbIdsItem(variantSetDbId); @@ -78,13 +79,15 @@ public List findVariantSets(String variantSetDbId, String variantDbI return findVariantSets(request, metadata); } - public List findVariantSets(VariantSetsSearchRequest request, Metadata metadata) { + public List findVariantSets(VariantSetsSearchRequest request, Metadata metadata) + throws BrAPIServerException { List variantSets = findVariantSetEntities(request, metadata).stream().map(this::convertFromEntity) .collect(Collectors.toList()); return variantSets; } - private List findVariantSetEntities(VariantSetsSearchRequest request, Metadata metadata) { + private List findVariantSetEntities(VariantSetsSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( VariantSetEntity.class); @@ -97,7 +100,7 @@ private List findVariantSetEntities(VariantSetsSearchRequest r searchQuery.appendList(request.getStudyDbIds(), "study.id") .appendList(request.getStudyNames(), "study.studyName").appendList(request.getVariantSetDbIds(), "id"); - Page page = variantSetRepository.findAllBySearch(searchQuery, pageReq); + Page page = variantSetRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, page); return page.getContent(); } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossService.java index 93932f1e..e35d0911 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossService.java @@ -49,7 +49,8 @@ public CrossService(CrossRepository crossRepository, CrossingProjectService cros public List findCrosses(String crossingProjectDbId, String crossingProjectName, String crossDbId, String crossName, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { List crosses = findCrossEntities(crossingProjectDbId, crossingProjectName, crossDbId, crossName, null, commonCropName, programDbId, externalReferenceId, externalReferenceID, externalReferenceSource, false, metadata).map(this::convertToCross).getContent(); @@ -58,7 +59,8 @@ public List findCrosses(String crossingProjectDbId, String crossingProjec public List findPlannedCrosses(String crossingProjectDbId, String crossingProjectName, String crossDbId, String crossName, String status, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { List crosses = findCrossEntities(crossingProjectDbId, crossingProjectName, crossDbId, crossName, status, commonCropName, programDbId, externalReferenceId, externalReferenceID, externalReferenceSource, true, metadata).map(this::convertToPlanned).getContent(); @@ -67,7 +69,8 @@ public List findPlannedCrosses(String crossingProjectDbId, String public Page findCrossEntities(String crossingProjectDbId, String crossingProjectName, String crossDbId, String crossName, String status, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Boolean plannedCross, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Boolean plannedCross, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(CrossEntity.class); @@ -81,7 +84,7 @@ public Page findCrossEntities(String crossingProjectDbId, String cr if (plannedCross != null) searchQuery = searchQuery.appendSingle(plannedCross, "planned"); - Page page = crossRepository.findAllBySearch(searchQuery, pageReq); + Page page = crossRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, page); return page; } diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossingProjectService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossingProjectService.java index b7cabfc5..d8edb9ec 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossingProjectService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/CrossingProjectService.java @@ -41,7 +41,8 @@ public CrossingProjectService(CrossingProjectRepository crossingProjectRepositor public List findCrossingProjects(String crossingProjectDbId, String crossingProjectName, Boolean includePotentialParents, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( @@ -59,7 +60,7 @@ public List findCrossingProjects(String crossingProjectDbId, St searchQuery = searchQuery.withExRefs(Arrays.asList(externalReferenceID), Arrays.asList(externalReferenceSource)); - Page page = crossingProjectRepository.findAllBySearch(searchQuery, pageReq); + Page page = crossingProjectRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List crossingProjects = new ArrayList<>(); for (CrossingProjectEntity entity : page) { crossingProjects.add(convertFromEntity(entity, includePotentialParents)); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeService.java index a22ca6e9..1a912ede 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeService.java @@ -43,7 +43,8 @@ public List findGermplasmAttributes(String attributeCategory String attributeName, String attributePUI, String germplasmDbId, String methodDbId, String methodName, String methodPUI, String scaleDbId, String scaleName, String scalePUI, String traitDbId, String traitName, String traitPUI, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { GermplasmAttributeSearchRequest request = new GermplasmAttributeSearchRequest(); if (attributeCategory != null) @@ -85,7 +86,7 @@ public List findGermplasmAttributes(String attributeCategory } public List findGermplasmAttributes(@Valid GermplasmAttributeSearchRequest request, - Metadata metadata) { + Metadata metadata) throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmAttributeDefinitionEntity.class); @@ -125,7 +126,7 @@ public List findGermplasmAttributes(@Valid GermplasmAttribut .appendList(request.getOntologyDbIds(), "ontology.id") .appendList(request.getCommonCropNames(), "crop.crop_name"); - Page page = attributeRepository.findAllBySearch(searchQuery, pageReq); + Page page = attributeRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List attributes = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return attributes; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeValueService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeValueService.java index ca892a77..d74ca453 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeValueService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmAttributeValueService.java @@ -40,7 +40,8 @@ public GermplasmAttributeValueService(GermplasmAttributeValueRepository attribut public List findGermplasmAttributeValues(String attributeValueDbId, String attributeDbId, String attributeName, String germplasmDbId, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { GermplasmAttributeValueSearchRequest request = new GermplasmAttributeValueSearchRequest(); if (attributeValueDbId != null) @@ -62,7 +63,8 @@ public List findGermplasmAttributeValues(String attribu } public List findGermplasmAttributeValues( - @Valid GermplasmAttributeValueSearchRequest request, Metadata metadata) { + @Valid GermplasmAttributeValueSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmAttributeValueEntity.class) @@ -78,7 +80,7 @@ public List findGermplasmAttributeValues( .appendList(request.getGermplasmDbIds(), "germplasm.id") .appendList(request.getGermplasmNames(), "germplasm.germplasmName"); - Page page = attributeValueRepository.findAllBySearch(searchQuery, pageReq); + Page page = attributeValueRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List attributeValues = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return attributeValues; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmService.java index 5c36949d..7513d84c 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/GermplasmService.java @@ -27,7 +27,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -59,7 +58,8 @@ public List findGermplasm(String germplasmPUI, String germplasmDbId, String accessionNumber, String collection, String binomialName, String genus, String species, String trialDbId, String studyDbId, String synonym, String parentDbId, String progenyDbId, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { GermplasmSearchRequest request = new GermplasmSearchRequest(); if (germplasmPUI != null) @@ -98,7 +98,8 @@ public List findGermplasm(String germplasmPUI, String germplasmDbId, return findGermplasm(request, metadata); } - public List findGermplasm(@Valid GermplasmSearchRequest request, Metadata metadata) { + public List findGermplasm(@Valid GermplasmSearchRequest request, Metadata metadata) + throws BrAPIServerException { log.debug("starting germplasm search"); Page page = findGermplasmEntities(request, metadata); log.debug("germplasm search complete, converting germplasm entities"); @@ -108,8 +109,59 @@ public List findGermplasm(@Valid GermplasmSearchRequest request, Meta return germplasms; } - public Page findGermplasmEntities(@Valid GermplasmSearchRequest request, Metadata metadata) { + public List findGermplasmWithoutPaging(@Valid GermplasmSearchRequest request) { + List entities = findGermplasmEntitiesWithoutPaging(request); + return entities.stream().map(this::convertFromEntity).collect(Collectors.toList()); + } + + public Page findGermplasmEntities(@Valid GermplasmSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); + + SearchQueryBuilder searchQuery = buildGermplasmSearchQuery(request); + + // Since the built searchQuery contains a lazily loaded collection retrieved with a LEFT JOIN FETCH, + // use findAllBySearchPaginatingWithFetches() + Page germs = germplasmRepository.findAllBySearchPaginatingWithFetches(searchQuery, pageReq); + + // Hopefully this retains insertion order in the page? + List ids = germs.map(BrAPIBaseEntity::getId).toList(); + + if (!germs.isEmpty()) { + // If records were found, iterate through the rest of the lazily loaded collections of the GermplasmEntity + // and LEFT JOIN FETCH all of them. + log.debug("Fetching xrefs"); + fetchXrefs(ids, germs); + log.debug("fetching attributes"); + fetchAttributes(ids, germs); + log.debug("fetching donors"); + fetchDonors(ids, germs); + log.debug("fetching origins"); + fetchOrigin(ids, germs); + log.debug("fetching institutes"); + fetchInstitutes(ids, germs); + log.debug("fetching taxons"); + fetchTaxons(ids, germs); + log.debug("fetching storage codes"); + fetchStorageCodes(ids, germs); + log.debug("fetching pedigree edges"); + fetchPedigreeEdges(ids, germs); + } + return germs; + } + + public List findGermplasmEntitiesWithoutPaging(@Valid GermplasmSearchRequest request) { + SearchQueryBuilder searchQuery = buildGermplasmSearchQuery(request); + + List germs = germplasmRepository.findAllBySearch(searchQuery); + + if (!germs.isEmpty()) { + fetchRemainingGermCollectionsUsingQuery(searchQuery, germs); + } + return germs; + } + + private SearchQueryBuilder buildGermplasmSearchQuery(GermplasmSearchRequest request) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("synonyms", "synonyms") @@ -148,150 +200,250 @@ public Page findGermplasmEntities(@Valid GermplasmSearchRequest .appendList(request.getGenus(), "genus").appendList(request.getSpecies(), "species") .appendNamesList(request.getBinomialNames(), "genus", "genus", "species") .appendList(request.getFamilyCodes(), "familyCode"); + return searchQuery; + } - Page page = germplasmRepository.findAllBySearch(searchQuery, pageReq); + // TODO: These left join fetch methods could be optimized a bit by guaranteeing a search on the same sort, because if the fetched + // TODO: list comes back with the same number of elements as the entities that come in, the two entity lists are in the exact same order. + // TODO: Therefore no need to store a map and can just iterate through both at the same time O(n). - if(!page.isEmpty()) { - log.debug("fetching xrefs"); - fetchXrefs(page); - log.debug("fetching attributes"); - fetchAttributes(page); - log.debug("fetching donors"); - fetchDonors(page); - log.debug("fetching origins"); - fetchOrigin(page); - log.debug("fetching institutes"); - fetchInstitutes(page); - log.debug("fetching taxons"); - fetchTaxons(page); - log.debug("fetching storage codes"); - fetchStorageCodes(page); - log.debug("fetching pedigree edges"); - fetchPedigreeEdges(page); - } - - return page; - } + // TODO: Revisit when sortBy is added to GermplasmSearchRequest and we can pass through a sortBy value. + private void fetchXrefs(List ids, Page germEntities) { + SearchQueryBuilder searchQuery = new SearchQueryBuilder<>(GermplasmEntity.class); - private void fetchXrefs(Page page) { - SearchQueryBuilder searchQuery = new SearchQueryBuilder(GermplasmEntity.class); searchQuery.leftJoinFetch("externalReferences", "externalReferences") - .leftJoinFetch("pedigree", "pedigree") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page xrefs = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List xrefs = germplasmRepository.findAllBySearch(searchQuery); Map> xrefByEntity = new HashMap<>(); xrefs.forEach(entity -> xrefByEntity.put(entity.getId(), entity.getExternalReferences())); - page.forEach(entity -> entity.setExternalReferences(xrefByEntity.get(entity.getId()))); + germEntities.forEach(entity -> entity.setExternalReferences(xrefByEntity.get(entity.getId().toString()))); } - private void fetchAttributes(Page page) { + private void fetchAttributes(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("attributes", "attributes") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page attributes = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List attributes = germplasmRepository.findAllBySearch(searchQuery); Map> attributesByGerm = new HashMap<>(); attributes.forEach(germ -> attributesByGerm.put(germ.getId(), germ.getAttributes())); - page.forEach(germ -> germ.setAttributes(attributesByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setAttributes(attributesByGerm.get(germ.getId().toString()))); } - private void fetchDonors(Page page) { + private void fetchDonors(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("donors", "donors") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page donors = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List donors = germplasmRepository.findAllBySearch(searchQuery); Map> donorsByGerm = new HashMap<>(); donors.forEach(germ -> donorsByGerm.put(germ.getId(), germ.getDonors())); - page.forEach(germ -> germ.setDonors(donorsByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setDonors(donorsByGerm.get(germ.getId().toString()))); } - private void fetchOrigin(Page page) { + private void fetchOrigin(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("germplasmOrigin", "germplasmOrigin") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page origins = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List origins = germplasmRepository.findAllBySearch(searchQuery); Map> originsByGerm = new HashMap<>(); origins.forEach(germ -> originsByGerm.put(germ.getId(), germ.getGermplasmOrigin())); - page.forEach(germ -> germ.setGermplasmOrigin(originsByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setGermplasmOrigin(originsByGerm.get(germ.getId().toString()))); } - private void fetchInstitutes(Page page) { + private void fetchInstitutes(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("institutes", "institutes") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page institutes = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List institutes = germplasmRepository.findAllBySearch(searchQuery); Map> institutesByGerm = new HashMap<>(); institutes.forEach(germ -> institutesByGerm.put(germ.getId(), germ.getInstitutes())); - page.forEach(germ -> germ.setInstitutes(institutesByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setInstitutes(institutesByGerm.get(germ.getId().toString()))); } - private void fetchTaxons(Page page) { + private void fetchTaxons(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("taxonIds", "taxonIds") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page taxonIds = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List taxonIds = germplasmRepository.findAllBySearch(searchQuery); Map> taxonIdsByGerm = new HashMap<>(); taxonIds.forEach(germ -> taxonIdsByGerm.put(germ.getId(), germ.getTaxonIds())); - page.forEach(germ -> germ.setTaxonIds(taxonIdsByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setTaxonIds(taxonIdsByGerm.get(germ.getId().toString()))); } - private void fetchStorageCodes(Page page) { + private void fetchStorageCodes(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("typeOfGermplasmStorageCode", "typeOfGermplasmStorageCode") - .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); + .appendIds(ids); - Page storageCodes = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List storageCodes = germplasmRepository.findAllBySearch(searchQuery); Map> storageCodesByGerm = new HashMap<>(); storageCodes.forEach(germ -> storageCodesByGerm.put(germ.getId(), germ.getTypeOfGermplasmStorageCode())); - page.forEach(germ -> germ.setTypeOfGermplasmStorageCode(storageCodesByGerm.get(germ.getId()))); + germEntities.forEach(germ -> germ.setTypeOfGermplasmStorageCode(storageCodesByGerm.get(germ.getId().toString()))); } - private void fetchPedigreeEdges(Page page) { + private void fetchPedigreeEdges(List ids, Page germEntities) { SearchQueryBuilder searchQuery = new SearchQueryBuilder( GermplasmEntity.class); searchQuery.leftJoinFetch("pedigree", "pedigree") .leftJoinFetch("*pedigree.crossingProject", "crossingProject") .leftJoinFetch("*pedigree.edges", "pedigreeEdges") .leftJoinFetch("*pedigreeEdges.connectedNode", "connectedNode") - .appendList(page.stream() - .map(BrAPIBaseEntity::getId) - .collect(Collectors.toList()), "id"); + .appendIds(ids); - Page pedigree = germplasmRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + List pedigree = germplasmRepository.findAllBySearch(searchQuery); Map pedigreeByGerm = new HashMap<>(); pedigree.forEach(germ -> pedigreeByGerm.put(germ.getId(), germ.getPedigree())); - page.forEach(germ -> { - germ.setPedigree(pedigreeByGerm.get(germ.getId())); + germEntities.forEach(germ -> { + germ.setPedigree(pedigreeByGerm.get(germ.getId().toString())); }); } + private void fetchRemainingGermCollectionsUsingQuery(SearchQueryBuilder searchQuery, List germEntities) { + // Remove unused left join fetches from the base query + searchQuery.removeLeftJoinFetch("synonyms", "synonyms") + .removeLeftJoinFetch("breedingMethod", "breedingMethod") + .removeLeftJoinFetch("crop", "crop") + .removeLeftJoinFetch("pedigree", "pedigree") + .removeLeftJoinFetch("*pedigree.crossingProject", "crossingProject"); + // Fetch xrefs + log.debug("Fetching xrefs"); + searchQuery.leftJoinFetch("externalReferences", + "externalReferences"); + + List xrefs = germplasmRepository.findAllBySearch(searchQuery); + + Map> xrefByEntity = new HashMap<>(); + xrefs.forEach(entity -> xrefByEntity.put(entity.getId().toString(), entity.getExternalReferences())); + + germEntities.forEach(entity -> entity.setExternalReferences(xrefByEntity.get(entity.getId().toString()))); + // Fetch attributes + log.debug("fetching attributes"); + searchQuery.removeAndReplaceLeftJoinFetch("attributes", + "attributes", + "externalReferences", + "externalReferences"); + + List attributes = germplasmRepository.findAllBySearch(searchQuery); + + Map> attributesByGerm = new HashMap<>(); + attributes.forEach(germ -> attributesByGerm.put(germ.getId().toString(), germ.getAttributes())); + + germEntities.forEach(germ -> germ.setAttributes(attributesByGerm.get(germ.getId().toString()))); + // Fetch donors + log.debug("fetching donors"); + searchQuery.removeAndReplaceLeftJoinFetch("donors", + "donors", + "attributes", + "attributes"); + + List donors = germplasmRepository.findAllBySearch(searchQuery); + + Map> donorsByGerm = new HashMap<>(); + donors.forEach(germ -> donorsByGerm.put(germ.getId().toString(), germ.getDonors())); + + germEntities.forEach(germ -> germ.setDonors(donorsByGerm.get(germ.getId().toString()))); + // Fetch origins + log.debug("fetching origins"); + searchQuery.removeAndReplaceLeftJoinFetch("germplasmOrigin", + "germplasmOrigin", + "donors", + "donors"); + + + List origins = germplasmRepository.findAllBySearch(searchQuery); + + Map> originsByGerm = new HashMap<>(); + origins.forEach(germ -> originsByGerm.put(germ.getId().toString(), germ.getGermplasmOrigin())); + + germEntities.forEach(germ -> germ.setGermplasmOrigin(originsByGerm.get(germ.getId().toString()))); + + // Fetch institutes + log.debug("fetching institutes"); + searchQuery.removeAndReplaceLeftJoinFetch("institutes", + "institutes", + "germplasmOrigin", + "germplasmOrigin"); + + List institutes = germplasmRepository.findAllBySearch(searchQuery); + + Map> institutesByGerm = new HashMap<>(); + institutes.forEach(germ -> institutesByGerm.put(germ.getId().toString(), germ.getInstitutes())); + + germEntities.forEach(germ -> germ.setInstitutes(institutesByGerm.get(germ.getId().toString()))); + + // Fetch taxons + log.debug("fetching taxons"); + searchQuery.removeAndReplaceLeftJoinFetch("taxonIds", + "taxonIds", + "institutes", + "institutes"); + + List taxonIds = germplasmRepository.findAllBySearch(searchQuery); + + Map> taxonIdsByGerm = new HashMap<>(); + taxonIds.forEach(germ -> taxonIdsByGerm.put(germ.getId().toString(), germ.getTaxonIds())); + + germEntities.forEach(germ -> germ.setTaxonIds(taxonIdsByGerm.get(germ.getId().toString()))); + // Fetch storage codes + log.debug("fetching storage codes"); + searchQuery.removeAndReplaceLeftJoinFetch("typeOfGermplasmStorageCode", + "typeOfGermplasmStorageCode", + "taxonIds", + "taxonIds"); + + List storageCodes = germplasmRepository.findAllBySearch(searchQuery); + + Map> storageCodesByGerm = new HashMap<>(); + storageCodes.forEach(germ -> storageCodesByGerm.put(germ.getId().toString(), germ.getTypeOfGermplasmStorageCode())); + + germEntities.forEach(germ -> germ.setTypeOfGermplasmStorageCode(storageCodesByGerm.get(germ.getId().toString()))); + // Fetch pedigree edges + log.debug("fetching pedigree edges"); + searchQuery.removeAndReplaceLeftJoinFetch("pedigree", + "pedigree", + "typeOfGermplasmStorageCode", + "typeOfGermplasmStorageCode") + .leftJoinFetch("*pedigree.crossingProject", "crossingProject") + .leftJoinFetch("*pedigree.edges", "pedigreeEdges") + .leftJoinFetch("*pedigreeEdges.connectedNode", "connectedNode"); + + List pedigree = germplasmRepository.findAllBySearch(searchQuery); + + Map pedigreeByGerm = new HashMap<>(); + pedigree.forEach(germ -> pedigreeByGerm.put(germ.getId().toString(), germ.getPedigree())); + + germEntities.forEach(germ -> { + germ.setPedigree(pedigreeByGerm.get(germ.getId().toString())); + }); + }; + public Germplasm getGermplasm(String germplasmDbId) throws BrAPIServerException { return convertFromEntity(getGermplasmEntity(germplasmDbId, HttpStatus.NOT_FOUND)); } @@ -525,7 +677,8 @@ private void updateSynonymEntities(List synonyms, G } } - public GermplasmEntity findByUnknownIdentity(String germplasmStr) { + public GermplasmEntity findByUnknownIdentity(String germplasmStr) + throws BrAPIServerException { List germplasmList = Arrays.asList(germplasmStr); Metadata metadata = new Metadata().pagination(new IndexPagination()); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/PedigreeService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/PedigreeService.java index 875ab788..0921c89d 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/PedigreeService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/PedigreeService.java @@ -64,7 +64,8 @@ public List findPedigree(String germplasmPUI, String germplasmDbId String trialDbId, String studyDbId, String synonym, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceSource, Boolean includeParents, Boolean includeSiblings, Boolean includeProgeny, Boolean includeFullTree, Integer pedigreeDepth, Integer progenyDepth, - Metadata metadata) { + Metadata metadata) + throws BrAPIServerException { PedigreeSearchRequest request = new PedigreeSearchRequest(); if (germplasmPUI != null) @@ -111,13 +112,15 @@ public List findPedigree(String germplasmPUI, String germplasmDbId return findPedigree(request, metadata); } - public List findPedigree(PedigreeSearchRequest request, Metadata metadata) { + public List findPedigree(PedigreeSearchRequest request, Metadata metadata) + throws BrAPIServerException { List page = findPedigreeEntities(request, metadata); List pedigreeNodes = convertFromEntities(page, request); return pedigreeNodes; } - public List findPedigreeEntities(PedigreeSearchRequest request, Metadata metadata) { + public List findPedigreeEntities(PedigreeSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( PedigreeNodeEntity.class); @@ -153,7 +156,7 @@ public List findPedigreeEntities(PedigreeSearchRequest reque .appendNamesList(request.getBinomialNames(), "germplasm.genus", "germplasm.genus", "germplasm.species") .appendList(request.getFamilyCodes(), "familyCode"); - Page page = pedigreeRepository.findAllBySearch(searchQuery, pageReq); + Page page = pedigreeRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List filteredNodes = filterGenerations(request, page.getContent()); @@ -304,7 +307,8 @@ public void updateGermplasmPedigree(List data) throws BrAPIServerExce } - private Map getExistingPedigreeNodes(List germplasmDbIds) { + private Map getExistingPedigreeNodes(List germplasmDbIds) + throws BrAPIServerException { Map nodesByGermplasm = new HashMap<>(); if (null != germplasmDbIds && !germplasmDbIds.isEmpty()) { @@ -495,7 +499,7 @@ private void updateEntityWithEdges(PedigreeNodeEntity entity, PedigreeNode node) search.appendSingle(node.getGermplasmDbId(), "connectedNode.germplasm.id"); search.appendEnum(PedigreeEdgeEntity.EdgeType.child, "edgeType"); Pageable defaultPageSize = PagingUtility.getPageRequest(new Metadata().pagination(new IndexPagination().pageSize(10000000))); - Page existingParentEdges = pedigreeEdgeRepository.findAllBySearch(search, defaultPageSize); + Page existingParentEdges = pedigreeEdgeRepository.findAllBySearchAndPaginate(search, defaultPageSize); List edgeIdsToDelete = new ArrayList<>(); edgeIdsToDelete.addAll(entity.getParentEdges().stream().map(e -> e.getId()).collect(Collectors.toList())); @@ -518,7 +522,7 @@ private void updateEntityWithEdges(PedigreeNodeEntity entity, PedigreeNode node) search.appendSingle(node.getGermplasmDbId(), "connectedNode.germplasm.id"); search.appendEnum(PedigreeEdgeEntity.EdgeType.parent, "edgeType"); Pageable defaultPageSize = PagingUtility.getPageRequest(new Metadata().pagination(new IndexPagination().pageSize(10000000))); - Page existingProgenyEdges = pedigreeEdgeRepository.findAllBySearch(search, defaultPageSize); + Page existingProgenyEdges = pedigreeEdgeRepository.findAllBySearchAndPaginate(search, defaultPageSize); List edgeIdsToDelete = new ArrayList<>(); edgeIdsToDelete.addAll(entity.getProgenyEdges().stream().map(e -> e.getId()).collect(Collectors.toList())); @@ -537,7 +541,8 @@ private void updateEntityWithEdges(PedigreeNodeEntity entity, PedigreeNode node) } } - public PedigreeNode convertFromGermplasmToPedigree(Germplasm germplasm) { + public PedigreeNode convertFromGermplasmToPedigree(Germplasm germplasm) + throws BrAPIServerException { PedigreeNode node = new PedigreeNode(); List pedigreeList = new ArrayList<>(); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/SeedLotService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/SeedLotService.java index 9ab9c104..630aa755 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/germ/SeedLotService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/germ/SeedLotService.java @@ -60,7 +60,8 @@ public SeedLotService(SeedLotRepository seedLotRepository, public List findSeedLots(String seedLotDbId, String germplasmDbId, String germplasmName, String crossDbId, String crossName, String commonCropName, String programDbId, String externalReferenceId, - String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(SeedLotEntity.class); @@ -81,7 +82,7 @@ public List findSeedLots(String seedLotDbId, String germplasmDbId, Stri searchQuery = searchQuery.withExRefs(Arrays.asList(externalReferenceID), Arrays.asList(externalReferenceSource)); - Page page = seedLotRepository.findAllBySearch(searchQuery, pageReq); + Page page = seedLotRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List seedLots = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return seedLots; @@ -140,7 +141,8 @@ public List findSeedLotTransactions(String seedLotDbId, Stri public List findSeedLotTransactions(String transactionDbId, String seedLotDbId, String germplasmDbId, String germplasmName, String crossDbId, String crossName, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, String externalReferenceSource, - Metadata metadata) { + Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( SeedLotTransactionEntity.class); @@ -165,7 +167,7 @@ public List findSeedLotTransactions(String transactionDbId, searchQuery = searchQuery.withExRefs(Arrays.asList(externalReferenceID), Arrays.asList(externalReferenceSource)); - Page page = seedLotTransactionRepository.findAllBySearch(searchQuery, pageReq); + Page page = seedLotTransactionRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List transactions = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return transactions; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/EventService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/EventService.java index 76eeb10a..318d9fd9 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/EventService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/EventService.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.stream.Collectors; +import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException; import org.brapi.test.BrAPITestServer.model.entity.pheno.EventEntity; import org.brapi.test.BrAPITestServer.repository.pheno.EventRepository; import org.brapi.test.BrAPITestServer.service.DateUtility; @@ -29,7 +30,8 @@ public EventService(EventRepository eventRepository) { } public List findEvents(String eventDbId, String studyDbId, String observationUnitDbId, String eventType, - OffsetDateTime dateRangeStart, OffsetDateTime dateRangeEnd, Metadata metadata) { + OffsetDateTime dateRangeStart, OffsetDateTime dateRangeEnd, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(EventEntity.class); @@ -45,7 +47,7 @@ public List findEvents(String eventDbId, String studyDbId, String observa "*dateOccured"); } - Page page = eventRepository.findAllBySearch(searchQuery, pageReq); + Page page = eventRepository.findAllBySearchAndPaginate(searchQuery, pageReq); List crossingProjects = page.map(this::convertFromEntity).getContent(); PagingUtility.calculateMetaData(metadata, page); return crossingProjects; diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ImageService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ImageService.java index 040541de..ac24e8d8 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ImageService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ImageService.java @@ -42,7 +42,8 @@ public ImageService(ImageRepository imageRepository, ObservationService observat public List findImages(String imageDbId, String imageName, String observationUnitDbId, String observationDbId, String descriptiveOntologyTerm, String commonCropName, String programDbId, - String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { ImageSearchRequest request = new ImageSearchRequest(); if (imageDbId != null) request.addImageDbIdsItem(imageDbId); @@ -64,7 +65,8 @@ public List findImages(String imageDbId, String imageName, String observa return findImages(request, metadata); } - public List findImageEntities(ImageSearchRequest request, Metadata metadata) { + public List findImageEntities(ImageSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(ImageEntity.class); if (request.getDescriptiveOntologyTerms() != null) { @@ -86,12 +88,13 @@ public List findImageEntities(ImageSearchRequest request, Metadata "timeStamp") .appendGeoJSONArea(request.getImageLocation()); - Page imagePage = imageRepository.findAllBySearch(searchQuery, pageReq); + Page imagePage = imageRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, imagePage); return imagePage.getContent(); } - public List findImages(ImageSearchRequest request, Metadata metadata) { + public List findImages(ImageSearchRequest request, Metadata metadata) + throws BrAPIServerException { List imagePage = findImageEntities(request, metadata); List images = imagePage.stream().map(this::convertFromEntity).collect(Collectors.toList()); return images; @@ -170,7 +173,8 @@ public byte[] getImageData(String imageDbId) { return bytes; } - public List deleteImages(ImageSearchRequest body, Metadata metadata) { + public List deleteImages(ImageSearchRequest body, Metadata metadata) + throws BrAPIServerException { List deletedImageDbIds = new ArrayList<>(); if (body.getTotalParameterCount() > 0) { diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/MethodService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/MethodService.java index f7802388..e0b29d07 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/MethodService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/MethodService.java @@ -34,7 +34,8 @@ public MethodService(MethodRepository methodRepository, OntologyService ontology public List findMethods(String methodDbId, String observationVariableDbId, String ontologyDbId, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(MethodEntity.class); if (observationVariableDbId != null) { @@ -43,7 +44,7 @@ public List findMethods(String methodDbId, String observationVariableDbI } searchQuery = searchQuery.appendSingle(methodDbId, "id").withExRefs(externalReferenceID, externalReferenceSource); - Page methodPage = methodRepository.findAllBySearch(searchQuery, pageReq); + Page methodPage = methodRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, methodPage); List methods = methodPage.map(this::convertFromEntity).getContent(); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationService.java index e729306a..7e6b0b51 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationService.java @@ -146,7 +146,8 @@ public ObservationTable findObservationsTable(String observationUnitDbId, String return findObservationsTable(obsRequest, metadata); } - public ObservationTable findObservationsTable(ObservationSearchRequest obsRequest, Metadata metadata) { + public ObservationTable findObservationsTable(ObservationSearchRequest obsRequest, Metadata metadata) + throws BrAPIServerException { Page page = findObservationEntities(obsRequest, metadata); log.debug("converting "+page.getSize()+" entities"); @@ -162,7 +163,8 @@ public ObservationTable findObservationsTable(ObservationSearchRequest obsReques return table; } - public List findObservations(@Valid ObservationSearchRequest request, Metadata metadata) { + public List findObservations(@Valid ObservationSearchRequest request, Metadata metadata) + throws BrAPIServerException { Page page = findObservationEntities(request, metadata); log.debug("converting "+page.getSize()+" entities"); List observations = page.map(this::convertFromEntity).getContent(); @@ -171,7 +173,8 @@ public List findObservations(@Valid ObservationSearchRequest reques return observations; } - public Page findObservationEntities(@Valid ObservationSearchRequest request, Metadata metadata) { + public Page findObservationEntities(@Valid ObservationSearchRequest request, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( ObservationEntity.class); @@ -243,7 +246,7 @@ public Page findObservationEntities(@Valid ObservationSearchR .appendList(request.getTrialDbIds(), "trial.id").appendList(request.getTrialNames(), "trial.trialName"); log.debug("starting search"); - Page page = observationRepository.findAllBySearch(searchQuery, pageReq); + Page page = observationRepository.findAllBySearchAndPaginate(searchQuery, pageReq); log.debug("search complete"); if(!page.isEmpty()) { @@ -297,7 +300,8 @@ public List updateObservations(@Valid Map deleteObservations(ObservationSearchRequest body, Metadata metadata) { + public List deleteObservations(ObservationSearchRequest body, Metadata metadata) + throws BrAPIServerException { List deletedObservationDbIds = new ArrayList<>(); if (body.getTotalParameterCount() > 0) { diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationUnitService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationUnitService.java index 7b145f8b..d87556d7 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationUnitService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationUnitService.java @@ -75,7 +75,8 @@ public List findObservationUnits(String observationUnitDbId, St String observationUnitLevelCode, String observationUnitLevelRelationshipName, String observationUnitLevelRelationshipOrder, String observationUnitLevelRelationshipCode, String observationUnitLevelRelationshipDbId, String commonCropName, Boolean includeObservations, - String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) { + String externalReferenceId, String externalReferenceID, String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { ObservationUnitSearchRequest request = buildObservationUnitsSearchRequest(observationUnitDbId, observationUnitName, germplasmDbId, studyDbId, locationDbId, trialDbId, programDbId, seasonDbId, observationUnitLevelName, observationUnitLevelOrder, observationUnitLevelCode, @@ -148,7 +149,8 @@ public ObservationUnitTable findObservationUnitsTable(String observationUnitDbId String seasonDbId, String observationLevel, String observationUnitLevelName, String observationUnitLevelOrder, String observationUnitLevelCode, String observationUnitLevelRelationshipName, String observationUnitLevelRelationshipOrder, - String observationUnitLevelRelationshipCode, String observationUnitLevelRelationshipDbId) { + String observationUnitLevelRelationshipCode, String observationUnitLevelRelationshipDbId) + throws BrAPIServerException { ObservationUnitSearchRequest ouRequest = buildObservationUnitsSearchRequest(observationUnitDbId, null, germplasmDbId, studyDbId, locationDbId, trialDbId, programDbId, seasonDbId, observationUnitLevelName, @@ -169,7 +171,8 @@ public ObservationUnitTable findObservationUnitsTable(String observationUnitDbId return table; } - public List findObservationUnits(@Valid ObservationUnitSearchRequest request, Metadata metadata) { + public List findObservationUnits(@Valid ObservationUnitSearchRequest request, Metadata metadata) + throws BrAPIServerException { Page page = findObservationUnitEntities(request, metadata); boolean includeObservations = request.isIncludeObservations() != null && request.isIncludeObservations(); @@ -191,7 +194,8 @@ public List findObservationUnits(@Valid ObservationUnitSearchRe } public Page findObservationUnitEntities(@Valid ObservationUnitSearchRequest request, - Metadata metadata) { + Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( ObservationUnitEntity.class); @@ -261,7 +265,7 @@ public Page findObservationUnitEntities(@Valid Observatio .appendList(request.getTrialNames(), "trial.trailName"); log.debug("Starting search"); - Page page = observationUnitRepository.findAllBySearch(searchQuery, pageReq); + Page page = observationUnitRepository.findAllBySearchAndPaginate(searchQuery, pageReq); log.debug("Search complete"); if(!page.isEmpty()) { @@ -272,13 +276,14 @@ public Page findObservationUnitEntities(@Valid Observatio return page; } - private void fetchTreatments(Page page) { + private void fetchTreatments(Page page) + throws BrAPIServerException { SearchQueryBuilder searchQuery = new SearchQueryBuilder( ObservationUnitEntity.class); searchQuery.leftJoinFetch("treatments", "treatments") .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); - Page treatments = observationUnitRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + Page treatments = observationUnitRepository.findAllBySearchAndPaginate(searchQuery, PageRequest.of(0, page.getSize())); Map> treatmentsByOu = new HashMap<>(); treatments.forEach(ou -> treatmentsByOu.put(ou.getId(), ou.getTreatments())); @@ -286,14 +291,15 @@ private void fetchTreatments(Page page) { page.forEach(ou -> ou.setTreatments(treatmentsByOu.get(ou.getId()))); } - private void fetchObsUnitLevelRelationships(Page page) { + private void fetchObsUnitLevelRelationships(Page page) + throws BrAPIServerException { SearchQueryBuilder searchQuery = new SearchQueryBuilder( ObservationUnitEntity.class); searchQuery.leftJoinFetch("position", "position") .leftJoinFetch("*position.observationLevelRelationships", "observationLevelRelationships") .appendList(page.stream().map(BrAPIBaseEntity::getId).collect(Collectors.toList()), "id"); - Page positions = observationUnitRepository.findAllBySearch(searchQuery, PageRequest.of(0, page.getSize())); + Page positions = observationUnitRepository.findAllBySearchAndPaginate(searchQuery, PageRequest.of(0, page.getSize())); Map positionByOu = new HashMap<>(); positions.forEach(ou -> positionByOu.put(ou.getId(), ou.getPosition())); @@ -367,7 +373,8 @@ public ObservationUnit updateObservationUnit(String observationUnitDbId, @Valid } public List findObservationLevels(String studyDbId, String trialDbId, - String programDbId, Metadata metadata) { + String programDbId, Metadata metadata) + throws BrAPIServerException { List allLevels = Arrays.asList(ObservationUnitHierarchyLevelEnum.values()).stream() .map(levelEnum -> { diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationVariableService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationVariableService.java index 18261b1e..8ba0b935 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationVariableService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ObservationVariableService.java @@ -68,7 +68,8 @@ public List findObservationVariables(String observationVari String methodPUI, String scaleDbId, String scaleName, String scalePUI, String traitDbId, String traitName, String traitPUI, String traitClass, String ontologyDbId, String commonCropName, String programDbId, String trialDbId, String studyDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { ObservationVariableSearchRequest request = new ObservationVariableSearchRequest(); @@ -113,7 +114,8 @@ public List findObservationVariables(String observationVari } public List findObservationVariables(ObservationVariableSearchRequest request, - Metadata metadata) { + Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder( ObservationVariableEntity.class); @@ -137,7 +139,7 @@ public List findObservationVariables(ObservationVariableSea .appendEnumList(request.getDataTypes(), "scale.dataType"); log.debug("Starting variable search"); - Page page = observationVariableRepository.findAllBySearch(searchQuery, pageReq); + Page page = observationVariableRepository.findAllBySearchAndPaginate(searchQuery, pageReq); log.debug("Variable search complete"); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ScaleService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ScaleService.java index 501f28fa..a4700d81 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ScaleService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/ScaleService.java @@ -39,7 +39,8 @@ public ScaleService(ScaleRepository scaleRepository, OntologyService ontologySer public List findScales(String scaleDbId, String observationVariableDbId, String ontologyDbId, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(ScaleEntity.class); if (observationVariableDbId != null) { @@ -48,7 +49,7 @@ public List findScales(String scaleDbId, String observationVariableDbId, } searchQuery = searchQuery.appendSingle(scaleDbId, "id").withExRefs(externalReferenceID, externalReferenceSource); - Page scalePage = scaleRepository.findAllBySearch(searchQuery, pageReq); + Page scalePage = scaleRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, scalePage); List scales = scalePage.map(this::convertFromEntity).getContent(); diff --git a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/TraitService.java b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/TraitService.java index d9dddc14..adb0bf7d 100644 --- a/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/TraitService.java +++ b/src/main/java/org/brapi/test/BrAPITestServer/service/pheno/TraitService.java @@ -35,7 +35,8 @@ public TraitService(TraitRepository traitRepository, OntologyService ontologySer public List findTraits(String traitDbId, String observationVariableDbId, String ontologyDbId, String commonCropName, String programDbId, String externalReferenceId, String externalReferenceID, - String externalReferenceSource, Metadata metadata) { + String externalReferenceSource, Metadata metadata) + throws BrAPIServerException { Pageable pageReq = PagingUtility.getPageRequest(metadata); SearchQueryBuilder searchQuery = new SearchQueryBuilder(TraitEntity.class); if (observationVariableDbId != null) { @@ -44,7 +45,7 @@ public List findTraits(String traitDbId, String observationVariableDbId, } searchQuery = searchQuery.appendSingle(traitDbId, "id").withExRefs(externalReferenceID, externalReferenceSource); - Page traitPage = traitRepository.findAllBySearch(searchQuery, pageReq); + Page traitPage = traitRepository.findAllBySearchAndPaginate(searchQuery, pageReq); PagingUtility.calculateMetaData(metadata, traitPage); List traits = traitPage.map(this::convertFromEntity).getContent(); diff --git a/src/main/resources/application.properties.template b/src/main/resources/application.properties.template index 6263bd9c..8baf2928 100644 --- a/src/main/resources/application.properties.template +++ b/src/main/resources/application.properties.template @@ -19,3 +19,13 @@ spring.mvc.dispatch-options-request=true security.oidc_discovery_url=https://example.com/auth/.well-known/openid-configuration security.enabled=true +security.issuer_url=http://example.com/issuerurl + +# This should either be set in accordance with a maximum number of SQL parameters (on JOIN FETCHES of collections, +# if there is more than one collection the IDs of each entity need to be passed through as parameters, and there is a SQL +# maximum of 65535. See GermplasmService.findGermplasmEntities()), +# whatever returns in a reasonable amount of time, +# or if you want to limit for the sake of server efficiency. +paging.page-size.max-allowed=65000 + +paging.page-size.default=1000