@ -22,19 +22,19 @@ import com.mongodb.BasicDBObject;
@@ -22,19 +22,19 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject ;
/ * *
* Encapsulates the aggregation framework { @code $lookup } - operation .
* We recommend to use the static factory method { @link Aggregation # lookup ( String , String , String , String ) } instead of
* creating instances of this class directly .
* Encapsulates the aggregation framework { @code $lookup } - operation . We recommend to use the static factory method
* { @link Aggregation # lookup ( String , String , String , String ) } instead of creating instances of this class directly .
*
* @author Alessio Fachechi
* @author Christoph Strobl
* @see http : //docs.mongodb.org/manual/reference/aggregation/lookup/#stage._S_lookup
* @since 1 . 9
* /
public class LookupOperation implements Additional FieldsExposingAggregationOperation {
public class LookupOperation implements FieldsExposingAggregationOperation {
private Exposed Field from ;
private Exposed Field localField ;
private Exposed Field foreignField ;
private Field from ;
private Field localField ;
private Field foreignField ;
private ExposedField as ;
/ * *
@ -46,24 +46,38 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
@@ -46,24 +46,38 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
* @param as must not be { @literal null } .
* /
public LookupOperation ( Field from , Field localField , Field foreignField , Field as ) {
Assert . notNull ( from , "From must not be null!" ) ;
Assert . notNull ( localField , "LocalField must not be null!" ) ;
Assert . notNull ( foreignField , "ForeignField must not be null!" ) ;
Assert . notNull ( as , "As must not be null!" ) ;
this . from = new ExposedField ( from , true ) ;
this . localField = new ExposedField ( localField , true ) ;
this . foreignField = new ExposedField ( foreignField , true ) ;
this . from = from ;
this . localField = localField ;
this . foreignField = foreignField ;
this . as = new ExposedField ( as , true ) ;
}
private LookupOperation ( ) {
// used by builder
}
/ *
* ( non - Javadoc )
* @see org . springframework . data . mongodb . core . aggregation . FieldsExposingAggregationOperation # getFields ( )
* /
@Override
public ExposedFields getFields ( ) {
return ExposedFields . from ( as ) ;
}
/ *
* ( non - Javadoc )
* @see org . springframework . data . mongodb . core . aggregation . AggregationOperation # toDBObject ( org . springframework . data . mongodb . core . aggregation . AggregationOperationContext )
* /
@Override
public DBObject toDBObject ( AggregationOperationContext context ) {
BasicDBObject lookupObject = new BasicDBObject ( ) ;
lookupObject . append ( "from" , from . getTarget ( ) ) ;
@ -73,4 +87,106 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
@@ -73,4 +87,106 @@ public class LookupOperation implements AdditionalFieldsExposingAggregationOpera
return new BasicDBObject ( "$lookup" , lookupObject ) ;
}
/ * *
* Get a builder that allows creation of { @link LookupOperation } .
*
* @return
* /
public static FromBuilder newLookup ( ) {
return new LookupOperationBuilder ( ) ;
}
public static interface FromBuilder {
/ * *
* @param name
* @return
* /
LocalFieldBuilder from ( String name ) ;
}
public static interface LocalFieldBuilder {
/ * *
* @param name
* @return
* /
ForeignFieldBuilder localField ( String name ) ;
}
public static interface ForeignFieldBuilder {
/ * *
* @param name
* @return
* /
AsBuilder foreignField ( String name ) ;
}
public static interface AsBuilder {
/ * *
* @param name
* @return
* /
LookupOperation as ( String name ) ;
}
/ * *
* Builder for fluent { @link LookupOperation } creation .
*
* @author Christoph Strobl
* @since 1 . 9
* /
public static final class LookupOperationBuilder
implements FromBuilder , LocalFieldBuilder , ForeignFieldBuilder , AsBuilder {
private LookupOperation lookupOperation ;
private LookupOperationBuilder ( ) {
this . lookupOperation = new LookupOperation ( ) ;
}
/ * *
* Creates new builder for { @link LookupOperation } .
*
* @return never { @literal null } .
* /
public static FromBuilder newBuilder ( ) {
return new LookupOperationBuilder ( ) ;
}
@Override
public LocalFieldBuilder from ( String name ) {
Assert . hasText ( name , "'From' must not be null or empty!" ) ;
lookupOperation . from = Fields . field ( name ) ;
return this ;
}
@Override
public LookupOperation as ( String name ) {
Assert . hasText ( name , "'As' must not be null or empty!" ) ;
lookupOperation . as = new ExposedField ( Fields . field ( name ) , true ) ;
return null ;
}
@Override
public AsBuilder foreignField ( String name ) {
Assert . hasText ( name , "'ForeignField' must not be null or empty!" ) ;
lookupOperation . foreignField = Fields . field ( name ) ;
return this ;
}
@Override
public ForeignFieldBuilder localField ( String name ) {
Assert . hasText ( name , "'LocalField' must not be null or empty!" ) ;
lookupOperation . localField = Fields . field ( name ) ;
return this ;
}
}
}