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

org.flips.condition.DateTimeFlipCondition Maven / Gradle / Ivy

Go to download

Flips Core framework, provides all the flip annotations, conditions and different advices

There is a newer version: 1.1
Show newest version
package org.flips.condition;

import org.flips.model.FeatureContext;
import org.flips.model.FlipAnnotationAttributes;
import org.flips.utils.DateTimeUtils;
import org.flips.utils.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;

@Component
public class DateTimeFlipCondition implements FlipCondition {

    private static final Logger logger = LoggerFactory.getLogger(DateTimeFlipCondition.class);

    @Override
    public boolean evaluateCondition(FeatureContext featureContext,
                                     FlipAnnotationAttributes flipAnnotationAttributes) {

        String dateTimeProperty = flipAnnotationAttributes.getAttributeValue("cutoffDateTimeProperty", "");
        ValidationUtils.requireNonEmpty(dateTimeProperty, "cutoffDateTimeProperty element can not be NULL or EMPTY when using @FlipOnDateTime");

        String dateTime         = featureContext.getPropertyValueOrDefault(dateTimeProperty, String.class, "");
        ValidationUtils.requireNonEmpty(dateTime, dateTimeProperty + " containing datetime can not be NULL or EMPTY when using @FlipOnDateTime");

        return isCurrentDateTimeAfterOrEqualCutoffDateTime(getCutoffDateTime(dateTime), DateTimeUtils.getCurrentTime());
    }

    private boolean isCurrentDateTimeAfterOrEqualCutoffDateTime(ZonedDateTime cutoffDateTime, ZonedDateTime currentUtcTime) {
        logger.info("DateTimeFlipCondition: cutoffDateTime {}, currentUtcTime {}", cutoffDateTime, currentUtcTime);
        return currentUtcTime.isEqual(cutoffDateTime) || currentUtcTime.isAfter(cutoffDateTime);
    }

    private ZonedDateTime getCutoffDateTime(String datetime){
        logger.info("DateTimeFlipCondition: parsing {}", datetime);
        try{
            return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC);
        }
        catch (DateTimeParseException e){
            logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ");
            throw e;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy