Project

General

Profile

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