package eu.dnetlib.ariadneplus.reader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; import javax.annotation.PostConstruct; import eu.dnetlib.ariadneplus.reader.json.ParseRDFJSON; import eu.dnetlib.ariadneplus.reader.utils.ClassSpec; import eu.dnetlib.ariadneplus.reader.utils.Mappings; import eu.dnetlib.ariadneplus.reader.utils.PropertiesMap; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; @Service public class ResourceManager { @Value("${type.path:undefined}") private String type_path; @Value("${general.classpath:undefined}") private String general_classpath; @Value("${exclude.predicates:[]}") private String exclude_predicates; @Value("${class.map.specifications:undefined}") private String spec; private List not_parsable; private ParseRDFJSON parser; private PropertiesMap propertiesMap; public void setup(String type_path, String general_classpath, String exclude_predicates, String spec) { this.type_path = type_path; this.general_classpath = general_classpath; this.exclude_predicates = exclude_predicates; propertiesMap = new PropertiesMap(); propertiesMap.fill(spec); } @PostConstruct public void init(){ Type listType = new TypeToken>(){}.getType(); not_parsable = new Gson().fromJson(exclude_predicates, listType); } private String getFieldValue(Object value){ if (value instanceof LinkedHashMap) return (String)((LinkedHashMap)value).get("value"); return (String)((JSONObject)value).get("value"); } public void manage(ParseRDFJSON parser){ this.parser = parser; } public boolean hasNext(){ return parser.hasNextElement(); } public Object next() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { return manage(parser.getNextElement(),null); } private Object manage(Object entry, String class_name) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { if (class_name == null){ if(entry instanceof LinkedHashMap){ LinkedHashMap tmp = (LinkedHashMap)((JSONArray)((LinkedHashMap)entry).get(type_path)).get(0); class_name = (String)tmp.get("value"); if (class_name.equals("provided record")) { class_name = "AriadneCatalogEntry"; } } } if (entry == null) { System.out.println("entry NULL " ); return null; } System.out.println("class_name: " + class_name); Class c = Class.forName(general_classpath + class_name); Object class_instance = c.newInstance(); ClassSpec class_spec = propertiesMap.get(class_name); Set keySet; if(entry instanceof LinkedHashMap) keySet = ((LinkedHashMap)entry).keySet(); else keySet = ((JSONObject)entry).keySet(); for(Object predicate: keySet){//predicates in the json if (not_parsable.contains(predicate)) continue; Mappings map = class_spec.get((String)predicate); if (map==null) { continue; } JSONArray values; if(entry instanceof LinkedHashMap) values = (JSONArray)((LinkedHashMap)entry).get(predicate); else values = (JSONArray)((JSONObject)entry).get(predicate); if (!map.hasExternalReference()){ Method setField = c.getMethod("set" + map.getClass_field(), Class.forName(map.getElement_type())); setField.invoke(class_instance, getFieldValue(values.get(0))); } else{ if(propertiesMap.get(map.getExternal_reference()).getClass_type().equals("prototype")){ List value_list = new ArrayList<>(); for(Object value: values){ value_list.add(manage(ParseRDFJSON.get(getFieldValue(value)), map.getExternal_reference())); } Method setField = c.getMethod("set" + map.getClass_field(),List.class); setField.invoke(class_instance, value_list); } else{ Class ref = Class.forName(general_classpath + map.getExternal_reference()); Method setField = c.getMethod("set" + map.getClass_field(), ref); setField.invoke(class_instance, manage (ParseRDFJSON.get(getFieldValue(values.get(0))),map.getExternal_reference())); } } } return class_instance; } }