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

[MOB-9573] refactoring evaluateFieldLogic function #798

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,8 @@ private boolean evaluateEvent(JSONArray searchQueries, JSONObject eventData, Str
}

private boolean evaluateFieldLogic(JSONArray searchQueries, JSONObject eventData) throws JSONException {
boolean itemMatchResult = false;
String itemKey = null;
if (eventData.has(IterableConstants.KEY_ITEMS)) {
itemKey = IterableConstants.KEY_ITEMS;
} else if (eventData.has(IterableConstants.PURCHASE_ITEM)) {
itemKey = IterableConstants.PURCHASE_ITEM;
}

if (itemKey != null) {
boolean result = false;
JSONArray items = new JSONArray(eventData.getString(itemKey));
for (int j = 0; j < items.length(); j++) {
JSONObject item = items.getJSONObject(j);
if (doesItemMatchQueries(searchQueries, item)) {
result = true;
break;
}
}
if (!result && doesItemCriteriaExists(searchQueries)) {
return false;
}
itemMatchResult = result;
}
//evaluate item logic
boolean itemMatchResult = evaluateItemLogic(searchQueries, eventData);

ArrayList<String> filteredDataKeys = new ArrayList<>();
Iterator<String> localEventDataKeys = eventData.keys();
Expand Down Expand Up @@ -399,6 +378,36 @@ private boolean evaluateFieldLogic(JSONArray searchQueries, JSONObject eventData
return matchResult;
}

boolean evaluateItemLogic(JSONArray searchQueries, JSONObject eventData) throws JSONException {
String itemKey = getItemKey(eventData);

// if no items exist in event data, return false
if (itemKey == null) {
return false;
}

// check if each item matches search queries
JSONArray items = new JSONArray(eventData.getString(itemKey));
for (int j = 0; j < items.length(); j++) {
JSONObject item = items.getJSONObject(j);
if (doesItemMatchQueries(searchQueries, item)) {
return true;
}
}

//return true if item criteria does not exist
return !doesItemCriteriaExists(searchQueries);
}

private String getItemKey(JSONObject eventData) {
if (eventData.has(IterableConstants.KEY_ITEMS)) {
return IterableConstants.KEY_ITEMS;
} else if (eventData.has(IterableConstants.PURCHASE_ITEM)) {
return IterableConstants.PURCHASE_ITEM;
}
return null;
}

private Object getFieldValue(JSONObject data, String field) {
String[] fields = field.split("\\.");
try {
Expand Down
Loading