PropertyComparator.java
3.45 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
/*
* 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.nodetype.PropertyDefinition
* javax.jcr.query.Row
*/
package com.day.cq.search.impl.builder;
import com.day.cq.search.eval.EvaluationContext;
import java.util.Calendar;
import java.util.Comparator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.nodetype.PropertyDefinition;
import javax.jcr.query.Row;
public class PropertyComparator
implements Comparator<Row> {
private String property;
private EvaluationContext context;
public PropertyComparator(String property, EvaluationContext context) {
this.property = property;
this.context = context;
}
@Override
public int compare(Row r1, Row r2) {
Node node1 = this.context.getNode(r1);
if (node1 == null) {
return 0;
}
Node node2 = this.context.getNode(r2);
if (node2 == null) {
return 0;
}
try {
if (!node1.hasProperty(this.property)) {
return -1;
}
if (!node2.hasProperty(this.property)) {
return 1;
}
Property prop1 = node1.getProperty(this.property);
boolean multiValued = prop1.getDefinition().isMultiple();
Property prop2 = node2.getProperty(this.property);
if (prop2.getDefinition().isMultiple() != multiValued) {
return multiValued ? 1 : -1;
}
if (multiValued) {
Value[] values2;
Value[] values1 = prop1.getValues();
if (values1.length < (values2 = prop2.getValues()).length) {
return -1;
}
if (values1.length > values2.length) {
return 1;
}
for (int i = 0; i < values1.length; ++i) {
int c = this.compareValues(values1[i], values2[i]);
if (c == 0) continue;
return c;
}
return 0;
}
return this.compareValues(prop1.getValue(), prop2.getValue());
}
catch (RepositoryException e) {
return 0;
}
}
public int compareValues(Value val1, Value val2) throws ValueFormatException, IllegalStateException, RepositoryException {
if (val1.getType() != val2.getType()) {
try {
return val1.getString().compareTo(val2.getString());
}
catch (RepositoryException e) {
return val2.getType() - val1.getType();
}
}
switch (val1.getType()) {
case 2: {
return 0;
}
case 3: {
return Long.valueOf(val1.getLong()).compareTo(val2.getLong());
}
case 4: {
return Double.valueOf(val1.getDouble()).compareTo(val2.getDouble());
}
case 6: {
return Boolean.valueOf(val1.getBoolean()).compareTo(val2.getBoolean());
}
case 5: {
return val1.getDate().compareTo(val2.getDate());
}
}
return val1.getString().compareTo(val2.getString());
}
}