-
Notifications
You must be signed in to change notification settings - Fork 161
/
Filter.java
432 lines (350 loc) · 13.1 KB
/
Filter.java
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with
* separate copyright notices and license terms. Your use of the source
* code for these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package org.neo4j.ogm.cypher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import org.neo4j.ogm.cypher.function.DistanceComparison;
import org.neo4j.ogm.cypher.function.FilterFunction;
import org.neo4j.ogm.cypher.function.PropertyComparison;
import org.neo4j.ogm.exception.core.MappingException;
import org.neo4j.ogm.typeconversion.AttributeConverter;
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter;
/**
* A parameter along with filter information to be added to a query.
*
* @author Luanne Misquitta
* @author Jasper Blues
*/
public class Filter implements FilterWithRelationship {
/**
* Index is used to to ensure unique parameter names when a collection of filters are used.
*
* @see Filters
*/
private int index;
/**
* The property name on the entity to be used in the filter
*/
private String propertyName;
/**
* The comparison operator to use in the property filter
*/
private ComparisonOperator comparisonOperator;
/**
* The boolean operator used to append this filter to the previous ones.
* Mandatory if the filter is not the first and only filter in the list.
*/
private BooleanOperator booleanOperator = BooleanOperator.NONE;
/**
* Determines whether or not this filter condition should be negated when added to the query.
*/
private boolean negated;
/**
* The parent entity which owns this filter
*/
private Class<?> ownerEntityType;
/**
* The label of the entity which contains the nested property
*/
private String nestedEntityTypeLabel;
/**
* The property name of the nested property on the parent entity
*/
private String nestedPropertyName;
/**
* The type of the entity that owns the nested property
*/
private Class<?> nestedPropertyType;
/**
* The relationship type to be used for a nested property
*/
private String relationshipType;
/**
* The relationship direction from the parent entity to the nested property
*/
private String relationshipDirection;
private AttributeConverter propertyConverter;
private CompositeAttributeConverter compositeAttributeConverter;
/**
* Whether the nested property is backed by a relationship entity
*/
private boolean nestedRelationshipEntity;
private FilterFunction function;
private List<NestedPathSegment> nestedPath;
//Primary Constructor
public Filter(FilterFunction function) {
this.index = 0;
this.function = function;
this.function.setFilter(this);
}
public Filter(DistanceComparison distanceComparisonFunction, ComparisonOperator comparisonOperator) {
this.index = 0;
this.function = distanceComparisonFunction;
this.function.setFilter(this);
this.comparisonOperator = comparisonOperator;
}
//Convenience Constructor
public Filter(String propertyName, ComparisonOperator comparisonOperator, Object propertyValue) {
this(new PropertyComparison(propertyValue));
this.comparisonOperator = comparisonOperator;
this.propertyName = propertyName;
}
//Convenience Constructor
public Filter(String propertyName, FilterFunction filterFunction) {
this(filterFunction);
this.propertyName = propertyName;
}
// TODO: Split Operators up into binary and unary.
public Filter(String propertyName, ComparisonOperator comparisonOperator) {
this(new PropertyComparison(null));
this.propertyName = propertyName;
if (!EnumSet.of(ComparisonOperator.EXISTS, ComparisonOperator.IS_TRUE, ComparisonOperator.IS_NULL)
.contains(comparisonOperator)) {
throw new RuntimeException("This constructor can only be used with Unary comparison operators");
}
this.comparisonOperator = comparisonOperator;
}
public static void setNameFromProperty(Filter filter, String propertyName) {
filter.propertyName = propertyName;
}
public String getRelationshipDirection() {
return relationshipDirection;
}
public void setRelationshipDirection(String relationshipDirection) {
this.relationshipDirection = relationshipDirection;
}
public String getPropertyName() {
return propertyName;
}
@Deprecated
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public ComparisonOperator getComparisonOperator() {
return comparisonOperator;
}
public BooleanOperator getBooleanOperator() {
return booleanOperator;
}
public void setBooleanOperator(BooleanOperator booleanOperator) {
this.booleanOperator = booleanOperator;
}
/**
* Convenience method to chain filters using {@link BooleanOperator#AND}.
*
* @param filter to be chained
* @return new {@link Filters} object containing both filters.
*/
public Filters and(Filter filter) {
filter.setBooleanOperator(BooleanOperator.AND);
return new Filters(this, filter);
}
/**
* Convenience method to chain filters using {@link BooleanOperator#OR}.
*
* @param filter to be chained.
* @return new {@link Filters} object containing both filters.
*/
public Filters or(Filter filter) {
filter.setBooleanOperator(BooleanOperator.OR);
return new Filters(this, filter);
}
/**
* @return <code>true</code> if this filter expression is to be negated when it's appended to the query, <code>false</code>
* if not
*/
public boolean isNegated() {
return negated;
}
/**
* @param negated Whether or not the filter expression is to be negated
*/
public void setNegated(boolean negated) {
this.negated = negated;
}
public Class<?> getOwnerEntityType() {
return ownerEntityType;
}
public void setOwnerEntityType(Class<?> ownerEntityType) {
this.ownerEntityType = ownerEntityType;
}
public String getNestedPropertyName() {
return nestedPropertyName;
}
public void setNestedPropertyName(String nestedPropertyName) {
this.nestedPropertyName = nestedPropertyName;
}
public String getRelationshipType() {
return relationshipType;
}
public void setRelationshipType(String relationshipType) {
this.relationshipType = relationshipType;
}
public boolean isNested() {
return this.nestedPropertyName != null;
}
public Class<?> getNestedPropertyType() {
return nestedPropertyType;
}
public void setNestedPropertyType(Class<?> nestedPropertyType) {
this.nestedPropertyType = nestedPropertyType;
}
public String getNestedEntityTypeLabel() {
return nestedEntityTypeLabel;
}
public void setNestedEntityTypeLabel(String nestedEntityTypeLabel) {
this.nestedEntityTypeLabel = nestedEntityTypeLabel;
}
public void setNestedPath(NestedPathSegment... path) {
nestedPath = new ArrayList<>(Arrays.asList(path));
}
public List<NestedPathSegment> getNestedPath() {
return nestedPath;
}
public static class NestedPathSegment implements FilterWithRelationship {
private final String propertyName;
private final Class propertyType;
private String relationshipType;
private String relationshipDirection;
private String nestedEntityTypeLabel;
private boolean nestedRelationshipEntity;
public NestedPathSegment(String propertyName, Class propertyType) {
this.propertyName = propertyName;
this.propertyType = propertyType;
}
public String getPropertyName() {
return propertyName;
}
public Class getPropertyType() {
return propertyType;
}
public String getRelationshipType() {
return relationshipType;
}
public void setRelationshipType(String relationshipType) {
this.relationshipType = relationshipType;
}
public void setRelationshipDirection(String relationshipDirection) {
this.relationshipDirection = relationshipDirection;
}
public String getRelationshipDirection() {
return relationshipDirection;
}
public void setNestedEntityTypeLabel(String nestedEntityTypeLabel) {
this.nestedEntityTypeLabel = nestedEntityTypeLabel;
}
public String getNestedEntityTypeLabel() {
return nestedEntityTypeLabel;
}
public void setNestedRelationshipEntity(boolean nestedRelationshipEntity) {
this.nestedRelationshipEntity = nestedRelationshipEntity;
}
public boolean isNestedRelationshipEntity() {
return nestedRelationshipEntity;
}
}
public boolean isDeepNested() {
return getNestedPath() != null && !getNestedPath().isEmpty();
}
public boolean isNestedRelationshipEntity() {
return nestedRelationshipEntity;
}
public void setNestedRelationshipEntity(boolean nestedRelationshipEntity) {
this.nestedRelationshipEntity = nestedRelationshipEntity;
}
public String uniqueParameterName() {
return isNested() ? getNestedPropertyName() + "_" + getPropertyName() + "_" + index :
getPropertyName() + "_" + index;
}
public AttributeConverter getPropertyConverter() {
return propertyConverter;
}
public void setPropertyConverter(AttributeConverter propertyConverter) {
this.propertyConverter = propertyConverter;
}
public CompositeAttributeConverter getCompositeAttributeConverter() {
return compositeAttributeConverter;
}
public void setCompositeConverter(CompositeAttributeConverter compositeAttributeConverter) {
this.compositeAttributeConverter = compositeAttributeConverter;
}
/**
* Returns the result of passing the property value through the transformer associated with the comparison operator
* on this {@link Filter}.
*
* @return The transformed property value
*/
public Object getTransformedPropertyValue() {
Object value = this.function.getValue();
if (this.getPropertyConverter() != null) {
value = this.getPropertyConverter().toGraphProperty(value);
} else if (this.getCompositeAttributeConverter() != null) {
throw new MappingException("Properties with a CompositeAttributeConverter are not supported by " +
"Filters in this version of OGM. Consider implementing a custom FilterFunction.");
}
return transformPropertyValue(value);
}
private Object transformPropertyValue(Object value) {
if (this.comparisonOperator != null) {
return this.comparisonOperator.getPropertyValueTransformer().transformPropertyValue(value);
}
return new NoOpPropertyValueTransformer().transformPropertyValue(value);
}
public FilterFunction getFunction() {
return function;
}
public void setFunction(FilterFunction function) {
assert function != null;
this.function = function;
this.function.setFilter(this);
}
/**
* Used by Filters to assign an index, so that unique parameter names are ensured when filters are used in a
* collection. Should not be called directly.
*
* @param index The index
*/
void setIndex(int index) {
this.index = index;
}
/**
* @param nodeIdentifier The node identifier
* @param addWhereClause The add where clause.
* @return The filter state as a CYPHER fragment.
*/
public String toCypher(String nodeIdentifier, boolean addWhereClause) {
String fragment = this.function.expression(nodeIdentifier);
String suffix = isNegated() ? negate(fragment) : fragment;
return cypherPrefix(addWhereClause) + suffix;
}
public Map<String, Object> parameters() {
return function.parameters();
}
private String cypherPrefix(boolean addWhereClause) {
StringBuilder cypher = new StringBuilder();
if (addWhereClause) {
cypher.append("WHERE ");
} else {
if (!getBooleanOperator().equals(BooleanOperator.NONE)) {
cypher.append(getBooleanOperator().getValue()).append(" ");
}
}
return cypher.toString();
}
private String negate(String expression) {
return String.format("NOT(%s) ", expression);
}
}