17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .Arrays ;
20
- import java .util .Collections ;
21
20
import java .util .HashMap ;
21
+ import java .util .HashSet ;
22
22
import java .util .Iterator ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
27
27
import java .util .Stack ;
28
28
import java .util .regex .Pattern ;
29
29
30
- import org .bson .BasicBSONObject ;
31
30
import org .springframework .data .domain .Example ;
32
- import org .springframework .data .domain .ExampleSpec ;
33
- import org .springframework .data .domain .ExampleSpec .NullHandler ;
34
- import org .springframework .data .domain .ExampleSpec .PropertyValueTransformer ;
35
- import org .springframework .data .domain .ExampleSpec .StringMatcher ;
31
+ import org .springframework .data .domain .ExampleMatcher .NullHandler ;
32
+ import org .springframework .data .domain .ExampleMatcher .PropertyValueTransformer ;
33
+ import org .springframework .data .domain .ExampleMatcher .StringMatcher ;
36
34
import org .springframework .data .mapping .PropertyHandler ;
37
35
import org .springframework .data .mapping .context .MappingContext ;
38
36
import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
39
37
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
40
38
import org .springframework .data .mongodb .core .query .MongoRegexCreator ;
41
39
import org .springframework .data .mongodb .core .query .SerializationUtils ;
42
- import org .springframework .data .repository .core .support .ExampleSpecAccessor ;
40
+ import org .springframework .data .repository .core .support .ExampleMatcherAccessor ;
43
41
import org .springframework .data .repository .query .parser .Part .Type ;
42
+ import org .springframework .data .util .TypeInformation ;
43
+ import org .springframework .util .Assert ;
44
44
import org .springframework .util .ObjectUtils ;
45
45
import org .springframework .util .StringUtils ;
46
46
@@ -74,43 +74,59 @@ public MongoExampleMapper(MongoConverter converter) {
74
74
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
75
75
* {@link Example#getProbe()}.
76
76
*
77
- * @param example
77
+ * @param example must not be {@literal null}.
78
78
* @return
79
- * @since 1.8
80
79
*/
81
80
public DBObject getMappedExample (Example <?> example ) {
81
+
82
+ Assert .notNull (example , "Example must not be null!" );
83
+
82
84
return getMappedExample (example , mappingContext .getPersistentEntity (example .getProbeType ()));
83
85
}
84
86
85
87
/**
86
88
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
87
89
* {@link Example#getProbe()}.
88
90
*
89
- * @param example
90
- * @param entity
91
+ * @param example must not be {@literal null}.
92
+ * @param entity must not be {@literal null}.
91
93
* @return
92
- * @since 1.8
93
94
*/
95
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
94
96
public DBObject getMappedExample (Example <?> example , MongoPersistentEntity <?> entity ) {
95
97
98
+ Assert .notNull (example , "Example must not be null!" );
99
+ Assert .notNull (entity , "MongoPersistentEntity must not be null!" );
100
+
96
101
DBObject reference = (DBObject ) converter .convertToMongoType (example .getProbe ());
97
102
98
103
if (entity .hasIdProperty () && entity .getIdentifierAccessor (example .getProbe ()).getIdentifier () == null ) {
99
104
reference .removeField (entity .getIdProperty ().getFieldName ());
100
105
}
101
106
102
- ExampleSpecAccessor exampleSpecAccessor = new ExampleSpecAccessor (example .getExampleSpec ());
107
+ ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor (example .getMatcher ());
103
108
104
- applyPropertySpecs ("" , reference , example .getProbeType (), exampleSpecAccessor );
109
+ applyPropertySpecs ("" , reference , example .getProbeType (), matcherAccessor );
105
110
106
- if (exampleSpecAccessor .isTyped ()) {
107
- this .converter .getTypeMapper ().writeTypeRestrictions (reference , (Set ) Collections .singleton (example .getResultType ()));
108
- }
111
+ this .converter .getTypeMapper ().writeTypeRestrictions (reference , getTypesToMatch (example ));
109
112
110
- return ObjectUtils .nullSafeEquals (NullHandler .INCLUDE , exampleSpecAccessor .getNullHandler ()) ? reference
113
+ return ObjectUtils .nullSafeEquals (NullHandler .INCLUDE , matcherAccessor .getNullHandler ()) ? reference
111
114
: new BasicDBObject (SerializationUtils .flattenMap (reference ));
112
115
}
113
116
117
+ private Set <Class <?>> getTypesToMatch (Example <?> example ) {
118
+
119
+ Set <Class <?>> types = new HashSet <Class <?>>();
120
+
121
+ for (TypeInformation <?> reference : mappingContext .getManagedTypes ()) {
122
+ if (example .getProbeType ().isAssignableFrom (reference .getType ())) {
123
+ types .add (reference .getType ());
124
+ }
125
+ }
126
+
127
+ return types ;
128
+ }
129
+
114
130
private String getMappedPropertyPath (String path , Class <?> probeType ) {
115
131
116
132
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (probeType );
@@ -159,7 +175,7 @@ public void doWithPersistentProperty(MongoPersistentProperty property) {
159
175
}
160
176
161
177
private void applyPropertySpecs (String path , DBObject source , Class <?> probeType ,
162
- ExampleSpecAccessor exampleSpecAccessor ) {
178
+ ExampleMatcherAccessor exampleSpecAccessor ) {
163
179
164
180
if (!(source instanceof BasicDBObject )) {
165
181
return ;
@@ -172,12 +188,12 @@ private void applyPropertySpecs(String path, DBObject source, Class<?> probeType
172
188
Map .Entry <String , Object > entry = iter .next ();
173
189
String propertyPath = StringUtils .hasText (path ) ? path + "." + entry .getKey () : entry .getKey ();
174
190
String mappedPropertyPath = getMappedPropertyPath (propertyPath , probeType );
175
-
176
- if (isEmptyIdProperty (entry )) {
191
+
192
+ if (isEmptyIdProperty (entry )) {
177
193
iter .remove ();
178
194
continue ;
179
195
}
180
-
196
+
181
197
if (exampleSpecAccessor .isIgnoredPath (propertyPath ) || exampleSpecAccessor .isIgnoredPath (mappedPropertyPath )) {
182
198
iter .remove ();
183
199
continue ;
@@ -243,5 +259,4 @@ private void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher s
243
259
dbo .put ("$options" , "i" );
244
260
}
245
261
}
246
-
247
262
}
0 commit comments