|
17 | 17 |
|
18 | 18 | import java.lang.reflect.Constructor;
|
19 | 19 | import java.lang.reflect.Method;
|
20 |
| -import java.util.ArrayList; |
21 |
| -import java.util.Arrays; |
22 |
| -import java.util.Collection; |
23 |
| -import java.util.Collections; |
24 |
| -import java.util.HashSet; |
25 |
| -import java.util.LinkedHashMap; |
26 |
| -import java.util.List; |
27 |
| -import java.util.Map; |
| 20 | +import java.util.*; |
28 | 21 | import java.util.Map.Entry;
|
29 |
| -import java.util.Optional; |
30 |
| -import java.util.Set; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
31 | 24 | import java.util.stream.Collectors;
|
32 | 25 |
|
33 | 26 | import org.bson.Document;
|
|
52 | 45 | import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
53 | 46 | import org.springframework.data.mapping.callback.EntityCallbacks;
|
54 | 47 | import org.springframework.data.mapping.context.MappingContext;
|
| 48 | +import org.springframework.data.mapping.model.BeanWrapperPropertyAccessorFactory; |
55 | 49 | import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
56 | 50 | import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
|
57 | 51 | import org.springframework.data.mapping.model.EntityInstantiator;
|
@@ -814,8 +808,60 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
|
814 | 808 | if (conversionService.canConvert(valueType.getType(), DocumentPointer.class)) {
|
815 | 809 | accessor.put(prop, conversionService.convert(obj, DocumentPointer.class).getPointer());
|
816 | 810 | } else {
|
| 811 | + |
| 812 | + MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(prop.getAssociationTargetType()); |
| 813 | + // TODO: Move this to helper for queries |
| 814 | + |
| 815 | + |
| 816 | + if(!prop.getDocumentReference().lookup().toLowerCase().replaceAll("\\s", "").replaceAll("'","").equals("{_id:?#{#target}}")) { |
| 817 | + |
| 818 | + String lookup = prop.getDocumentReference().lookup(); |
| 819 | + String targetLookup = lookup; |
| 820 | + |
| 821 | + Pattern pattern = Pattern.compile("\\?#\\{#?[\\w\\d]*\\}"); |
| 822 | + |
| 823 | + Matcher matcher = pattern.matcher(lookup); |
| 824 | + int index = 0; |
| 825 | + Map<Integer, String> mapMap = new LinkedHashMap<>(); |
| 826 | + while(matcher.find()) { |
| 827 | + |
| 828 | + String expr = matcher.group(); |
| 829 | + mapMap.put(Integer.valueOf(index), expr.substring(0, expr.length()-1).replace("?#{#", "").replace("?#{", "").replace("target.","").replaceAll("'","")); |
| 830 | + targetLookup = targetLookup.replace(expr, index + ""); |
| 831 | + index++; |
| 832 | + } |
| 833 | + |
| 834 | + Document fetchDocument = Document.parse(targetLookup); |
| 835 | + |
| 836 | + PersistentPropertyAccessor<?> propertyAccessor = BeanWrapperPropertyAccessorFactory.INSTANCE.getPropertyAccessor(prop.getOwner(), obj); |
| 837 | + |
| 838 | + |
| 839 | + Document targetDocument = new Document(); |
| 840 | + for(Entry<String, Object> entry : fetchDocument.entrySet()) { |
| 841 | + |
| 842 | + if(entry.getKey().equals("target")) { |
| 843 | + |
| 844 | + String refKey = mapMap.get(entry.getValue()); |
| 845 | + |
| 846 | + if(persistentEntity.hasIdProperty()) { |
| 847 | + targetDocument.put(refKey, persistentEntity.getIdentifierAccessor(obj).getIdentifier()); |
| 848 | + } else { |
| 849 | + targetDocument.put(refKey, obj); |
| 850 | + } |
| 851 | + continue; |
| 852 | + } |
| 853 | + |
| 854 | + Object value = propertyAccessor.getProperty(persistentEntity.getPersistentProperty(entry.getKey())); |
| 855 | + String refKey = mapMap.get(entry.getValue()); |
| 856 | + targetDocument.put(refKey, value); |
| 857 | + } |
| 858 | + |
| 859 | + accessor.put(prop, targetDocument); |
| 860 | + return; |
| 861 | + } |
| 862 | + |
817 | 863 | // just take the id as a reference
|
818 |
| - accessor.put(prop, mappingContext.getPersistentEntity(prop.getAssociationTargetType()) |
| 864 | + accessor.put(prop, persistentEntity |
819 | 865 | .getIdentifierAccessor(obj).getIdentifier());
|
820 | 866 | }
|
821 | 867 | return;
|
|
0 commit comments