moved common code
This commit is contained in:
parent
d883252e8c
commit
8693a99880
@ -0,0 +1,20 @@
|
||||
package com.knecon.fforesight.databasetenantcommons.providers.models.common;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(of = "value")
|
||||
@RequiredArgsConstructor
|
||||
public class JSONPrimitive<T> {
|
||||
|
||||
private final T value;
|
||||
|
||||
|
||||
public static <T> JSONPrimitive<T> of(T value) {
|
||||
|
||||
return new JSONPrimitive<>(value);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.knecon.fforesight.databasetenantcommons.providers.models.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@Builder
|
||||
public class Page<T> {
|
||||
|
||||
@Builder.Default
|
||||
protected List<T> elements = new ArrayList<>();
|
||||
protected long totalHits;
|
||||
protected int page;
|
||||
protected int pageSize;
|
||||
|
||||
public List<T> getData(){
|
||||
return this.elements;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,125 @@
|
||||
package com.knecon.fforesight.databasetenantcommons.providers.utils;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.knecon.fforesight.databasetenantcommons.providers.models.common.Page;
|
||||
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
public class MagicConverter {
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> T convert(S source, Class<T> target, BiConsumer<S, T> deltaMapper) {
|
||||
|
||||
if (deltaMapper != null) {
|
||||
var targetObject = convert(source, target);
|
||||
deltaMapper.accept(source, targetObject);
|
||||
return targetObject;
|
||||
} else {
|
||||
return convert(source, target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> T convert(S source, Class<T> target) {
|
||||
|
||||
var noArgsConstructor = target.getConstructor();
|
||||
return convertOne(source, target, noArgsConstructor);
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> T convertOne(S source, Class<T> target, Constructor<T> constructor) {
|
||||
|
||||
var newInstance = constructor.newInstance();
|
||||
BeanUtils.copyProperties(source, newInstance);
|
||||
|
||||
for (var field : target.getDeclaredFields()) {
|
||||
if (field.getAnnotation(EmbeddedId.class) != null) {
|
||||
field.setAccessible(true);
|
||||
var id = field.getType().getConstructor().newInstance();
|
||||
BeanUtils.copyProperties(source, id);
|
||||
field.set(newInstance, id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (var field : source.getClass().getDeclaredFields()) {
|
||||
if (field.getAnnotation(EmbeddedId.class) != null) {
|
||||
field.setAccessible(true);
|
||||
var sourceId = field.get(source);
|
||||
BeanUtils.copyProperties(sourceId, newInstance);
|
||||
}
|
||||
}
|
||||
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> List<T> convert(List<S> sources, Class<T> target, BiConsumer<S, T> deltaMapper) {
|
||||
|
||||
if (deltaMapper != null) {
|
||||
var targetList = convert(sources, target);
|
||||
for (int i = 0; i < targetList.size(); i++) {
|
||||
deltaMapper.accept(sources.get(i), targetList.get(i));
|
||||
}
|
||||
return targetList;
|
||||
} else {
|
||||
return convert(sources, target);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> List<T> convert(List<S> sources, Class<T> target) {
|
||||
|
||||
var noArgsConstructor = target.getConstructor();
|
||||
|
||||
List<T> targetList = new ArrayList<>();
|
||||
for (var s : sources) {
|
||||
targetList.add(convertOne(s, target, noArgsConstructor));
|
||||
}
|
||||
return targetList;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> Page<T> convert(org.springframework.data.domain.Page<S> sources, Class<T> target) {
|
||||
|
||||
Page<T> result = new Page<>();
|
||||
result.setElements(convert(Lists.newArrayList(sources), target));
|
||||
result.setPage(sources.getNumber());
|
||||
result.setPageSize(sources.getSize());
|
||||
result.setTotalHits(sources.getTotalElements());
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <S, T> Set<T> convert(Set<S> sources, Class<T> target) {
|
||||
|
||||
var noArgsConstructor = target.getConstructor();
|
||||
|
||||
Set<T> targetList = new HashSet<>();
|
||||
for (var s : sources) {
|
||||
targetList.add(convertOne(s, target, noArgsConstructor));
|
||||
}
|
||||
return targetList;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user