From 2742a0697d2953661d7f11233160e6fed44393f8 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Tue, 27 Feb 2024 11:57:53 +0300 Subject: [PATCH 1/4] sanitize whitespaces from productName --- .../postgres/ProductCatalogueRepositoryImpl.java | 3 ++- src/main/java/org/opensrp/util/Utils.java | 5 +++++ src/test/java/org/opensrp/util/UtilTest.java | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java index b229cba38..39a975fd0 100644 --- a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java @@ -13,6 +13,7 @@ import org.opensrp.repository.ProductCatalogueRepository; import org.opensrp.repository.postgres.mapper.custom.CustomProductCatalogueMapper; import org.opensrp.search.ProductCatalogueSearchBean; +import org.opensrp.util.Utils; import org.smartregister.domain.ProductCatalogue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -225,7 +226,7 @@ private org.opensrp.domain.postgres.ProductCatalogue convert(ProductCatalogue pr org.opensrp.domain.postgres.ProductCatalogue pgProductCatalogue = new org.opensrp.domain.postgres.ProductCatalogue(); pgProductCatalogue.setUniqueId(primaryKey); pgProductCatalogue.setJson(productCatalogue); - pgProductCatalogue.setProductName(productCatalogue.getProductName()); + pgProductCatalogue.setProductName(Utils.replaceConsecutiveSpaces(productCatalogue.getProductName())); pgProductCatalogue.setServerVersion(productCatalogue.getServerVersion()); return pgProductCatalogue; diff --git a/src/main/java/org/opensrp/util/Utils.java b/src/main/java/org/opensrp/util/Utils.java index 55c22225b..45b1b33d6 100644 --- a/src/main/java/org/opensrp/util/Utils.java +++ b/src/main/java/org/opensrp/util/Utils.java @@ -157,6 +157,11 @@ public static void closeCloseable(Closeable closeable) { logger.error(e.getMessage(), e); } } + + public static String replaceConsecutiveSpaces(String input) { + // Use regular expression to replace consecutive spaces with a single space + return input.trim().replaceAll("\\s+", " "); + } public static class DatabaseConnectionParams { diff --git a/src/test/java/org/opensrp/util/UtilTest.java b/src/test/java/org/opensrp/util/UtilTest.java index e77dee530..b018871f8 100644 --- a/src/test/java/org/opensrp/util/UtilTest.java +++ b/src/test/java/org/opensrp/util/UtilTest.java @@ -70,4 +70,19 @@ public void testGetXlsToJsonForInvalidXls() throws IOException, JSONException { Utils.getXlsToJson(path); } + @Test + public void testReplaceConsecutiveSpaces(){ + // Test string with spaces in between words + String inputString = "Medicines (PPOS)"; + String modifiedString = Utils.replaceConsecutiveSpaces(inputString); + assertEquals("Medicines (PPOS)", modifiedString); + // Test string with space after words + inputString = "Medicines (PPOS) "; + modifiedString = Utils.replaceConsecutiveSpaces(inputString); + assertEquals("Medicines (PPOS)", modifiedString); + // Test string with space before words + inputString = "Medicines (PPOS)"; + modifiedString = Utils.replaceConsecutiveSpaces(inputString); + assertEquals("Medicines (PPOS)", modifiedString); + } } From 7aab6c24c5130391f446070469de785be95e7661 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 6 Mar 2024 16:37:04 +0300 Subject: [PATCH 2/4] sanitize spaces productname in productCatalogue valdiation stage --- pom.xml | 2 +- .../repository/postgres/ProductCatalogueRepositoryImpl.java | 2 +- src/main/java/org/opensrp/service/ProductCatalogueService.java | 2 ++ src/main/java/org/opensrp/service/StockService.java | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1222301ce..962595fa9 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.9-SNAPSHOT + 2.14.11-ALPHA2-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java index 39a975fd0..baea85dd9 100644 --- a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java @@ -226,7 +226,7 @@ private org.opensrp.domain.postgres.ProductCatalogue convert(ProductCatalogue pr org.opensrp.domain.postgres.ProductCatalogue pgProductCatalogue = new org.opensrp.domain.postgres.ProductCatalogue(); pgProductCatalogue.setUniqueId(primaryKey); pgProductCatalogue.setJson(productCatalogue); - pgProductCatalogue.setProductName(Utils.replaceConsecutiveSpaces(productCatalogue.getProductName())); + pgProductCatalogue.setProductName(productCatalogue.getProductName()); pgProductCatalogue.setServerVersion(productCatalogue.getServerVersion()); return pgProductCatalogue; diff --git a/src/main/java/org/opensrp/service/ProductCatalogueService.java b/src/main/java/org/opensrp/service/ProductCatalogueService.java index ea1aff509..1dcc906d2 100644 --- a/src/main/java/org/opensrp/service/ProductCatalogueService.java +++ b/src/main/java/org/opensrp/service/ProductCatalogueService.java @@ -3,6 +3,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.opensrp.util.Utils; import org.smartregister.domain.ProductCatalogue; import org.opensrp.repository.ProductCatalogueRepository; import org.opensrp.search.ProductCatalogueSearchBean; @@ -90,6 +91,7 @@ private void validateFields(ProductCatalogue productCatalogue) { } else if (productCatalogue.getAccountabilityPeriod() == null) { throw new IllegalArgumentException("Accountability period was not specified"); } else { + productCatalogue.setProductName(Utils.replaceConsecutiveSpaces(productCatalogue.getProductName())); logger.info("All validations on fields passed"); } } diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index af72005e7..7401a7e0f 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -9,6 +9,7 @@ import org.opensrp.domain.postgres.Structure; import org.opensrp.domain.postgres.StructureMetadataExample; import org.opensrp.repository.postgres.mapper.custom.CustomStructureMetadataMapper; +import org.opensrp.util.Utils; import org.smartregister.domain.Inventory; import org.smartregister.domain.ProductCatalogue; import org.smartregister.domain.PhysicalLocation; From c3abea5033c96f6996e88973746be21b6e0ef99a Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 6 Mar 2024 16:39:54 +0300 Subject: [PATCH 3/4] rm unused import --- .../repository/postgres/ProductCatalogueRepositoryImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java index baea85dd9..b229cba38 100644 --- a/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/ProductCatalogueRepositoryImpl.java @@ -13,7 +13,6 @@ import org.opensrp.repository.ProductCatalogueRepository; import org.opensrp.repository.postgres.mapper.custom.CustomProductCatalogueMapper; import org.opensrp.search.ProductCatalogueSearchBean; -import org.opensrp.util.Utils; import org.smartregister.domain.ProductCatalogue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; From 5b7da62262c68c04aa5cd63e88367b41d6d6ba2f Mon Sep 17 00:00:00 2001 From: hilary egesa Date: Mon, 29 Jul 2024 10:56:05 +0300 Subject: [PATCH 4/4] update product name spacing when convertingToFhir --- .../repository/postgres/StocksRepositoryImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensrp/repository/postgres/StocksRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/StocksRepositoryImpl.java index 795d1b15e..29b9ed3ae 100755 --- a/src/main/java/org/opensrp/repository/postgres/StocksRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/StocksRepositoryImpl.java @@ -20,6 +20,7 @@ import org.opensrp.repository.postgres.mapper.custom.CustomStockMetadataMapper; import org.opensrp.search.StockSearchBean; import org.opensrp.util.RepositoryUtil; +import org.opensrp.util.Utils; import org.smartregister.converters.StockConverter; import org.smartregister.domain.PhysicalLocation; import org.smartregister.domain.ProductCatalogue; @@ -453,8 +454,12 @@ public List getInventoryWithProductDetailsByStockId(Stri } private List convertToFHIR(List stockAndProductDetails) { - return stockAndProductDetails.stream().map(stockAndProductDetail -> StockConverter.convertStockToBundleResource(stockAndProductDetail)) - .collect(Collectors.toList()); + return stockAndProductDetails.stream().map(stockAndProductDetail -> { + String productNameWithProperWhiteSpacing = Utils.replaceConsecutiveSpaces(stockAndProductDetail.getProductCatalogue().getProductName()); + stockAndProductDetail.getProductCatalogue().setProductName(productNameWithProperWhiteSpacing); + return StockConverter.convertStockToBundleResource(stockAndProductDetail); + }) + .collect(Collectors.toList()); } }