DateRangePredicateEvaluator.java 6.97 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 *  org.apache.felix.scr.annotations.Component
 */
package com.day.cq.search.eval;

import com.day.cq.search.Predicate;
import com.day.cq.search.eval.EvaluationContext;
import com.day.cq.search.eval.RangePropertyPredicateEvaluator;
import com.day.cq.search.facets.FacetExtractor;
import com.day.cq.search.facets.buckets.PredefinedBucket;
import com.day.cq.search.facets.buckets.ValueRangeBucket;
import com.day.cq.search.facets.extractors.PredefinedBucketsFacetExtractor;
import com.day.cq.search.impl.util.DateUtil;
import com.day.cq.search.impl.util.InvalidDateException;
import java.util.Calendar;
import java.util.TimeZone;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Component;

@Component(metatype=0, factory="com.day.cq.search.eval.PredicateEvaluator/daterange")
public class DateRangePredicateEvaluator
extends RangePropertyPredicateEvaluator {
    public static final String TIME_ZONE = "timeZone";
    public static final String TODAY = "Today";
    public static final String THIS_WEEK = "This Week";
    public static final String THIS_MONTH = "This Month";
    public static final String LAST_THREE_MONTHS = "Last three months";
    public static final String THIS_YEAR = "This Year";
    public static final String LAST_YEAR = "Last Year";
    public static final String EARLIER_THAN_LAST_YEAR = "Earlier than last year";

    @Override
    public String getXPathExpression(Predicate p, EvaluationContext context) {
        return super.getXPathExpression(p.get("property"), DateRangePredicateEvaluator.parseDateString(p.get("lowerBound"), p.get("timeZone"), context.getSession()), p.get("lowerOperation"), DateRangePredicateEvaluator.parseDateString(p.get("upperBound"), p.get("timeZone"), context.getSession()), p.get("upperOperation"));
    }

    @Override
    public boolean canFilter(Predicate p, EvaluationContext context) {
        return false;
    }

    @Override
    public boolean canXpath(Predicate p, EvaluationContext context) {
        return true;
    }

    public static String parseDateString(String dateString, String timeZoneID, Session session) {
        if (dateString == null || dateString.length() == 0) {
            return null;
        }
        TimeZone timeZone = null;
        if (timeZoneID != null && timeZoneID.length() > 0) {
            timeZone = TimeZone.getTimeZone(timeZoneID);
        }
        try {
            Calendar date;
            ValueFactory vf = session.getValueFactory();
            try {
                long msec = Long.parseLong(dateString);
                date = vf.createValue(msec).getDate();
            }
            catch (NumberFormatException e) {
                try {
                    date = DateUtil.parseISO8601(dateString, timeZone);
                }
                catch (InvalidDateException e1) {
                    return "";
                }
            }
            return "xs:dateTime('" + vf.createValue(date).getString() + "')";
        }
        catch (RepositoryException e) {
            return "";
        }
    }

    @Override
    public FacetExtractor getFacetExtractor(Predicate p, EvaluationContext context) {
        if (!p.hasNonEmptyValue("property")) {
            return null;
        }
        PredefinedBucketsFacetExtractor extractor = new PredefinedBucketsFacetExtractor(p.get("property"));
        DateUtil dates = new DateUtil();
        Calendar endOfToday = dates.getToday();
        endOfToday.add(10, 24);
        Predicate predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getToday()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(endOfToday));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("Today", Long.valueOf(dates.getToday().getTimeInMillis()), true, Long.valueOf(endOfToday.getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getWeekStart()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(endOfToday));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("This Week", Long.valueOf(dates.getWeekStart().getTimeInMillis()), true, Long.valueOf(endOfToday.getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getMonthStart()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(endOfToday));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("This Month", Long.valueOf(dates.getMonthStart().getTimeInMillis()), true, Long.valueOf(endOfToday.getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getThreeMonthsAgo()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(endOfToday));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("Last three months", Long.valueOf(dates.getThreeMonthsAgo().getTimeInMillis()), true, Long.valueOf(endOfToday.getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getYearStart()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(endOfToday));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("This Year", Long.valueOf(dates.getYearStart().getTimeInMillis()), true, Long.valueOf(endOfToday.getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", DateUtil.getISO8601DateNoTime(dates.getLastYearStart()));
        predicate.set("lowerOperation", ">=");
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(dates.getYearStart()));
        predicate.set("upperOperation", "<=");
        extractor.addPredefinedBucket(new ValueRangeBucket("Last Year", Long.valueOf(dates.getLastYearStart().getTimeInMillis()), true, Long.valueOf(dates.getYearStart().getTimeInMillis()), true, predicate));
        predicate = p.clone();
        predicate.set("lowerBound", null);
        predicate.set("upperBound", DateUtil.getISO8601DateNoTime(dates.getLastYearStart()));
        predicate.set("upperOperation", "<");
        extractor.addPredefinedBucket(new ValueRangeBucket("Earlier than last year", null, true, Long.valueOf(dates.getLastYearStart().getTimeInMillis()), false, predicate));
        return extractor;
    }
}