15
15
*/
16
16
package org .springframework .data .mongodb .core .aggregation ;
17
17
18
+ import java .util .ArrayList ;
19
+ import java .util .List ;
20
+
18
21
import org .springframework .dao .InvalidDataAccessApiUsageException ;
19
22
import org .springframework .data .mongodb .core .query .CriteriaDefinition ;
20
23
import org .springframework .util .Assert ;
21
24
import org .springframework .util .ClassUtils ;
22
25
23
- import com .mongodb .BasicDBList ;
24
26
import com .mongodb .BasicDBObject ;
25
27
import com .mongodb .DBObject ;
26
28
32
34
*
33
35
* @see http://docs.mongodb.com/manual/reference/operator/aggregation/cond/
34
36
* @author Mark Paluch
37
+ * @author Christoph Strobl
35
38
* @since 1.10
36
39
*/
37
40
public class ConditionalOperator implements AggregationExpression {
@@ -42,7 +45,7 @@ public class ConditionalOperator implements AggregationExpression {
42
45
43
46
/**
44
47
* Creates a new {@link ConditionalOperator} for a given {@link Field} and {@code then}/{@code otherwise} values.
45
- *
48
+ *
46
49
* @param condition must not be {@literal null}.
47
50
* @param thenValue must not be {@literal null}.
48
51
* @param otherwiseValue must not be {@literal null}.
@@ -116,8 +119,7 @@ private Object resolveValue(AggregationOperationContext context, Object value) {
116
119
return ((ConditionalOperator ) value ).toDbObject (context );
117
120
}
118
121
119
- DBObject toMap = context .getMappedObject (new BasicDBObject ("$set" , value ));
120
- return toMap .get ("$set" );
122
+ return context .getMappedObject (new BasicDBObject ("$set" , value )).get ("$set" );
121
123
}
122
124
123
125
private Object resolveCriteria (AggregationOperationContext context , Object value ) {
@@ -129,7 +131,7 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
129
131
if (value instanceof CriteriaDefinition ) {
130
132
131
133
DBObject mappedObject = context .getMappedObject (((CriteriaDefinition ) value ).getCriteriaObject ());
132
- BasicDBList clauses = new BasicDBList ();
134
+ List < Object > clauses = new ArrayList < Object > ();
133
135
134
136
clauses .addAll (getClauses (context , mappedObject ));
135
137
@@ -144,9 +146,9 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
144
146
String .format ("Invalid value in condition. Supported: DBObject, Field references, Criteria, got: %s" , value ));
145
147
}
146
148
147
- private BasicDBList getClauses (AggregationOperationContext context , DBObject mappedObject ) {
149
+ private List < Object > getClauses (AggregationOperationContext context , DBObject mappedObject ) {
148
150
149
- BasicDBList clauses = new BasicDBList ();
151
+ List < Object > clauses = new ArrayList < Object > ();
150
152
151
153
for (String key : mappedObject .keySet ()) {
152
154
@@ -157,15 +159,17 @@ private BasicDBList getClauses(AggregationOperationContext context, DBObject map
157
159
return clauses ;
158
160
}
159
161
160
- private BasicDBList getClauses (AggregationOperationContext context , String key , Object predicate ) {
162
+ private List < Object > getClauses (AggregationOperationContext context , String key , Object predicate ) {
161
163
162
- BasicDBList clauses = new BasicDBList ();
164
+ List < Object > clauses = new ArrayList < Object > ();
163
165
164
- if (predicate instanceof BasicDBList ) {
166
+ if (predicate instanceof List ) {
165
167
166
- BasicDBList args = new BasicDBList ();
167
- for (Object clause : (BasicDBList ) predicate ) {
168
- args .addAll (getClauses (context , (BasicDBObject ) clause ));
168
+ List <Object > args = new ArrayList <Object >();
169
+ for (Object clause : (List <?>) predicate ) {
170
+ if (clause instanceof DBObject ) {
171
+ args .addAll (getClauses (context , (DBObject ) clause ));
172
+ }
169
173
}
170
174
171
175
clauses .add (new BasicDBObject (key , args ));
@@ -180,15 +184,15 @@ private BasicDBList getClauses(AggregationOperationContext context, String key,
180
184
continue ;
181
185
}
182
186
183
- BasicDBList args = new BasicDBList ();
187
+ List < Object > args = new ArrayList < Object > ();
184
188
args .add ("$" + key );
185
189
args .add (nested .get (s ));
186
190
clauses .add (new BasicDBObject (s , args ));
187
191
}
188
192
189
193
} else if (!isKeyword (key )) {
190
194
191
- BasicDBList args = new BasicDBList ();
195
+ List < Object > args = new ArrayList < Object > ();
192
196
args .add ("$" + key );
193
197
args .add (predicate );
194
198
clauses .add (new BasicDBObject ("$eq" , args ));
@@ -230,6 +234,9 @@ public static ConditionalExpressionBuilder newBuilder() {
230
234
return ConditionalExpressionBuilder .newBuilder ();
231
235
}
232
236
237
+ /**
238
+ * @since 1.10
239
+ */
233
240
public static interface WhenBuilder {
234
241
235
242
/**
@@ -257,6 +264,9 @@ public static interface WhenBuilder {
257
264
ThenBuilder when (CriteriaDefinition criteria );
258
265
}
259
266
267
+ /**
268
+ * @since 1.10
269
+ */
260
270
public static interface ThenBuilder {
261
271
262
272
/**
@@ -268,6 +278,9 @@ public static interface ThenBuilder {
268
278
OtherwiseBuilder then (Object value );
269
279
}
270
280
281
+ /**
282
+ * @since 1.10
283
+ */
271
284
public static interface OtherwiseBuilder {
272
285
273
286
/**
@@ -314,7 +327,7 @@ public ConditionalExpressionBuilder when(DBObject booleanExpression) {
314
327
return this ;
315
328
}
316
329
317
- /*
330
+ /*
318
331
* (non-Javadoc)
319
332
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.query.CriteriaDefinition)
320
333
*/
@@ -327,7 +340,7 @@ public ThenBuilder when(CriteriaDefinition criteria) {
327
340
return this ;
328
341
}
329
342
330
- /*
343
+ /*
331
344
* (non-Javadoc)
332
345
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.aggregation.Field)
333
346
*/
@@ -340,7 +353,7 @@ public ThenBuilder when(Field booleanField) {
340
353
return this ;
341
354
}
342
355
343
- /*
356
+ /*
344
357
* (non-Javadoc)
345
358
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(java.lang.String)
346
359
*/
@@ -353,7 +366,7 @@ public ThenBuilder when(String booleanField) {
353
366
return this ;
354
367
}
355
368
356
- /*
369
+ /*
357
370
* (non-Javadoc)
358
371
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.ThenBuilder#then(java.lang.Object)
359
372
*/
@@ -366,7 +379,7 @@ public OtherwiseBuilder then(Object thenValue) {
366
379
return this ;
367
380
}
368
381
369
- /*
382
+ /*
370
383
* (non-Javadoc)
371
384
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.OtherwiseBuilder#otherwise(java.lang.Object)
372
385
*/
0 commit comments