
org.jnosql.artemis.document.DocumentFieldConverters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-document Show documentation
Show all versions of artemis-document Show documentation
Eclipse JNoSQL Mapping, Artemis API, to document NoSQL databases
The newest version!
/*
* Copyright (c) 2017 Otávio Santana and others
* 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.jnosql.artemis.document;
import org.jnosql.artemis.AttributeConverter;
import org.jnosql.artemis.reflection.FieldMapping;
import org.jnosql.artemis.reflection.GenericFieldMapping;
import org.jnosql.diana.api.TypeReference;
import org.jnosql.diana.api.Value;
import org.jnosql.diana.api.document.Document;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import static org.jnosql.artemis.reflection.FieldType.COLLECTION;
import static org.jnosql.artemis.reflection.FieldType.EMBEDDED;
import static org.jnosql.artemis.reflection.FieldType.SUBENTITY;
class DocumentFieldConverters {
static class DocumentFieldConverterFactory {
private final EmbeddedFieldConverter embeddedFieldConverter = new EmbeddedFieldConverter();
private final DefaultConverter defaultConverter = new DefaultConverter();
private final CollectionEmbeddableConverter embeddableConverter = new CollectionEmbeddableConverter();
private final SubEntityConverter subEntityConverter = new SubEntityConverter();
DocumentFieldConverter get(FieldMapping field) {
if (EMBEDDED.equals(field.getType())) {
return embeddedFieldConverter;
} else if (SUBENTITY.equals(field.getType())) {
return subEntityConverter;
} else if (isCollectionEmbeddable(field)) {
return embeddableConverter;
} else {
return defaultConverter;
}
}
private boolean isCollectionEmbeddable(FieldMapping field) {
return COLLECTION.equals(field.getType()) && ((GenericFieldMapping) field).isEmbeddable();
}
}
private static class SubEntityConverter implements DocumentFieldConverter {
@Override
public void convert(T instance, List documents, Optional document,
FieldMapping field, AbstractDocumentEntityConverter converter) {
if (document.isPresent()) {
Document sudDocument = document.get();
Object value = sudDocument.get();
if (value instanceof Map) {
Map map = (Map) value;
List embeddedDocument = new ArrayList<>();
for (Map.Entry entry : (Set) map.entrySet()) {
embeddedDocument.add(Document.of(entry.getKey().toString(), entry.getValue()));
}
field.write(instance, converter.toEntity(field.getNativeField().getType(), embeddedDocument));
} else {
field.write(instance, converter.toEntity(field.getNativeField().getType(),
sudDocument.get(new TypeReference>() {
})));
}
} else {
field.write(instance, converter.toEntity(field.getNativeField().getType(), documents));
}
}
}
private static class EmbeddedFieldConverter implements DocumentFieldConverter {
@Override
public void convert(T instance, List documents, Optional document,
FieldMapping field, AbstractDocumentEntityConverter converter) {
Field nativeField = field.getNativeField();
Object subEntity = converter.toEntity(nativeField.getType(), documents);
field.write(instance, subEntity);
}
}
private static class DefaultConverter implements DocumentFieldConverter {
@Override
public void convert(T instance, List documents, Optional document,
FieldMapping field, AbstractDocumentEntityConverter converter) {
Value value = document.get().getValue();
Optional> optionalConverter = field.getConverter();
if (optionalConverter.isPresent()) {
AttributeConverter attributeConverter = converter.getConverters().get(optionalConverter.get());
Object attributeConverted = attributeConverter.convertToEntityAttribute(value.get());
field.write(instance, field.getValue(Value.of(attributeConverted)));
} else {
field.write(instance, field.getValue(value));
}
}
}
private static class CollectionEmbeddableConverter implements DocumentFieldConverter {
@Override
public void convert(T instance, List documents, Optional document,
FieldMapping field, AbstractDocumentEntityConverter converter) {
document.ifPresent(convertDocument(instance, field, converter));
}
private Consumer convertDocument(T instance, FieldMapping field, AbstractDocumentEntityConverter converter) {
return document -> {
GenericFieldMapping genericField = (GenericFieldMapping) field;
Collection collection = genericField.getCollectionInstance();
List> embeddable = (List>) document.get();
for (List documentList : embeddable) {
Object element = converter.toEntity(genericField.getElementType(), documentList);
collection.add(element);
}
field.write(instance, collection);
};
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy