Skip to content

Commit

Permalink
Merge pull request #636 from opensrp/sanitize-spaces-from-productName
Browse files Browse the repository at this point in the history
sanitize whitespaces from productName
  • Loading branch information
hilpitome authored Oct 17, 2024
2 parents 178a3b7 + f60c10f commit 559f317
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.14.10-SNAPSHOT</version>
<version>2.14.11-ALPHA2-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -453,8 +454,12 @@ public List<StockAndProductDetails> getInventoryWithProductDetailsByStockId(Stri
}

private List<Bundle> convertToFHIR(List<StockAndProductDetails> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/opensrp/service/StockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/opensrp/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/opensrp/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 559f317

Please sign in to comment.