|
16 | 16 | package org.springframework.data.elasticsearch.repository.query;
|
17 | 17 |
|
18 | 18 | import java.lang.reflect.Method;
|
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.List;
|
20 | 21 |
|
21 | 22 | import org.springframework.core.MethodParameter;
|
| 23 | +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
22 | 24 | import org.springframework.data.elasticsearch.repository.query.ElasticsearchParameters.ElasticsearchParameter;
|
23 | 25 | import org.springframework.data.geo.Distance;
|
24 | 26 | import org.springframework.data.repository.query.Parameter;
|
25 | 27 | import org.springframework.data.repository.query.Parameters;
|
| 28 | +import org.springframework.data.util.TypeInformation; |
26 | 29 |
|
27 | 30 | /**
|
28 | 31 | * @author Christoph Strobl
|
29 | 32 | * @author Peter-Josef Meisch
|
30 | 33 | * @since 3.2
|
31 | 34 | */
|
32 | 35 | public class ElasticsearchParameters extends Parameters<ElasticsearchParameters, ElasticsearchParameter> {
|
33 |
| - |
34 |
| - public ElasticsearchParameters(Method method) { |
35 |
| - super(method); |
36 |
| - } |
| 36 | + private final int indexCoordinatesIndex; |
37 | 37 |
|
38 | 38 | private ElasticsearchParameters(List<ElasticsearchParameter> parameters) {
|
39 | 39 | super(parameters);
|
| 40 | + this.indexCoordinatesIndex = initIndexCoordinatesIndex(); |
40 | 41 | }
|
41 | 42 |
|
42 |
| - @Override |
43 |
| - protected ElasticsearchParameter createParameter(MethodParameter parameter) { |
44 |
| - return new ElasticsearchParameter(parameter); |
| 43 | + public ElasticsearchParameters(Method method, TypeInformation<?> aggregateType) { |
| 44 | + super(method, (param) -> new ElasticsearchParameter(param, aggregateType)); |
| 45 | + this.indexCoordinatesIndex = initIndexCoordinatesIndex(); |
| 46 | + } |
| 47 | + |
| 48 | + private int initIndexCoordinatesIndex() { |
| 49 | + int index = 0; |
| 50 | + List<Integer> foundIndices = new ArrayList<>(); |
| 51 | + for (ElasticsearchParameter parameter : this) { |
| 52 | + if (parameter.isIndexCoordinatesParameter()) { |
| 53 | + foundIndices.add(index); |
| 54 | + } |
| 55 | + index++; |
| 56 | + } |
| 57 | + if (foundIndices.size() > 1) { |
| 58 | + throw new IllegalArgumentException(this + " can only contain at most one IndexCoordinates parameter."); |
| 59 | + } |
| 60 | + return foundIndices.isEmpty() ? -1 : foundIndices.get(0); |
45 | 61 | }
|
46 | 62 |
|
47 | 63 | @Override
|
48 | 64 | protected ElasticsearchParameters createFrom(List<ElasticsearchParameter> parameters) {
|
49 | 65 | return new ElasticsearchParameters(parameters);
|
50 | 66 | }
|
51 | 67 |
|
| 68 | + public boolean hasIndexCoordinatesParameter() { |
| 69 | + return this.indexCoordinatesIndex != -1; |
| 70 | + } |
| 71 | + |
| 72 | + public int getIndexCoordinatesIndex() { |
| 73 | + return indexCoordinatesIndex; |
| 74 | + } |
| 75 | + |
52 | 76 | /**
|
53 | 77 | * Custom {@link Parameter} implementation adding parameters of type {@link Distance} to the special ones.
|
54 | 78 | *
|
55 | 79 | * @author Christoph Strobl
|
56 | 80 | */
|
57 |
| - class ElasticsearchParameter extends Parameter { |
| 81 | + static class ElasticsearchParameter extends Parameter { |
| 82 | + |
| 83 | + private final boolean indexCoordinatesParameter; |
58 | 84 |
|
59 | 85 | /**
|
60 | 86 | * Creates a new {@link ElasticsearchParameter}.
|
61 | 87 | *
|
62 | 88 | * @param parameter must not be {@literal null}.
|
63 | 89 | */
|
64 |
| - ElasticsearchParameter(MethodParameter parameter) { |
65 |
| - super(parameter); |
| 90 | + ElasticsearchParameter(MethodParameter parameter, TypeInformation<?> aggregateType) { |
| 91 | + super(parameter, aggregateType); |
| 92 | + this.indexCoordinatesParameter = parameter.getParameter().getType().isAssignableFrom(IndexCoordinates.class); |
66 | 93 | }
|
67 | 94 |
|
| 95 | + @Override |
| 96 | + public boolean isSpecialParameter() { |
| 97 | + return isIndexCoordinatesParameter() || super.isSpecialParameter(); |
| 98 | + } |
| 99 | + |
| 100 | + public boolean isIndexCoordinatesParameter() { |
| 101 | + return this.indexCoordinatesParameter; |
| 102 | + } |
68 | 103 | }
|
69 | 104 | }
|
0 commit comments