2019-02-16 11:04:41 +01:00
|
|
|
package eu.dnetlib.repo.manager.utils;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2023-01-11 17:50:31 +01:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2019-02-16 11:04:41 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-08-05 10:48:16 +02:00
|
|
|
import eu.dnetlib.repo.manager.domain.*;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.Logger;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
2021-07-21 13:51:18 +02:00
|
|
|
import org.springframework.stereotype.Component;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2022-12-01 14:19:29 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2021-07-21 13:51:18 +02:00
|
|
|
@Component
|
2019-02-16 11:04:41 +01:00
|
|
|
public class Converter {
|
|
|
|
|
2023-01-11 17:50:31 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Converter.class);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
private final ObjectMapper objectMapper;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public Converter() {
|
2023-01-11 17:50:31 +01:00
|
|
|
objectMapper = new ObjectMapper()/*.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)*/;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
2022-08-05 10:48:16 +02:00
|
|
|
public Repository toRepository(Object repositoryObject) {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
Repository repository = objectMapper.convertValue(repositoryObject, Repository.class);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
return repository;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) {
|
2022-03-15 12:33:49 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
RepositorySnippet snippet = objectMapper.convertValue(repositorySnippetObject, RepositorySnippet.class);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
return snippet;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public List<Repository> toRepositoryList(JSONObject json) throws JSONException {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
List<Repository> resultSet = new ArrayList<>();
|
|
|
|
JSONArray rs = json.getJSONArray("datasourceInfo");
|
2021-07-21 13:51:18 +02:00
|
|
|
for (int i = 0; i < rs.length(); i++)
|
2022-08-05 10:48:16 +02:00
|
|
|
resultSet.add(toRepository(rs.getJSONObject(i)));
|
2019-02-16 11:04:41 +01:00
|
|
|
return resultSet;
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public List<RepositoryInterface> toRepositoryInterfaceList(List<ApiDetails> apiDetailsList) throws JSONException {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
List<RepositoryInterface> resultSet = new ArrayList<>();
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
for (ApiDetails entry : apiDetailsList)
|
|
|
|
resultSet.add(toRepositoryInterface(entry));
|
|
|
|
return resultSet;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public RepositoryInterface toRepositoryInterface(Object repositoryInterfaceObject) {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
RepositoryInterface repoInterface = objectMapper.convertValue(repositoryInterfaceObject, RepositoryInterface.class);
|
2019-08-26 14:48:40 +02:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
return repoInterface;
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public String toJson(Repository repository) throws JsonProcessingException {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
return objectMapper.writeValueAsString(repository);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public List<String> readFile(String filename) {
|
2019-02-16 11:04:41 +01:00
|
|
|
String line;
|
2022-08-05 10:48:16 +02:00
|
|
|
List<String> list = new ArrayList<>();
|
2019-02-16 11:04:41 +01:00
|
|
|
try {
|
2022-12-07 12:37:55 +01:00
|
|
|
InputStream in = Converter.class.getResourceAsStream("/eu/**/" + filename);
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(in)); // It may throw an NPE.
|
2021-07-21 13:51:18 +02:00
|
|
|
while ((line = br.readLine()) != null) {
|
2019-02-16 11:04:41 +01:00
|
|
|
list.add(line.trim());
|
|
|
|
}
|
|
|
|
br.close();
|
2022-12-07 12:37:55 +01:00
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error opening file!", e);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
2022-12-07 12:37:55 +01:00
|
|
|
return list; // It may be empty.
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
public List<Timezone> toTimezones(List<String> timezones) {
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
List<Timezone> tmz = new ArrayList<>();
|
2021-07-21 13:51:18 +02:00
|
|
|
for (String t : timezones) {
|
2019-02-16 11:04:41 +01:00
|
|
|
String[] s = t.split("\t");
|
2021-07-21 13:51:18 +02:00
|
|
|
tmz.add(new Timezone(s[1], Double.parseDouble(s[0])));
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
return tmz;
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
private String getOpenaireId(String repositoryId) {
|
2019-02-16 11:04:41 +01:00
|
|
|
if (repositoryId != null && repositoryId.contains("::"))
|
|
|
|
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:48:16 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:31:23 +02:00
|
|
|
}
|
2022-08-05 10:48:16 +02:00
|
|
|
|