DateRangePredicateEvaluator.java
6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* 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;
}
}