Skip to content

Commit 4f1c1dd

Browse files
DATAMONGO-1326 - Polishing.
Updated javadoc and formatting. Added tests and avoid unnecessary code blocks. Original Pull Request: #344
1 parent 5c4d5bd commit 4f1c1dd

File tree

5 files changed

+193
-82
lines changed

5 files changed

+193
-82
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AdditionalFieldsExposingAggregationOperation.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,14 @@ public static MatchOperation match(Criteria criteria) {
272272
}
273273

274274
/**
275-
* Creates a new {@link LookupOperation} for the given fields.
275+
* Creates a new {@link LookupOperation}.
276276
*
277-
* @param fields must not be {@literal null}.
278-
* @return
277+
* @param from must not be {@literal null}.
278+
* @param localField must not be {@literal null}.
279+
* @param foreignField must not be {@literal null}.
280+
* @param as must not be {@literal null}.
281+
* @return never {@literal null}.
282+
* @since 1.9
279283
*/
280284
public static LookupOperation lookup(String from, String localField, String foreignField, String as) {
281285
return lookup(field(from), field(localField), field(foreignField), field(as));
@@ -284,8 +288,12 @@ public static LookupOperation lookup(String from, String localField, String fore
284288
/**
285289
* Creates a new {@link LookupOperation} for the given {@link Fields}.
286290
*
287-
* @param fields must not be {@literal null}.
288-
* @return
291+
* @param from must not be {@literal null}.
292+
* @param localField must not be {@literal null}.
293+
* @param foreignField must not be {@literal null}.
294+
* @param as must not be {@literal null}.
295+
* @return never {@literal null}.
296+
* @since 1.9
289297
*/
290298
public static LookupOperation lookup(Field from, Field localField, Field foreignField, Field as) {
291299
return new LookupOperation(from, localField, foreignField, as);
@@ -352,11 +360,9 @@ public DBObject toDbObject(String inputCollectionName, AggregationOperationConte
352360
operationDocuments.add(operation.toDBObject(context));
353361

354362
if (operation instanceof FieldsExposingAggregationOperation) {
355-
boolean additional = operation instanceof AdditionalFieldsExposingAggregationOperation ? true : false;
356363

357364
FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation;
358-
context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), rootContext,
359-
additional);
365+
context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), rootContext);
360366
}
361367
}
362368

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFieldsAggregationOperationContext.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
1919
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
20-
import org.springframework.data.mongodb.core.aggregation.Fields.AggregationField;
2120
import org.springframework.util.Assert;
22-
import org.springframework.util.StringUtils;
2321

2422
import com.mongodb.DBObject;
2523

@@ -29,14 +27,12 @@
2927
*
3028
* @author Thomas Darimont
3129
* @author Oliver Gierke
32-
* @author Alessio Fachechi
3330
* @since 1.4
3431
*/
3532
class ExposedFieldsAggregationOperationContext implements AggregationOperationContext {
3633

3734
private final ExposedFields exposedFields;
3835
private final AggregationOperationContext rootContext;
39-
private final boolean additional;
4036

4137
/**
4238
* Creates a new {@link ExposedFieldsAggregationOperationContext} from the given {@link ExposedFields}. Uses the given
@@ -46,28 +42,13 @@ class ExposedFieldsAggregationOperationContext implements AggregationOperationCo
4642
* @param rootContext must not be {@literal null}.
4743
*/
4844
public ExposedFieldsAggregationOperationContext(ExposedFields exposedFields,
49-
AggregationOperationContext rootContext) {
50-
this(exposedFields, rootContext, false);
51-
}
52-
53-
/**
54-
* Creates a new {@link ExposedFieldsAggregationOperationContext} from the given {@link ExposedFields}. Uses the given
55-
* {@link AggregationOperationContext} to perform a mapping to mongo types if necessary.
56-
*
57-
* @param exposedFields must not be {@literal null}.
58-
* @param rootContext must not be {@literal null}.
59-
* @param additional {@literal true} if the context exposes new fields in addition to the previous ones, e.g. in the
60-
* case of a lookup operation, {@literal false} otherwise.
61-
*/
62-
public ExposedFieldsAggregationOperationContext(ExposedFields exposedFields, AggregationOperationContext rootContext,
63-
boolean additional) {
45+
AggregationOperationContext rootContext) {
6446

6547
Assert.notNull(exposedFields, "ExposedFields must not be null!");
6648
Assert.notNull(rootContext, "RootContext must not be null!");
6749

6850
this.exposedFields = exposedFields;
6951
this.rootContext = rootContext;
70-
this.additional = additional;
7152
}
7253

7354
/*
@@ -132,16 +113,6 @@ private FieldReference getReference(Field field, String name) {
132113
}
133114
}
134115

135-
if (additional) {
136-
137-
// if no exposed fields found propagate to root context.
138-
if (field != null) {
139-
return rootContext.getReference(field);
140-
} else if (StringUtils.hasText(name)) {
141-
return rootContext.getReference(name);
142-
}
143-
}
144-
145116
throw new IllegalArgumentException(String.format("Invalid reference '%s'!", name));
146117
}
147118
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/LookupOperation.java

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
import com.mongodb.DBObject;
2323

2424
/**
25-
* Encapsulates the aggregation framework {@code $lookup}-operation.
26-
* We recommend to use the static factory method {@link Aggregation#lookup(String, String, String, String)} instead of
27-
* creating instances of this class directly.
25+
* Encapsulates the aggregation framework {@code $lookup}-operation. We recommend to use the static factory method
26+
* {@link Aggregation#lookup(String, String, String, String)} instead of creating instances of this class directly.
2827
*
2928
* @author Alessio Fachechi
29+
* @author Christoph Strobl
3030
* @see http://docs.mongodb.org/manual/reference/aggregation/lookup/#stage._S_lookup
3131
* @since 1.9
3232
*/
33-
public class LookupOperation implements AdditionalFieldsExposingAggregationOperation {
33+
public class LookupOperation implements FieldsExposingAggregationOperation {
3434

35-
private ExposedField from;
36-
private ExposedField localField;
37-
private ExposedField foreignField;
35+
private Field from;
36+
private Field localField;
37+
private Field foreignField;
3838
private ExposedField as;
3939

4040
/**
@@ -46,24 +46,38 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
4646
* @param as must not be {@literal null}.
4747
*/
4848
public LookupOperation(Field from, Field localField, Field foreignField, Field as) {
49+
4950
Assert.notNull(from, "From must not be null!");
5051
Assert.notNull(localField, "LocalField must not be null!");
5152
Assert.notNull(foreignField, "ForeignField must not be null!");
5253
Assert.notNull(as, "As must not be null!");
5354

54-
this.from = new ExposedField(from, true);
55-
this.localField = new ExposedField(localField, true);
56-
this.foreignField = new ExposedField(foreignField, true);
55+
this.from = from;
56+
this.localField = localField;
57+
this.foreignField = foreignField;
5758
this.as = new ExposedField(as, true);
5859
}
5960

61+
private LookupOperation() {
62+
// used by builder
63+
}
64+
65+
/*
66+
* (non-Javadoc)
67+
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
68+
*/
6069
@Override
6170
public ExposedFields getFields() {
6271
return ExposedFields.from(as);
6372
}
6473

74+
/*
75+
* (non-Javadoc)
76+
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
77+
*/
6578
@Override
6679
public DBObject toDBObject(AggregationOperationContext context) {
80+
6781
BasicDBObject lookupObject = new BasicDBObject();
6882

6983
lookupObject.append("from", from.getTarget());
@@ -73,4 +87,106 @@ public DBObject toDBObject(AggregationOperationContext context) {
7387

7488
return new BasicDBObject("$lookup", lookupObject);
7589
}
90+
91+
/**
92+
* Get a builder that allows creation of {@link LookupOperation}.
93+
*
94+
* @return
95+
*/
96+
public static FromBuilder newLookup() {
97+
return new LookupOperationBuilder();
98+
}
99+
100+
public static interface FromBuilder {
101+
102+
/**
103+
* @param name
104+
* @return
105+
*/
106+
LocalFieldBuilder from(String name);
107+
}
108+
109+
public static interface LocalFieldBuilder {
110+
111+
/**
112+
* @param name
113+
* @return
114+
*/
115+
ForeignFieldBuilder localField(String name);
116+
}
117+
118+
public static interface ForeignFieldBuilder {
119+
120+
/**
121+
* @param name
122+
* @return
123+
*/
124+
AsBuilder foreignField(String name);
125+
}
126+
127+
public static interface AsBuilder {
128+
129+
/**
130+
* @param name
131+
* @return
132+
*/
133+
LookupOperation as(String name);
134+
}
135+
136+
/**
137+
* Builder for fluent {@link LookupOperation} creation.
138+
*
139+
* @author Christoph Strobl
140+
* @since 1.9
141+
*/
142+
public static final class LookupOperationBuilder
143+
implements FromBuilder, LocalFieldBuilder, ForeignFieldBuilder, AsBuilder {
144+
145+
private LookupOperation lookupOperation;
146+
147+
private LookupOperationBuilder() {
148+
this.lookupOperation = new LookupOperation();
149+
}
150+
151+
/**
152+
* Creates new builder for {@link LookupOperation}.
153+
*
154+
* @return never {@literal null}.
155+
*/
156+
public static FromBuilder newBuilder() {
157+
return new LookupOperationBuilder();
158+
}
159+
160+
@Override
161+
public LocalFieldBuilder from(String name) {
162+
163+
Assert.hasText(name, "'From' must not be null or empty!");
164+
lookupOperation.from = Fields.field(name);
165+
return this;
166+
}
167+
168+
@Override
169+
public LookupOperation as(String name) {
170+
171+
Assert.hasText(name, "'As' must not be null or empty!");
172+
lookupOperation.as = new ExposedField(Fields.field(name), true);
173+
return null;
174+
}
175+
176+
@Override
177+
public AsBuilder foreignField(String name) {
178+
179+
Assert.hasText(name, "'ForeignField' must not be null or empty!");
180+
lookupOperation.foreignField = Fields.field(name);
181+
return this;
182+
}
183+
184+
@Override
185+
public ForeignFieldBuilder localField(String name) {
186+
187+
Assert.hasText(name, "'LocalField' must not be null or empty!");
188+
lookupOperation.localField = Fields.field(name);
189+
return this;
190+
}
191+
}
76192
}

0 commit comments

Comments
 (0)