From cdb1956fe9cade7202c69733a264a2fb55b7f297 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Fri, 12 Jun 2020 11:36:59 +0200 Subject: [PATCH] WIP: graph cleaner implementation --- .../dhp/oa/graph/clean/OafNavigator2.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/OafNavigator2.java 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 0000000000..74f064e471 --- /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; + } + +}