diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrdersTasklet.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrdersTasklet.java index b729a9df6..f78df6a0b 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrdersTasklet.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrdersTasklet.java @@ -1,7 +1,7 @@ package org.folio.dew.batch.acquisitions.edifact.jobs; import static org.folio.dew.utils.QueryUtils.combineCqlExpressions; -import static org.folio.dew.utils.QueryUtils.convertFieldListToCqlQuery; +import static org.folio.dew.utils.QueryUtils.convertFieldListToEnclosedCqlQuery; import static org.folio.dew.utils.QueryUtils.getCqlExpressionForFieldNullValue; import static org.folio.dew.utils.QueryUtils.negateQuery; @@ -64,7 +64,7 @@ protected String getPoLineQuery(VendorEdiOrdersExportConfig ediConfig) { // Order line filters "automaticExport==true", // line with automatic export getCqlExpressionForFieldNullValue("lastEDIExportDate"), // has not been exported yet - convertFieldListToCqlQuery(acqMethods, "acquisitionMethod", true), // acquisitionMethod in default list + convertFieldListToEnclosedCqlQuery(acqMethods, "acquisitionMethod", true), // acquisitionMethod in default list getVendorAccountFilter(ediConfig) // vendor account no filter ); log.info("getPoLineQuery:: Fetching purchase order lines with query: {}", resultQuery); @@ -76,10 +76,10 @@ private String getVendorAccountFilter(VendorEdiOrdersExportConfig ediConfig) { var configQuery = "configName==%s_%s*".formatted(ExportType.EDIFACT_ORDERS_EXPORT, ediConfig.getVendorId()); var configs = dataExportSpringClient.getExportConfigs(configQuery); return configs.getTotalRecords() > 1 - ? negateQuery(convertFieldListToCqlQuery(extractAllAccountNoLists(configs), "vendorDetail.vendorAccount", true)) - : null; + ? negateQuery(convertFieldListToEnclosedCqlQuery(extractAllAccountNoLists(configs), "vendorDetail.vendorAccount", true)) + : ""; } - return convertFieldListToCqlQuery(ediConfig.getEdiConfig().getAccountNoList(), "vendorDetail.vendorAccount", true); + return convertFieldListToEnclosedCqlQuery(ediConfig.getEdiConfig().getAccountNoList(), "vendorDetail.vendorAccount", true); } private Set extractAllAccountNoLists(ExportConfigCollection configs) { diff --git a/src/main/java/org/folio/dew/utils/QueryUtils.java b/src/main/java/org/folio/dew/utils/QueryUtils.java index e4bc9788a..f96101c93 100644 --- a/src/main/java/org/folio/dew/utils/QueryUtils.java +++ b/src/main/java/org/folio/dew/utils/QueryUtils.java @@ -63,7 +63,7 @@ public static String combineCqlExpressions(String operator, String... expression * @return A CQL query string representing the IDs. */ public static String convertIdsToCqlQuery(Collection ids, String idField) { - return convertFieldListToCqlQuery(ids, idField, true); + return convertFieldListToCqlQuery(ids, idField, true, false); } /** @@ -73,7 +73,19 @@ public static String convertIdsToCqlQuery(Collection ids, String idField * @return A CQL query string representing the IDs. */ public static String convertIdsToCqlQuery(Collection ids) { - return convertFieldListToCqlQuery(ids, ID, true); + return convertFieldListToCqlQuery(ids, ID, true, false); + } + + /** + * Transform list of values for some property to CQL query using 'or' operation and enclosing values with quotes + * + * @param values list of field values + * @param fieldName the property name to search by + * @param strictMatch indicates whether strict match mode (i.e. ==) should be used or not (i.e. =) + * @return String representing CQL query to get records by some property enclosed values + */ + public static String convertFieldListToEnclosedCqlQuery(Collection values, String fieldName, boolean strictMatch) { + return convertFieldListToCqlQuery(values, fieldName, strictMatch, true); } /** @@ -82,13 +94,15 @@ public static String convertIdsToCqlQuery(Collection ids) { * @param values list of field values * @param fieldName the property name to search by * @param strictMatch indicates whether strict match mode (i.e. ==) should be used or not (i.e. =) + * @param enclosed indicates whether values should be enclosed with quotes (i.e. asd) or not (i.e. "asd") * @return String representing CQL query to get records by some property values */ - public static String convertFieldListToCqlQuery(Collection values, String fieldName, boolean strictMatch) { + public static String convertFieldListToCqlQuery(Collection values, String fieldName, boolean strictMatch, boolean enclosed) { var prefix = String.format(strictMatch ? CQL_MATCH_STRICT : CQL_MATCH, fieldName, CQL_PREFIX); + var enclose = enclosed ? "\"%s\"" : "%s"; return StreamEx.of(values) .map(Object::toString) - .map("\"%s\""::formatted) + .map(enclose::formatted) .joining(" or ", prefix, CQL_SUFFIX); } diff --git a/src/test/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrderTaskletTest.java b/src/test/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrderTaskletTest.java index 052de0668..cbd9b14ba 100644 --- a/src/test/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrderTaskletTest.java +++ b/src/test/java/org/folio/dew/batch/acquisitions/edifact/jobs/MapToEdifactOrderTaskletTest.java @@ -52,13 +52,13 @@ protected void setUp() { @Test void testEdifactOrdersExport() throws Exception { JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob); - String cqlString = "purchaseOrder.workflowStatus==Open" + - " AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" + + String cqlString = "(purchaseOrder.workflowStatus==Open)" + + " AND (purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1)" + " AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" + - " AND automaticExport==true" + + " AND (automaticExport==true)" + " AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")" + - " AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")" + - " AND vendorDetail.vendorAccount==(\"BRXXXXX-01\")"; + " AND (acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\"))" + + " AND (vendorDetail.vendorAccount==(\"BRXXXXX-01\"))"; doReturn(poLines).when(ordersService).getPoLinesByQuery(cqlString); doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList()); doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString()); @@ -73,12 +73,12 @@ void testEdifactOrdersExport() throws Exception { @Test void testEdifactOrdersExportDefaultConfig() throws Exception { JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob); - String cqlString = "purchaseOrder.workflowStatus==Open" + - " AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" + + String cqlString = "(purchaseOrder.workflowStatus==Open)" + + " AND (purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1)" + " AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" + - " AND automaticExport==true" + + " AND (automaticExport==true)" + " AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")" + - " AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")"; + " AND (acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\"))"; String configSql = "configName==EDIFACT_ORDERS_EXPORT_d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1*"; ExportConfigCollection exportConfigCollection = new ExportConfigCollection(); exportConfigCollection.setTotalRecords(1); @@ -99,13 +99,13 @@ void testEdifactOrdersExportDefaultConfig() throws Exception { @Test void testEdifactOrdersExportDefaultConfigWithTwoExportConfigs() throws Exception { JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob); - String cqlString = "purchaseOrder.workflowStatus==Open" + - " AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" + + String cqlString = "(purchaseOrder.workflowStatus==Open)" + + " AND (purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1)" + " AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" + - " AND automaticExport==true" + + " AND (automaticExport==true)" + " AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")" + - " AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")" + - " AND cql.allRecords=1 NOT vendorDetail.vendorAccount==(\"org1\" OR \"org2\")"; + " AND (acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\"))" + + " AND (cql.allRecords=1 NOT vendorDetail.vendorAccount==(\"org1\" or \"org2\"))"; String configSql = "configName==EDIFACT_ORDERS_EXPORT_d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1*"; ExportConfigCollection exportConfigCollection = objectMapper.readValue(getMockData(DATA_EXPORT_CONFIGS_PATH), ExportConfigCollection.class); poLines.get(0).getVendorDetail().setVendorAccount(null);