package eu.dnetlib.repo.manager.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.repo.manager.domain.*; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Objects; @Component public class Converter { private static final Logger logger = LoggerFactory.getLogger(Converter.class); private final ObjectMapper objectMapper; public Converter() { objectMapper = new ObjectMapper()/*.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)*/; } public Repository toRepository(Object repositoryObject) { Repository repository = objectMapper.convertValue(repositoryObject, Repository.class); return repository; } public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) { RepositorySnippet snippet = objectMapper.convertValue(repositorySnippetObject, RepositorySnippet.class); return snippet; } public List toRepositoryList(JSONObject json) throws JSONException { List resultSet = new ArrayList<>(); JSONArray rs = json.getJSONArray("datasourceInfo"); for (int i = 0; i < rs.length(); i++) resultSet.add(toRepository(rs.getJSONObject(i))); return resultSet; } public List toRepositoryInterfaceList(List apiDetailsList) throws JSONException { List resultSet = new ArrayList<>(); for (ApiDetails entry : apiDetailsList) resultSet.add(toRepositoryInterface(entry)); return resultSet; } public RepositoryInterface toRepositoryInterface(Object repositoryInterfaceObject) { RepositoryInterface repoInterface = objectMapper.convertValue(repositoryInterfaceObject, RepositoryInterface.class); return repoInterface; } public String toJson(Repository repository) throws JsonProcessingException { return objectMapper.writeValueAsString(repository); } public List readFile(String filename) { String line; List list = new ArrayList<>(); try { InputStream in = Converter.class.getResourceAsStream("/eu/**/" + filename); BufferedReader br = new BufferedReader(new InputStreamReader(in)); // It may throw an NPE. while ((line = br.readLine()) != null) { list.add(line.trim()); } br.close(); } catch (Exception e) { logger.error("Error opening file!", e); } return list; // It may be empty. } public List 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; } private String getOpenaireId(String repositoryId) { if (repositoryId != null && repositoryId.contains("::")) return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]); return null; } private Boolean convertStringToBoolean(String value) { return value.equals("null") ? null : Boolean.valueOf(value); } private Double toDouble(String number) { if (Objects.equals(number, "null")) return 0.0; else return Double.valueOf(number); } }