|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * https://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.convert; |
| 17 | + |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Map.Entry; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +import org.bson.Document; |
| 26 | +import org.springframework.core.convert.ConversionService; |
| 27 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 28 | +import org.springframework.data.mapping.context.MappingContext; |
| 29 | +import org.springframework.data.mapping.model.BeanWrapperPropertyAccessorFactory; |
| 30 | +import org.springframework.data.mongodb.core.mapping.DocumentPointer; |
| 31 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
| 32 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Christoph Strobl |
| 36 | + * @since 3.3 |
| 37 | + */ |
| 38 | +class DocumentPointerFactory { |
| 39 | + |
| 40 | + private ConversionService conversionService; |
| 41 | + private MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext; |
| 42 | + private Map<String, LinkageDocument> linkageMap; |
| 43 | + |
| 44 | + public DocumentPointerFactory(ConversionService conversionService, |
| 45 | + MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) { |
| 46 | + |
| 47 | + this.conversionService = conversionService; |
| 48 | + this.mappingContext = mappingContext; |
| 49 | + this.linkageMap = new HashMap<>(); |
| 50 | + } |
| 51 | + |
| 52 | + public DocumentPointer<?> computePointer(MongoPersistentProperty property, Object value, Class<?> typeHint) { |
| 53 | + |
| 54 | + if (value instanceof LazyLoadingProxy) { |
| 55 | + return () -> ((LazyLoadingProxy) value).getSource(); |
| 56 | + } |
| 57 | + |
| 58 | + if (conversionService.canConvert(typeHint, DocumentPointer.class)) { |
| 59 | + return conversionService.convert(value, DocumentPointer.class); |
| 60 | + } else { |
| 61 | + |
| 62 | + MongoPersistentEntity<?> persistentEntity = mappingContext |
| 63 | + .getPersistentEntity(property.getAssociationTargetType()); |
| 64 | + |
| 65 | + if (!property.getDocumentReference().lookup().toLowerCase().replaceAll("\\s", "").replaceAll("'", "") |
| 66 | + .equals("{_id:?#{#target}}")) { |
| 67 | + |
| 68 | + return () -> linkageMap.computeIfAbsent(property.getDocumentReference().lookup(), key -> { |
| 69 | + return new LinkageDocument(key); |
| 70 | + }).get(persistentEntity, |
| 71 | + BeanWrapperPropertyAccessorFactory.INSTANCE.getPropertyAccessor(property.getOwner(), value)); |
| 72 | + } |
| 73 | + |
| 74 | + // just take the id as a reference |
| 75 | + return () -> persistentEntity.getIdentifierAccessor(value).getIdentifier(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static class LinkageDocument { |
| 80 | + |
| 81 | + String lookup; |
| 82 | + org.bson.Document fetchDocument; |
| 83 | + Map<Integer, String> mapMap; |
| 84 | + |
| 85 | + public LinkageDocument(String lookup) { |
| 86 | + |
| 87 | + this.lookup = lookup; |
| 88 | + String targetLookup = lookup; |
| 89 | + |
| 90 | + Pattern pattern = Pattern.compile("\\?#\\{#?[\\w\\d]*\\}"); |
| 91 | + |
| 92 | + Matcher matcher = pattern.matcher(lookup); |
| 93 | + int index = 0; |
| 94 | + mapMap = new LinkedHashMap<>(); |
| 95 | + while (matcher.find()) { |
| 96 | + |
| 97 | + String expr = matcher.group(); |
| 98 | + mapMap.put(Integer.valueOf(index), expr.substring(0, expr.length() - 1).replace("?#{#", "").replace("?#{", "") |
| 99 | + .replace("target.", "").replaceAll("'", "")); |
| 100 | + targetLookup = targetLookup.replace(expr, index + ""); |
| 101 | + index++; |
| 102 | + } |
| 103 | + |
| 104 | + fetchDocument = org.bson.Document.parse(targetLookup); |
| 105 | + } |
| 106 | + |
| 107 | + org.bson.Document get(MongoPersistentEntity<?> persistentEntity, PersistentPropertyAccessor<?> propertyAccessor) { |
| 108 | + |
| 109 | + org.bson.Document targetDocument = new Document(); |
| 110 | + |
| 111 | + // TODO: recursive matching over nested Documents or would the parameter binding json parser be a thing? |
| 112 | + // like we have it ordered by index values and could provide the parameter array from it. |
| 113 | + |
| 114 | + for (Entry<String, Object> entry : fetchDocument.entrySet()) { |
| 115 | + |
| 116 | + if (entry.getKey().equals("target")) { |
| 117 | + |
| 118 | + String refKey = mapMap.get(entry.getValue()); |
| 119 | + |
| 120 | + if (persistentEntity.hasIdProperty()) { |
| 121 | + targetDocument.put(refKey, propertyAccessor.getProperty(persistentEntity.getIdProperty())); |
| 122 | + } else { |
| 123 | + targetDocument.put(refKey, propertyAccessor.getBean()); |
| 124 | + } |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + Object target = propertyAccessor.getProperty(persistentEntity.getPersistentProperty(entry.getKey())); |
| 129 | + String refKey = mapMap.get(entry.getValue()); |
| 130 | + targetDocument.put(refKey, target); |
| 131 | + } |
| 132 | + return targetDocument; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments