dnet-hadoop/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/OafNavigator2.java

97 lines
2.8 KiB
Java
Raw Normal View History

2020-06-12 11:36:59 +02:00
package eu.dnetlib.dhp.oa.graph.clean;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
2020-06-12 12:03:25 +02:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.schema.oaf.Oaf;
2020-06-12 11:36:59 +02:00
public class OafNavigator2 {
public static <E extends Oaf> E apply(E oaf, Map<Class, Function<Object, Object>> mapping) {
navigate(oaf, mapping);
return oaf;
}
private static void navigate(Object o, Map<Class, Function<Object, Object>> mapping) {
if (Objects.isNull(o) || isPrimitive(o)) {
return;
} else {
try {
for (Field field : getAllFields(o.getClass())) {
2020-06-12 12:03:25 +02:00
//System.out.println("VISITING " + field.getName() + " in " + o.getClass());
2020-06-12 11:36:59 +02:00
field.setAccessible(true);
Object value = field.get(o);
if (Objects.nonNull(value)) {
final Class<?> fieldType = field.getType();
if ((fieldType.isArray() && !fieldType.getComponentType().isPrimitive())) {
Object[] fs = (Object[]) value;
for (Object fi : fs) {
navigate(fi, mapping);
}
2020-06-12 12:03:25 +02:00
}
if (Iterable.class.isAssignableFrom(fieldType)) {
2020-06-12 11:36:59 +02:00
Iterable fs = (Iterable) value;
for (Object fi : fs) {
navigate(fi, mapping);
}
} else {
final Function<Object, Object> cleaningFn = mapping.get(value.getClass());
if (Objects.nonNull(cleaningFn)) {
final Object newValue = cleaningFn.apply(value);
if (!Objects.equals(value, newValue)) {
2020-06-12 12:03:25 +02:00
//System.out.println("PATCHING " + field.getName() + " " + value.getClass());
//System.out.println("OLD VALUE " + getObjectMapper().writeValueAsString(value));
//System.out.println("NEW VALUE " + getObjectMapper().writeValueAsString(newValue));
2020-06-12 11:36:59 +02:00
field.set(o, newValue);
}
}
}
}
}
2020-06-12 12:03:25 +02:00
} catch (IllegalAccessException | IllegalArgumentException /*| JsonProcessingException*/ e) {
2020-06-12 11:36:59 +02:00
throw new RuntimeException(e);
}
}
}
private static ObjectMapper getObjectMapper() {
final ObjectMapper mapper = new ObjectMapper();
return mapper;
}
private static boolean isPrimitive(Object o) {
return o.getClass().isPrimitive()
|| o instanceof Class
|| o instanceof Integer
|| o instanceof Double
|| o instanceof Float
|| o instanceof Long
|| o instanceof Boolean
|| o instanceof String
|| o instanceof Byte;
}
private static List<Field> getAllFields(Class<?> clazz) {
return getAllFields(new LinkedList<>(), clazz);
}
private static List<Field> getAllFields(List<Field> fields, Class<?> clazz) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
final Class<?> superclass = clazz.getSuperclass();
if (Objects.nonNull(superclass) && superclass.getPackage().equals(Oaf.class.getPackage())) {
getAllFields(fields, superclass);
}
return fields;
}
}