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