com.moengage.attribute_inspector.UserAttributeInspector Maven / Gradle / Ivy
package com.moengage.attribute_inspector;
import com.moengage.enum_models.FilterParameter;
import com.moengage.enum_models.FilterType;
import org.json.JSONArray;
import org.json.JSONObject;
public class UserAttributeInspector {
private JSONObject filterCondition;
/**
* UserAttributeInspector will check whether the filters contain only user attribute queries.
* @param filterCondition This is the front end filters. Sample structure is given below:
* condition = {'included_filters': {
* filter_operator: "and"
* filters: [
* {
* filter_type: 'user_attributes',
* name: "u_mb",
* operator: "is",
* value: "9898989989",
* data_type: "string",
* "negate": true,
* "case_sensitive": false
* },
* { filter_type: 'user_attributes',
* "operator": "lessThan",
* "data_type": "datetime",
* "name": "u_l_a",
* "value": "2015-07-30T00:00:00"
* },
* ]
* }
* }
**/
public UserAttributeInspector(JSONObject filterCondition) {
this.filterCondition = filterCondition;
}
public boolean isOnlyUserAttribute() throws RequiredKeyMissingException{
JSONObject included_filters = filterCondition.optJSONObject(FilterParameter.INCLUDED_FILTERS);
JSONObject excluded_filters = filterCondition.optJSONObject(FilterParameter.EXCLUDED_FILTERS);
return isOnlyUserAttributesFilters(included_filters, FilterParameter.INCLUDED_FILTERS) &&
isOnlyUserAttributesFilters(excluded_filters, FilterParameter.EXCLUDED_FILTERS);
}
private boolean isOnlyUserAttributesFilters(JSONObject filters, String type) throws RequiredKeyMissingException{
if(filters == null) return type.equals(FilterParameter.EXCLUDED_FILTERS);
Object attributeFilters = filters.opt(FilterParameter.FILTERS);
if (!(attributeFilters instanceof JSONArray)) return false;
JSONArray attributeFilterList = (JSONArray) attributeFilters;
for (int i = 0; i < attributeFilterList.length(); i++) {
JSONObject condition = attributeFilterList.getJSONObject(i);
String filter_type = condition.optString(FilterParameter.FILTER_TYPE);
if(filter_type == null){
throw new RequiredKeyMissingException("Filter type is required");
}
// All user filter is skipped
if (filter_type.equals(FilterType.CUSTOM_SEGMENTS) &&
condition.optString(FilterParameter.ID).equals("moe_all_users")) continue;
if (!filter_type.equals(FilterType.USER_ATTRIBUTES)) return false;
}
return true;
}
}