package eu.dnetlib.repo.manager.service.utils; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.domain.data.Repository; import eu.dnetlib.domain.data.RepositoryInterface; import eu.dnetlib.repo.manager.shared.*; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Converter { private static final Logger LOGGER = Logger.getLogger(Converter.class); public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException { Repository repository = new Repository(); LOGGER.debug("datasource response -> " + repositoryObject); JSONObject datasource = repositoryObject.getJSONObject("datasource"); if( datasource.equals(null)) return null; repository.setActivationId(datasource.get("activationId").toString()); repository.setAggregator(datasource.get("aggregator").toString()); repository.setCertificates(datasource.get("certificates").toString()); repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString()); repository.setCollectedFrom( datasource.get("collectedfrom").toString()); repository.setContactEmail(datasource.get("contactemail").toString()); if(repository.getContactEmail().equals("null")) repository.setContactEmail(""); repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString()); repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString()); repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString()); repository.setDataUploadType(datasource.get("datauploadtype").toString()); repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString())); repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString())); repository.setDescription(datasource.get("description").toString()); if(repository.getDescription().equals("null")) repository.setDescription(""); repository.setEissn(datasource.get("eissn").toString()); repository.setEnglishName( datasource.get("englishname").toString()); if(repository.getEnglishName().equals("null")) repository.setEnglishName(""); repository.setId(datasource.get("id").toString()); repository.setIssn(datasource.get("issn").toString()); repository.setOdLanguages(datasource.get("languages").toString()); repository.setLatitude( toDouble(datasource.get("latitude").toString())); repository.setLissn(datasource.get("lissn").toString()); repository.setLogoUrl(datasource.get("logourl").toString()); if(repository.getLogoUrl().equals("null")) repository.setLogoUrl(""); repository.setLongitude(toDouble(datasource.get("longitude").toString())); //datasource.get("managed"); repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString()); repository.setNamespacePrefix(datasource.get("namespaceprefix").toString()); repository.setOdContentTypes(datasource.get("od_contenttypes").toString()); repository.setOfficialName(datasource.get("officialname").toString()); if(repository.getOfficialName().equals("null")) repository.setOfficialName(""); repository.setPidSystems(datasource.get("pidsystems").toString()); //datasource.get("platform"); repository.setProvenanceActionClass( datasource.get("provenanceaction").toString()); repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString()); repository.setRegisteredBy(datasource.get("registeredby").toString()); if(Objects.equals(repository.getRegisteredBy(),"null")) repository.setRegistered(true); repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString())); repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString())); repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString())); //datasource.get("subjects"); Double timezone = toDouble(datasource.get("timezone").toString()); repository.setTimezone(timezone!=null?timezone:0.0); repository.setTypology(datasource.get("platform").toString()); repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString())); repository.setWebsiteUrl(datasource.get("websiteurl").toString()); repository.setDatasourceClass(datasource.get("typology").toString()); //TODO change organization to list repository.setOrganization( ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("legalname").toString()); String countryCode = ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("country").toString(); repository.setCountryCode(countryCode); String collectedFrom = datasource.get("collectedfrom").toString(); //TODO check data consistency String type = "UNKNOWN"; if (collectedFrom.equalsIgnoreCase("openaire____::opendoar")) { type = "opendoar"; } else if (collectedFrom.equalsIgnoreCase("openaire____::re3data")) { type = "re3data"; } else if (collectedFrom.equalsIgnoreCase("infrastruct_::openaire")) { type = "journal"; } repository.setDatasourceType(type); return repository; } public static Date convertStringToDate(String date){ if(Objects.equals(date, "null")) return null; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); try { return formatter.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String convertDateToString(Date date){ if(Objects.equals(date, null)) return null; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(date); } public static Double toDouble(String number){ if(Objects.equals(number, "null")) return 0.0; else return Double.valueOf(number); } public static List jsonToRepositoryList(JSONObject json) throws JSONException { List resultSet = new ArrayList<>(); JSONArray rs = json.getJSONArray("datasourceInfo"); for(int i=0;i jsonToRepositoryInterfaceList(JSONArray rs) throws JSONException { List resultSet = new ArrayList<>(); for(int i=0;i accessParams = new HashMap<>(); Map extraFields = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiparam"); for(int i=0;i readFile(String filename) { String line; ArrayList list = new ArrayList(); try { //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename); InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while((line = br.readLine()) != null) { list.add(line.trim()); } br.close(); } catch (IOException e) { LOGGER.debug("Error opening file!"); e.printStackTrace(); } return list; } public static List getAggregationHistoryFromJson(JSONObject repositoryObject) throws JSONException { /* if( repositoryObject.get("aggregationHistory").toString().equals("[]") || repositoryObject.get("aggregationHistory")!= null) return null;*/ if(repositoryObject.get("aggregationHistory").toString().equals("[]")) return null; JSONArray rs = new JSONArray(repositoryObject.get("aggregationHistory").toString()); LOGGER.debug(rs.length()); List aggregationDetailsList = new ArrayList<>(); for(int i=0;i toTimezones(List timezones) { List tmz = new ArrayList<>(); for(String t : timezones){ String[] s = t.split("\t"); tmz.add(new Timezone(s[1],Double.parseDouble(s[0]))); } return tmz; } }