22
22
import com .mongodb .DBObject ;
23
23
24
24
/**
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.
28
27
*
29
28
* @author Alessio Fachechi
29
+ * @author Christoph Strobl
30
30
* @see http://docs.mongodb.org/manual/reference/aggregation/lookup/#stage._S_lookup
31
31
* @since 1.9
32
32
*/
33
- public class LookupOperation implements AdditionalFieldsExposingAggregationOperation {
33
+ public class LookupOperation implements FieldsExposingAggregationOperation {
34
34
35
- private ExposedField from ;
36
- private ExposedField localField ;
37
- private ExposedField foreignField ;
35
+ private Field from ;
36
+ private Field localField ;
37
+ private Field foreignField ;
38
38
private ExposedField as ;
39
39
40
40
/**
@@ -46,24 +46,38 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
46
46
* @param as must not be {@literal null}.
47
47
*/
48
48
public LookupOperation (Field from , Field localField , Field foreignField , Field as ) {
49
+
49
50
Assert .notNull (from , "From must not be null!" );
50
51
Assert .notNull (localField , "LocalField must not be null!" );
51
52
Assert .notNull (foreignField , "ForeignField must not be null!" );
52
53
Assert .notNull (as , "As must not be null!" );
53
54
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 ;
57
58
this .as = new ExposedField (as , true );
58
59
}
59
60
61
+ private LookupOperation () {
62
+ // used by builder
63
+ }
64
+
65
+ /*
66
+ * (non-Javadoc)
67
+ * @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
68
+ */
60
69
@ Override
61
70
public ExposedFields getFields () {
62
71
return ExposedFields .from (as );
63
72
}
64
73
74
+ /*
75
+ * (non-Javadoc)
76
+ * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
77
+ */
65
78
@ Override
66
79
public DBObject toDBObject (AggregationOperationContext context ) {
80
+
67
81
BasicDBObject lookupObject = new BasicDBObject ();
68
82
69
83
lookupObject .append ("from" , from .getTarget ());
@@ -73,4 +87,106 @@ public DBObject toDBObject(AggregationOperationContext context) {
73
87
74
88
return new BasicDBObject ("$lookup" , lookupObject );
75
89
}
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
+ }
76
192
}
0 commit comments