|
21 | 21 |
|
22 | 22 | import java.util.Collection;
|
23 | 23 | import java.util.Map;
|
| 24 | +import java.util.Optional; |
24 | 25 |
|
25 | 26 | import org.bson.Document;
|
| 27 | + |
26 | 28 | import org.springframework.core.convert.ConversionService;
|
27 | 29 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
28 | 30 | import org.springframework.data.mapping.IdentifierAccessor;
|
29 | 31 | import org.springframework.data.mapping.MappingException;
|
| 32 | +import org.springframework.data.mapping.PersistentEntity; |
30 | 33 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
31 | 34 | import org.springframework.data.mapping.context.MappingContext;
|
32 | 35 | import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
33 | 36 | import org.springframework.data.mongodb.core.convert.MongoWriter;
|
34 | 37 | import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
35 | 38 | import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
36 | 39 | import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
|
| 40 | +import org.springframework.data.mongodb.core.query.Collation; |
37 | 41 | import org.springframework.data.mongodb.core.query.Criteria;
|
38 | 42 | import org.springframework.data.mongodb.core.query.Query;
|
39 | 43 | import org.springframework.lang.Nullable;
|
@@ -176,6 +180,20 @@ private static Document parse(String source) {
|
176 | 180 | }
|
177 | 181 | }
|
178 | 182 |
|
| 183 | + public <T> TypedOperations<T> forType(@Nullable Class<T> entityClass) { |
| 184 | + |
| 185 | + if (entityClass != null) { |
| 186 | + |
| 187 | + MongoPersistentEntity<?> entity = context.getPersistentEntity(entityClass); |
| 188 | + |
| 189 | + if (entity != null) { |
| 190 | + return new TypedEntityOperations(entity); |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + return UntypedOperations.instance(); |
| 195 | + } |
| 196 | + |
179 | 197 | /**
|
180 | 198 | * A representation of information about an entity.
|
181 | 199 | *
|
@@ -263,7 +281,7 @@ default boolean isVersionedEntity() {
|
263 | 281 |
|
264 | 282 | /**
|
265 | 283 | * Returns whether the entity is considered to be new.
|
266 |
| - * |
| 284 | + * |
267 | 285 | * @return
|
268 | 286 | * @since 2.1.2
|
269 | 287 | */
|
@@ -414,7 +432,7 @@ public T getBean() {
|
414 | 432 | return map;
|
415 | 433 | }
|
416 | 434 |
|
417 |
| - /* |
| 435 | + /* |
418 | 436 | * (non-Javadoc)
|
419 | 437 | * @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
|
420 | 438 | */
|
@@ -585,7 +603,7 @@ public T getBean() {
|
585 | 603 | return propertyAccessor.getBean();
|
586 | 604 | }
|
587 | 605 |
|
588 |
| - /* |
| 606 | + /* |
589 | 607 | * (non-Javadoc)
|
590 | 608 | * @see org.springframework.data.mongodb.core.EntityOperations.Entity#isNew()
|
591 | 609 | */
|
@@ -698,4 +716,102 @@ public T incrementVersion() {
|
698 | 716 | return propertyAccessor.getBean();
|
699 | 717 | }
|
700 | 718 | }
|
| 719 | + |
| 720 | + /** |
| 721 | + * Type-specific operations abstraction. |
| 722 | + * |
| 723 | + * @author Mark Paluch |
| 724 | + * @param <T> |
| 725 | + * @since 2.2 |
| 726 | + */ |
| 727 | + interface TypedOperations<T> { |
| 728 | + |
| 729 | + /** |
| 730 | + * Return the optional {@link Collation} for the underlying entity. |
| 731 | + * |
| 732 | + * @return |
| 733 | + */ |
| 734 | + Optional<Collation> getCollation(); |
| 735 | + |
| 736 | + /** |
| 737 | + * Return the optional {@link Collation} from the given {@link Query} and fall back to the collation configured for |
| 738 | + * the underlying entity. |
| 739 | + * |
| 740 | + * @return |
| 741 | + */ |
| 742 | + Optional<Collation> getCollation(Query query); |
| 743 | + } |
| 744 | + |
| 745 | + /** |
| 746 | + * {@link TypedOperations} for generic entities that are not represented with {@link PersistentEntity} (e.g. custom |
| 747 | + * conversions). |
| 748 | + */ |
| 749 | + @RequiredArgsConstructor |
| 750 | + enum UntypedOperations implements TypedOperations<Object> { |
| 751 | + |
| 752 | + INSTANCE; |
| 753 | + |
| 754 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 755 | + public static <T> TypedOperations<T> instance() { |
| 756 | + return (TypedOperations) INSTANCE; |
| 757 | + } |
| 758 | + |
| 759 | + /* |
| 760 | + * (non-Javadoc) |
| 761 | + * @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation() |
| 762 | + */ |
| 763 | + @Override |
| 764 | + public Optional<Collation> getCollation() { |
| 765 | + return Optional.empty(); |
| 766 | + } |
| 767 | + |
| 768 | + /* |
| 769 | + * (non-Javadoc) |
| 770 | + * @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query) |
| 771 | + */ |
| 772 | + @Override |
| 773 | + public Optional<Collation> getCollation(Query query) { |
| 774 | + |
| 775 | + if (query == null) { |
| 776 | + return Optional.empty(); |
| 777 | + } |
| 778 | + |
| 779 | + return query.getCollation(); |
| 780 | + } |
| 781 | + } |
| 782 | + |
| 783 | + /** |
| 784 | + * {@link TypedOperations} backed by {@link MongoPersistentEntity}. |
| 785 | + * |
| 786 | + * @param <T> |
| 787 | + */ |
| 788 | + @RequiredArgsConstructor |
| 789 | + static class TypedEntityOperations<T> implements TypedOperations<T> { |
| 790 | + |
| 791 | + private final @NonNull MongoPersistentEntity<T> entity; |
| 792 | + |
| 793 | + /* |
| 794 | + * (non-Javadoc) |
| 795 | + * @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation() |
| 796 | + */ |
| 797 | + @Override |
| 798 | + public Optional<Collation> getCollation() { |
| 799 | + return Optional.ofNullable(entity.getCollation()); |
| 800 | + } |
| 801 | + |
| 802 | + /* |
| 803 | + * (non-Javadoc) |
| 804 | + * @see org.springframework.data.mongodb.core.EntityOperations.TypedOperations#getCollation(org.springframework.data.mongodb.core.query.Query) |
| 805 | + */ |
| 806 | + @Override |
| 807 | + public Optional<Collation> getCollation(Query query) { |
| 808 | + |
| 809 | + if (query.getCollation().isPresent()) { |
| 810 | + return query.getCollation(); |
| 811 | + } |
| 812 | + |
| 813 | + return Optional.ofNullable(entity.getCollation()); |
| 814 | + } |
| 815 | + } |
| 816 | + |
701 | 817 | }
|
0 commit comments