|
| 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