From d94de80cbddeb232084180557b2e11ceef4de587 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 31 Mar 2024 07:00:46 +0100 Subject: [PATCH] feat: update structure cursorexecutor Signed-off-by: Otavio Santana --- .../semistructured/CursorExecutor.java | 28 ++++++++++++--- .../semistructured/DatabaseManagerTest.java | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/jnosql-communication/jnosql-communication-semistructured/src/main/java/org/eclipse/jnosql/communication/semistructured/CursorExecutor.java b/jnosql-communication/jnosql-communication-semistructured/src/main/java/org/eclipse/jnosql/communication/semistructured/CursorExecutor.java index fecac332b..447cea1b2 100644 --- a/jnosql-communication/jnosql-communication-semistructured/src/main/java/org/eclipse/jnosql/communication/semistructured/CursorExecutor.java +++ b/jnosql-communication/jnosql-communication-semistructured/src/main/java/org/eclipse/jnosql/communication/semistructured/CursorExecutor.java @@ -19,6 +19,7 @@ import jakarta.data.page.PageRequest; import jakarta.data.page.impl.CursoredPageRecord; import org.eclipse.jnosql.communication.CommunicationException; +import org.eclipse.jnosql.communication.TypeReference; import java.util.ArrayList; import java.util.Collections; @@ -152,14 +153,33 @@ public static CursorExecutor of(PageRequest.Mode value) { private static PageRequest.Cursor getCursor(List> sorts, CommunicationEntity entity) { List keys = new ArrayList<>(sorts.size()); for (Sort sort : sorts) { - String name = sort.property(); - Element element = entity.find(name) - .orElseThrow(() -> new CommunicationException("The sort name does not exist in the entity: " + name)); - keys.add(element.get()); + String[] names = sort.property().split("\\."); + keys.add(value(names, entity)); } return PageRequest.Cursor.forKey(keys.toArray()); } + private static Object value(String[] names, CommunicationEntity entity) { + Element element = entity.find(names[0]) + .orElseThrow(() -> new CommunicationException("The sort name does not exist in the entity: " + names[0])); + return value(names, element, 0); + + } + + private static Object value(String[] names, Element element, int index) { + if (names.length == 1) { + return element.get(); + } + List elements = element.get(new TypeReference<>() {}); + Element subElement = elements.stream().filter(e -> e.name().equals(names[index + 1])) + .findFirst().orElseThrow(() -> new CommunicationException("The sort name does not exist in the entity: " + names[index])); + if (names.length == index + 2) { + return subElement.get(); + } else { + return value(names, subElement, index + 1); + } + } + private static DefaultSelectQuery updateQuery(int pageRequest, SelectQuery query, CriteriaCondition condition) { return new DefaultSelectQuery(pageRequest, 0, query.name(), query.columns(), query.sorts(), query.condition().map(c -> CriteriaCondition.and(c, condition)) diff --git a/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java b/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java index 34875dd27..6bbbd0161 100644 --- a/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java +++ b/jnosql-communication/jnosql-communication-semistructured/src/test/java/org/eclipse/jnosql/communication/semistructured/DatabaseManagerTest.java @@ -241,6 +241,29 @@ void shouldReturnPaginationAfterKeySingleElementWhenThereIsCondition() { }); } + @Test + void shouldFindSubElement() { + SelectQuery query = SelectQuery.select().from("person") + .orderBy("address.street").asc() + .build(); + + Mockito.when(databaseManager.select(Mockito.any(SelectQuery.class))) + .thenReturn(streamSubDocument()); + CursoredPage entities = databaseManager.selectCursor(query, + PageRequest.ofSize(10).afterKey("Paulista Avenue")); + + + assertSoftly(soft -> { + PageRequest nextedPageRequest = entities.nextPageRequest(); + PageRequest.Cursor cursor = nextedPageRequest.cursor().orElseThrow(); + + soft.assertThat(entities).hasSize(1); + soft.assertThat(cursor.get(0)).isEqualTo("Paulista Avenue"); + + }); + } + + @Test void shouldReturnPaginationBeforeKeySingleElementWhenConditionIsNull() { @@ -524,5 +547,16 @@ private Stream stream() { return Stream.of(entity, entity2); } + private Stream streamSubDocument() { + var entity = CommunicationEntity.of("name"); + entity.add("name", "Ada"); + entity.add("age", 10); + entity.add("id", UUID.randomUUID().toString()); + entity.add("address", List.of( + Element.of("street", "Paulista Avenue"), + Element.of("number", 100))); + return Stream.of(entity); + } + } \ No newline at end of file