
driven-adapter.redis.redis-repository.helper.repository-adapter-operations.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}}.redis.repository.helper;
import org.reactivecommons.utils.ObjectMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.StreamSupport.stream;
public abstract class RepositoryAdapterOperations & QueryByExampleExecutor> {
protected R repository;
private final Class dataClass;
protected ObjectMapper mapper;
private final Function toEntityFn;
@SuppressWarnings("unchecked")
public RepositoryAdapterOperations(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 E save(E entity) {
D data = toData(entity);
return toEntity(saveData(data));
}
protected List saveAllEntities(List entities) {
List list = entities.stream().map(this::toData).collect(Collectors.toList());
return toList(saveData(list));
}
public List toList(Iterable iterable) {
return stream(iterable.spliterator(), false).map(this::toEntity).collect(Collectors.toList());
}
protected D saveData(D data) {
return repository.save(data);
}
protected Iterable saveData(List data) {
return repository.saveAll(data);
}
public E findById(I id) {
return toEntity(repository.findById(id).orElse(null));
}
public List findByExample(E entity) {
return toList(repository.findAll( Example.of(toData(entity))));
}
public List findAll(){
return toList(repository.findAll());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy