1
|
package eu.dnetlib.repo.manager.service.utils;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
import eu.dnetlib.domain.data.Repository;
|
5
|
import eu.dnetlib.domain.data.RepositoryInterface;
|
6
|
import eu.dnetlib.repo.manager.shared.AggregationDetails;
|
7
|
import eu.dnetlib.repo.manager.shared.Timezone;
|
8
|
import org.apache.commons.codec.digest.DigestUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.json.JSONArray;
|
11
|
import org.json.JSONException;
|
12
|
import org.json.JSONObject;
|
13
|
|
14
|
import java.io.BufferedReader;
|
15
|
import java.io.IOException;
|
16
|
import java.io.InputStream;
|
17
|
import java.io.InputStreamReader;
|
18
|
import java.text.ParseException;
|
19
|
import java.text.SimpleDateFormat;
|
20
|
import java.util.*;
|
21
|
|
22
|
public class Converter {
|
23
|
|
24
|
private static final Logger LOGGER = Logger.getLogger(Converter.class);
|
25
|
|
26
|
public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
|
27
|
|
28
|
Repository repository = new Repository();
|
29
|
|
30
|
JSONObject datasource = repositoryObject.getJSONObject("datasource");
|
31
|
|
32
|
//if( datasource.equals(null))
|
33
|
// return null;
|
34
|
|
35
|
repository.setId(datasource.get("id").toString());
|
36
|
repository.setOfficialName(datasource.get("officialname").toString());
|
37
|
|
38
|
repository.setEnglishName( datasource.get("englishname").toString());
|
39
|
if(repository.getEnglishName().equals("null"))
|
40
|
repository.setEnglishName("");
|
41
|
|
42
|
repository.setWebsiteUrl(datasource.get("websiteurl").toString());
|
43
|
if(repository.getWebsiteUrl().equals("null"))
|
44
|
repository.setWebsiteUrl("");
|
45
|
|
46
|
repository.setLogoUrl(datasource.get("logourl").toString());
|
47
|
if(repository.getLogoUrl().equals("null"))
|
48
|
repository.setLogoUrl("");
|
49
|
|
50
|
repository.setContactEmail(datasource.get("contactemail").toString());
|
51
|
if(repository.getContactEmail().equals("null"))
|
52
|
repository.setContactEmail("");
|
53
|
|
54
|
|
55
|
repository.setLatitude( toDouble(datasource.get("latitude").toString()));
|
56
|
repository.setLongitude(toDouble(datasource.get("longitude").toString()));
|
57
|
Double timezone = toDouble(datasource.get("timezone").toString());
|
58
|
repository.setTimezone(timezone!=null?timezone:0.0);
|
59
|
repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
|
60
|
repository.setOdLanguages(datasource.get("languages").toString());
|
61
|
repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
|
62
|
|
63
|
/* typology -> platform
|
64
|
* datasource class -> typology */
|
65
|
repository.setTypology(datasource.get("platform").toString());
|
66
|
if(repository.getTypology().equals("null"))
|
67
|
repository.setTypology("");
|
68
|
repository.setDatasourceClass(datasource.get("typology").toString());
|
69
|
|
70
|
repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
|
71
|
repository.setActivationId(datasource.get("activationId").toString());
|
72
|
|
73
|
repository.setDescription(datasource.get("description").toString());
|
74
|
if(repository.getDescription().equals("null"))
|
75
|
repository.setDescription("");
|
76
|
|
77
|
repository.setIssn(datasource.get("issn").toString());
|
78
|
repository.setLissn(datasource.get("lissn").toString());
|
79
|
if(repository.getLissn().equals("null"))
|
80
|
repository.setLissn("");
|
81
|
repository.setEissn(datasource.get("eissn").toString());
|
82
|
if(repository.getEissn().equals("null"))
|
83
|
repository.setEissn("");
|
84
|
repository.setRegisteredBy(datasource.get("registeredby").toString());
|
85
|
|
86
|
/* managed field */
|
87
|
repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString()));
|
88
|
|
89
|
//subjects
|
90
|
|
91
|
repository.setAggregator(datasource.get("aggregator").toString());
|
92
|
repository.setCollectedFrom(datasource.get("collectedfrom").toString());
|
93
|
|
94
|
//TODO change organization to list
|
95
|
JSONArray organizations = ((JSONArray)datasource.get("organizations"));
|
96
|
if(organizations.length() != 0) {
|
97
|
repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
|
98
|
String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString();
|
99
|
repository.setCountryCode(countryCode);
|
100
|
}
|
101
|
|
102
|
/* identities field */
|
103
|
|
104
|
return repository;
|
105
|
}
|
106
|
|
107
|
public static Date convertStringToDate(String date){
|
108
|
|
109
|
if(Objects.equals(date, "null"))
|
110
|
return null;
|
111
|
|
112
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
113
|
try {
|
114
|
return formatter.parse(date);
|
115
|
} catch (ParseException e) {
|
116
|
e.printStackTrace();
|
117
|
}
|
118
|
return null;
|
119
|
}
|
120
|
|
121
|
public static String convertDateToString(Date date){
|
122
|
|
123
|
if(Objects.equals(date, null))
|
124
|
return null;
|
125
|
|
126
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
127
|
return formatter.format(date);
|
128
|
}
|
129
|
|
130
|
public static Double toDouble(String number){
|
131
|
if(Objects.equals(number, "null"))
|
132
|
return 0.0;
|
133
|
else
|
134
|
return Double.valueOf(number);
|
135
|
}
|
136
|
|
137
|
public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {
|
138
|
|
139
|
List<Repository> resultSet = new ArrayList<>();
|
140
|
JSONArray rs = json.getJSONArray("datasourceInfo");
|
141
|
for(int i=0;i<rs.length();i++)
|
142
|
resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
|
143
|
return resultSet;
|
144
|
}
|
145
|
|
146
|
public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException {
|
147
|
|
148
|
List<RepositoryInterface> resultSet = new ArrayList<>();
|
149
|
JSONArray rs = json.getJSONArray("api");
|
150
|
for(int i=0;i<rs.length();i++)
|
151
|
resultSet.add(jsonToRepositoryInterfaceObject( rs.getJSONObject(i)) );
|
152
|
return resultSet;
|
153
|
}
|
154
|
|
155
|
public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
|
156
|
|
157
|
RepositoryInterface repositoryInterface = new RepositoryInterface();
|
158
|
|
159
|
repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
|
160
|
repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
|
161
|
repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
|
162
|
repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
|
163
|
repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
|
164
|
repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString());
|
165
|
|
166
|
repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
|
167
|
repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
|
168
|
|
169
|
|
170
|
// repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
|
171
|
repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
|
172
|
//repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
|
173
|
|
174
|
|
175
|
Map<String, String> accessParams = new HashMap<>();
|
176
|
Map<String, String> extraFields = new HashMap<>();
|
177
|
|
178
|
ObjectMapper mapper = new ObjectMapper();
|
179
|
JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiParams");
|
180
|
|
181
|
for(int i=0;i<apiparams.length();i++)
|
182
|
accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value"));
|
183
|
|
184
|
repositoryInterface.setAccessParams(accessParams);
|
185
|
|
186
|
return repositoryInterface;
|
187
|
}
|
188
|
|
189
|
public static String repositoryObjectToJson(Repository repository) throws JSONException {
|
190
|
|
191
|
JSONObject jsonObject = new JSONObject();
|
192
|
|
193
|
jsonObject.put("id",repository.getId());
|
194
|
jsonObject.put("openaireId",getOpenaireId(repository.getId()));
|
195
|
jsonObject.put("officialname",repository.getOfficialName());
|
196
|
jsonObject.put("englishname",repository.getEnglishName());
|
197
|
jsonObject.put("websiteurl",repository.getWebsiteUrl());
|
198
|
jsonObject.put("logourl",repository.getLogoUrl());
|
199
|
jsonObject.put("contactemail",repository.getContactEmail());
|
200
|
jsonObject.put("longitude",repository.getLongitude().toString());
|
201
|
jsonObject.put("latitude",repository.getLatitude().toString());
|
202
|
jsonObject.put("timezone",repository.getTimezone());
|
203
|
jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
|
204
|
jsonObject.put("languages",repository.getOdLanguages());
|
205
|
jsonObject.put("dateofvalidation",convertDateToString(repository.getDateOfValidation()));
|
206
|
|
207
|
/*
|
208
|
* typology -> platform
|
209
|
* datasource class -> typology
|
210
|
* */
|
211
|
jsonObject.put("typology",repository.getDatasourceClass());
|
212
|
jsonObject.put("platform",repository.getTypology());
|
213
|
|
214
|
jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
|
215
|
jsonObject.put("activationId",repository.getActivationId());
|
216
|
jsonObject.put("description",repository.getDescription());
|
217
|
jsonObject.put("eissn",repository.getEissn());
|
218
|
jsonObject.put("issn",repository.getIssn());
|
219
|
jsonObject.put("lissn",repository.getLissn());
|
220
|
jsonObject.put("registeredby",repository.getRegisteredBy());
|
221
|
|
222
|
jsonObject.put("aggregator",repository.getAggregator());
|
223
|
jsonObject.put("collectedfrom",repository.getCollectedFrom());
|
224
|
|
225
|
jsonObject.put("managed",repository.isRegistered());
|
226
|
|
227
|
JSONObject organization = new JSONObject();
|
228
|
organization.put("legalname",repository.getOrganization());
|
229
|
organization.put("country",repository.getCountryCode());
|
230
|
organization.put("legalshortname","");
|
231
|
organization.put("websiteurl","");
|
232
|
organization.put("logourl","");
|
233
|
|
234
|
JSONArray organizations = new JSONArray();
|
235
|
organizations.put(organization);
|
236
|
jsonObject.put("organizations",organizations);
|
237
|
|
238
|
|
239
|
|
240
|
|
241
|
//TODO check fields
|
242
|
/* jsonObject.put("certificates",repository.getCertificates());
|
243
|
jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
|
244
|
jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
|
245
|
jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
|
246
|
jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
|
247
|
jsonObject.put("datauploadtype",repository.getDataUploadType());
|
248
|
jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
|
249
|
jsonObject.put("od_contenttypes",repository.getOdContentTypes());
|
250
|
jsonObject.put("officialname",repository.getOfficialName());
|
251
|
jsonObject.put("pidsystems",repository.getPidSystems());
|
252
|
jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
|
253
|
jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
|
254
|
jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
|
255
|
jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
|
256
|
jsonObject.put("serviceprovider",repository.getServiceProvider());
|
257
|
jsonObject.put("versioning",repository.getVersioning());
|
258
|
//datasource.get("platform");
|
259
|
//datasource.get("subjects");*/
|
260
|
return jsonObject.toString();
|
261
|
}
|
262
|
|
263
|
public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException {
|
264
|
|
265
|
JSONObject jsonObject = new JSONObject();
|
266
|
|
267
|
jsonObject.put("id",repositoryInterface.getId());
|
268
|
jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
|
269
|
jsonObject.put("datasource",repository.getId());
|
270
|
jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
|
271
|
jsonObject.put("typology",repositoryInterface.getTypology());
|
272
|
jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
|
273
|
jsonObject.put("compatibilityOverride",repositoryInterface.getDesiredCompatibilityLevel());
|
274
|
|
275
|
jsonObject.put("lastCollectionTotal","");
|
276
|
|
277
|
jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
|
278
|
jsonObject.put("lastAggregationTotal","");
|
279
|
jsonObject.put("lastAggregationDate","");
|
280
|
jsonObject.put("lastDownloadTotal","");
|
281
|
jsonObject.put("lastDownloadDate","");
|
282
|
|
283
|
jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
|
284
|
jsonObject.put("removable",repositoryInterface.isRemovable());
|
285
|
|
286
|
|
287
|
JSONArray apiparams = new JSONArray();
|
288
|
for(String param: repositoryInterface.getAccessParams().keySet()){
|
289
|
JSONObject jo = new JSONObject();
|
290
|
jo.put("param",param);
|
291
|
jo.put("value",repositoryInterface.getAccessParams().get(param));
|
292
|
apiparams.put(jo);
|
293
|
}
|
294
|
jsonObject.put("apiParams",apiparams);
|
295
|
|
296
|
|
297
|
// jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
|
298
|
|
299
|
|
300
|
return jsonObject.toString();
|
301
|
}
|
302
|
|
303
|
public static ArrayList<String> readFile(String filename) {
|
304
|
String line;
|
305
|
ArrayList<String> list = new ArrayList<String>();
|
306
|
try {
|
307
|
//InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
|
308
|
InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
|
309
|
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
310
|
while((line = br.readLine()) != null) {
|
311
|
list.add(line.trim());
|
312
|
}
|
313
|
br.close();
|
314
|
} catch (IOException e) {
|
315
|
LOGGER.debug("Error opening file!");
|
316
|
e.printStackTrace();
|
317
|
}
|
318
|
return list;
|
319
|
}
|
320
|
|
321
|
public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject datasourceInfo) throws JSONException {
|
322
|
|
323
|
|
324
|
// if(datasourceInfo.get("aggregationHistory").toString().equals("[]"))
|
325
|
// return null;
|
326
|
|
327
|
JSONArray rs = new JSONArray(datasourceInfo.get("aggregationHistory").toString());
|
328
|
List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
|
329
|
for(int i=0;i<rs.length();i++)
|
330
|
aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
|
331
|
return aggregationDetailsList;
|
332
|
}
|
333
|
|
334
|
private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {
|
335
|
|
336
|
AggregationDetails aggregationDetails = new AggregationDetails();
|
337
|
|
338
|
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
|
339
|
if(aggregationObject.has("collectionMode"))
|
340
|
aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
|
341
|
aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
|
342
|
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
|
343
|
return aggregationDetails;
|
344
|
}
|
345
|
|
346
|
public static AggregationDetails getLastCollectionFromJson(JSONObject datasourceInfo) throws JSONException {
|
347
|
|
348
|
if( datasourceInfo.get("lastCollection").equals(null))
|
349
|
return null;
|
350
|
|
351
|
return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastCollection"));
|
352
|
}
|
353
|
|
354
|
public static AggregationDetails getLastTransformationFromJson(JSONObject datasourceInfo) throws JSONException {
|
355
|
|
356
|
if( datasourceInfo.get("lastTransformation").equals(null))
|
357
|
return null;
|
358
|
|
359
|
return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastTransformation"));
|
360
|
}
|
361
|
|
362
|
public static List<Timezone> toTimezones(List<String> timezones) {
|
363
|
|
364
|
List<Timezone> tmz = new ArrayList<>();
|
365
|
for(String t : timezones){
|
366
|
String[] s = t.split("\t");
|
367
|
tmz.add(new Timezone(s[1],Double.parseDouble(s[0])));
|
368
|
}
|
369
|
return tmz;
|
370
|
}
|
371
|
|
372
|
public static String getOpenaireId(String repositoryId) {
|
373
|
if (repositoryId != null && repositoryId.contains("::"))
|
374
|
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
|
375
|
return null;
|
376
|
}
|
377
|
}
|