Skip to content

Commit 323b0a8

Browse files
committed
DATAMONGO-1992 - Extract common entity operations API from (Reactive)MongoTemplate.
Introduced EntityOperations and MappedDocument to allow to share common operations from MongoTemplate and ReactiveMongoTemplate.
1 parent d1b1dfb commit 323b0a8

17 files changed

+1138
-645
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java

+629
Large diffs are not rendered by default.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableAggregationOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ private String getCollectionName(Aggregation aggregation) {
119119
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
120120

121121
if (typedAggregation.getInputType() != null) {
122-
return template.determineCollectionName(typedAggregation.getInputType());
122+
return template.getCollectionName(typedAggregation.getInputType());
123123
}
124124
}
125125

126-
return template.determineCollectionName(domainType);
126+
return template.getCollectionName(domainType);
127127
}
128128
}
129129
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private CursorPreparer getCursorPreparer(Query query, @Nullable CursorPreparer p
230230
}
231231

232232
private String getCollectionName() {
233-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
233+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
234234
}
235235

236236
private String asString() {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public TerminatingBulkInsert<T> withBulkMode(BulkMode bulkMode) {
129129
}
130130

131131
private String getCollectionName() {
132-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
132+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
133133
}
134134
}
135135
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableMapReduceOperationSupport.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import java.util.List;
19-
2018
import lombok.NonNull;
2119
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.List;
22+
2223
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
2324
import org.springframework.data.mongodb.core.query.Query;
2425
import org.springframework.lang.Nullable;
@@ -67,8 +68,9 @@ static class ExecutableMapReduceSupport<T>
6768
private final @Nullable String reduceFunction;
6869
private final @Nullable MapReduceOptions options;
6970

70-
ExecutableMapReduceSupport(MongoTemplate template, Class<?> domainType, Class<T> returnType, @Nullable String collection,
71-
Query query, @Nullable String mapFunction, @Nullable String reduceFunction, @Nullable MapReduceOptions options) {
71+
ExecutableMapReduceSupport(MongoTemplate template, Class<?> domainType, Class<T> returnType,
72+
@Nullable String collection, Query query, @Nullable String mapFunction, @Nullable String reduceFunction,
73+
@Nullable MapReduceOptions options) {
7274

7375
this.template = template;
7476
this.domainType = domainType;
@@ -169,7 +171,7 @@ public ExecutableMapReduce<T> reduce(String reduceFunction) {
169171
}
170172

171173
private String getCollectionName() {
172-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
174+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
173175
}
174176
}
175177
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableRemoveOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public List<T> findAndRemove() {
123123
}
124124

125125
private String getCollectionName() {
126-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
126+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
127127
}
128128
}
129129
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private UpdateResult doUpdate(boolean multi, boolean upsert) {
221221
}
222222

223223
private String getCollectionName() {
224-
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
224+
return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
225225
}
226226
}
227227
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import lombok.Getter;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.Collection;
22+
import java.util.List;
23+
24+
import org.bson.Document;
25+
import org.bson.conversions.Bson;
26+
import org.springframework.data.mongodb.core.query.Update;
27+
import org.springframework.data.util.StreamUtils;
28+
29+
import com.mongodb.client.model.Filters;
30+
31+
/**
32+
* A MongoDB document in its mapped state. I.e. after a source document has been mapped using mapping information of the
33+
* entity the source document was supposed to represent.
34+
*
35+
* @author Oliver Gierke
36+
* @since 2.1
37+
*/
38+
@RequiredArgsConstructor(staticName = "of")
39+
public class MappedDocument {
40+
41+
private static final String ID_FIELD = "_id";
42+
private static final Document ID_ONLY_PROJECTION = new Document(ID_FIELD, 1);
43+
44+
private final @Getter Document document;
45+
46+
public static Document getIdOnlyProjection() {
47+
return ID_ONLY_PROJECTION;
48+
}
49+
50+
public static Document getIdIn(Collection<?> ids) {
51+
return new Document(ID_FIELD, new Document("$in", ids));
52+
}
53+
54+
public static List<Object> toIds(Collection<Document> documents) {
55+
56+
return documents.stream()//
57+
.map(it -> it.get(ID_FIELD))//
58+
.collect(StreamUtils.toUnmodifiableList());
59+
}
60+
61+
public boolean hasId() {
62+
return document.containsKey(ID_FIELD);
63+
}
64+
65+
public boolean hasNonNullId() {
66+
return hasId() && document.get(ID_FIELD) != null;
67+
}
68+
69+
public Object getId() {
70+
return document.get(ID_FIELD);
71+
}
72+
73+
public <T> T getId(Class<T> type) {
74+
return document.get(ID_FIELD, type);
75+
}
76+
77+
public boolean isIdPresent(Class<?> type) {
78+
return type.isInstance(getId());
79+
}
80+
81+
public Bson getIdFilter() {
82+
return Filters.eq(ID_FIELD, document.get(ID_FIELD));
83+
}
84+
85+
public Update updateWithoutId() {
86+
return Update.fromDocument(document, ID_FIELD);
87+
}
88+
}

0 commit comments

Comments
 (0)