27
27
import java .util .Map ;
28
28
import java .util .Set ;
29
29
import java .util .concurrent .TimeUnit ;
30
- import java .util .function .Supplier ;
31
30
import java .util .stream .Collectors ;
32
31
33
32
import org .apache .commons .logging .Log ;
34
33
import org .apache .commons .logging .LogFactory ;
35
34
import org .jspecify .annotations .Nullable ;
36
35
import org .springframework .core .annotation .MergedAnnotation ;
36
+ import org .springframework .core .env .StandardEnvironment ;
37
37
import org .springframework .dao .InvalidDataAccessApiUsageException ;
38
38
import org .springframework .data .domain .Sort ;
39
+ import org .springframework .data .expression .ValueEvaluationContext ;
39
40
import org .springframework .data .mapping .Association ;
40
41
import org .springframework .data .mapping .AssociationHandler ;
41
42
import org .springframework .data .mapping .MappingException ;
77
78
* @author Mark Paluch
78
79
* @author Dave Perryman
79
80
* @author Stefan Tirea
81
+ * @author Sangbeen Moon
80
82
* @since 1.5
81
83
*/
82
84
public class MongoPersistentEntityIndexResolver implements IndexResolver {
@@ -492,7 +494,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
492
494
return new org .bson .Document (dotPath , 1 );
493
495
}
494
496
495
- Object keyDefToUse = ExpressionUtils .evaluate (keyDefinitionString , () -> getEvaluationContextForProperty (entity ));
497
+ Object keyDefToUse = ExpressionUtils .evaluate (keyDefinitionString , () -> getValueEvaluationContextForProperty (entity ));
496
498
497
499
org .bson .Document dbo = (keyDefToUse instanceof org .bson .Document document ) ? document
498
500
: org .bson .Document .parse (ObjectUtils .nullSafeToString (keyDefToUse ));
@@ -560,7 +562,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
560
562
}
561
563
562
564
Duration timeout = computeIndexTimeout (index .expireAfter (),
563
- () -> getEvaluationContextForProperty (persistentProperty .getOwner ()));
565
+ getValueEvaluationContextForProperty (persistentProperty .getOwner ()));
564
566
if (!timeout .isNegative ()) {
565
567
indexDefinition .expire (timeout );
566
568
}
@@ -576,7 +578,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
576
578
577
579
private PartialIndexFilter evaluatePartialFilter (String filterExpression , @ Nullable PersistentEntity <?, ?> entity ) {
578
580
579
- Object result = ExpressionUtils .evaluate (filterExpression , () -> getEvaluationContextForProperty (entity ));
581
+ Object result = ExpressionUtils .evaluate (filterExpression , () -> getValueEvaluationContextForProperty (entity ));
580
582
581
583
if (result instanceof org .bson .Document document ) {
582
584
return PartialIndexFilter .of (document );
@@ -587,7 +589,7 @@ private PartialIndexFilter evaluatePartialFilter(String filterExpression, @Nulla
587
589
588
590
private org .bson .Document evaluateWildcardProjection (String projectionExpression , @ Nullable PersistentEntity <?, ?> entity ) {
589
591
590
- Object result = ExpressionUtils .evaluate (projectionExpression , () -> getEvaluationContextForProperty (entity ));
592
+ Object result = ExpressionUtils .evaluate (projectionExpression , () -> getValueEvaluationContextForProperty (entity ));
591
593
592
594
if (result instanceof org .bson .Document document ) {
593
595
return document ;
@@ -598,7 +600,7 @@ private org.bson.Document evaluateWildcardProjection(String projectionExpression
598
600
599
601
private Collation evaluateCollation (String collationExpression , @ Nullable PersistentEntity <?, ?> entity ) {
600
602
601
- Object result = ExpressionUtils .evaluate (collationExpression , () -> getEvaluationContextForProperty (entity ));
603
+ Object result = ExpressionUtils .evaluate (collationExpression , () -> getValueEvaluationContextForProperty (entity ));
602
604
if (result instanceof org .bson .Document document ) {
603
605
return Collation .from (document );
604
606
}
@@ -649,24 +651,19 @@ protected EvaluationContext getEvaluationContext() {
649
651
}
650
652
651
653
/**
652
- * Get the {@link EvaluationContext } for a given {@link PersistentEntity entity} the default one.
654
+ * Get the {@link ValueEvaluationContext } for a given {@link PersistentEntity entity} the default one.
653
655
*
654
656
* @param persistentEntity can be {@literal null}
655
657
* @return
656
658
*/
657
- private EvaluationContext getEvaluationContextForProperty (@ Nullable PersistentEntity <?, ?> persistentEntity ) {
659
+ private ValueEvaluationContext getValueEvaluationContextForProperty (@ Nullable PersistentEntity <?, ?> persistentEntity ) {
658
660
659
- if (persistentEntity == null || !( persistentEntity instanceof BasicMongoPersistentEntity ) ) {
660
- return getEvaluationContext ( );
661
+ if (persistentEntity instanceof BasicMongoPersistentEntity <?> mongoEntity ) {
662
+ return mongoEntity . getValueEvaluationContext ( null );
661
663
}
662
664
663
- EvaluationContext contextFromEntity = ((BasicMongoPersistentEntity <?>) persistentEntity ).getEvaluationContext (null );
664
-
665
- if (contextFromEntity != null && !EvaluationContextProvider .DEFAULT .equals (contextFromEntity )) {
666
- return contextFromEntity ;
667
- }
668
-
669
- return getEvaluationContext ();
665
+ return ValueEvaluationContext .of (
666
+ new StandardEnvironment (), getEvaluationContext ());
670
667
}
671
668
672
669
/**
@@ -718,7 +715,7 @@ private String pathAwareIndexName(String indexName, String dotPath, @Nullable Pe
718
715
String nameToUse = "" ;
719
716
if (StringUtils .hasText (indexName )) {
720
717
721
- Object result = ExpressionUtils .evaluate (indexName , () -> getEvaluationContextForProperty (entity ));
718
+ Object result = ExpressionUtils .evaluate (indexName , () -> getValueEvaluationContextForProperty (entity ));
722
719
723
720
if (result != null ) {
724
721
nameToUse = ObjectUtils .nullSafeToString (result );
@@ -779,7 +776,7 @@ private void resolveAndAddIndexesForAssociation(Association<MongoPersistentPrope
779
776
* @since 2.2
780
777
* @throws IllegalArgumentException for invalid duration values.
781
778
*/
782
- private static Duration computeIndexTimeout (String timeoutValue , Supplier < EvaluationContext > evaluationContext ) {
779
+ private static Duration computeIndexTimeout (String timeoutValue , ValueEvaluationContext evaluationContext ) {
783
780
return DurationUtil .evaluate (timeoutValue , evaluationContext );
784
781
}
785
782
0 commit comments