Project

General

Profile

1
package eu.dnetlib.repo.manager.service.utils;
2

    
3
import eu.dnetlib.domain.data.Repository;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
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
import org.springframework.beans.factory.annotation.Autowired;
12

    
13
import java.io.*;
14
import java.text.ParseException;
15
import java.text.SimpleDateFormat;
16
import java.util.*;
17

    
18
public class Converter {
19

    
20
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
21
    
22
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
23

    
24
        Repository repository = new Repository();
25

    
26
        if( repositoryObject.get("datasource").equals(null))
27
            return null;
28

    
29
        JSONObject datasource = (JSONObject) repositoryObject.get("datasource");
30

    
31
        repository.setActivationId(datasource.get("activationId").toString());
32
        repository.setAggregator(datasource.get("aggregator").toString());
33
        repository.setCertificates(datasource.get("certificates").toString());
34
        repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString());
35
        repository.setCollectedFrom( datasource.get("collectedfrom").toString());
36

    
37
        repository.setContactEmail(datasource.get("contactemail").toString());
38
        if(repository.getContactEmail().equals("null"))
39
            repository.setContactEmail("");
40

    
41
        repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString());
42
        repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString());
43
        repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString());
44
        repository.setDataUploadType(datasource.get("datauploadtype").toString());
45
        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
46
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
47

    
48
        repository.setDescription(datasource.get("description").toString());
49
        if(repository.getDescription().equals("null"))
50
            repository.setDescription("");
51

    
52
        repository.setEissn(datasource.get("eissn").toString());
53

    
54
        repository.setEnglishName( datasource.get("englishname").toString());
55
        if(repository.getEnglishName().equals("null"))
56
            repository.setEnglishName("");
57

    
58

    
59
        repository.setId(datasource.get("id").toString());
60
        repository.setIssn(datasource.get("issn").toString());
61
        repository.setOdLanguages(datasource.get("languages").toString());
62
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
63
        repository.setLissn(datasource.get("lissn").toString());
64

    
65
        repository.setLogoUrl(datasource.get("logourl").toString());
66
        if(repository.getLogoUrl().equals("null"))
67
            repository.setLogoUrl("");
68

    
69
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
70
        //datasource.get("managed");
71
        repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString());
72
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
73
        repository.setOdContentTypes(datasource.get("od_contenttypes").toString());
74
        repository.setOfficialName(datasource.get("officialname").toString());
75
        if(repository.getOfficialName().equals("null"))
76
            repository.setOfficialName("");
77

    
78
        repository.setPidSystems(datasource.get("pidsystems").toString());
79
        //datasource.get("platform");
80
        repository.setProvenanceActionClass( datasource.get("provenanceaction").toString());
81
        repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString());
82
        repository.setRegisteredBy(datasource.get("registeredby").toString());
83
        repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString()));
84
        repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString()));
85
        repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString()));
86
        //datasource.get("subjects");
87
        Double timezone = toDouble(datasource.get("timezone").toString());
88
        repository.setTimezone(timezone!=null?timezone:0.0);
89
        repository.setTypology(datasource.get("platform").toString());
90
        repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString()));
91
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
92
        repository.setDatasourceClass(datasource.get("typology").toString());
93

    
94
        //TODO change organization to list
95
        repository.setOrganization( ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
96
        String countryCode = ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("country").toString();
97
        repository.setCountryCode(countryCode);
98

    
99
        return repository;
100
    }
101

    
102

    
103

    
104
    public static Date convertStringToDate(String date){
105

    
106
        if(Objects.equals(date, "null"))
107
            return null;
108

    
109
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
110
        try {
111
            return formatter.parse(date);
112
        } catch (ParseException e) {
113
            e.printStackTrace();
114
        }
115
        return null;
116
    }
117

    
118
    public static String convertDateToString(Date date){
119

    
120
        if(Objects.equals(date, null))
121
            return null;
122

    
123
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
124
        return formatter.format(date);
125
    }
126

    
127
    public static Double toDouble(String number){
128
        if(Objects.equals(number, "null"))
129
            return null;
130
        else
131
            return Double.valueOf(number);
132
    }
133

    
134
    public static List<Repository> jsonToRepositoryList(JSONArray rs) throws JSONException {
135

    
136
        List<Repository> resultSet = new ArrayList<>();
137
        for(int i=0;i<rs.length();i++)
138
            resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
139
        return resultSet;
140
    }
141

    
142
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
143

    
144
        RepositoryInterface repositoryInterface = new RepositoryInterface();
145

    
146
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
147
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
148
        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
149
        repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
150
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
151
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
152
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
153
        repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
154
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
155

    
156
        return repositoryInterface;
157
    }
158

    
159
    public static String repositoryObjectToJson(Repository repository) throws JSONException {
160

    
161
        JSONObject jsonObject = new JSONObject();
162
        jsonObject.put("activationId",repository.getActivationId());
163
        jsonObject.put("aggregator",repository.getAggregator());
164
        jsonObject.put("certificates",repository.getCertificates());
165
        jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
166
        jsonObject.put("collectedfrom",repository.getCollectedFrom());
167
        jsonObject.put("contactemail",repository.getContactEmail());
168
        jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
169
        jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
170
        jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
171
        jsonObject.put("datauploadtype",repository.getDataUploadType());
172
        jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
173
        jsonObject.put("dateofvalidation",convertDateToString(repository.getDateOfValidation()));
174
        jsonObject.put("description",repository.getDescription());
175
        jsonObject.put("eissn",repository.getEissn());
176
        jsonObject.put("englishname",repository.getEnglishName());
177
        jsonObject.put("id",repository.getId());
178
        jsonObject.put("issn",repository.getIssn());
179
        jsonObject.put("languages",repository.getOdLanguages());
180
        jsonObject.put("latitude",repository.getLatitude().toString());
181
        jsonObject.put("lissn",repository.getLissn());
182
        jsonObject.put("logourl",repository.getLogoUrl());
183
        jsonObject.put("longitude",repository.getLongitude().toString());
184
        jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
185
        jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
186
        jsonObject.put("od_contenttypes",repository.getOdContentTypes());
187
        jsonObject.put("officialname",repository.getOfficialName());
188
        jsonObject.put("pidsystems",repository.getPidSystems());
189
        jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
190
        jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
191
        jsonObject.put("registeredby",repository.getRegisteredBy());
192
        jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
193
        jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
194
        jsonObject.put("serviceprovider",repository.getServiceProvider());
195
        jsonObject.put("timezone",repository.getTimezone());
196
        jsonObject.put("typology",repository.getTypology());
197
        jsonObject.put("versioning",repository.getVersioning());
198
        jsonObject.put("websiteurl",repository.getWebsiteUrl());
199

    
200
        //datasource.get("managed");
201
        //datasource.get("platform");
202
        //datasource.get("subjects");
203
        return jsonObject.toString();
204
    }
205

    
206
    public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException {
207

    
208
        JSONObject jsonObject = new JSONObject();
209

    
210
        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
211
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
212
        jsonObject.put("id",repositoryInterface.getId());
213
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
214
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
215
        jsonObject.put("typology",repositoryInterface.getTypology());
216
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
217
        jsonObject.put("datasource",repository.getId());
218
        //jsonObject.put("lastAggregationDate");
219
        //jsonObject.put("lastAggregationMdid");
220
        //jsonObject.put("lastAggregationTotal");
221
        jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
222
        //jsonObject.put("lastCollectionMdid",repositoryInterface);
223
        //jsonObject.put("lastCollectionTotal");
224
        //jsonObject.put("lastDownloadDate");
225
//        jsonObject.put("lastDownloadMdid");
226
//        jsonObject.put("lastDownloadTotal");
227
//        jsonObject.put("lastValidationJob");
228
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
229
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
230
        jsonObject.put("typology",repositoryInterface.getTypology());
231
        //jsonObject.put("removable",repositoryInterface.getRemovable());
232
        //jsonObject.put("active",repositoryInterface.getActive());
233
        return jsonObject.toString();
234
    }
235

    
236
    public static ArrayList<String> readFile(String filename) {
237
        String line;
238
        ArrayList<String> list = new ArrayList<String>();
239
        try {
240
            //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
241
            InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
242
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
243
            while((line = br.readLine()) != null) {
244
                list.add(line.trim());
245
            }
246
            br.close();
247
        } catch (IOException e) {
248
            LOGGER.debug("Error opening file!");
249
            e.printStackTrace();
250
        }
251
        return list;
252
    }
253

    
254
    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject repositoryObject) throws JSONException {
255

    
256
        if( repositoryObject.get("aggregationHistory").toString().equals("[]") ||
257
                repositoryObject.get("aggregationHistory")!= null)
258
            return null;
259

    
260

    
261
        JSONArray rs = new JSONArray(repositoryObject.get("aggregationHistory").toString());
262
        List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
263
        for(int i=0;i<rs.length();i++)
264
            aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
265
        return aggregationDetailsList;
266
    }
267

    
268
    private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {
269

    
270
        AggregationDetails aggregationDetails = new AggregationDetails();
271
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
272
        //aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
273
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
274
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
275
        return aggregationDetails;
276
    }
277

    
278
    public static AggregationDetails getLastCollectionFromJson(JSONObject repositoryObject) throws JSONException {
279

    
280
        if( repositoryObject.get("lastCollection").equals(null))
281
            return null;
282

    
283
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastCollection"));
284
    }
285

    
286
    public static AggregationDetails getLastTransformationFromJson(JSONObject repositoryObject) throws JSONException {
287

    
288
        if( repositoryObject.get("lastTransformation").equals(null))
289
            return null;
290

    
291
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastTransformation"));
292
    }
293

    
294
    public static List<Timezone> toTimezones(List<String> timezones) {
295

    
296
        List<Timezone> tmz = new ArrayList<>();
297
        for(String t : timezones){
298
            String[] s = t.split("\t");
299
            tmz.add(new Timezone(s[1],Double.parseDouble(s[0])));
300
        }
301
        return tmz;
302
    }
303
}
(1-1/2)