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