141 lines
4.8 KiB
Java
141 lines
4.8 KiB
Java
package eu.dnetlib.repo.manager.utils;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import eu.dnetlib.enabling.datasources.common.AggregationInfo;
|
|
import eu.dnetlib.repo.manager.domain.*;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.log4j.Logger;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
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 = Logger.getLogger(Converter.class);
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
public Converter() {
|
|
objectMapper = new ObjectMapper();
|
|
}
|
|
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<Repository> toRepositoryList(JSONObject json) throws JSONException {
|
|
|
|
List<Repository> 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<RepositoryInterface> toRepositoryInterfaceList(JSONObject json) throws JSONException {
|
|
|
|
List<RepositoryInterface> resultSet = new ArrayList<>();
|
|
JSONArray rs = json.getJSONArray("api");
|
|
for (int i = 0; i < rs.length(); i++)
|
|
resultSet.add(toRepositoryInterface(rs.getJSONObject(i)));
|
|
return resultSet;
|
|
}
|
|
|
|
public List<RepositoryInterface> toRepositoryInterfaceList(List<ApiDetails> apiDetailsList) throws JSONException {
|
|
|
|
List<RepositoryInterface> 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<String> readFile(String filename) {
|
|
String line;
|
|
List<String> list = new ArrayList<>();
|
|
try {
|
|
//InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
|
|
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.debug("Error opening file!");
|
|
LOGGER.error(e);
|
|
}
|
|
return list; // It may be empty.
|
|
}
|
|
|
|
public List<AggregationInfo> toAggregationHistory(String aggregationHistoryResponse) throws JSONException {
|
|
List<AggregationInfo> aggregationInfoList = new ArrayList<>();
|
|
JSONArray aggregationInfo = new JSONObject(aggregationHistoryResponse).getJSONArray("aggregationInfo");
|
|
|
|
for (int i = 0; i < aggregationInfo.length(); i++)
|
|
aggregationInfoList.add(objectMapper.convertValue(aggregationInfo.getJSONObject(i), AggregationInfo.class));
|
|
|
|
return aggregationInfoList;
|
|
}
|
|
|
|
public List<Timezone> toTimezones(List<String> timezones) {
|
|
|
|
List<Timezone> 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);
|
|
}
|
|
|
|
}
|
|
|