diff --git a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBUtil.java b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBUtil.java index 014835b7c..1b4c53343 100644 --- a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBUtil.java +++ b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBUtil.java @@ -123,7 +123,7 @@ private static JsonObject toJsonObject(Iterable elements) { if (KEY.equals(document.name()) && Objects.isNull(document.get())) { continue; } - Object value = ValueUtil.convert(document.value()); + Object value = ValueUtil.convert(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER); builder.add(document.name(), toJsonValue(value)); } return builder.build(); diff --git a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecorator.java b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecorator.java new file mode 100644 index 000000000..4b47bf8a1 --- /dev/null +++ b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecorator.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024 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.databases.arangodb.communication; + +import org.eclipse.jnosql.communication.ValueWriter; +import org.eclipse.jnosql.communication.ValueWriterDecorator; + +import java.util.UUID; + +final class ArangoDBValueWriteDecorator implements ValueWriter { + + @SuppressWarnings("rawtypes") + static final ValueWriter ARANGO_DB_VALUE_WRITER = new ArangoDBValueWriteDecorator(); + + @SuppressWarnings("rawtypes") + private static final ValueWriter DEFAULT = ValueWriterDecorator.getInstance(); + + private static final UUIDValueWriter UUID_VALUE_WRITER = new UUIDValueWriter(); + + + @Override + public boolean test(Class type) { + return UUID_VALUE_WRITER.test(type) || DEFAULT.test(type); + } + + @SuppressWarnings("unchecked") + @Override + public S write(T type) { + if(type != null && UUID_VALUE_WRITER.test(type.getClass())) { + return (S) UUID_VALUE_WRITER.write((UUID) type); + } else { + return (S) DEFAULT.write(type); + } + } + +} diff --git a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java index 834b609a7..94e748e3c 100644 --- a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java +++ b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java @@ -195,7 +195,7 @@ private static void appendCondition(StringBuilder aql, Map param aql.append(SEPARATOR).append(entity).append('.').append(document.name()) .append(condition).append(PARAM_APPENDER).append(nameParam); if (IN.equals(condition)) { - params.put(nameParam, ValueUtil.convertToList(document.value())); + params.put(nameParam, ValueUtil.convertToList(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER)); } else { params.put(nameParam, document.get()); } diff --git a/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/UUIDValueWriter.java b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/UUIDValueWriter.java new file mode 100644 index 000000000..dcddc3fb7 --- /dev/null +++ b/jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/UUIDValueWriter.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 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.databases.arangodb.communication; + +import org.eclipse.jnosql.communication.ValueWriter; + +import java.util.Objects; +import java.util.UUID; + +public class UUIDValueWriter implements ValueWriter { + + @Override + public boolean test(Class type) { + return UUID.class.equals(type); + } + + + @Override + public String write(UUID uuid) { + if(Objects.nonNull(uuid)) { + return uuid.toString(); + } + return null; + } + +} diff --git a/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java b/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java index 94311a80d..3c67b1cde 100644 --- a/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java +++ b/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java @@ -328,6 +328,21 @@ void shouldExposeArangoDB() { assertThat(adb.getVersion()).isNotNull(); } + @Test + void shouldInsertUUID() { + var entity = getEntity(); + entity.add("uuid", UUID.randomUUID()); + var documentEntity = entityManager.insert(entity); + Optional uuid = documentEntity.find("uuid"); + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(uuid).isPresent(); + Element element = uuid.orElseThrow(); + soft.assertThat(element.name()).isEqualTo("uuid"); + soft.assertThat(element.get(UUID.class)).isInstanceOf(UUID.class); + }); + + } + private CommunicationEntity getEntity() { CommunicationEntity entity = CommunicationEntity.of(COLLECTION_NAME); Map map = new HashMap<>(); diff --git a/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecoratorTest.java b/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecoratorTest.java new file mode 100644 index 000000000..715fecd6a --- /dev/null +++ b/jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBValueWriteDecoratorTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 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.databases.arangodb.communication; + +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.Assert.assertTrue; + +@SuppressWarnings("rawtypes") +class ArangoDBValueWriteDecoratorTest { + + private final ArangoDBValueWriteDecorator valueWriter = new ArangoDBValueWriteDecorator<>(); + + @Test + void shouldTestUUIDType() { + assertTrue(valueWriter.test(UUID.class)); + } + +} diff --git a/pom.xml b/pom.xml index ef8a9c2a7..012c14a13 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ 4.0.0 + org.eclipse.jnosql.mapping jnosql-mapping-parent 1.1.4-SNAPSHOT