1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.search.lucene.query;
25
26 import java.io.IOException;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.search.Query;
30 import org.apache.lucene.search.Searcher;
31 import org.apache.lucene.search.Weight;
32 import org.modeshape.graph.property.Path;
33 import org.modeshape.graph.property.ValueComparators;
34 import org.modeshape.graph.property.ValueFactories;
35 import org.modeshape.graph.property.ValueFactory;
36 import org.modeshape.graph.query.model.Comparison;
37
38
39
40
41
42
43
44 public class ComparePathQuery extends CompareQuery<Path> {
45
46 private static final long serialVersionUID = 1L;
47 protected static final Evaluator<Path> PATH_IS_LESS_THAN = new Evaluator<Path>() {
48 private static final long serialVersionUID = 1L;
49
50 public boolean satisfiesConstraint( Path nodePath,
51 Path constraintPath ) {
52 return ValueComparators.PATH_COMPARATOR.compare(nodePath, constraintPath) < 0;
53 }
54
55 @Override
56 public String toString() {
57 return " < ";
58 }
59 };
60 protected static final Evaluator<Path> PATH_IS_LESS_THAN_OR_EQUAL_TO = new Evaluator<Path>() {
61 private static final long serialVersionUID = 1L;
62
63 public boolean satisfiesConstraint( Path nodePath,
64 Path constraintPath ) {
65 return ValueComparators.PATH_COMPARATOR.compare(nodePath, constraintPath) <= 0;
66 }
67
68 @Override
69 public String toString() {
70 return " <= ";
71 }
72 };
73 protected static final Evaluator<Path> PATH_IS_GREATER_THAN = new Evaluator<Path>() {
74 private static final long serialVersionUID = 1L;
75
76 public boolean satisfiesConstraint( Path nodePath,
77 Path constraintPath ) {
78 return ValueComparators.PATH_COMPARATOR.compare(nodePath, constraintPath) > 0;
79 }
80
81 @Override
82 public String toString() {
83 return " > ";
84 }
85 };
86 protected static final Evaluator<Path> PATH_IS_GREATER_THAN_OR_EQUAL_TO = new Evaluator<Path>() {
87 private static final long serialVersionUID = 1L;
88
89 public boolean satisfiesConstraint( Path nodePath,
90 Path constraintPath ) {
91 return ValueComparators.PATH_COMPARATOR.compare(nodePath, constraintPath) >= 0;
92 }
93
94 @Override
95 public String toString() {
96 return " >= ";
97 }
98 };
99
100
101
102
103
104
105
106
107
108
109
110
111 public static ComparePathQuery createQueryForNodesWithPathGreaterThan( Path constraintPath,
112 String fieldName,
113 ValueFactories factories,
114 boolean caseSensitive ) {
115 return new ComparePathQuery(fieldName, constraintPath, factories.getPathFactory(), factories.getStringFactory(),
116 PATH_IS_GREATER_THAN, caseSensitive);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 public static ComparePathQuery createQueryForNodesWithPathGreaterThanOrEqualTo( Path constraintPath,
131 String fieldName,
132 ValueFactories factories,
133 boolean caseSensitive ) {
134 return new ComparePathQuery(fieldName, constraintPath, factories.getPathFactory(), factories.getStringFactory(),
135 PATH_IS_GREATER_THAN_OR_EQUAL_TO, caseSensitive);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 public static ComparePathQuery createQueryForNodesWithPathLessThan( Path constraintPath,
150 String fieldName,
151 ValueFactories factories,
152 boolean caseSensitive ) {
153 return new ComparePathQuery(fieldName, constraintPath, factories.getPathFactory(), factories.getStringFactory(),
154 PATH_IS_LESS_THAN, caseSensitive);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 public static ComparePathQuery createQueryForNodesWithPathLessThanOrEqualTo( Path constraintPath,
169 String fieldName,
170 ValueFactories factories,
171 boolean caseSensitive ) {
172 return new ComparePathQuery(fieldName, constraintPath, factories.getPathFactory(), factories.getStringFactory(),
173 PATH_IS_LESS_THAN_OR_EQUAL_TO, caseSensitive);
174 }
175
176 private final boolean caseSensitive;
177
178
179
180
181
182
183
184
185
186
187
188
189
190 protected ComparePathQuery( String fieldName,
191 Path constraintPath,
192 ValueFactory<Path> pathFactory,
193 ValueFactory<String> stringFactory,
194 Evaluator<Path> evaluator,
195 boolean caseSensitive ) {
196 super(fieldName, constraintPath, pathFactory, stringFactory, evaluator);
197 this.caseSensitive = caseSensitive;
198 }
199
200
201
202
203
204
205 @Override
206 protected Path readFromDocument( IndexReader reader,
207 int docId ) throws IOException {
208 Document doc = reader.document(docId, fieldSelector);
209 String valueString = doc.get(fieldName);
210 if (!caseSensitive) valueString = valueString.toLowerCase();
211 return valueTypeFactory.create(valueString);
212 }
213
214
215
216
217
218
219 @Override
220 public Object clone() {
221 return new ComparePathQuery(fieldName, constraintValue, valueTypeFactory, stringFactory, evaluator, caseSensitive);
222 }
223 }