uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java

178 lines
6.0 KiB
Java

package eu.dnetlib.repo.manager.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@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.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!");
LOGGER.error(e);
}
return list;
}
public List<AggregationDetails> toAggregationHistory(JSONArray aggregationInfo) throws JSONException {
List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
for (int i = 0; i < aggregationInfo.length(); i++)
aggregationDetailsList.add(toAggregationDetails(aggregationInfo.getJSONObject(i)));
return aggregationDetailsList;
}
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 Date toDate(String date) {
if (Objects.equals(date, "null"))
return null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
return formatter.parse(date);
} catch (ParseException e) {
LOGGER.error(e);
}
return null;
}
public String toString(Date date) {
if (Objects.equals(date, null))
return null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
private Double toDouble(String number) {
if (Objects.equals(number, "null"))
return 0.0;
else
return Double.valueOf(number);
}
private AggregationDetails toAggregationDetails(JSONObject aggregationObject) throws JSONException {
AggregationDetails aggregationDetails = new AggregationDetails();
if (aggregationObject.has("collectionMode"))
aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
if (aggregationObject.has("indexedVersion"))
aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
aggregationDetails.setDate(toDate(aggregationObject.get("date").toString()));
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
return aggregationDetails;
}
}