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