/
ResultProcessor.java
333 lines (268 loc) · 9.85 KB
/
ResultProcessor.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
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.query;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.Slice;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.util.ReactiveWrapperConverters;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A {@link ResultProcessor} to expose metadata about query result element projection and eventually post processing raw
* query results into projections and data transfer objects.
*
* @author Oliver Gierke
* @author John Blum
* @author Mark Paluch
* @author Christoph Strobl
* @since 1.12
*/
public class ResultProcessor {
private final QueryMethod method;
private final ProjectingConverter converter;
private final ProjectionFactory factory;
private final ReturnedType type;
/**
* Creates a new {@link ResultProcessor} from the given {@link QueryMethod} and {@link ProjectionFactory}.
*
* @param method must not be {@literal null}.
* @param factory must not be {@literal null}.
*/
ResultProcessor(QueryMethod method, ProjectionFactory factory) {
this(method, factory, method.getReturnedObjectType());
}
/**
* Creates a new {@link ResultProcessor} for the given {@link QueryMethod}, {@link ProjectionFactory} and type.
*
* @param method must not be {@literal null}.
* @param factory must not be {@literal null}.
* @param type must not be {@literal null}.
*/
private ResultProcessor(QueryMethod method, ProjectionFactory factory, Class<?> type) {
Assert.notNull(method, "QueryMethod must not be null");
Assert.notNull(factory, "ProjectionFactory must not be null");
Assert.notNull(type, "Type must not be null");
this.method = method;
this.type = ReturnedType.of(type, method.getDomainClass(), factory);
this.converter = new ProjectingConverter(this.type, factory);
this.factory = factory;
}
private ResultProcessor(QueryMethod method, ProjectingConverter converter, ProjectionFactory factory,
ReturnedType type) {
this.method = method;
this.converter = converter;
this.factory = factory;
this.type = type;
}
/**
* Returns a new {@link ResultProcessor} with a new projection type obtained from the given {@link ParameterAccessor}.
*
* @param accessor must not be {@literal null}.
* @return
*/
public ResultProcessor withDynamicProjection(ParameterAccessor accessor) {
Assert.notNull(accessor, "Parameter accessor must not be null");
Class<?> projection = accessor.findDynamicProjection();
return projection == null //
? this //
: withType(projection);
}
/**
* Returns the {@link ReturnedType}.
*
* @return
*/
public ReturnedType getReturnedType() {
return type;
}
/**
* Post-processes the given query result.
*
* @param source can be {@literal null}.
* @return
*/
@Nullable
public <T> T processResult(@Nullable Object source) {
return processResult(source, NoOpConverter.INSTANCE);
}
/**
* Post-processes the given query result using the given preparing {@link Converter} to potentially prepare collection
* elements.
*
* @param source can be {@literal null}.
* @param preparingConverter must not be {@literal null}.
* @return
*/
@Nullable
@SuppressWarnings("unchecked")
public <T> T processResult(@Nullable Object source, Converter<Object, Object> preparingConverter) {
if (source == null || type.isInstance(source) || !type.isProjecting()) {
return (T) source;
}
Assert.notNull(preparingConverter, "Preparing converter must not be null");
ChainingConverter converter = ChainingConverter.of(type.getReturnedType(), preparingConverter).and(this.converter);
if (source instanceof Window<?> && method.isScrollQuery()) {
return (T) ((Window<?>) source).map(converter::convert);
}
if (source instanceof Slice && (method.isPageQuery() || method.isSliceQuery())) {
return (T) ((Slice<?>) source).map(converter::convert);
}
if (source instanceof Collection<?> collection && method.isCollectionQuery()) {
Collection<Object> target = createCollectionFor(collection);
for (Object columns : collection) {
target.add(type.isInstance(columns) ? columns : converter.convert(columns));
}
return (T) target;
}
if (source instanceof Stream && method.isStreamQuery()) {
return (T) ((Stream<Object>) source).map(t -> type.isInstance(t) ? t : converter.convert(t));
}
if (ReactiveWrapperConverters.supports(source.getClass())) {
return (T) ReactiveWrapperConverters.map(source, it -> processResult(it, preparingConverter));
}
return (T) converter.convert(source);
}
private ResultProcessor withType(Class<?> type) {
ReturnedType returnedType = ReturnedType.of(type, method.getDomainClass(), factory);
return new ResultProcessor(method, converter.withType(returnedType), factory, returnedType);
}
/**
* Creates a new {@link Collection} for the given source. Will try to create an instance of the source collection's
* type first falling back to creating an approximate collection if the former fails.
*
* @param source must not be {@literal null}.
* @return
*/
private static Collection<Object> createCollectionFor(Collection<?> source) {
try {
return CollectionFactory.createCollection(source.getClass(), source.size());
} catch (RuntimeException o_O) {
return CollectionFactory.createApproximateCollection(source, source.size());
}
}
private static class ChainingConverter implements Converter<Object, Object> {
private final Class<?> targetType;
private final Converter<Object, Object> delegate;
private ChainingConverter(Class<?> targetType, Converter<Object, Object> delegate) {
this.targetType = targetType;
this.delegate = delegate;
}
public static ChainingConverter of(Class<?> targetType, Converter<Object, Object> delegate) {
return new ChainingConverter(targetType, delegate);
}
/**
* Returns a new {@link ChainingConverter} that hands the elements resulting from the current conversion to the
* given {@link Converter}.
*
* @param converter must not be {@literal null}.
* @return
*/
public ChainingConverter and(final Converter<Object, Object> converter) {
Assert.notNull(converter, "Converter must not be null");
return new ChainingConverter(targetType, source -> {
if (source == null || targetType.isInstance(source)) {
return source;
}
Object intermediate = ChainingConverter.this.convert(source);
return intermediate == null || targetType.isInstance(intermediate) ? intermediate
: converter.convert(intermediate);
});
}
@Nullable
@Override
public Object convert(Object source) {
return delegate.convert(source);
}
}
/**
* A simple {@link Converter} that will return the source value as is.
*
* @author Oliver Gierke
* @since 1.12
*/
private static enum NoOpConverter implements Converter<Object, Object> {
INSTANCE;
@Override
public Object convert(Object source) {
return source;
}
}
private static class ProjectingConverter implements Converter<Object, Object> {
private final ReturnedType type;
private final ProjectionFactory factory;
private final ConversionService conversionService;
/**
* Creates a new {@link ProjectingConverter} for the given {@link ReturnedType} and {@link ProjectionFactory}.
*
* @param type must not be {@literal null}.
* @param factory must not be {@literal null}.
*/
ProjectingConverter(ReturnedType type, ProjectionFactory factory) {
this(type, factory, DefaultConversionService.getSharedInstance());
}
public ProjectingConverter(ReturnedType type, ProjectionFactory factory, ConversionService conversionService) {
this.type = type;
this.factory = factory;
this.conversionService = conversionService;
}
/**
* Creates a new {@link ProjectingConverter} for the given {@link ReturnedType}.
*
* @param type must not be {@literal null}.
* @return
*/
ProjectingConverter withType(ReturnedType type) {
Assert.notNull(type, "ReturnedType must not be null");
return new ProjectingConverter(type, factory, conversionService);
}
@Nullable
@Override
public Object convert(Object source) {
Class<?> targetType = type.getReturnedType();
if (targetType.isInterface()) {
return factory.createProjection(targetType, getProjectionTarget(source));
}
return conversionService.convert(source, targetType);
}
private Object getProjectionTarget(Object source) {
if (source != null && source.getClass().isArray()) {
source = Arrays.asList((Object[]) source);
}
if (source instanceof Collection) {
return toMap((Collection<?>) source, type.getInputProperties());
}
return source;
}
private static Map<String, Object> toMap(Collection<?> values, List<String> names) {
int i = 0;
Map<String, Object> result = new HashMap<>(values.size());
for (Object element : values) {
result.put(names.get(i++), element);
}
return result;
}
}
}