Skip to content

Commit

Permalink
use pageable and simplify Listing and Crud interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelperezcolom committed Oct 6, 2024
1 parent 662c415 commit 81fdd3e
Show file tree
Hide file tree
Showing 46 changed files with 350 additions and 568 deletions.
8 changes: 7 additions & 1 deletion backend/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.3.4</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
Expand Down Expand Up @@ -104,7 +110,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
<version>2.0.16</version>
<scope>compile</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.mateu.core.application.usecases.fetchlist;

import io.mateu.core.domain.model.util.Serializer;
import io.mateu.core.domain.queries.getListCount.GetListCountQuery;
import io.mateu.core.domain.queries.getListCount.GetListCountQueryHandler;
import io.mateu.core.domain.queries.getListRows.GetListRowsQuery;
import io.mateu.core.domain.queries.getListRows.GetListRowsQueryHandler;
import io.mateu.dtos.Page;
Expand All @@ -18,17 +16,14 @@ public class FetchListUseCase {

private final Serializer serializer;
private final GetListRowsQueryHandler getListRowsQueryHandler;
private final GetListCountQueryHandler getListCountQueryHandler;
private final OrderingDeserializer orderingDeserializer;

public FetchListUseCase(
Serializer serializer,
GetListRowsQueryHandler getListRowsQueryHandler,
GetListCountQueryHandler getListCountQueryHandler,
OrderingDeserializer orderingDeserializer) {
this.serializer = serializer;
this.getListRowsQueryHandler = getListRowsQueryHandler;
this.getListCountQueryHandler = getListCountQueryHandler;
this.orderingDeserializer = orderingDeserializer;
}

Expand All @@ -54,12 +49,6 @@ public Mono<Page> fetchPage(
page,
page_size,
orderingDeserializer.deserialize(ordering)))
.collectList()
.zipWith(
page > 0
? Mono.just(-1L)
: getListCountQueryHandler.run(
new GetListCountQuery(componentType, searchText, data, serverHttpRequest)))
.map(tuple -> new Page(tuple.getT1(), tuple.getT2()));
.map(p -> new Page(p.stream().toList(), p.getTotalElements()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
import io.mateu.core.domain.uidefinition.core.interfaces.Message;
import io.mateu.core.domain.uidefinition.core.interfaces.ResponseWrapper;
import io.mateu.core.domain.uidefinition.core.views.SingleComponentView;
import io.mateu.core.domain.uidefinition.shared.annotations.*;
import io.mateu.core.domain.uidefinition.shared.annotations.Action;
import io.mateu.core.domain.uidefinition.shared.annotations.ActionTarget;
import io.mateu.core.domain.uidefinition.shared.annotations.*;
import io.mateu.core.domain.uidefinition.shared.data.ClientSideEvent;
import io.mateu.core.domain.uidefinition.shared.data.CloseModal;
import io.mateu.core.domain.uidefinition.shared.data.GoBack;
import io.mateu.core.domain.uidefinition.shared.interfaces.JourneyStarter;
import io.mateu.dtos.*;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.lang.reflect.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -71,14 +70,20 @@ public boolean applies(Object viewInstance, String actionId, Map<String, Object>

private Map<String, Method> getActions(Object viewInstance) {
Map<String, Method> methodMap = new LinkedHashMap<>();
methodMap.putAll(reflectionHelper.getAllMethods(viewInstance.getClass()).stream()
.filter(m -> m.isAnnotationPresent(Action.class) || m.isAnnotationPresent(MainAction.class))
.collect(Collectors.toMap(m -> m.getName(), m -> m)));
reflectionHelper.getAllFields(viewInstance.getClass()).forEach(f -> {
methodMap.putAll(reflectionHelper.getAllMethods(f.getType()).stream()
.filter(m -> m.isAnnotationPresent(On.class))
.collect(Collectors.toMap(m -> f.getName() + "." + m.getName(), m -> m)));
});
methodMap.putAll(
reflectionHelper.getAllMethods(viewInstance.getClass()).stream()
.filter(
m -> m.isAnnotationPresent(Action.class) || m.isAnnotationPresent(MainAction.class))
.collect(Collectors.toMap(m -> m.getName(), m -> m)));
reflectionHelper
.getAllFields(viewInstance.getClass())
.forEach(
f -> {
methodMap.putAll(
reflectionHelper.getAllMethods(f.getType()).stream()
.filter(m -> m.isAnnotationPresent(On.class))
.collect(Collectors.toMap(m -> f.getName() + "." + m.getName(), m -> m)));
});
return methodMap;
}

Expand Down Expand Up @@ -117,7 +122,9 @@ private Object[] injectParameters(
continue;
}
if (ClientSideEvent.class.equals(m.getParameterTypes()[i])) {
values.add(new ClientSideEvent((String) data.get("__eventName"), (Map<String, Object>) data.get("__event")));
values.add(
new ClientSideEvent(
(String) data.get("__eventName"), (Map<String, Object>) data.get("__event")));
continue;
}
values.add(
Expand All @@ -131,17 +138,18 @@ public Mono<UIIncrement> runMethod(
Map<String, Object> data,
ServerHttpRequest serverHttpRequest,
String componentId,
String actionId) throws Throwable {
String actionId)
throws Throwable {

Method m = getActions(actualViewInstance).get(actionId);

var methodOwner = actualViewInstance;
if (actionId.contains(".")) {
methodOwner = reflectionHelper.getValue(actionId.substring(0, actionId.indexOf(".")), actualViewInstance);
methodOwner =
reflectionHelper.getValue(
actionId.substring(0, actionId.indexOf(".")), actualViewInstance);
}



if (!Modifier.isPublic(m.getModifiers())) m.setAccessible(true);

// todo: inject parameters (ServerHttpRequest, selection for jpacrud)
Expand Down Expand Up @@ -340,6 +348,9 @@ private boolean isTargetMessage(AnnotatedElement m) {
if (m.isAnnotationPresent(Button.class)) {
return ActionTarget.Message.equals(m.getAnnotation(Button.class).target());
}
if (m.isAnnotationPresent(On.class)) {
return ActionTarget.Message.equals(m.getAnnotation(On.class).target());
}
return false;
}

Expand Down Expand Up @@ -394,6 +405,9 @@ private boolean mustCloseModal(AnnotatedElement m) {
if (m.isAnnotationPresent(Button.class)) {
return m.getAnnotation(Button.class).closeModalWindow();
}
if (m.isAnnotationPresent(On.class)) {
return m.getAnnotation(On.class).closeModalWindow();
}
return false;
}

Expand All @@ -407,6 +421,9 @@ protected String getModalStyle(AnnotatedElement m) {
if (m.isAnnotationPresent(Button.class)) {
return m.getAnnotation(Button.class).modalStyle();
}
if (m.isAnnotationPresent(On.class)) {
return m.getAnnotation(On.class).modalStyle();
}
return null;
}

Expand All @@ -423,6 +440,9 @@ protected String getTargetId(AnnotatedElement m, String componentId) {
if (m.isAnnotationPresent(Button.class)) {
return m.getAnnotation(Button.class).targetId();
}
if (m.isAnnotationPresent(On.class)) {
return m.getAnnotation(On.class).targetId();
}
return null;
}

Expand All @@ -446,6 +466,9 @@ protected ActionTarget getActionTarget(AnnotatedElement m) {
if (m.isAnnotationPresent(Button.class)) {
return m.getAnnotation(Button.class).target();
}
if (m.isAnnotationPresent(On.class)) {
return m.getAnnotation(On.class).target();
}
return null;
}

Expand Down Expand Up @@ -488,6 +511,9 @@ private boolean needsValidation(Method m) {
if (m.isAnnotationPresent(Button.class)) {
return m.getAnnotation(Button.class).validateBefore();
}
if (m.isAnnotationPresent(On.class)) {
return m.getAnnotation(On.class).validateBefore();
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -67,12 +70,11 @@ public Mono<UIIncrement> run(

// todo: recover ordering
var ordering = List.of(); // store.getLastUsedOrders(journeyContainer, stepId, listId);
var pageable = PageRequest.of(__index, 1, Sort.unsorted());

row =
crud.fetchRows(searchText, filtersDeserialized, ordering, __index, 1)
.next()
.toFuture()
.get();
Page page = (Page) crud.fetchRows(searchText, filtersDeserialized, pageable).toFuture().get();

row = page.get().findFirst().get();
}

Object editor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Crud build(String listId, Object crudInstance) {
captionProvider.getCaption(rpcView),
getSubtitle(rpcView),
reflectionHelper.isOverridden(rpcView, "getDetail"),
reflectionHelper.isOverridden(rpcView, "onRowSelected"),
rpcView.isSearchable(),
buildSearchForm(rpcView, listId),
buildColumns(rpcView),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,21 @@ public List<Pair> buildAttributes(Object view, Field field) {
}
if (field.getType().isAnnotationPresent(Element.class)) {
reflectionHelper.getAllFields(field.getType()).stream()
.filter(m -> m.isAnnotationPresent(Content.class))
.forEach(m -> {
.filter(m -> m.isAnnotationPresent(Content.class))
.forEach(
m -> {
attributes.add(new Pair("contentField", m.getName()));
});
reflectionHelper.getAllMethods(field.getType()).stream()
.filter(m -> m.isAnnotationPresent(On.class))
.forEach(m -> {
.filter(m -> m.isAnnotationPresent(On.class))
.forEach(
m -> {
var on = m.getAnnotation(On.class);
attributes.add(new Pair("listener", new Listener(
on.value(),
field.getName() + "." + m.getName(),
on.js()
)));
});
attributes.add(
new Pair(
"listener",
new Listener(on.value(), field.getName() + "." + m.getName(), on.js())));
});
}
return attributes;
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 81fdd3e

Please sign in to comment.