|
16 | 16 |
|
17 | 17 | package io.github.kaiso.relmongo.events.callback; |
18 | 18 |
|
19 | | -import java.lang.reflect.Field; |
20 | | -import java.util.Arrays; |
21 | | -import java.util.Collection; |
22 | | - |
23 | | -import org.bson.types.ObjectId; |
24 | | -import org.springframework.util.ReflectionUtils; |
25 | | -import org.springframework.util.ReflectionUtils.FieldCallback; |
26 | | - |
27 | 19 | import io.github.kaiso.relmongo.annotation.CascadeType; |
28 | 20 | import io.github.kaiso.relmongo.annotation.OneToMany; |
29 | 21 | import io.github.kaiso.relmongo.annotation.OneToOne; |
|
34 | 26 | import io.github.kaiso.relmongo.util.ObjectIdReaderCallback; |
35 | 27 | import io.github.kaiso.relmongo.util.ReflectionsUtil; |
36 | 28 |
|
| 29 | +import org.bson.types.ObjectId; |
| 30 | +import org.springframework.util.ReflectionUtils; |
| 31 | +import org.springframework.util.ReflectionUtils.FieldCallback; |
| 32 | + |
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collection; |
| 36 | + |
37 | 37 | /** |
38 | 38 | * |
39 | 39 | * @author Kais OMRI |
40 | 40 | * |
41 | 41 | */ |
42 | 42 | public class PersistentPropertyConvertingCallback implements FieldCallback { |
43 | 43 |
|
44 | | - private Object source; |
45 | | - |
46 | | - public PersistentPropertyConvertingCallback(Object source) { |
47 | | - super(); |
48 | | - this.source = source; |
49 | | - } |
50 | | - |
51 | | - public void doWith(Field field) throws IllegalAccessException { |
52 | | - ReflectionUtils.makeAccessible(field); |
53 | | - |
54 | | - if (AnnotationsUtils.isMappedBy(field)) { |
55 | | - field.set(source, null); |
56 | | - return; |
57 | | - } |
58 | | - |
59 | | - MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field)); |
60 | | - |
61 | | - if (field.isAnnotationPresent(OneToMany.class)) { |
62 | | - fillIdentifiers(field, field.getAnnotation(OneToMany.class).cascade()); |
63 | | - } else if (field.isAnnotationPresent(OneToOne.class)) { |
64 | | - fillIdentifiers(field, field.getAnnotation(OneToOne.class).cascade()); |
65 | | - } |
66 | | - |
67 | | - } |
68 | | - |
69 | | - private void fillIdentifiers(Field field, CascadeType cascadeType) throws IllegalAccessException { |
70 | | - Object reference = field.get(source); |
71 | | - if (reference == null) { |
72 | | - return; |
73 | | - } |
74 | | - if (Arrays.asList(CascadeType.PERSIST, CascadeType.ALL).contains(cascadeType)) { |
75 | | - if (Collection.class.isAssignableFrom(reference.getClass())) { |
76 | | - ((Collection<?>) reference).stream().forEach(this::checkIdentifier); |
77 | | - } else { |
78 | | - checkIdentifier(reference); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - } |
83 | | - |
84 | | - private void checkIdentifier(Object obj) { |
85 | | - |
86 | | - try { |
87 | | - ObjectIdReaderCallback objectIdReaderCallback = new ObjectIdReaderCallback(obj); |
88 | | - ReflectionUtils.doWithFields(obj.getClass(), objectIdReaderCallback); |
89 | | - if (objectIdReaderCallback.getIdField() == null) { |
90 | | - throw new RelMongoConfigurationException("the Id field of class [" + obj.getClass() |
91 | | - + "] must be annotated by @Id (org.springframework.data.annotation.Id)"); |
92 | | - } |
93 | | - Object id = objectIdReaderCallback.getIdField().get(obj); |
94 | | - if (id == null) { |
95 | | - objectIdReaderCallback.getIdField().set(obj, ObjectId.get()); |
96 | | - } |
97 | | - |
98 | | - } catch (IllegalArgumentException | IllegalAccessException e) { |
99 | | - throw new RelMongoProcessingException(e); |
100 | | - } |
101 | | - |
102 | | - } |
| 44 | + private Object source; |
| 45 | + |
| 46 | + public PersistentPropertyConvertingCallback(Object source) { |
| 47 | + super(); |
| 48 | + this.source = source; |
| 49 | + } |
| 50 | + |
| 51 | + public void doWith(Field field) throws IllegalAccessException { |
| 52 | + ReflectionUtils.makeAccessible(field); |
| 53 | + |
| 54 | + if (AnnotationsUtils.isMappedBy(field)) { |
| 55 | + field.set(source, null); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field)); |
| 60 | + |
| 61 | + if (field.isAnnotationPresent(OneToMany.class)) { |
| 62 | + fillIdentifiers(field, field.getAnnotation(OneToMany.class).cascade()); |
| 63 | + } else if (field.isAnnotationPresent(OneToOne.class)) { |
| 64 | + fillIdentifiers(field, field.getAnnotation(OneToOne.class).cascade()); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private void fillIdentifiers(Field field, CascadeType cascadeType) throws IllegalAccessException { |
| 70 | + Object reference = field.get(source); |
| 71 | + if (reference == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + if (Arrays.asList(CascadeType.PERSIST, CascadeType.ALL).contains(cascadeType)) { |
| 75 | + if (Collection.class.isAssignableFrom(reference.getClass())) { |
| 76 | + ((Collection<?>) reference).stream().forEach(this::checkIdentifier); |
| 77 | + } else { |
| 78 | + checkIdentifier(reference); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private void checkIdentifier(Object obj) { |
| 85 | + |
| 86 | + try { |
| 87 | + ObjectIdReaderCallback objectIdReaderCallback = new ObjectIdReaderCallback(obj); |
| 88 | + ReflectionUtils.doWithFields(obj.getClass(), objectIdReaderCallback); |
| 89 | + Field idField = objectIdReaderCallback.getIdField(); |
| 90 | + if (idField == null) { |
| 91 | + throw new RelMongoConfigurationException("the Id field of class [" + obj.getClass() |
| 92 | + + "] must be annotated by @Id (org.springframework.data.annotation.Id)"); |
| 93 | + } |
| 94 | + if (idField.get(obj) == null) { |
| 95 | + if (idField.getType().equals(ObjectId.class)) { |
| 96 | + idField.set(obj, ObjectId.get()); |
| 97 | + } else if (idField.getType().equals(String.class)) { |
| 98 | + idField.set(obj, ObjectId.get().toString()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 103 | + throw new RelMongoProcessingException(e); |
| 104 | + } |
| 105 | + |
| 106 | + } |
103 | 107 |
|
104 | 108 | } |
0 commit comments