All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moengage.operator.date_extraction.BaseOperation Maven / Gradle / Ivy

package com.moengage.operator.date_extraction;

import com.moengage.datatype.MOEDatetime;
import com.moengage.enum_models.Operator;
import com.moengage.enum_models.ValueType;
import com.moengage.operator.DatetimeOperation;
import org.json.JSONArray;

import java.util.TimeZone;

public class BaseOperation extends DatetimeOperation {

    public BaseOperation(TimeZone appTimeZone) {
        super(appTimeZone);
    }

    public boolean apply(String operator, MOEDatetime filterObject, MOEDatetime conditionObject, MOEDatetime conditionObject2) {
        switch (operator) {
            // ON operator not present in the expected filter formats witch extract types. For ON, operator passed is "is" i.e. EQUAL_TO
            case Operator.EQUAL_TO:
                return on(filterObject, conditionObject);
            case Operator.TODAY:
                return today(filterObject, conditionObject);
            case Operator.IN:
                return inTheFollowing(filterObject, conditionObject);
            case Operator.BETWEEN:
                return between(filterObject, conditionObject, conditionObject2);
            case Operator.IN_THE_LAST:
                return inTheLast(filterObject, conditionObject);
            case Operator.IN_THE_NEXT:
                return inTheNext(filterObject, conditionObject);
            default:
                return false;
        }
    }

    public boolean on(MOEDatetime filterObject, MOEDatetime conditionObject) {
        return filterObject.getFilterValue().equals(conditionObject.getConditionValue());
    }

    public boolean today(MOEDatetime filterObject, MOEDatetime conditionObject) {
        conditionObject.value = 0;
        return on(filterObject, conditionObject);
    }

    public boolean inTheFollowing(MOEDatetime filterObject, MOEDatetime conditionObject) {
        JSONArray conditionValues = (JSONArray) conditionObject.getConditionValue();
        for (int i = 0; i < conditionValues.length(); i++) {
            if ((Integer) filterObject.getFilterValue() == conditionValues.getInt(i)) {
                return true;
            }
        }
        return false;
    }

    public boolean between(MOEDatetime filterObject, MOEDatetime conditionObject, MOEDatetime conditionObject2) {
        Object filterValue = filterObject.getFilterValue();
        Object conditionValue = conditionObject.getConditionValue();
        Object conditionValue2 = conditionObject2.getConditionValue();
        return between(filterValue, conditionValue, conditionValue2, conditionObject.valueType);
    }

    public boolean inTheLast(MOEDatetime filterObject, MOEDatetime conditionObject) {
        return between(filterObject.getFilterValue(), conditionObject.getCurrentDateTimeValue(), conditionObject.getConditionValue(), conditionObject.valueType);
    }

    public boolean inTheNext(MOEDatetime filterObject, MOEDatetime conditionObject) {
        return between(filterObject.getFilterValue(), conditionObject.getCurrentDateTimeValue(), conditionObject.getConditionValue(), conditionObject.valueType);
    }

    private boolean between(Object filterValueObj, Object conditionValue1Obj, Object conditionValue2Obj, String valueType) {
        int filterValue = (Integer) filterValueObj;
        int conditionValue1 = (Integer) conditionValue1Obj;
        int conditionValue2 = (Integer) conditionValue2Obj;
        if (ValueType.PAST.equals(valueType)) {
            int temp = conditionValue1;
            conditionValue1 = conditionValue2;
            conditionValue2 = temp;
        }
        if (conditionValue1 <= conditionValue2) {
            return conditionValue1 <= filterValue && filterValue <= conditionValue2;
        } else {
            return filterValue >= conditionValue1 || filterValue <= conditionValue2;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy