From 9654d6423674b55c9952b22c3af1fc0e2b44c02f Mon Sep 17 00:00:00 2001 From: LZRS Date: Wed, 2 Jun 2021 09:51:57 +0300 Subject: [PATCH] update getProductCatalogues to allow limit and order by serverVersion --- pom.xml | 2 +- .../repository/ProductCatalogueRepository.java | 3 +++ .../postgres/ProductCatalogueRepositoryImpl.java | 13 ++++++++++--- .../opensrp/service/ProductCatalogueService.java | 9 +++++++-- .../postgres/ProductCatalogueRepositoryTest.java | 13 ++++++++++++- .../service/ProductCatalogueServiceTest.java | 13 ++++++++++++- 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index fad0c2382..810c62cb3 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.11.3-SNAPSHOT + 2.11.4-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/repository/ProductCatalogueRepository.java b/src/main/java/org/opensrp/repository/ProductCatalogueRepository.java index 5176ea135..eeb215d24 100644 --- a/src/main/java/org/opensrp/repository/ProductCatalogueRepository.java +++ b/src/main/java/org/opensrp/repository/ProductCatalogueRepository.java @@ -9,8 +9,11 @@ public interface ProductCatalogueRepository extends BaseRepository getProductCataloguesBySearchBean(ProductCatalogueSearchBean productCatalogueSearchBean, String baseUrl); + List getProductCataloguesBySearchBean(ProductCatalogueSearchBean productCatalogueSearchBean, int limit, String baseUrl); + void safeRemove(Long id); ProductCatalogue getProductCatalogueByName(String productName); diff --git a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java index f04347744..b229cba38 100644 --- a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java @@ -158,11 +158,17 @@ public ProductCatalogue getById(Long uniqueId, String baseUrl) { } @Override - public List getProductCataloguesBySearchBean(ProductCatalogueSearchBean productCatalogueSearchBean, String baseUrl) { + public List getProductCataloguesBySearchBean(ProductCatalogueSearchBean productCatalogueSearchBean, int limit, String baseUrl) { ProductCatalogueExample productCatalogueExample = new ProductCatalogueExample(); populateProductCatalogueSearchCriteria(productCatalogueSearchBean, productCatalogueExample); - return convert(customProductCatalogueMapper.selectMany(productCatalogueExample, 0, DEFAULT_FETCH_SIZE), baseUrl); + productCatalogueExample.setOrderByClause(this.getOrderByClause(SERVER_VERSION, ASCENDING)); + return convert(customProductCatalogueMapper.selectMany(productCatalogueExample, 0, limit), baseUrl); + } + + @Override + public List getProductCataloguesBySearchBean(ProductCatalogueSearchBean productCatalogueSearchBean, String baseUrl){ + return getProductCataloguesBySearchBean(productCatalogueSearchBean, DEFAULT_FETCH_SIZE, baseUrl); } @Override @@ -231,8 +237,9 @@ private ProductCatalogueExample.Criteria populateProductCatalogueSearchCriteria( ProductCatalogueExample.Criteria criteria = productCatalogueExample.createCriteria(); - if (productCatalogueSearchBean.getServerVersion() != null && productCatalogueSearchBean.getServerVersion() != 0) + if (productCatalogueSearchBean.getServerVersion() != null && productCatalogueSearchBean.getServerVersion() >= 0) { criteria.andServerVersionGreaterThanOrEqualTo(productCatalogueSearchBean.getServerVersion()); + } if (StringUtils.isNotEmpty(productCatalogueSearchBean.getProductName())) criteria.andProductNameEqualTo(productCatalogueSearchBean.getProductName()); diff --git a/src/main/java/org/opensrp/service/ProductCatalogueService.java b/src/main/java/org/opensrp/service/ProductCatalogueService.java index e640255f7..ea1aff509 100644 --- a/src/main/java/org/opensrp/service/ProductCatalogueService.java +++ b/src/main/java/org/opensrp/service/ProductCatalogueService.java @@ -59,16 +59,21 @@ public void deleteProductCatalogueById(Long uniqueId) { } - public List getProductCatalogues(ProductCatalogueSearchBean productCatalogueSearchBean, String baseUrl) { + public List getProductCatalogues(ProductCatalogueSearchBean productCatalogueSearchBean, int limit, String baseUrl) { if (StringUtils.isBlank(productCatalogueSearchBean.getProductName()) && productCatalogueSearchBean.getUniqueId() == 0 && productCatalogueSearchBean.getServerVersion() == null) { return findAllProductCatalogues(baseUrl); } else { - return productCatalogueRepository.getProductCataloguesBySearchBean(productCatalogueSearchBean, baseUrl); + return productCatalogueRepository.getProductCataloguesBySearchBean(productCatalogueSearchBean, limit, baseUrl); } } + @Deprecated(since = "2.11.4-SNAPSHOT") + public List getProductCatalogues(ProductCatalogueSearchBean productCatalogueSearchBean, String baseUrl) { + return getProductCatalogues(productCatalogueSearchBean, Integer.MAX_VALUE, baseUrl); + } + public ProductCatalogue getProductCatalogueByName(String productName) { return productCatalogueRepository.getProductCatalogueByName(productName); } diff --git a/src/test/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryTest.java b/src/test/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryTest.java index e80e672bf..7e45f1d9c 100644 --- a/src/test/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryTest.java +++ b/src/test/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryTest.java @@ -81,7 +81,18 @@ public void testGetById() { } @Test - public void testGetProductCataloguesBySearchBean(){ + public void testGetProductCataloguesBySearchBeanWithLimit(){ + ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean(); + productCatalogueSearchBean.setUniqueId(1l); + int limit = Integer.MAX_VALUE; + List productCatalogues = productCatalogueRepository.getProductCataloguesBySearchBean(productCatalogueSearchBean, limit, "http://localhost:8080/opensrp"); + assertEquals(1,productCatalogues.size()); + assertEquals("Midwifery Kit", productCatalogues.get(0).getProductName()); + + } + + @Test + public void testGetProductCataloguesBySearchBeanWithNoLimitSet(){ ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean(); productCatalogueSearchBean.setUniqueId(1l); List productCatalogues = productCatalogueRepository.getProductCataloguesBySearchBean(productCatalogueSearchBean, "http://localhost:8080/opensrp"); diff --git a/src/test/java/org/opensrp/service/ProductCatalogueServiceTest.java b/src/test/java/org/opensrp/service/ProductCatalogueServiceTest.java index c7ad5e64b..f1973c771 100644 --- a/src/test/java/org/opensrp/service/ProductCatalogueServiceTest.java +++ b/src/test/java/org/opensrp/service/ProductCatalogueServiceTest.java @@ -36,7 +36,18 @@ protected Set getDatabaseScripts() { } @Test - public void testFindAllProductCatalogs() { + public void testFindAllProductCatalogsWithLimitSet() { + ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean(); + productCatalogueSearchBean.setProductName(""); + productCatalogueSearchBean.setUniqueId(0l); + int limit = Integer.MAX_VALUE; + List productCatalogueList = productCatalogueService + .getProductCatalogues(productCatalogueSearchBean, limit, "http://localhost:8080/opensrp"); + assertEquals(1, productCatalogueList.size()); + } + + @Test + public void testFindAllProductCatalogsWithNoLimitSet() { ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean(); productCatalogueSearchBean.setProductName(""); productCatalogueSearchBean.setUniqueId(0l);