All Downloads are FREE. Search and download functionalities are using the official Maven repository.

driven-adapter.r2dbc-postgresql.helper.reactive-adapter-operations.mustache Maven / Gradle / Ivy

Go to download

Gradle plugin to create a clean application in Java that already works, It follows our best practices!

There is a newer version: 3.20.10
Show newest version
package {{package}}.r2dbc.helper;

import org.reactivecommons.utils.ObjectMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.lang.reflect.ParameterizedType;
import java.util.function.Function;

public abstract class ReactiveAdapterOperations & ReactiveQueryByExampleExecutor> {
    protected R repository;
    protected ObjectMapper mapper;
    private final Class dataClass;
    private final Function toEntityFn;

    @SuppressWarnings("unchecked")
    protected ReactiveAdapterOperations(R repository, ObjectMapper mapper, Function toEntityFn) {
        this.repository = repository;
        this.mapper = mapper;
        ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
        this.dataClass = (Class) genericSuperclass.getActualTypeArguments()[1];
        this.toEntityFn = toEntityFn;
    }

    protected D toData(E entity) {
        return mapper.map(entity, dataClass);
    }

    protected E toEntity(D data) {
        return data != null ? toEntityFn.apply(data) : null;
    }

    public Mono save(E entity) {
        return saveData(toData(entity))
                .map(this::toEntity);
    }

    protected Flux saveAllEntities(Flux entities) {
        return saveData(entities.map(this::toData))
                .map(this::toEntity);
    }

    protected Mono saveData(D data) {
        return repository.save(data);
    }

    protected Flux saveData(Flux data) {
        return repository.saveAll(data);
    }

    public Mono findById(I id) {
        return repository.findById(id).map(this::toEntity);
    }

    public Flux findByExample(E entity) {
        return repository.findAll(Example.of(toData(entity)))
                .map(this::toEntity);
    }

    public Flux findAll() {
        return repository.findAll()
                .map(this::toEntity);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy