/
ColumnEntityConverter.java
259 lines (228 loc) · 11.1 KB
/
ColumnEntityConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.semistructured;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.mapping.core.Converters;
import jakarta.data.exceptions.MappingException;
import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder;
import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.metadata.FieldMetadata;
import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata;
import org.eclipse.jnosql.mapping.metadata.MappingType;
import org.eclipse.jnosql.mapping.metadata.ParameterMetaData;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import static java.util.Objects.requireNonNull;
import static org.eclipse.jnosql.mapping.metadata.MappingType.EMBEDDED;
import static org.eclipse.jnosql.mapping.metadata.MappingType.ENTITY;
/**
* This interface represents the converter between an entity and the {@link CommunicationEntity}
*/
public abstract class ColumnEntityConverter {
protected abstract EntitiesMetadata getEntities();
protected abstract Converters getConverters();
/**
* Converts the instance entity to {@link CommunicationEntity}
*
* @param entity the instance
* @return a {@link CommunicationEntity} instance
* @throws NullPointerException when entity is null
*/
public CommunicationEntity toColumn(Object entity) {
requireNonNull(entity, "entity is required");
EntityMetadata mapping = getEntities().get(entity.getClass());
CommunicationEntity communication = CommunicationEntity.of(mapping.name());
mapping.fields().stream()
.map(f -> to(f, entity))
.map(f -> f.toElements(this, getConverters()))
.flatMap(List::stream)
.forEach(communication::add);
mapping.inheritance().ifPresent(i -> communication.add(i.discriminatorColumn(),
i.discriminatorValue()));
return communication;
}
/**
* Converts a {@link CommunicationEntity} to entity
*
* @param type the entity class
* @param entity the {@link CommunicationEntity} to be converted
* @param <T> the entity type
* @return the instance from {@link CommunicationEntity}
* @throws NullPointerException when either type or entity are null
*/
public <T> T toEntity(Class<T> type, CommunicationEntity entity) {
requireNonNull(entity, "entity is required");
requireNonNull(type, "type is required");
return toEntity(type, entity.elements());
}
/**
* Converts a {@link CommunicationEntity} to entity
* Instead of creating a new object is uses the instance used in this parameters
*
* @param type the instance
* @param entity the {@link CommunicationEntity} to be converted
* @param <T> the entity type
* @return the same instance with values set from {@link CommunicationEntity}
* @throws NullPointerException when either type or entity are null
*/
public <T> T toEntity(T type, CommunicationEntity entity) {
requireNonNull(entity, "entity is required");
requireNonNull(type, "type is required");
if (type.getClass().isRecord()) {
return (T) toEntity(type.getClass(), entity.elements());
}
EntityMetadata mapping = getEntities().get(type.getClass());
return convertEntity(entity.elements(), mapping, type);
}
/**
* Similar to {@link ColumnEntityConverter#toEntity(Class, CommunicationEntity)}, but
* search the instance type from {@link CommunicationEntity#name()}
*
* @param entity the {@link CommunicationEntity} to be converted
* @param <T> the entity type
* @return the instance from {@link CommunicationEntity}
* @throws NullPointerException when entity is null
*/
public <T> T toEntity(CommunicationEntity entity) {
requireNonNull(entity, "entity is required");
EntityMetadata mapping = getEntities().findByName(entity.name());
if (mapping.isInheritance()) {
return mapInheritanceEntity(entity, mapping.type());
}
ConstructorMetadata constructor = mapping.constructor();
if (constructor.isDefault()) {
T instance = mapping.newInstance();
return convertEntity(entity.elements(), mapping, instance);
} else {
return convertEntityByConstructor(entity.elements(), mapping);
}
}
protected ColumnFieldValue to(FieldMetadata field, Object entity) {
Object value = field.read(entity);
return DefaultColumnFieldValue.of(value, field);
}
protected <T> Consumer<String> feedObject(T entity, List<Element> elements, Map<String, FieldMetadata> fieldsGroupByName) {
return (String k) -> {
Optional<Element> element = elements.stream().filter(c -> c.name().equals(k)).findFirst();
FieldMetadata field = fieldsGroupByName.get(k);
FieldConverter fieldConverter = FieldConverter.get(field);
if (ENTITY.equals(field.mappingType())) {
element.ifPresent(c -> fieldConverter.convert(entity, c, field, this));
} else {
fieldConverter.convert(entity, elements, element.orElse(null), field, this);
}
};
}
protected <T> T toEntity(Class<T> type, List<Element> elements) {
EntityMetadata mapping = getEntities().get(type);
if (mapping.isInheritance()) {
return inheritanceToEntity(elements, mapping);
}
ConstructorMetadata constructor = mapping.constructor();
if (constructor.isDefault()) {
T instance = mapping.newInstance();
return convertEntity(elements, mapping, instance);
} else {
return convertEntityByConstructor(elements, mapping);
}
}
private <T> T convertEntityByConstructor(List<Element> elements, EntityMetadata mapping) {
ConstructorBuilder builder = ConstructorBuilder.of(mapping.constructor());
for (ParameterMetaData parameter : builder.parameters()) {
Optional<Element> element = elements.stream()
.filter(c -> c.name().equals(parameter.name()))
.findFirst();
element.ifPresentOrElse(c -> {
ParameterConverter converter = ParameterConverter.of(parameter, getEntities());
converter.convert(this, c, parameter, builder);
}, builder::addEmptyParameter);
}
return builder.build();
}
private <T> T convertEntity(List<Element> elements, EntityMetadata mapping, T instance) {
final Map<String, FieldMetadata> fieldsGroupByName = mapping.fieldsGroupByName();
final List<String> names = elements.stream().map(Element::name).sorted().toList();
final Predicate<String> existField = k -> Collections.binarySearch(names, k) >= 0;
final Predicate<String> isElementType = k -> {
MappingType type = fieldsGroupByName.get(k).mappingType();
return EMBEDDED.equals(type) || ENTITY.equals(type);
};
fieldsGroupByName.keySet().stream()
.filter(existField.or(isElementType))
.forEach(feedObject(instance, elements, fieldsGroupByName));
return instance;
}
private <T> T mapInheritanceEntity(CommunicationEntity entity, Class<?> type) {
Map<String, InheritanceMetadata> group = getEntities()
.findByParentGroupByDiscriminatorValue(type);
if (group.isEmpty()) {
throw new MappingException("There is no discriminator inheritance to the document collection "
+ entity.name());
}
String column = group.values()
.stream()
.findFirst()
.map(InheritanceMetadata::discriminatorColumn)
.orElseThrow();
String discriminator = entity.find(column, String.class)
.orElseThrow(
() -> new MappingException("To inheritance there is the discriminator column missing" +
" on the Document Collection, the document name: " + column));
InheritanceMetadata inheritance = Optional.ofNullable(group.get(discriminator))
.orElseThrow(() -> new MappingException("There is no inheritance map to the discriminator" +
" column value " + discriminator));
EntityMetadata mapping = getEntities().get(inheritance.entity());
ConstructorMetadata constructor = mapping.constructor();
if (constructor.isDefault()) {
T instance = mapping.newInstance();
return convertEntity(entity.elements(), mapping, instance);
} else {
return convertEntityByConstructor(entity.elements(), mapping);
}
}
private <T> T inheritanceToEntity(List<Element> elements, EntityMetadata mapping) {
Map<String, InheritanceMetadata> group = getEntities()
.findByParentGroupByDiscriminatorValue(mapping.type());
if (group.isEmpty()) {
throw new MappingException("There is no discriminator inheritance to the structure "
+ mapping.name());
}
String column = group.values()
.stream()
.findFirst()
.map(InheritanceMetadata::discriminatorColumn)
.orElseThrow();
String discriminator = elements.stream()
.filter(d -> d.name().equals(column))
.findFirst()
.map(d -> d.get(String.class))
.orElseThrow(
() -> new MappingException("To inheritance there is the discriminator element missing" +
" at the structure, the field's name: " + column));
InheritanceMetadata inheritance = Optional.ofNullable(group.get(discriminator))
.orElseThrow(() -> new MappingException("There is no inheritance map to the discriminator" +
" column value " + discriminator));
EntityMetadata inheritanceMetadata = getEntities().get(inheritance.entity());
T instance = inheritanceMetadata.newInstance();
return convertEntity(elements, inheritanceMetadata, instance);
}
}