RangePropertyPredicateEvaluator.java
11.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.jcr.ValueFormatException
* javax.jcr.query.Row
* org.apache.felix.scr.annotations.Component
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.search.eval;
import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;
import com.day.cq.search.eval.XPath;
import java.math.BigDecimal;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.query.Row;
import org.apache.felix.scr.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, factory="com.day.cq.search.eval.PredicateEvaluator/rangeproperty")
public class RangePropertyPredicateEvaluator
extends AbstractPredicateEvaluator {
private static final Logger log = LoggerFactory.getLogger(RangePropertyPredicateEvaluator.class);
public static final String PROPERTY = "property";
public static final String LOWER_BOUND = "lowerBound";
public static final String LOWER_OPERATION = "lowerOperation";
public static final String UPPER_BOUND = "upperBound";
public static final String UPPER_OPERATION = "upperOperation";
public static final String PROPERTY_DECIMAL = "decimal";
@Override
public String getXPathExpression(Predicate p, EvaluationContext context) {
if (p.getBool("decimal")) {
return null;
}
return this.getXPathExpression(p.get("property"), p.get("lowerBound"), p.get("lowerOperation"), p.get("upperBound"), p.get("upperOperation"));
}
protected String getXPathExpression(String property, String lowerValue, String lowerOperation, String upperValue, String upperOperation) {
String op;
StringBuffer buffer = new StringBuffer();
if (lowerValue != null) {
op = lowerOperation;
if (!">".equals(op) && !">=".equals(op)) {
op = ">";
}
buffer.append(XPath.getPropertyPath(property)).append(" ").append(op).append(" ").append(lowerValue);
}
if (upperValue != null) {
if (buffer.length() > 1) {
buffer.append(" and ");
}
if (!"<".equals(op = upperOperation) && !"<=".equals(op)) {
op = "<";
}
buffer.append(XPath.getPropertyPath(property)).append(" ").append(op).append(" ").append(upperValue);
}
if (buffer.length() > 0) {
return "(" + buffer.toString() + ")";
}
return null;
}
@Override
public String[] getOrderByProperties(Predicate p, EvaluationContext context) {
return new String[]{p.get("property")};
}
@Override
public boolean canXpath(Predicate p, EvaluationContext context) {
return !p.getBool("decimal");
}
@Override
public boolean canFilter(Predicate p, EvaluationContext context) {
return true;
}
@Override
public boolean includes(Predicate p, Row row, EvaluationContext context) {
return this.includes(context.getNode(row), p.get("property"), p.get("lowerBound"), p.get("lowerOperation"), p.get("upperBound"), p.get("upperOperation"), p.getBool("decimal"));
}
protected boolean includes(Node node, String property, String lowerValue, String lowerOperation, String upperValue, String upperOperation, boolean decimal) {
block56 : {
if (property == null || property.length() == 0 || (lowerValue == null || lowerValue.length() == 0) && (upperValue == null || upperValue.length() == 0)) {
return true;
}
String path = null;
try {
boolean match;
path = node.getPath();
if (!node.hasProperty(property)) {
return false;
}
Property nodeProperty = node.getProperty(property);
if (decimal) {
boolean match2;
if (nodeProperty.getType() != 12) {
return false;
}
if (lowerValue != null) {
BigDecimal bdLowerValue = new BigDecimal(lowerValue);
if (nodeProperty.isMultiple()) {
match2 = false;
for (Value value : nodeProperty.getValues()) {
if ((!">=".equals(lowerOperation) || value.getDecimal().compareTo(bdLowerValue) < 0) && (">=".equals(lowerOperation) || value.getDecimal().compareTo(bdLowerValue) <= 0)) continue;
match2 = true;
break;
}
if (!match2) {
return false;
}
} else if (">=".equals(lowerOperation) && nodeProperty.getDecimal().compareTo(bdLowerValue) < 0 || !">=".equals(lowerOperation) && nodeProperty.getDecimal().compareTo(bdLowerValue) <= 0) {
return false;
}
}
if (upperValue != null) {
BigDecimal bdUpperValue = new BigDecimal(upperValue);
if (nodeProperty.isMultiple()) {
match2 = false;
for (Value value : nodeProperty.getValues()) {
if ((!"<=".equals(upperOperation) || value.getDecimal().compareTo(bdUpperValue) > 0) && ("<=".equals(upperOperation) || value.getDecimal().compareTo(bdUpperValue) >= 0)) continue;
match2 = true;
break;
}
if (!match2) {
return false;
}
} else if ("<=".equals(upperOperation) && nodeProperty.getDecimal().compareTo(bdUpperValue) > 0 || !"<=".equals(upperOperation) && nodeProperty.getDecimal().compareTo(bdUpperValue) >= 0) {
return false;
}
}
break block56;
}
boolean longValue = false;
boolean doubleValue = false;
long lLowerValue = 0;
long lUpperValue = 0;
double dLowerValue = 0.0;
double dUpperValue = 0.0;
try {
if (lowerValue != null) {
lLowerValue = Long.parseLong(lowerValue);
}
if (upperValue != null) {
lUpperValue = Long.parseLong(upperValue);
}
longValue = true;
}
catch (NumberFormatException e) {
try {
if (lowerValue != null) {
dLowerValue = Double.parseDouble(lowerValue);
}
if (upperValue != null) {
dUpperValue = Double.parseDouble(upperValue);
}
doubleValue = true;
}
catch (NumberFormatException e2) {
log.warn("invalid values for rangeproperty includes <{},{}>", (Object)lowerValue, (Object)upperValue);
return true;
}
}
if (longValue) {
if (nodeProperty.getType() != 3) {
return false;
}
if (lowerValue != null) {
if (nodeProperty.isMultiple()) {
boolean match3 = false;
for (Value value : nodeProperty.getValues()) {
if ((!">=".equals(lowerOperation) || value.getLong() < lLowerValue) && (">=".equals(lowerOperation) || value.getLong() <= lLowerValue)) continue;
match3 = true;
break;
}
if (!match3) {
return false;
}
} else if (">=".equals(upperOperation) && nodeProperty.getLong() < lUpperValue || !">".equals(upperOperation) && nodeProperty.getLong() <= lUpperValue) {
return false;
}
}
if (upperValue != null) {
if (nodeProperty.isMultiple()) {
match = false;
for (Value value : nodeProperty.getValues()) {
if ((!"<=".equals(upperOperation) || value.getLong() > lUpperValue) && ("<=".equals(upperOperation) || value.getLong() >= lUpperValue)) continue;
match = true;
break;
}
if (!match) {
return false;
}
} else if ("<=".equals(upperOperation) && nodeProperty.getLong() > lUpperValue || !"<=".equals(upperOperation) && nodeProperty.getLong() >= lUpperValue) {
return false;
}
}
} else if (doubleValue) {
if (nodeProperty.getType() != 4) {
return false;
}
if (lowerValue != null) {
if (nodeProperty.isMultiple()) {
boolean match4 = false;
for (Value value : nodeProperty.getValues()) {
if ((!">=".equals(lowerOperation) || value.getDouble() < dLowerValue) && (">=".equals(lowerOperation) || value.getDouble() <= dLowerValue)) continue;
match4 = true;
break;
}
if (!match4) {
return false;
}
} else if (">=".equals(lowerOperation) && nodeProperty.getDouble() < dLowerValue || !">=".equals(lowerOperation) && nodeProperty.getDouble() <= dLowerValue) {
return false;
}
}
if (upperValue != null) {
if (nodeProperty.isMultiple()) {
match = false;
for (Value value : nodeProperty.getValues()) {
if ((!"<=".equals(upperOperation) || value.getDouble() > dUpperValue) && ("<=".equals(upperOperation) || value.getDouble() >= dUpperValue)) continue;
match = true;
break;
}
if (!match) {
return false;
}
} else if ("<=".equals(upperOperation) && nodeProperty.getDouble() > dUpperValue || !"<=".equals(upperOperation) && nodeProperty.getDouble() >= dUpperValue) {
return false;
}
}
}
}
catch (ValueFormatException e) {
log.warn("Could not evaluate range: property = '" + property + "', node = '" + path + "'", (Throwable)e);
}
catch (RepositoryException e) {
throw new RuntimeException("Could not evaluate range: property = '" + property + "', node = '" + path + "'", (Throwable)e);
}
}
return true;
}
}