Project

General

Profile

1 49236 panagiotis
package eu.dnetlib.repo.manager.service.utils;
2
3 50051 panagiotis
import com.fasterxml.jackson.databind.ObjectMapper;
4 49236 panagiotis
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.data.RepositoryInterface;
6 51330 panagiotis
import eu.dnetlib.repo.manager.shared.AggregationDetails;
7
import eu.dnetlib.repo.manager.shared.Timezone;
8
import org.apache.commons.codec.digest.DigestUtils;
9 49763 panagiotis
import org.apache.log4j.Logger;
10 49236 panagiotis
import org.json.JSONArray;
11 49362 panagiotis
import org.json.JSONException;
12 49236 panagiotis
import org.json.JSONObject;
13
14 51330 panagiotis
import java.io.BufferedReader;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
18 49236 panagiotis
import java.text.ParseException;
19
import java.text.SimpleDateFormat;
20
import java.util.*;
21
22
public class Converter {
23
24 49763 panagiotis
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
25 51330 panagiotis
26 49763 panagiotis
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
27
28 49236 panagiotis
        Repository repository = new Repository();
29 50845 panagiotis
30 50219 panagiotis
        JSONObject datasource = repositoryObject.getJSONObject("datasource");
31 49236 panagiotis
32 52781 panagiotis
        //if( datasource.equals(null))
33
        //    return null;
34 49898 panagiotis
35 51525 panagiotis
        repository.setId(datasource.get("id").toString());
36
        repository.setOfficialName(datasource.get("officialname").toString());
37 49898 panagiotis
38 49236 panagiotis
        repository.setEnglishName( datasource.get("englishname").toString());
39 49898 panagiotis
        if(repository.getEnglishName().equals("null"))
40
            repository.setEnglishName("");
41
42 51525 panagiotis
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
43 51549 panagiotis
        if(repository.getWebsiteUrl().equals("null"))
44
            repository.setWebsiteUrl("");
45
46 49236 panagiotis
        repository.setLogoUrl(datasource.get("logourl").toString());
47 49898 panagiotis
        if(repository.getLogoUrl().equals("null"))
48
            repository.setLogoUrl("");
49
50 51525 panagiotis
        repository.setContactEmail(datasource.get("contactemail").toString());
51 51549 panagiotis
        if(repository.getContactEmail().equals("null"))
52
            repository.setContactEmail("");
53
54
55 51525 panagiotis
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
56 49236 panagiotis
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
57 51525 panagiotis
        Double timezone = toDouble(datasource.get("timezone").toString());
58
        repository.setTimezone(timezone!=null?timezone:0.0);
59 49236 panagiotis
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
60 51525 panagiotis
        repository.setOdLanguages(datasource.get("languages").toString());
61
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
62 49898 panagiotis
63 51525 panagiotis
        /*  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 51549 panagiotis
        if(repository.getLissn().equals("null"))
80
            repository.setLissn("");
81 51525 panagiotis
        repository.setEissn(datasource.get("eissn").toString());
82 51549 panagiotis
        if(repository.getEissn().equals("null"))
83
            repository.setEissn("");
84 49236 panagiotis
        repository.setRegisteredBy(datasource.get("registeredby").toString());
85 50219 panagiotis
86 51525 panagiotis
        /* managed field */
87
        repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString()));
88 50219 panagiotis
89 51525 panagiotis
        //subjects
90 51549 panagiotis
91 51525 panagiotis
        repository.setAggregator(datasource.get("aggregator").toString());
92 51549 panagiotis
        repository.setCollectedFrom(datasource.get("collectedfrom").toString());
93 49898 panagiotis
94 51525 panagiotis
        //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 49236 panagiotis
        return repository;
105
    }
106
107 49763 panagiotis
    public static Date convertStringToDate(String date){
108 49236 panagiotis
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 49763 panagiotis
    public static String convertDateToString(Date date){
122 49236 panagiotis
123 49908 panagiotis
        if(Objects.equals(date, null))
124 49236 panagiotis
            return null;
125
126 49763 panagiotis
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
127
        return formatter.format(date);
128 49236 panagiotis
    }
129
130
    public static Double toDouble(String number){
131
        if(Objects.equals(number, "null"))
132 49975 panagiotis
            return 0.0;
133 49236 panagiotis
        else
134
            return Double.valueOf(number);
135
    }
136
137 50219 panagiotis
    public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {
138 49236 panagiotis
139
        List<Repository> resultSet = new ArrayList<>();
140 50219 panagiotis
        JSONArray rs = json.getJSONArray("datasourceInfo");
141 49236 panagiotis
        for(int i=0;i<rs.length();i++)
142
            resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
143
        return resultSet;
144
    }
145
146 50631 panagiotis
    public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException {
147 49988 panagiotis
148
        List<RepositoryInterface> resultSet = new ArrayList<>();
149 50631 panagiotis
        JSONArray rs = json.getJSONArray("api");
150 49988 panagiotis
        for(int i=0;i<rs.length();i++)
151
            resultSet.add(jsonToRepositoryInterfaceObject( rs.getJSONObject(i)) );
152
        return resultSet;
153
    }
154
155 49763 panagiotis
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
156 49236 panagiotis
157
        RepositoryInterface repositoryInterface = new RepositoryInterface();
158
159 49763 panagiotis
        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
160
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
161 51525 panagiotis
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
162 49763 panagiotis
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
163 50051 panagiotis
        repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
164 51330 panagiotis
        repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString());
165 49236 panagiotis
166 51525 panagiotis
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
167
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
168 51330 panagiotis
169 51525 panagiotis
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 50051 panagiotis
        Map<String, String> accessParams = new HashMap<>();
176
        Map<String, String> extraFields = new HashMap<>();
177
178
        ObjectMapper mapper = new ObjectMapper();
179 50631 panagiotis
        JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiParams");
180 50051 panagiotis
181
        for(int i=0;i<apiparams.length();i++)
182
            accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value"));
183
184 51525 panagiotis
        repositoryInterface.setAccessParams(accessParams);
185
186 49236 panagiotis
        return repositoryInterface;
187
    }
188
189 49362 panagiotis
    public static String repositoryObjectToJson(Repository repository) throws JSONException {
190 49236 panagiotis
191
        JSONObject jsonObject = new JSONObject();
192 51330 panagiotis
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 51525 panagiotis
207
        /*
208
        * typology -> platform
209
        * datasource class -> typology
210
        * */
211
        jsonObject.put("typology",repository.getDatasourceClass());
212
        jsonObject.put("platform",repository.getTypology());
213
214 51330 panagiotis
        jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
215 49236 panagiotis
        jsonObject.put("activationId",repository.getActivationId());
216 51330 panagiotis
        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 51525 panagiotis
222 49236 panagiotis
        jsonObject.put("aggregator",repository.getAggregator());
223 51330 panagiotis
        jsonObject.put("collectedfrom",repository.getCollectedFrom());
224
225 51525 panagiotis
        jsonObject.put("managed",repository.isRegistered());
226 51330 panagiotis
227 51525 panagiotis
        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 51330 panagiotis
        //TODO check fields
242
       /* jsonObject.put("certificates",repository.getCertificates());
243 49236 panagiotis
        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 49763 panagiotis
        jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
255
        jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
256 49236 panagiotis
        jsonObject.put("serviceprovider",repository.getServiceProvider());
257
        jsonObject.put("versioning",repository.getVersioning());
258
        //datasource.get("platform");
259 51330 panagiotis
        //datasource.get("subjects");*/
260 49236 panagiotis
        return jsonObject.toString();
261
    }
262
263 49960 panagiotis
    public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException {
264 49236 panagiotis
265
        JSONObject jsonObject = new JSONObject();
266
267
        jsonObject.put("id",repositoryInterface.getId());
268
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
269 51525 panagiotis
        jsonObject.put("datasource",repository.getId());
270
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
271 49236 panagiotis
        jsonObject.put("typology",repositoryInterface.getTypology());
272
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
273 51525 panagiotis
        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 50051 panagiotis
        jsonObject.put("removable",repositoryInterface.isRemovable());
285
286 51525 panagiotis
287 50051 panagiotis
        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 51525 panagiotis
        jsonObject.put("apiParams",apiparams);
295 50051 panagiotis
296
297 51525 panagiotis
//        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
298 50051 panagiotis
299 51525 panagiotis
300 49236 panagiotis
        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 49790 panagiotis
            //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
308 49813 panagiotis
            InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
309 49770 panagiotis
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
310 49236 panagiotis
            while((line = br.readLine()) != null) {
311
                list.add(line.trim());
312
            }
313
            br.close();
314
        } catch (IOException e) {
315 49770 panagiotis
            LOGGER.debug("Error opening file!");
316 49236 panagiotis
            e.printStackTrace();
317
        }
318
        return list;
319
    }
320 49763 panagiotis
321 50845 panagiotis
    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject datasourceInfo) throws JSONException {
322 49763 panagiotis
323 50845 panagiotis
324 52781 panagiotis
      // if(datasourceInfo.get("aggregationHistory").toString().equals("[]"))
325
       //    return null;
326 49763 panagiotis
327 50845 panagiotis
        JSONArray rs = new JSONArray(datasourceInfo.get("aggregationHistory").toString());
328 49763 panagiotis
        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 50332 panagiotis
338 49763 panagiotis
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
339 50333 panagiotis
        if(aggregationObject.has("collectionMode"))
340
            aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
341 49763 panagiotis
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
342
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
343
        return aggregationDetails;
344
    }
345
346 50845 panagiotis
    public static AggregationDetails getLastCollectionFromJson(JSONObject datasourceInfo) throws JSONException {
347 49763 panagiotis
348 50845 panagiotis
        if( datasourceInfo.get("lastCollection").equals(null))
349 49763 panagiotis
            return null;
350
351 50845 panagiotis
        return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastCollection"));
352 49763 panagiotis
    }
353
354 50845 panagiotis
    public static AggregationDetails getLastTransformationFromJson(JSONObject datasourceInfo) throws JSONException {
355 49763 panagiotis
356 50845 panagiotis
        if( datasourceInfo.get("lastTransformation").equals(null))
357 49763 panagiotis
            return null;
358
359 50845 panagiotis
        return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastTransformation"));
360 49763 panagiotis
    }
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 51330 panagiotis
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 49236 panagiotis
}