Skip to content

Commit

Permalink
feat: update structure cursorexecutor
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 31, 2024
1 parent 7c68f1f commit d94de80
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,14 +153,33 @@ public static CursorExecutor of(PageRequest.Mode value) {
private static PageRequest.Cursor getCursor(List<Sort<?>> sorts, CommunicationEntity entity) {
List<Object> 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<Element> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommunicationEntity> entities = databaseManager.selectCursor(query,
PageRequest.ofSize(10).afterKey("Paulista Avenue"));


assertSoftly(soft -> {
PageRequest<CommunicationEntity> 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() {
Expand Down Expand Up @@ -524,5 +547,16 @@ private Stream<CommunicationEntity> stream() {
return Stream.of(entity, entity2);
}

private Stream<CommunicationEntity> 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);
}


}

0 comments on commit d94de80

Please sign in to comment.