27
27
import org .springframework .data .mongodb .core .aggregation .ExposedFields .DirectFieldReference ;
28
28
import org .springframework .data .mongodb .core .aggregation .ExposedFields .ExposedField ;
29
29
import org .springframework .data .mongodb .core .aggregation .ExposedFields .FieldReference ;
30
+ import org .springframework .data .mongodb .core .aggregation .FacetOperation .FacetOperationBuilder ;
30
31
import org .springframework .data .mongodb .core .aggregation .FieldsExposingAggregationOperation .InheritsFieldsAggregationOperation ;
31
32
import org .springframework .data .mongodb .core .aggregation .GraphLookupOperation .StartWithBuilder ;
32
33
import org .springframework .data .mongodb .core .aggregation .ReplaceRootOperation .ReplaceRootDocumentOperationBuilder ;
@@ -68,7 +69,7 @@ public class Aggregation {
68
69
*/
69
70
public static final String CURRENT = SystemVariable .CURRENT .toString ();
70
71
71
- public static final AggregationOperationContext DEFAULT_CONTEXT = new NoOpAggregationOperationContext () ;
72
+ public static final AggregationOperationContext DEFAULT_CONTEXT = AggregationOperationRenderer . DEFAULT_CONTEXT ;
72
73
public static final AggregationOptions DEFAULT_OPTIONS = newAggregationOptions ().build ();
73
74
74
75
protected final List <AggregationOperation > operations ;
@@ -460,6 +461,25 @@ public static BucketAutoOperation bucketAuto(AggregationExpression groupByExpres
460
461
return new BucketAutoOperation (groupByExpression , buckets );
461
462
}
462
463
464
+ /**
465
+ * Creates a new {@link FacetOperation}.
466
+ *
467
+ * @return
468
+ */
469
+ public static FacetOperation facet () {
470
+ return FacetOperation .EMPTY ;
471
+ }
472
+
473
+ /**
474
+ * Creates a new {@link FacetOperationBuilder} given {@link Aggregation}.
475
+ *
476
+ * @param aggregationOperations the sub-pipeline, must not be {@literal null}.
477
+ * @return
478
+ */
479
+ public static FacetOperationBuilder facet (AggregationOperation ... aggregationOperations ) {
480
+ return facet ().and (aggregationOperations );
481
+ }
482
+
463
483
/**
464
484
* Creates a new {@link LookupOperation}.
465
485
*
@@ -551,26 +571,7 @@ public static AggregationOptions.Builder newAggregationOptions() {
551
571
*/
552
572
public DBObject toDbObject (String inputCollectionName , AggregationOperationContext rootContext ) {
553
573
554
- AggregationOperationContext context = rootContext ;
555
- List <DBObject > operationDocuments = new ArrayList <DBObject >(operations .size ());
556
-
557
- for (AggregationOperation operation : operations ) {
558
-
559
- operationDocuments .add (operation .toDBObject (context ));
560
-
561
- if (operation instanceof FieldsExposingAggregationOperation ) {
562
-
563
- FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation ) operation ;
564
- ExposedFields fields = exposedFieldsOperation .getFields ();
565
-
566
- if (operation instanceof InheritsFieldsAggregationOperation ) {
567
- context = new InheritingExposedFieldsAggregationOperationContext (fields , context );
568
- } else {
569
- context = fields .exposesNoFields () ? DEFAULT_CONTEXT
570
- : new ExposedFieldsAggregationOperationContext (fields , context );
571
- }
572
- }
573
- }
574
+ List <DBObject > operationDocuments = AggregationOperationRenderer .toDBObject (operations , rootContext );
574
575
575
576
DBObject command = new BasicDBObject ("aggregate" , inputCollectionName );
576
577
command .put ("pipeline" , operationDocuments );
@@ -586,43 +587,7 @@ public DBObject toDbObject(String inputCollectionName, AggregationOperationConte
586
587
*/
587
588
@ Override
588
589
public String toString () {
589
- return SerializationUtils
590
- .serializeToJsonSafely (toDbObject ("__collection__" , new NoOpAggregationOperationContext ()));
591
- }
592
-
593
- /**
594
- * Simple {@link AggregationOperationContext} that just returns {@link FieldReference}s as is.
595
- *
596
- * @author Oliver Gierke
597
- */
598
- private static class NoOpAggregationOperationContext implements AggregationOperationContext {
599
-
600
- /*
601
- * (non-Javadoc)
602
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getMappedObject(com.mongodb.DBObject)
603
- */
604
- @ Override
605
- public DBObject getMappedObject (DBObject dbObject ) {
606
- return dbObject ;
607
- }
608
-
609
- /*
610
- * (non-Javadoc)
611
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getReference(org.springframework.data.mongodb.core.aggregation.ExposedFields.AvailableField)
612
- */
613
- @ Override
614
- public FieldReference getReference (Field field ) {
615
- return new DirectFieldReference (new ExposedField (field , true ));
616
- }
617
-
618
- /*
619
- * (non-Javadoc)
620
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getReference(java.lang.String)
621
- */
622
- @ Override
623
- public FieldReference getReference (String name ) {
624
- return new DirectFieldReference (new ExposedField (new AggregationField (name ), true ));
625
- }
590
+ return SerializationUtils .serializeToJsonSafely (toDbObject ("__collection__" , DEFAULT_CONTEXT ));
626
591
}
627
592
628
593
/**
@@ -662,7 +627,7 @@ public static boolean isReferingToSystemVariable(String fieldRef) {
662
627
return false ;
663
628
}
664
629
665
- /*
630
+ /*
666
631
* (non-Javadoc)
667
632
* @see java.lang.Enum#toString()
668
633
*/
0 commit comments