1
|
package eu.dnetlib.repo.manager.utils;
|
2
|
|
3
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import eu.dnetlib.domain.data.Repository;
|
6
|
import eu.dnetlib.domain.data.RepositoryInterface;
|
7
|
import eu.dnetlib.repo.manager.domain.AggregationDetails;
|
8
|
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
|
9
|
import eu.dnetlib.repo.manager.domain.Timezone;
|
10
|
import org.apache.commons.codec.digest.DigestUtils;
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.json.JSONArray;
|
13
|
import org.json.JSONException;
|
14
|
import org.json.JSONObject;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
|
17
|
import java.io.BufferedReader;
|
18
|
import java.io.IOException;
|
19
|
import java.io.InputStream;
|
20
|
import java.io.InputStreamReader;
|
21
|
import java.text.ParseException;
|
22
|
import java.text.SimpleDateFormat;
|
23
|
import java.util.*;
|
24
|
|
25
|
@Component
|
26
|
public class Converter {
|
27
|
|
28
|
private static final Logger LOGGER = Logger.getLogger(Converter.class);
|
29
|
|
30
|
public Repository toRepository(JSONObject repositoryObject) throws JSONException {
|
31
|
|
32
|
Repository repository = new Repository();
|
33
|
|
34
|
// JSONObject datasource = repositoryObject.getJSONObject("datasource");
|
35
|
JSONObject datasource = repositoryObject;
|
36
|
|
37
|
//if( datasource.equals(null))
|
38
|
// return null;
|
39
|
|
40
|
repository.setId(datasource.get("id").toString());
|
41
|
repository.setOfficialName(datasource.get("officialname").toString());
|
42
|
|
43
|
repository.setEnglishName(datasource.get("englishname").toString());
|
44
|
if (repository.getEnglishName().equals("null"))
|
45
|
repository.setEnglishName("");
|
46
|
|
47
|
repository.setWebsiteUrl(datasource.get("websiteurl").toString());
|
48
|
if (repository.getWebsiteUrl().equals("null"))
|
49
|
repository.setWebsiteUrl("");
|
50
|
|
51
|
repository.setLogoUrl(datasource.get("logourl").toString());
|
52
|
if (repository.getLogoUrl().equals("null"))
|
53
|
repository.setLogoUrl("");
|
54
|
|
55
|
repository.setContactEmail(datasource.get("contactemail").toString());
|
56
|
if (repository.getContactEmail().equals("null"))
|
57
|
repository.setContactEmail("");
|
58
|
|
59
|
|
60
|
repository.setLatitude(toDouble(datasource.get("latitude").toString()));
|
61
|
repository.setLongitude(toDouble(datasource.get("longitude").toString()));
|
62
|
Double timezone = toDouble(datasource.get("timezone").toString());
|
63
|
repository.setTimezone(timezone != null ? timezone : 0.0);
|
64
|
repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
|
65
|
repository.setOdLanguages(datasource.get("languages").toString());
|
66
|
repository.setDateOfValidation(toDate(datasource.get("dateofvalidation").toString()));
|
67
|
|
68
|
/* typology -> platform
|
69
|
* datasource class -> typology */
|
70
|
repository.setTypology(datasource.get("platform").toString());
|
71
|
if (repository.getTypology().equals("null"))
|
72
|
repository.setTypology("");
|
73
|
|
74
|
// // TODO: enable in future release
|
75
|
// /* 07-04-2022 | "typology" -> "eoscDatasourceType" */
|
76
|
// try { // FIXME: remove attemp to get typology if eoscDatasourceType fails
|
77
|
// repository.setDatasourceClass(datasource.get("eoscDatasourceType").toString());
|
78
|
// } catch (JSONException e) {
|
79
|
// repository.setDatasourceClass(datasource.get("typology").toString());
|
80
|
// }
|
81
|
repository.setDatasourceClass(datasource.get("typology").toString());
|
82
|
// <--
|
83
|
|
84
|
repository.setDateOfCollection(toDate(datasource.get("dateofcollection").toString()));
|
85
|
repository.setActivationId(datasource.get("activationId").toString());
|
86
|
|
87
|
repository.setDescription(datasource.get("description").toString());
|
88
|
if (repository.getDescription().equals("null"))
|
89
|
repository.setDescription("");
|
90
|
|
91
|
repository.setIssn(datasource.get("issn").toString());
|
92
|
repository.setLissn(datasource.get("lissn").toString());
|
93
|
if (repository.getLissn().equals("null"))
|
94
|
repository.setLissn("");
|
95
|
repository.setEissn(datasource.get("eissn").toString());
|
96
|
if (repository.getEissn().equals("null"))
|
97
|
repository.setEissn("");
|
98
|
repository.setRegisteredBy(datasource.get("registeredby").toString());
|
99
|
|
100
|
/* managed field */
|
101
|
repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString()));
|
102
|
|
103
|
//subjects
|
104
|
|
105
|
repository.setAggregator(datasource.get("aggregator").toString());
|
106
|
repository.setCollectedFrom(datasource.get("collectedfrom").toString());
|
107
|
|
108
|
//TODO change organization to list
|
109
|
JSONArray organizations = ((JSONArray) datasource.get("organizations"));
|
110
|
if (organizations.length() != 0) {
|
111
|
repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
|
112
|
String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString();
|
113
|
repository.setCountryCode(countryCode);
|
114
|
}
|
115
|
|
116
|
repository.setConsentTermsOfUse(convertStringToBoolean(datasource.get("consentTermsOfUse").toString()));
|
117
|
repository.setConsentTermsOfUseDate(null);
|
118
|
repository.setLastConsentTermsOfUseDate(null);
|
119
|
try {
|
120
|
repository.setConsentTermsOfUseDate(toDate(datasource.get("consentTermsOfUseDate").toString()));
|
121
|
repository.setLastConsentTermsOfUseDate(toDate(datasource.get("lastConsentTermsOfUseDate").toString()));
|
122
|
} catch (JSONException e) {
|
123
|
LOGGER.error("Error setting consentTermsOfUseDate date and lastConsentTermsOfUseDate", e);
|
124
|
}
|
125
|
repository.setFullTextDownload(convertStringToBoolean(datasource.get("fullTextDownload").toString()));
|
126
|
|
127
|
/* identities field */
|
128
|
|
129
|
return repository;
|
130
|
}
|
131
|
|
132
|
public List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {
|
133
|
|
134
|
List<RepositorySnippet> resultSet = new ArrayList<>();
|
135
|
JSONArray rs = json.getJSONArray("datasourceInfo");
|
136
|
for (int i = 0; i < rs.length(); i++)
|
137
|
resultSet.add(toRepositorySnippet(rs.getJSONObject(i)));
|
138
|
return resultSet;
|
139
|
}
|
140
|
|
141
|
public RepositorySnippet toRepositorySnippet(JSONObject repositorySnippetObject) throws JSONException {
|
142
|
|
143
|
|
144
|
RepositorySnippet repositorySnippet = new RepositorySnippet();
|
145
|
|
146
|
// JSONObject datasource = repositorySnippetObject.getJSONObject("datasource");
|
147
|
|
148
|
|
149
|
repositorySnippet.setId(repositorySnippetObject.get("id").toString());
|
150
|
repositorySnippet.setOfficialname(repositorySnippetObject.get("officialname").toString());
|
151
|
|
152
|
repositorySnippet.setEnglishname(repositorySnippetObject.get("englishname").toString());
|
153
|
if (repositorySnippet.getEnglishname().equals("null"))
|
154
|
repositorySnippet.setEnglishname("");
|
155
|
|
156
|
repositorySnippet.setWebsiteurl(repositorySnippetObject.get("websiteurl").toString());
|
157
|
if (repositorySnippet.getWebsiteurl().equals("null"))
|
158
|
repositorySnippet.setWebsiteurl("");
|
159
|
|
160
|
repositorySnippet.setRegisteredby(repositorySnippetObject.get("registeredby").toString());
|
161
|
if (repositorySnippet.getRegisteredby().equals("null"))
|
162
|
repositorySnippet.setRegisteredby("");
|
163
|
|
164
|
repositorySnippet.setConsentTermsOfUse(repositorySnippetObject.get("consenttermsofuse").toString());
|
165
|
repositorySnippet.setFullTextDownload(repositorySnippetObject.get("fulltextdownload").toString());
|
166
|
repositorySnippet.setConsentTermsOfUseDate(toDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
|
167
|
|
168
|
return repositorySnippet;
|
169
|
|
170
|
}
|
171
|
|
172
|
public List<Repository> toRepositoryList(JSONObject json) throws JSONException {
|
173
|
|
174
|
List<Repository> resultSet = new ArrayList<>();
|
175
|
JSONArray rs = json.getJSONArray("datasourceInfo");
|
176
|
for (int i = 0; i < rs.length(); i++)
|
177
|
resultSet.add(toRepository(rs.getJSONObject(i)));
|
178
|
return resultSet;
|
179
|
}
|
180
|
|
181
|
public List<RepositoryInterface> toRepositoryInterfaceList(JSONObject json) throws JSONException {
|
182
|
|
183
|
List<RepositoryInterface> resultSet = new ArrayList<>();
|
184
|
JSONArray rs = json.getJSONArray("api");
|
185
|
for (int i = 0; i < rs.length(); i++)
|
186
|
resultSet.add(toRepositoryInterface(rs.getJSONObject(i)));
|
187
|
return resultSet;
|
188
|
}
|
189
|
|
190
|
public RepositoryInterface toRepositoryInterface(JSONObject repositoryInterfaceObject) throws JSONException {
|
191
|
|
192
|
RepositoryInterface repositoryInterface = new RepositoryInterface();
|
193
|
|
194
|
repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
|
195
|
repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
|
196
|
repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
|
197
|
|
198
|
// /* 07-04-2022 | "typology" -> "eoscDatasourceType" */
|
199
|
// // TODO: enable in future release
|
200
|
// try { // FIXME: remove attemp to get typology if eoscDatasourceType fails
|
201
|
// repositoryInterface.setTypology(repositoryInterfaceObject.get("eoscDatasourceType").toString());
|
202
|
// } catch (JSONException e) {
|
203
|
// repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
|
204
|
// }
|
205
|
repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
|
206
|
// <--
|
207
|
|
208
|
repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
|
209
|
repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString());
|
210
|
|
211
|
repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
|
212
|
repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
|
213
|
|
214
|
|
215
|
// repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
|
216
|
repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
|
217
|
//repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
|
218
|
|
219
|
|
220
|
Map<String, String> accessParams = new HashMap<>();
|
221
|
Map<String, String> extraFields = new HashMap<>();
|
222
|
|
223
|
ObjectMapper mapper = new ObjectMapper();
|
224
|
JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiParams");
|
225
|
|
226
|
for (int i = 0; i < apiparams.length(); i++)
|
227
|
accessParams.put(apiparams.getJSONObject(i).getString("param"), apiparams.getJSONObject(i).getString("value"));
|
228
|
|
229
|
repositoryInterface.setAccessParams(accessParams);
|
230
|
|
231
|
return repositoryInterface;
|
232
|
}
|
233
|
|
234
|
public String toJson(Repository repository) throws JSONException, JsonProcessingException {
|
235
|
|
236
|
HashMap<String, Object> repositoryMap = new HashMap<>();
|
237
|
ObjectMapper mapper = new ObjectMapper();
|
238
|
|
239
|
repositoryMap.put("id", repository.getId());
|
240
|
repositoryMap.put("openaireId", getOpenaireId(repository.getId()));
|
241
|
repositoryMap.put("officialname", repository.getOfficialName());
|
242
|
repositoryMap.put("englishname", repository.getEnglishName());
|
243
|
repositoryMap.put("websiteurl", repository.getWebsiteUrl());
|
244
|
repositoryMap.put("logourl", repository.getLogoUrl());
|
245
|
repositoryMap.put("contactemail", repository.getContactEmail());
|
246
|
repositoryMap.put("longitude", repository.getLongitude().toString());
|
247
|
repositoryMap.put("latitude", repository.getLatitude().toString());
|
248
|
repositoryMap.put("timezone", repository.getTimezone());
|
249
|
|
250
|
repositoryMap.put("namespaceprefix", repository.getNamespacePrefix() != null ? repository.getNamespacePrefix() : "");
|
251
|
repositoryMap.put("languages", repository.getOdLanguages() != null ? repository.getOdLanguages() : "");
|
252
|
|
253
|
repositoryMap.put("dateofcollection", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
|
254
|
|
255
|
/*
|
256
|
* typology -> platform
|
257
|
* datasource class -> typology
|
258
|
* */
|
259
|
// repositoryMap.put("eoscDatasourceType", repository.getDatasourceClass()); // TODO: enable in future release
|
260
|
repositoryMap.put("platform", repository.getTypology());
|
261
|
|
262
|
repositoryMap.put("dateofvalidation", repository.getDateOfCollection() != null ? toString(repository.getDateOfCollection()) : "");
|
263
|
repositoryMap.put("activationId", repository.getActivationId() != null ? repository.getActivationId() : "");
|
264
|
|
265
|
repositoryMap.put("description", repository.getDescription());
|
266
|
|
267
|
repositoryMap.put("eissn", repository.getEissn() != null ? repository.getEissn() : "");
|
268
|
repositoryMap.put("issn", repository.getIssn() != null ? repository.getIssn() : "");
|
269
|
repositoryMap.put("lissn", repository.getLissn() != null ? repository.getLissn() : "");
|
270
|
|
271
|
repositoryMap.put("registeredby", repository.getRegisteredBy());
|
272
|
|
273
|
repositoryMap.put("aggregator", repository.getAggregator() != null ? repository.getAggregator() : "");
|
274
|
repositoryMap.put("collectedfrom", repository.getCollectedFrom() != null ? repository.getCollectedFrom() : "");
|
275
|
|
276
|
repositoryMap.put("managed", repository.isRegistered());
|
277
|
|
278
|
Map<String, String> organization = new HashMap<>();
|
279
|
organization.put("legalname", repository.getOrganization());
|
280
|
organization.put("country", repository.getCountryCode());
|
281
|
organization.put("legalshortname", "");
|
282
|
organization.put("websiteurl", "");
|
283
|
organization.put("logourl", "");
|
284
|
|
285
|
List organizations = new ArrayList();
|
286
|
organizations.add(organization);
|
287
|
repositoryMap.put("organizations", organizations);
|
288
|
|
289
|
//TODO check identitites
|
290
|
//Map<String,String> identity = new HashMap<>();
|
291
|
|
292
|
if (repository.getPiwikInfo() != null) {
|
293
|
Map<String, Object> identity = new HashMap<>();
|
294
|
HashSet<Map<String, Object>> identities = new HashSet<>();
|
295
|
|
296
|
identity.put("issuertype", "piwik");
|
297
|
identity.put("pid", "piwik:" + repository.getPiwikInfo().getSiteId());
|
298
|
|
299
|
identities.add(identity);
|
300
|
|
301
|
repositoryMap.put("identities", identities);
|
302
|
}
|
303
|
|
304
|
repositoryMap.put("subjects", "");
|
305
|
repositoryMap.put("consentTermsOfUse", repository.getConsentTermsOfUse());
|
306
|
repositoryMap.put("fullTextDownload", repository.getFullTextDownload());
|
307
|
repositoryMap.put("consentTermsOfUseDate", toString(repository.getConsentTermsOfUseDate()));
|
308
|
repositoryMap.put("lastConsentTermsOfUseDate", toString(repository.getLastConsentTermsOfUseDate()));
|
309
|
|
310
|
return mapper.writeValueAsString(repositoryMap);
|
311
|
}
|
312
|
|
313
|
public String toJson(Repository repository, RepositoryInterface repositoryInterface) throws JSONException {
|
314
|
|
315
|
JSONObject jsonObject = new JSONObject();
|
316
|
|
317
|
jsonObject.put("id", repositoryInterface.getId());
|
318
|
jsonObject.put("protocol", repositoryInterface.getAccessProtocol());
|
319
|
jsonObject.put("datasource", repository.getId());
|
320
|
jsonObject.put("contentdescription", repositoryInterface.getContentDescription());
|
321
|
jsonObject.put("typology", repositoryInterface.getTypology());
|
322
|
jsonObject.put("compatibility", repositoryInterface.getDesiredCompatibilityLevel());
|
323
|
jsonObject.put("compatibilityOverride", repositoryInterface.getDesiredCompatibilityLevel());
|
324
|
|
325
|
jsonObject.put("lastCollectionTotal", "");
|
326
|
|
327
|
jsonObject.put("lastCollectionDate", repositoryInterface.getLastCollectionDate());
|
328
|
jsonObject.put("lastAggregationTotal", "");
|
329
|
jsonObject.put("lastAggregationDate", "");
|
330
|
jsonObject.put("lastDownloadTotal", "");
|
331
|
jsonObject.put("lastDownloadDate", "");
|
332
|
|
333
|
jsonObject.put("baseurl", repositoryInterface.getBaseUrl());
|
334
|
jsonObject.put("removable", repositoryInterface.isRemovable());
|
335
|
|
336
|
|
337
|
JSONArray apiparams = new JSONArray();
|
338
|
for (String param : repositoryInterface.getAccessParams().keySet()) {
|
339
|
JSONObject jo = new JSONObject();
|
340
|
jo.put("param", param);
|
341
|
jo.put("value", repositoryInterface.getAccessParams().get(param));
|
342
|
apiparams.put(jo);
|
343
|
}
|
344
|
jsonObject.put("apiParams", apiparams);
|
345
|
|
346
|
|
347
|
// jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
|
348
|
|
349
|
|
350
|
return jsonObject.toString();
|
351
|
}
|
352
|
|
353
|
public ArrayList<String> readFile(String filename) {
|
354
|
String line;
|
355
|
ArrayList<String> list = new ArrayList<String>();
|
356
|
try {
|
357
|
//InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
|
358
|
InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
|
359
|
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
360
|
while ((line = br.readLine()) != null) {
|
361
|
list.add(line.trim());
|
362
|
}
|
363
|
br.close();
|
364
|
} catch (IOException e) {
|
365
|
LOGGER.debug("Error opening file!");
|
366
|
LOGGER.error(e);
|
367
|
}
|
368
|
return list;
|
369
|
}
|
370
|
|
371
|
public List<AggregationDetails> toAggregationHistory(JSONArray aggregationInfo) throws JSONException {
|
372
|
List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
|
373
|
|
374
|
for (int i = 0; i < aggregationInfo.length(); i++)
|
375
|
aggregationDetailsList.add(toAggregationDetails(aggregationInfo.getJSONObject(i)));
|
376
|
|
377
|
return aggregationDetailsList;
|
378
|
}
|
379
|
|
380
|
public List<Timezone> toTimezones(List<String> timezones) {
|
381
|
|
382
|
List<Timezone> tmz = new ArrayList<>();
|
383
|
for (String t : timezones) {
|
384
|
String[] s = t.split("\t");
|
385
|
tmz.add(new Timezone(s[1], Double.parseDouble(s[0])));
|
386
|
}
|
387
|
return tmz;
|
388
|
}
|
389
|
|
390
|
private String getOpenaireId(String repositoryId) {
|
391
|
if (repositoryId != null && repositoryId.contains("::"))
|
392
|
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
|
393
|
return null;
|
394
|
}
|
395
|
|
396
|
private Boolean convertStringToBoolean(String value) {
|
397
|
return value.equals("null") ? null : Boolean.valueOf(value);
|
398
|
}
|
399
|
|
400
|
private Date toDate(String date) {
|
401
|
|
402
|
if (Objects.equals(date, "null"))
|
403
|
return null;
|
404
|
|
405
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
406
|
try {
|
407
|
return formatter.parse(date);
|
408
|
} catch (ParseException e) {
|
409
|
LOGGER.error(e);
|
410
|
}
|
411
|
return null;
|
412
|
}
|
413
|
|
414
|
private String toString(Date date) {
|
415
|
|
416
|
if (Objects.equals(date, null))
|
417
|
return null;
|
418
|
|
419
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
420
|
return formatter.format(date);
|
421
|
}
|
422
|
|
423
|
private Double toDouble(String number) {
|
424
|
if (Objects.equals(number, "null"))
|
425
|
return 0.0;
|
426
|
else
|
427
|
return Double.valueOf(number);
|
428
|
}
|
429
|
|
430
|
private AggregationDetails toAggregationDetails(JSONObject aggregationObject) throws JSONException {
|
431
|
|
432
|
AggregationDetails aggregationDetails = new AggregationDetails();
|
433
|
|
434
|
if (aggregationObject.has("collectionMode"))
|
435
|
aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
|
436
|
if (aggregationObject.has("indexedVersion"))
|
437
|
aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
|
438
|
|
439
|
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
|
440
|
aggregationDetails.setDate(toDate(aggregationObject.get("date").toString()));
|
441
|
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
|
442
|
|
443
|
return aggregationDetails;
|
444
|
}
|
445
|
}
|