JcrBoolPropertyPredicateEvaluator.java
3.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.ValueFormatException
* javax.jcr.query.Row
* org.apache.felix.scr.annotations.Component
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
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.JcrPropertyPredicateEvaluator;
import com.day.cq.search.eval.XPath;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.ValueFormatException;
import javax.jcr.query.Row;
import org.apache.felix.scr.annotations.Component;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, factory="com.day.cq.search.eval.PredicateEvaluator/boolproperty")
public class JcrBoolPropertyPredicateEvaluator
extends JcrPropertyPredicateEvaluator {
private static final Logger log = LoggerFactory.getLogger(JcrBoolPropertyPredicateEvaluator.class);
public static final String BOOLPROPERTY = "boolproperty";
@Override
public String getXPathExpression(Predicate p, EvaluationContext context) {
if (!p.hasNonEmptyValue("boolproperty") || !p.hasNonEmptyValue("value")) {
return null;
}
String property = p.get("boolproperty");
String value = p.get("value");
if ("false".equals(value)) {
return "(" + XPath.getEqualsExpression(property, value) + " or " + XPath.getNotExpression(property) + ")";
}
if ("true".equals(value)) {
return XPath.getEqualsExpression(property, value);
}
return null;
}
@Override
public boolean includes(Predicate p, Row row, EvaluationContext context) {
if (!p.hasNonEmptyValue("boolproperty") || !p.hasNonEmptyValue("value")) {
return true;
}
String property = p.get("boolproperty");
String value = p.get("value");
Node node = context.getNode(row);
String path = context.getPath(row);
try {
String childNode = Text.getRelativeParent((String)property, (int)1);
String propName = Text.getName((String)property);
if (childNode.length() > 0) {
if (node.hasNode(childNode)) {
node = node.getNode(childNode);
} else {
return false;
}
}
if (node.hasProperty(propName)) {
String propValue = node.getProperty(propName).getString();
if (propValue == null) {
if ("false".equals(value)) {
return true;
}
return false;
}
return propValue.equals(value);
}
if ("false".equals(value)) {
return true;
}
return false;
}
catch (ValueFormatException e) {
log.warn("Could not evaluate property = '" + property + "', value = '" + value + "', node = '" + path + "'", (Throwable)e);
}
catch (RepositoryException e) {
log.error("Could not evaluate property = '" + property + "', value = '" + value + "', node = '" + path + "'", (Throwable)e);
throw new RuntimeException("", (Throwable)e);
}
return true;
}
}