This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
/
PartTree.java
457 lines (373 loc) · 14.7 KB
/
PartTree.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package org.springframework.roo.addon.finder.addon.parser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder;
import org.springframework.roo.classpath.details.FieldMetadata;
import org.springframework.roo.classpath.details.MemberHoldingTypeDetails;
import org.springframework.roo.classpath.operations.jsr303.DateFieldPersistenceType;
import org.springframework.roo.classpath.scanner.MemberDetails;
import org.springframework.roo.model.DataType;
import org.springframework.roo.model.JavaType;
/**
* This class is based on PartTree.java class from Spring Data commons project.
*
* It has some little changes to be able to work properly on Spring Roo project
* and make easy Spring Data query parser.
*
* Get more information about original class on:
*
* https://github.com/spring-projects/spring-data-commons/blob/master/src/main/java/org/springframework/data/repository/query/parser/PartTree.java
*
* Class to parse a {@link String} into a {@link Subject} and a {@link Predicate}.
* Takes a entity details to extract the
* properties of the domain class. The {@link PartTree} can then be used to
* build queries based on its API instead of parsing the method name for each
* query execution.
*
* @author Paula Navarro
* @author Juan Carlos García
* @since 2.0
*/
public class PartTree {
private static final String KEYWORD_TEMPLATE = "(%s)(?=(\\p{Lu}|\\z))";
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(" + Subject.QUERY_PATTERN + "|"
+ Subject.COUNT_PATTERN + ")((\\p{Lu}.*?))??By");
/**
* Subject is delimited by a prefix (find, read , query or count) and {@literal By} delimiter, for
* example "findDistinctUserByNameOrderByAge" would have the subject
* "DistinctUser".
*/
private final Subject subject;
/**
* Predicate contains conditions, and optionally order clause subject. E.g. "findDistinctUserByNameOrderByAge" would have
* the predicate "NameOrderByAge".
*/
private final Predicate predicate;
/**
* Query used to generate the Subject and Predicate
*/
private final String originalQuery;
/**
* Interface that provides operations to obtain useful information during finder autocomplete
*/
private final FinderAutocomplete finderAutocomplete;
/**
* Return type of generated finder
*/
private JavaType returnType;
/**
* Return type provided in constructor when it is different from target entity. Can be null.
*/
private JavaType providedReturnType;
/**
* Parameters of generated finder
*/
List<FinderParameter> finderParameters;
/**
* Creates a new {@link PartTree} by parsing the given {@link String}.
*
* @param source
* the {@link String} to parse
* @param memberDetails
* the member details of the entity class to extract the fields
* to expose them as options.
* @param finderAutocomplete interface that provides operations to obtain useful information during autocomplete
*/
public PartTree(String source, MemberDetails memberDetails,
FinderAutocomplete finderAutocomplete, JavaType providedReturnType) {
Validate.notNull(source, "Source must not be null");
Validate.notNull(memberDetails, "MemberDetails must not be null");
this.originalQuery = source;
this.finderAutocomplete = finderAutocomplete;
// Extracts entity fields removing persistence fields and list type
// fields
List<FieldMetadata> fields = getValidProperties(memberDetails.getFields());
Matcher matcher = PREFIX_TEMPLATE.matcher(source);
if (!matcher.find()) {
this.subject = new Subject(this, source, fields);
this.predicate = new Predicate(this, "", fields);
} else {
this.subject = new Subject(this, matcher.group(0), fields);
this.predicate = new Predicate(this, source.substring(matcher.group().length()), fields);
}
this.providedReturnType = providedReturnType;
this.returnType = extractReturnType(memberDetails);
this.finderParameters = predicate.getParameters();
}
public PartTree(String source, MemberDetails memberDetails, FinderAutocomplete finderAutocomplete) {
this(source, memberDetails, finderAutocomplete, null);
}
/**
* Extracts the java type of the results to be returned by the PartTree query
*
* @param entityDetails the entity details to extract the object to return by default
* @return
*/
private JavaType extractReturnType(MemberDetails entityDetails) {
Integer maxResults = subject.getMaxResults();
Pair<FieldMetadata, String> property = subject.getProperty();
JavaType type = null;
// Count subject returns Long
if (subject.isCountProjection()) {
return JavaType.LONG_OBJECT;
}
if (property != null && property.getLeft() != null) {
// Returns the property type if it is specified
type = property.getLeft().getFieldType();
} else if (providedReturnType != null) {
type = providedReturnType;
} else {
// By default returns entity type
List<MemberHoldingTypeDetails> details = entityDetails.getDetails();
for (MemberHoldingTypeDetails detail : details) {
if (finderAutocomplete != null
&& finderAutocomplete.getEntityDetails(detail.getType()).equals(entityDetails)) {
type = detail.getType();
break;
} else {
type = detail.getType();
}
}
}
// Check number of results to return.
if (maxResults != null && maxResults == 1) {
// Unique result
return type;
}
//If it is not an unique result, returns a list
if (type.isPrimitive()) {
// Lists cannot return primitive types, so primitive types are transformed into their wrapper class
type =
new JavaType(type.getFullyQualifiedTypeName(), type.getArray(), DataType.TYPE,
type.getArgName(), type.getParameters(), type.getModule());
}
return new JavaType("java.util.List", 0, DataType.TYPE, null, Arrays.asList(type));
}
/**
* Creates a new {@link PartTree} by parsing the given {@link String}.
*
* @param source
* the {@link String} to parse
* @param memberDetails
* the member details of the entity class to extract the fields
* to expose them as options.
*/
public PartTree(String source, MemberDetails memberDetails) {
this(source, memberDetails, null);
}
/**
* Filters the entity properties that can be used to build Spring Data
* expressions. Persistence version property is excluded as well as multivalued properties
* since Spring Data does not support operations with them
*
* @param memberDetails
* @return entity properties which type is supported by SpringData
*/
private List<FieldMetadata> getValidProperties(List<FieldMetadata> fields) {
List<FieldMetadata> validProperties = new ArrayList<FieldMetadata>();
for (FieldMetadata fieldMetadata : fields) {
// Check if its type is List/Map/etc
if (fieldMetadata.getFieldType().isMultiValued())
continue;
// Check if it is annotated with @Version
if (fieldMetadata.getAnnotation(new JavaType("javax.persistence.Version")) != null)
continue;
validProperties.add(fieldMetadata);
}
return validProperties;
}
/**
* Filters the entity properties of a javaType that can be used to build Spring Data
* expressions. Persistence version field is excluded, and multivalued fields
* are removed since Spring Data does not supports operations with them.
*
* @param javaType
* @return entity properties which type is supported by SpringData
*/
public List<FieldMetadata> getValidProperties(JavaType javaType) {
if (finderAutocomplete != null) {
final MemberDetails entityDetails = finderAutocomplete.getEntityDetails(javaType);
if (entityDetails != null) {
return getValidProperties((List<FieldMetadata>) entityDetails.getFields());
}
}
return null;
}
/**
* Extract entity property name from raw property and returns the property metadata and the property name.
* If raw property references a property of a related entity, returns a Pair with the related entity property metadata and
* a string composed by the reference property name and the related entity property name.
* E.g. if raw property contains "petName" and current entity has a relation with Pet, it will return Pair(NameMetadata, "petName"))
*
* @param rawProperty the string that contains property name
* @param fields entity properties
* @return Pair that contains the property metadata and the property name.
*/
public Pair<FieldMetadata, String> extractValidProperty(String rawProperty,
List<FieldMetadata> fields) {
if (StringUtils.isBlank(rawProperty) || fields == null) {
return null;
}
FieldMetadata tempField = null;
rawProperty = StringUtils.uncapitalize(rawProperty);
// ExtractProperty can contain other information after property name. For that reason, it is necessary find the property that matches more letters with the property contained into extractProperty
for (FieldMetadata field : fields) {
if (field.getFieldName().toString().compareTo(rawProperty) == 0) {
tempField = field;
break;
}
if (rawProperty.startsWith(field.getFieldName().toString())) {
if (tempField == null
|| tempField.getFieldName().toString().length() < field.getFieldName().toString()
.length())
tempField = field;
}
}
if (tempField == null) {
return null;
}
// If extracted property is a reference to other entity, the fields of this related entity are inspected to check if extractProperty contains information about them
Pair<FieldMetadata, String> related = extractRelatedEntityValidProperty(rawProperty, tempField);
if (related != null) {
return Pair.of(
related.getLeft() == null ? tempField : related.getLeft(),
StringUtils.capitalize(tempField.getFieldName().toString()).concat(
StringUtils.capitalize(related.getRight())));
}
return Pair.of(tempField, StringUtils.capitalize(tempField.getFieldName().toString()));
}
/**
* Gets the property of a related entity, using raw property information.
*
* @param rawProperty string that contains the definition of a property, which can be a property accessed by a relation.
* @param referenceProperty property that represents a relation with other entity.
* @return Pair that contains a property metadata and its name.
*/
private Pair<FieldMetadata, String> extractRelatedEntityValidProperty(String extractProperty,
FieldMetadata referenceProperty) {
if (StringUtils.isBlank(extractProperty) || referenceProperty == null) {
return null;
}
// Extract the property of a related entity
String property =
StringUtils.substringAfter(extractProperty, referenceProperty.getFieldName().toString());
if (StringUtils.isBlank(property)) {
return null;
}
return extractValidProperty(property, getValidProperties(referenceProperty.getFieldType()));
}
/**
* Returns the different queries that can be build based on the current defined query.
* First it lists the subject expressions that can be build. Once it is completed, it returns the queries available to
* define the predicate.
*
* @return
*/
public List<String> getOptions() {
if (!subject.isComplete()) {
return subject.getOptions();
} else if (!predicate.hasOrderClause()) {
return predicate.getOptions(subject.toString());
} else {
return predicate.getOrderOptions(subject.toString());
}
}
/**
* Returns whether we indicate distinct lookup of entities.
*
* @return {@literal true} if distinct
*/
public boolean isDistinct() {
return subject.isDistinct();
}
/**
* Returns whether a count projection shall be applied.
*
* @return
*/
public Boolean isCountProjection() {
return subject.isCountProjection();
}
@Override
public String toString() {
return subject.toString().concat(predicate.toString());
}
/**
* Splits the given text at the given keyword. Expects camel-case style to
* only match concrete keywords and not derivatives of it.
*
* @param text
* the text to split
* @param keyword
* the keyword to split around
* @param limit the limit controls the number of times the pattern is applied and therefore affects the length of the resulting array
* @return an array of split items
*/
static String[] split(String text, String keyword, int limit) {
Pattern pattern = Pattern.compile(String.format(KEYWORD_TEMPLATE, keyword));
return pattern.split(text, limit);
}
/**
* Returns true if PartTree query is well-defined and the query generated is the same that the one used to build its structure.
* @return
*/
public boolean isValid() {
return subject.isValid() && predicate.IsValid() && toString().equals(originalQuery);
}
/**
* Returns true if query is well-defined, which means that subject and predicate have a correct structure.
* However, it does not validate if entity properties exist.
* @return
*/
public static boolean isValid(String query) {
Matcher matcher = PREFIX_TEMPLATE.matcher(query);
if (!matcher.find()) {
return Subject.isValid(query) && Predicate.IsValid("");
} else {
return Subject.isValid(matcher.group(0))
&& Predicate.IsValid(query.substring(matcher.group().length()));
}
}
/**
* Returns the number of maximal results to return or {@literal null} if
* not restricted.
*
* @return
*/
public Integer getMaxResults() {
return subject.getMaxResults();
}
/**
* Returns true if the query matches with the given {@link Pattern}. Otherwise, returns false.
* If the query is null, returns false.
*
* @param query
* @param pattern
* @return
*/
public final static boolean matches(String query, Pattern pattern) {
return query == null ? false : pattern.matcher(query).find();
}
/**
* Method that obtains the return type of current finder
*
* @return JavaType with return type
*/
public JavaType getReturnType() {
return returnType;
}
/**
* Method that obtains the necessary parameters of current finder
*
* @return List that contains all necessary parameters
*/
public List<FinderParameter> getParameters() {
return finderParameters;
}
}