tidied up the Converter
This commit is contained in:
parent
504efc4244
commit
db094db709
|
@ -72,6 +72,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
@Value("${services.provide.usageStatisticsNumbersBaseURL}")
|
||||
private String usageStatisticsNumbersBaseURL;
|
||||
|
||||
private Converter converter;
|
||||
|
||||
private static final Map<String, List<String>> dataSourceClass = new HashMap<>();
|
||||
private static final Map<String, String> invertedDataSourceClass = new HashMap<>();
|
||||
|
@ -91,6 +92,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
AuthoritiesUpdater authoritiesUpdater,
|
||||
VocabularyLoader vocabularyLoader,
|
||||
RestTemplate restTemplate,
|
||||
Converter converter,
|
||||
@Lazy EmailUtils emailUtils,
|
||||
@Lazy ValidatorService validatorService,
|
||||
@Lazy PiWikService piWikService) {
|
||||
|
@ -100,6 +102,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
this.authoritiesUpdater = authoritiesUpdater;
|
||||
this.vocabularyLoader = vocabularyLoader;
|
||||
this.piWikService = piWikService;
|
||||
this.converter = converter;
|
||||
this.emailUtils = emailUtils;
|
||||
this.validatorService = validatorService;
|
||||
this.restTemplate = restTemplate;
|
||||
|
@ -177,7 +180,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
requestFilter.setId(repoId);
|
||||
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
||||
|
||||
repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs)));
|
||||
repos.addAll(converter.toRepositoryList(new JSONObject(rs)));
|
||||
}
|
||||
|
||||
for (Repository r : repos)
|
||||
|
@ -363,7 +366,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
if (jsonArray.length() == 0)
|
||||
throw new ResourceNotFoundException();
|
||||
|
||||
repo = Converter.jsonToRepositorySnippetObject(jsonArray.getJSONObject(0));
|
||||
repo = converter.toRepositorySnippet(jsonArray.getJSONObject(0));
|
||||
return repo;
|
||||
}
|
||||
|
||||
|
@ -382,7 +385,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
if (jsonArray.length() == 0)
|
||||
throw new ResourceNotFoundException();
|
||||
|
||||
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0));
|
||||
repo = converter.toRepository(jsonArray.getJSONObject(0));
|
||||
return updateRepositoryInfo(repo);
|
||||
}
|
||||
|
||||
|
@ -396,7 +399,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
||||
JSONArray aggregationInfo = new JSONObject(rs).getJSONArray("aggregationInfo");
|
||||
|
||||
List<AggregationDetails> aggregationHistory = new ArrayList<>(Converter.getAggregationHistoryFromJson(aggregationInfo));
|
||||
List<AggregationDetails> aggregationHistory = new ArrayList<>(converter.toAggregationHistory(aggregationInfo));
|
||||
|
||||
return aggregationHistory;
|
||||
}
|
||||
|
@ -441,7 +444,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
requestFilter.setOfficialname(name);
|
||||
|
||||
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
|
||||
List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs));
|
||||
List<Repository> repos = converter.toRepositoryList(new JSONObject(rs));
|
||||
for (Repository r : repos)
|
||||
updateRepositoryInfo(r);
|
||||
return repos;
|
||||
|
@ -457,7 +460,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
.build().expand(id).encode();
|
||||
|
||||
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
||||
return Converter.jsonToRepositoryInterfaceList(new JSONObject(rs));
|
||||
return converter.toRepositoryInterfaceList(new JSONObject(rs));
|
||||
|
||||
}
|
||||
|
||||
|
@ -526,7 +529,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
.build()
|
||||
.encode();
|
||||
|
||||
String json_repository = Converter.repositoryObjectToJson(repository);
|
||||
String json_repository = converter.toJson(repository);
|
||||
LOGGER.debug("JSON to add(update) -> " + json_repository);
|
||||
|
||||
HttpEntity<String> httpEntity = new HttpEntity<>(json_repository, httpHeaders);
|
||||
|
@ -552,7 +555,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
.build()
|
||||
.encode();
|
||||
|
||||
String json_repository = Converter.repositoryObjectToJson(repository);
|
||||
String json_repository = converter.toJson(repository);
|
||||
|
||||
LOGGER.debug("JSON to update -> " + json_repository);
|
||||
|
||||
|
@ -585,7 +588,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
.fromHttpUrl(baseAddress + "/ds/add/")
|
||||
.build()
|
||||
.encode();
|
||||
String json_repository = Converter.repositoryObjectToJson(repository);
|
||||
String json_repository = converter.toJson(repository);
|
||||
HttpEntity<String> httpEntity = new HttpEntity<String>(json_repository, httpHeaders);
|
||||
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
|
||||
|
||||
|
@ -619,7 +622,7 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
String comment, RepositoryInterface repositoryInterface) throws Exception {
|
||||
Repository e = this.getRepositoryById(repoId);
|
||||
repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype);
|
||||
String json_interface = Converter.repositoryInterfaceObjectToJson(e, repositoryInterface);
|
||||
String json_interface = converter.toJson(e, repositoryInterface);
|
||||
|
||||
UriComponents uriComponents = UriComponentsBuilder
|
||||
.fromHttpUrl(baseAddress + "/ds/api/add/")
|
||||
|
@ -725,18 +728,18 @@ public class RepositoryServiceImpl implements RepositoryService {
|
|||
@Override
|
||||
public List<String> getDnetCountries() {
|
||||
LOGGER.debug("Getting dnet-countries!");
|
||||
return Converter.readFile("countries.txt");
|
||||
return converter.readFile("countries.txt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypologies() {
|
||||
return Converter.readFile("typologies.txt");
|
||||
return converter.readFile("typologies.txt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Timezone> getTimezones() {
|
||||
List<String> timezones = Converter.readFile("timezones.txt");
|
||||
return Converter.toTimezones(timezones);
|
||||
List<String> timezones = converter.readFile("timezones.txt");
|
||||
return converter.toTimezones(timezones);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -27,7 +27,7 @@ public class Converter {
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(Converter.class);
|
||||
|
||||
public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
|
||||
public Repository toRepository(JSONObject repositoryObject) throws JSONException {
|
||||
|
||||
Repository repository = new Repository();
|
||||
|
||||
|
@ -63,7 +63,7 @@ public class Converter {
|
|||
repository.setTimezone(timezone != null ? timezone : 0.0);
|
||||
repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
|
||||
repository.setOdLanguages(datasource.get("languages").toString());
|
||||
repository.setDateOfValidation(convertStringToDate(datasource.get("dateofvalidation").toString()));
|
||||
repository.setDateOfValidation(toDate(datasource.get("dateofvalidation").toString()));
|
||||
|
||||
/* typology -> platform
|
||||
* datasource class -> typology */
|
||||
|
@ -81,7 +81,7 @@ public class Converter {
|
|||
repository.setDatasourceClass(datasource.get("typology").toString());
|
||||
// <--
|
||||
|
||||
repository.setDateOfCollection(convertStringToDate(datasource.get("dateofcollection").toString()));
|
||||
repository.setDateOfCollection(toDate(datasource.get("dateofcollection").toString()));
|
||||
repository.setActivationId(datasource.get("activationId").toString());
|
||||
|
||||
repository.setDescription(datasource.get("description").toString());
|
||||
|
@ -117,10 +117,10 @@ public class Converter {
|
|||
repository.setConsentTermsOfUseDate(null);
|
||||
repository.setLastConsentTermsOfUseDate(null);
|
||||
try {
|
||||
repository.setConsentTermsOfUseDate(convertStringToDate(datasource.get("consentTermsOfUseDate").toString()));
|
||||
repository.setLastConsentTermsOfUseDate(convertStringToDate(datasource.get("lastConsentTermsOfUseDate").toString()));
|
||||
repository.setConsentTermsOfUseDate(toDate(datasource.get("consentTermsOfUseDate").toString()));
|
||||
repository.setLastConsentTermsOfUseDate(toDate(datasource.get("lastConsentTermsOfUseDate").toString()));
|
||||
} catch (JSONException e) {
|
||||
LOGGER.info("Error setting consentTermsOfUseDate date and lastConsentTermsOfUseDate", e);
|
||||
LOGGER.error("Error setting consentTermsOfUseDate date and lastConsentTermsOfUseDate", e);
|
||||
}
|
||||
repository.setFullTextDownload(convertStringToBoolean(datasource.get("fullTextDownload").toString()));
|
||||
|
||||
|
@ -129,50 +129,16 @@ public class Converter {
|
|||
return repository;
|
||||
}
|
||||
|
||||
public static Boolean convertStringToBoolean(String value) {
|
||||
return value.equals("null") ? null : Boolean.valueOf(value);
|
||||
}
|
||||
|
||||
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) {
|
||||
LOGGER.error(e);
|
||||
}
|
||||
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<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {
|
||||
public List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {
|
||||
|
||||
List<RepositorySnippet> resultSet = new ArrayList<>();
|
||||
JSONArray rs = json.getJSONArray("datasourceInfo");
|
||||
for (int i = 0; i < rs.length(); i++)
|
||||
resultSet.add(jsonToRepositorySnippetObject(rs.getJSONObject(i)));
|
||||
resultSet.add(toRepositorySnippet(rs.getJSONObject(i)));
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public static RepositorySnippet jsonToRepositorySnippetObject(JSONObject repositorySnippetObject) throws JSONException {
|
||||
public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) throws JSONException {
|
||||
|
||||
|
||||
RepositorySnippet repositorySnippet = new RepositorySnippet();
|
||||
|
@ -197,31 +163,31 @@ public class Converter {
|
|||
|
||||
repositorySnippet.setConsentTermsOfUse(repositorySnippetObject.get("consenttermsofuse").toString());
|
||||
repositorySnippet.setFullTextDownload(repositorySnippetObject.get("fulltextdownload").toString());
|
||||
repositorySnippet.setConsentTermsOfUseDate(convertStringToDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
|
||||
repositorySnippet.setConsentTermsOfUseDate(toDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
|
||||
|
||||
return repositorySnippet;
|
||||
|
||||
}
|
||||
|
||||
public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {
|
||||
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(jsonToRepositoryObject(rs.getJSONObject(i)));
|
||||
resultSet.add(toRepository(rs.getJSONObject(i)));
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException {
|
||||
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(jsonToRepositoryInterfaceObject(rs.getJSONObject(i)));
|
||||
resultSet.add(toRepositoryInterface(rs.getJSONObject(i)));
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
|
||||
public RepositoryInterface toRepositoryInterface(JSONObject repositoryInterfaceObject) throws JSONException {
|
||||
|
||||
RepositoryInterface repositoryInterface = new RepositoryInterface();
|
||||
|
||||
|
@ -265,7 +231,7 @@ public class Converter {
|
|||
return repositoryInterface;
|
||||
}
|
||||
|
||||
public static String repositoryObjectToJson(Repository repository) throws JSONException, JsonProcessingException {
|
||||
public String toJson(Repository repository) throws JSONException, JsonProcessingException {
|
||||
|
||||
HashMap<String, Object> repositoryMap = new HashMap<>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
@ -284,7 +250,7 @@ public class Converter {
|
|||
repositoryMap.put("namespaceprefix", repository.getNamespacePrefix() != null ? repository.getNamespacePrefix() : "");
|
||||
repositoryMap.put("languages", repository.getOdLanguages() != null ? repository.getOdLanguages() : "");
|
||||
|
||||
repositoryMap.put("dateofcollection", repository.getDateOfCollection() != null ? convertDateToString(repository.getDateOfCollection()) : "");
|
||||
repositoryMap.put("dateofcollection", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
|
||||
|
||||
/*
|
||||
* typology -> platform
|
||||
|
@ -293,7 +259,7 @@ public class Converter {
|
|||
// repositoryMap.put("eoscDatasourceType", repository.getDatasourceClass()); // TODO: enable in future release
|
||||
repositoryMap.put("platform", repository.getTypology());
|
||||
|
||||
repositoryMap.put("dateofvalidation", repository.getDateOfCollection() != null ? convertDateToString(repository.getDateOfCollection()) : "");
|
||||
repositoryMap.put("dateofvalidation", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
|
||||
repositoryMap.put("activationId", repository.getActivationId() != null ? repository.getActivationId() : "");
|
||||
|
||||
repositoryMap.put("description", repository.getDescription());
|
||||
|
@ -338,13 +304,13 @@ public class Converter {
|
|||
repositoryMap.put("subjects", "");
|
||||
repositoryMap.put("consentTermsOfUse", repository.getConsentTermsOfUse());
|
||||
repositoryMap.put("fullTextDownload", repository.getFullTextDownload());
|
||||
repositoryMap.put("consentTermsOfUseDate", convertDateToString(repository.getConsentTermsOfUseDate()));
|
||||
repositoryMap.put("lastConsentTermsOfUseDate", convertDateToString(repository.getLastConsentTermsOfUseDate()));
|
||||
repositoryMap.put("consentTermsOfUseDate", toString(repository.getConsentTermsOfUseDate()));
|
||||
repositoryMap.put("lastConsentTermsOfUseDate", toString(repository.getLastConsentTermsOfUseDate()));
|
||||
|
||||
return mapper.writeValueAsString(repositoryMap);
|
||||
}
|
||||
|
||||
public static String repositoryInterfaceObjectToJson(Repository repository, RepositoryInterface repositoryInterface) throws JSONException {
|
||||
public String toJson(Repository repository, RepositoryInterface repositoryInterface) throws JSONException {
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
|
@ -384,7 +350,7 @@ public class Converter {
|
|||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
public static ArrayList<String> readFile(String filename) {
|
||||
public ArrayList<String> readFile(String filename) {
|
||||
String line;
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
try {
|
||||
|
@ -402,32 +368,16 @@ public class Converter {
|
|||
return list;
|
||||
}
|
||||
|
||||
public static List<AggregationDetails> getAggregationHistoryFromJson(JSONArray aggregationInfo) throws JSONException {
|
||||
public List<AggregationDetails> toAggregationHistory(JSONArray aggregationInfo) throws JSONException {
|
||||
List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < aggregationInfo.length(); i++)
|
||||
aggregationDetailsList.add(jsonToAggregationDetails(aggregationInfo.getJSONObject(i)));
|
||||
aggregationDetailsList.add(toAggregationDetails(aggregationInfo.getJSONObject(i)));
|
||||
|
||||
return aggregationDetailsList;
|
||||
}
|
||||
|
||||
private static AggregationDetails jsonToAggregationDetails(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(convertStringToDate(aggregationObject.get("date").toString()));
|
||||
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
|
||||
|
||||
return aggregationDetails;
|
||||
}
|
||||
|
||||
public static List<Timezone> toTimezones(List<String> timezones) {
|
||||
public List<Timezone> toTimezones(List<String> timezones) {
|
||||
|
||||
List<Timezone> tmz = new ArrayList<>();
|
||||
for (String t : timezones) {
|
||||
|
@ -437,10 +387,59 @@ public class Converter {
|
|||
return tmz;
|
||||
}
|
||||
|
||||
private static String getOpenaireId(String repositoryId) {
|
||||
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;
|
||||
}
|
||||
|
||||
private 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue