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

com.moengage.operator.DatetimeOperation Maven / Gradle / Ivy

There is a newer version: 1.3.7
Show newest version
package com.moengage.operator;

import com.moengage.datatype.MOEDataType;
import com.moengage.datatype.MOEDatetime;
import com.moengage.enum_models.Operator;
import com.moengage.enum_models.ValueType;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@SuppressWarnings("unchecked")
public class DatetimeOperation & MOEDataType> extends DoubleOperation {

    private TimeZone appTimeZone;

    public DatetimeOperation(TimeZone appTimeZone) {
        super();
        this.appTimeZone = appTimeZone;
    }

    public boolean inTheLast(T filterObject, T conditionObject) {
        Object timeValue = getISOStringFromDate(new Date());
        // For in the last operation, the value should be compared from now.
        // Now value is passed as a third argument here for between.
        MOEDatetime upperLimitObject = new MOEDatetime(timeValue, ValueType.ABSOLUTE, Operator.IN_THE_LAST, appTimeZone);
        return between(filterObject, conditionObject, upperLimitObject);
    }

    public boolean inTheNext(T filterObject, T conditionObject) {
        // For in the next operation, the value should be compared from now.
        // Now value is passed as a second argument here for between.
        Object timeValue = getISOStringFromDate(new Date());
        MOEDatetime lowerLimitObject = new MOEDatetime(timeValue, ValueType.ABSOLUTE, Operator.IN_THE_NEXT, appTimeZone);
        return between(filterObject, lowerLimitObject, conditionObject);
    }

    public boolean on(T filterObject, T conditionObject) {
        // For on operation, the value should be a full day.
        // This value is passed as a third argument here for between.
        Object timeValue = getTimeObjectForOn(conditionObject);
        MOEDatetime upperLimitObject = new MOEDatetime(timeValue, ValueType.ABSOLUTE, Operator.ON, appTimeZone);
        return between(filterObject, conditionObject, upperLimitObject);
    }

    public boolean after(T filterObject, T conditionObject) {
        return greaterThan(filterObject, conditionObject);
    }


    public boolean before(T filterObject, T conditionObject) {
        return lessThan(filterObject, conditionObject);
    }

    private Object getTimeObjectForOn(T dataTypeObj) {
        Double timeInMillis = dataTypeObj.getValue();
        long endOfDay = (long) (timeInMillis + 24 * 60 * 60 * 1000);
        Date newDate = new Date(endOfDay);
        return getISOStringFromDate(newDate);
    }

    private Object getISOStringFromDate(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        if (appTimeZone != null)
            dateFormat.setTimeZone(appTimeZone);
        return dateFormat.format(date);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy