Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitize whitespaces from productName #636

Open
wants to merge 6 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading