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

fix(Search): Fixed lookup by externalID when using wildcard searching #312

Merged
merged 1 commit into from
Apr 22, 2020
Merged
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 src/main/java/com/bullhorn/dataloader/rest/RestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public <T extends SearchEntity> List<T> searchForList(Class<T> type,
printUtil.log(Level.DEBUG, "Find(" + type.getSimpleName() + " Search): " + query
+ ", fields: " + correctedFieldSet.stream().sorted().collect(Collectors.toList()));
boolean isSupportedEntity = type != JobOrder.class && type != Lead.class && type != Opportunity.class;
String externalId = FindUtil.getExternalIdValue(query);
String externalId = FindUtil.getExternalIdSearchValue(query);
if (isSupportedEntity && !externalId.isEmpty()) {
SearchResult<T> searchResult = restApiExtension.getByExternalId(this, type, externalId, correctedFieldSet);
if (searchResult.getSuccess()) {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/bullhorn/dataloader/util/FindUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FindUtil {
private static final Integer MAX_NUM_FIELDS = 30; // A safe, low number, since actual number depends on total length of the query string

// Returns the format of a single term in a lucene search
@SuppressWarnings("rawtypes")
private static String getLuceneSearch(String field, String value, Class fieldType, EntityInfo fieldEntityInfo,
PropertyFileUtil propertyFileUtil) {
// Fix for the Note entity doing it's own thing when it comes to the 'id' field
Expand Down Expand Up @@ -82,11 +83,12 @@ private static String getLuceneSearch(Field field, PropertyFileUtil propertyFile
/**
* Returns the format of a single term in a query where clause
*/
@SuppressWarnings("rawtypes")
private static String getSqlQuery(String field, String value, Class fieldType, PropertyFileUtil propertyFileUtil) {
if (Integer.class.equals(fieldType) || BigDecimal.class.equals(fieldType) || Double.class.equals(fieldType)) {
return field + "=" + value;
} else if (Boolean.class.equals(fieldType)) {
return field + "=" + (value.equals("1") ? "true" : Boolean.toString(Boolean.valueOf(value)));
return field + "=" + (value.equals("1") ? "true" : Boolean.valueOf(value));
} else if (String.class.equals(fieldType)) {
if (propertyFileUtil.getWildcardMatching()) {
// Not all string fields in query entities support the like syntax. Only use it if there is a non-escaped asterisk.
Expand Down Expand Up @@ -189,17 +191,16 @@ public static String getMultipleRecordsExistMessage(EntityInfo entityInfo, List<
/**
* Convenience method that extracts the externalID from the search string if it exists.
*
* @param searchString the search string that has been created for use in the rest find call
* @return empty string if it does not exist or is blank
* @param searchString the lucene search string that has been created for use in the rest find call
* @return empty string if it does not exist, or there are extra search criteria involved
*/
public static String getExternalIdValue(String searchString) {
final String externalIdStart = StringConsts.EXTERNAL_ID + ":\"";
final String externalIdEnd = "\"";
public static String getExternalIdSearchValue(String searchString) {
final String externalIdStart = StringConsts.EXTERNAL_ID + ":";

String externalId = "";
if (searchString.contains(externalIdStart)) {
externalId = searchString.substring(searchString.indexOf(externalIdStart) + externalIdStart.length());
externalId = externalId.substring(0, externalId.indexOf(externalIdEnd));
if (searchString.contains(externalIdStart) && !searchString.contains(" AND ")) {
final String remaining = searchString.substring(searchString.indexOf(externalIdStart) + externalIdStart.length());
externalId = remaining.replaceAll("\"", "").trim();
}
return externalId;
}
Expand Down
43 changes: 39 additions & 4 deletions src/test/java/com/bullhorn/dataloader/util/FindUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bullhorn.dataloader.util;

import com.google.common.collect.Sets;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -15,19 +16,53 @@ public void testConstructor() {
}

@Test
public void testGetExternalIdValueSuccess() {
String externalIdValue = FindUtil.getExternalIdValue(
public void testGetExternalIdSearchValueQuotedSingleSuccess() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"externalID:\"ext 1\"");
Assert.assertEquals(externalIdValue, "ext 1");
}

@Test
public void testGetExternalIdSearchValueQuotedMultipleFailure() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"firstName:\"Data\" AND externalID:\"ext 1\" AND lastName:\"Loader\"");
Assert.assertEquals(externalIdValue, "");
}

@Test
public void testGetExternalIdSearchValueUnquotedSingleSuccess() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"externalID: ext-1");
Assert.assertEquals(externalIdValue, "ext-1");
}

@Test
public void testGetExternalIdSearchValueUnquotedSingleSpaceSuccess() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"externalID: ext 1");
Assert.assertEquals(externalIdValue, "ext 1");
}

@Test
public void testGetExternalIdValueMissing() {
String externalIdValue = FindUtil.getExternalIdValue(
public void testGetExternalIdSearchValueUnquotedMultipleFailure() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"firstName: Data AND externalID: ext 1 AND lastName: Loader");
Assert.assertEquals(externalIdValue, "");
}

@Test
public void testGetExternalIdSearchValueMissing() {
String externalIdValue = FindUtil.getExternalIdSearchValue(
"firstName:\"Data\" AND lastName:\"Loader\"");
Assert.assertEquals("", externalIdValue);
}

@Test
public void testGetExternalIdSearchValueEmpty() {
String externalIdValue = FindUtil.getExternalIdSearchValue("");
Assert.assertEquals("", externalIdValue);
}

@Test
public void testGetCorrectedFieldSet() {
Set<String> correctedFieldSet = FindUtil.getCorrectedFieldSet(Sets.newHashSet("id", "firstName", "lastName"));
Expand Down