1
1
/*
2
- * Copyright 2015 the original author or authors.
2
+ * Copyright 2015-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import java .util .regex .Pattern ;
25
25
26
26
import org .springframework .data .domain .Example ;
27
- import org .springframework .data .domain .Example . NullHandler ;
28
- import org .springframework .data .domain .Example . StringMatcher ;
29
- import org .springframework .data .domain .PropertySpecifier ;
27
+ import org .springframework .data .domain .ExampleSpec ;
28
+ import org .springframework .data .domain .ExampleSpec . PropertyValueTransformer ;
29
+ import org .springframework .data .domain .ExampleSpecAccessor ;
30
30
import org .springframework .data .mapping .PropertyHandler ;
31
31
import org .springframework .data .mapping .context .MappingContext ;
32
32
import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
41
41
42
42
/**
43
43
* @author Christoph Strobl
44
+ * @author Mark Paluch
44
45
* @since 1.8
45
46
*/
46
47
public class MongoExampleMapper {
@@ -57,41 +58,43 @@ public MongoExampleMapper(MongoConverter converter) {
57
58
/**
58
59
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
59
60
* {@link Example#getProbe()}.
60
- *
61
+ *
61
62
* @param example
62
63
* @return
63
64
* @since 1.8
64
65
*/
65
66
public DBObject getMappedExample (Example <?> example ) {
66
- return getMappedExample (example , mappingContext .getPersistentEntity (example .getSampleType ()));
67
+ return getMappedExample (example , mappingContext .getPersistentEntity (example .getProbeType ()));
67
68
}
68
69
69
70
/**
70
71
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
71
72
* {@link Example#getProbe()}.
72
- *
73
+ *
73
74
* @param example
74
75
* @param entity
75
76
* @return
76
77
* @since 1.8
77
78
*/
78
79
public DBObject getMappedExample (Example <?> example , MongoPersistentEntity <?> entity ) {
79
80
80
- DBObject reference = (DBObject ) converter .convertToMongoType (example .getSampleObject ());
81
+ DBObject reference = (DBObject ) converter .convertToMongoType (example .getProbe ());
81
82
82
- if (entity .hasIdProperty () && entity .getIdentifierAccessor (example .getSampleObject ()).getIdentifier () == null ) {
83
+ if (entity .hasIdProperty () && entity .getIdentifierAccessor (example .getProbe ()).getIdentifier () == null ) {
83
84
reference .removeField (entity .getIdProperty ().getFieldName ());
84
85
}
85
86
86
- applyPropertySpecs ( "" , reference , example );
87
+ ExampleSpecAccessor exampleSpecAccessor = ExampleSpecAccessor . of ( example );
87
88
88
- return ObjectUtils .nullSafeEquals (NullHandler .INCLUDE , example .getNullHandler ()) ? reference : new BasicDBObject (
89
- SerializationUtils .flatMap (reference ));
89
+ applyPropertySpecs ("" , reference , example .getProbeType (), exampleSpecAccessor );
90
+
91
+ return ObjectUtils .nullSafeEquals (ExampleSpec .NullHandler .INCLUDE , exampleSpecAccessor .getNullHandler ()) ? reference
92
+ : new BasicDBObject (SerializationUtils .flatMap (reference ));
90
93
}
91
94
92
- private String getMappedPropertyPath (String path , Example <?> example ) {
95
+ private String getMappedPropertyPath (String path , Class <?> probeType , ExampleSpecAccessor exampleSpecAccessor ) {
93
96
94
- MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (example . getSampleType () );
97
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (probeType );
95
98
96
99
Iterator <String > parts = Arrays .asList (path .split ("\\ ." )).iterator ();
97
100
@@ -136,7 +139,8 @@ public void doWithPersistentProperty(MongoPersistentProperty property) {
136
139
137
140
}
138
141
139
- private void applyPropertySpecs (String path , DBObject source , Example <?> example ) {
142
+ private void applyPropertySpecs (String path , DBObject source , Class <?> probeType ,
143
+ ExampleSpecAccessor exampleSpecAccessor ) {
140
144
141
145
if (!(source instanceof BasicDBObject )) {
142
146
return ;
@@ -155,39 +159,30 @@ private void applyPropertySpecs(String path, DBObject source, Example<?> example
155
159
156
160
String propertyPath = StringUtils .hasText (path ) ? path + "." + entry .getKey () : entry .getKey ();
157
161
158
- String mappedPropertyPath = getMappedPropertyPath (propertyPath , example );
159
- if (example .isIgnoredPath (propertyPath ) || example .isIgnoredPath (mappedPropertyPath )) {
162
+ String mappedPropertyPath = getMappedPropertyPath (propertyPath , probeType , exampleSpecAccessor );
163
+ if (exampleSpecAccessor .isIgnoredPath (propertyPath ) || exampleSpecAccessor .isIgnoredPath (mappedPropertyPath )) {
160
164
iter .remove ();
161
165
continue ;
162
166
}
163
167
164
- PropertySpecifier specifier = null ;
165
- StringMatcher stringMatcher = example .getDefaultStringMatcher ();
168
+ ExampleSpec .StringMatcher stringMatcher = exampleSpecAccessor .getDefaultStringMatcher ();
166
169
Object value = entry .getValue ();
167
- boolean ignoreCase = example .isIngnoreCaseEnabled ();
168
-
169
- if (example .hasPropertySpecifiers ()) {
170
-
171
- mappedPropertyPath = example .hasPropertySpecifier (propertyPath ) ? propertyPath : getMappedPropertyPath (
172
- propertyPath , example );
170
+ boolean ignoreCase = exampleSpecAccessor .isIgnoreCaseEnabled ();
173
171
174
- specifier = example . getPropertySpecifier ( mappedPropertyPath );
172
+ if ( exampleSpecAccessor . hasPropertySpecifiers ()) {
175
173
176
- if (specifier != null ) {
177
- if (specifier .hasStringMatcher ()) {
178
- stringMatcher = specifier .getStringMatcher ();
179
- }
180
- if (specifier .getIgnoreCase () != null ) {
181
- ignoreCase = specifier .getIgnoreCase ();
182
- }
174
+ mappedPropertyPath = exampleSpecAccessor .hasPropertySpecifier (propertyPath ) ? propertyPath
175
+ : getMappedPropertyPath (propertyPath , probeType , exampleSpecAccessor );
183
176
184
- }
177
+ stringMatcher = exampleSpecAccessor .getStringMatcherForPath (mappedPropertyPath );
178
+ ignoreCase = exampleSpecAccessor .isIgnoreCaseForPath (mappedPropertyPath );
185
179
}
186
180
187
181
// TODO: should a PropertySpecifier outrule the later on string matching?
188
- if (specifier != null ) {
182
+ if (exampleSpecAccessor . hasPropertySpecifier ( mappedPropertyPath ) ) {
189
183
190
- value = specifier .transformValue (value );
184
+ PropertyValueTransformer valueTransformer = exampleSpecAccessor .getValueTransformerForPath (mappedPropertyPath );
185
+ value = valueTransformer .convert (value );
191
186
if (value == null ) {
192
187
iter .remove ();
193
188
continue ;
@@ -199,16 +194,17 @@ private void applyPropertySpecs(String path, DBObject source, Example<?> example
199
194
if (entry .getValue () instanceof String ) {
200
195
applyStringMatcher (entry , stringMatcher , ignoreCase );
201
196
} else if (entry .getValue () instanceof BasicDBObject ) {
202
- applyPropertySpecs (propertyPath , (BasicDBObject ) entry .getValue (), example );
197
+ applyPropertySpecs (propertyPath , (BasicDBObject ) entry .getValue (), probeType , exampleSpecAccessor );
203
198
}
204
199
}
205
200
}
206
201
207
- private void applyStringMatcher (Map .Entry <String , Object > entry , StringMatcher stringMatcher , boolean ignoreCase ) {
202
+ private void applyStringMatcher (Map .Entry <String , Object > entry , ExampleSpec .StringMatcher stringMatcher ,
203
+ boolean ignoreCase ) {
208
204
209
205
BasicDBObject dbo = new BasicDBObject ();
210
206
211
- if (ObjectUtils .nullSafeEquals (StringMatcher .DEFAULT , stringMatcher )) {
207
+ if (ObjectUtils .nullSafeEquals (ExampleSpec . StringMatcher .DEFAULT , stringMatcher )) {
212
208
213
209
if (ignoreCase ) {
214
210
dbo .put ("$regex" , Pattern .quote ((String ) entry .getValue ()));
0 commit comments