diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/OafNavigator2.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/OafNavigator2.java new file mode 100644 index 000000000..74f064e47 --- /dev/null +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/OafNavigator2.java @@ -0,0 +1,94 @@ + +package eu.dnetlib.dhp.oa.graph.clean; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import eu.dnetlib.dhp.schema.oaf.Oaf; + +import java.lang.reflect.Field; +import java.util.*; +import java.util.function.Function; + +public class OafNavigator2 { + + public static E apply(E oaf, Map> mapping) { + navigate(oaf, mapping); + return oaf; + } + + private static void navigate(Object o, Map> mapping) { + if (Objects.isNull(o) || isPrimitive(o)) { + return; + } else { + try { + for (Field field : getAllFields(o.getClass())) { + System.out.println("VISITING " + field.getName() + " in " + o.getClass()); + 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); + } + } if (Iterable.class.isAssignableFrom(fieldType)) { + Iterable fs = (Iterable) value; + for (Object fi : fs) { + navigate(fi, mapping); + } + } else { + final Function cleaningFn = mapping.get(value.getClass()); + if (Objects.nonNull(cleaningFn)) { + final Object newValue = cleaningFn.apply(value); + if (!Objects.equals(value, newValue)) { + System.out.println("PATCHING " + field.getName()+ " " + value.getClass()); + System.out.println("OLD VALUE " + getObjectMapper().writeValueAsString(value)); + System.out.println("NEW VALUE " + getObjectMapper().writeValueAsString(newValue)); + field.set(o, newValue); + } + } + } + } + } + + } catch (IllegalAccessException | IllegalArgumentException | JsonProcessingException e) { + 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 getAllFields(Class clazz) { + return getAllFields(new LinkedList<>(), clazz); + } + + private static List getAllFields(List 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; + } + +}