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.shared.*;
6
import org.apache.log4j.Logger;
7
import org.json.JSONArray;
8
import org.json.JSONException;
9
import org.json.JSONObject;
10

    
11
import java.io.*;
12
import java.text.ParseException;
13
import java.text.SimpleDateFormat;
14
import java.util.*;
15

    
16
public class Converter {
17

    
18
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
19

    
20
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
21

    
22
        Repository repository = new Repository();
23

    
24
        if( repositoryObject.get("datasource").equals(null))
25
            return null;
26

    
27
        JSONObject datasource = (JSONObject) repositoryObject.get("datasource");
28

    
29
        repository.setActivationId(datasource.get("activationId").toString());
30
        repository.setAggregator(datasource.get("aggregator").toString());
31
        repository.setCertificates(datasource.get("certificates").toString());
32
        repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString());
33
        repository.setCollectedFrom( datasource.get("collectedfrom").toString());
34
        repository.setContactEmail(datasource.get("contactemail").toString());
35
        repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString());
36
        repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString());
37
        repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString());
38
        repository.setDataUploadType(datasource.get("datauploadtype").toString());
39
        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
40
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
41
        repository.setDescription(datasource.get("description").toString());
42
        repository.setEissn(datasource.get("eissn").toString());
43
        repository.setEnglishName( datasource.get("englishname").toString());
44
        repository.setId(datasource.get("id").toString());
45
        repository.setIssn(datasource.get("issn").toString());
46
        repository.setOdLanguages(datasource.get("languages").toString());
47
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
48
        repository.setLissn(datasource.get("lissn").toString());
49
        repository.setLogoUrl(datasource.get("logourl").toString());
50
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
51
        //datasource.get("managed");
52
        repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString());
53
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
54
        repository.setOdContentTypes(datasource.get("od_contenttypes").toString());
55
        repository.setOfficialName(datasource.get("officialname").toString());
56
        repository.setPidSystems(datasource.get("pidsystems").toString());
57
        //datasource.get("platform");
58
        repository.setProvenanceActionClass( datasource.get("provenanceaction").toString());
59
        repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString());
60
        repository.setRegisteredBy(datasource.get("registeredby").toString());
61
        repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString()));
62
        repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString()));
63
        repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString()));
64
        //datasource.get("subjects");
65
        Double timezone = toDouble(datasource.get("timezone").toString());
66
        repository.setTimezone(timezone!=null?timezone:0.0);
67
        repository.setTypology(datasource.get("typology").toString());
68
        repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString()));
69
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
70
        return repository;
71
    }
72

    
73
    public static Date convertStringToDate(String date){
74

    
75
        if(Objects.equals(date, "null"))
76
            return null;
77

    
78
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
79
        try {
80
            return formatter.parse(date);
81
        } catch (ParseException e) {
82
            e.printStackTrace();
83
        }
84
        return null;
85
    }
86

    
87
    public static String convertDateToString(Date date){
88

    
89
        if(Objects.equals(date, "null"))
90
            return null;
91

    
92
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
93
        return formatter.format(date);
94
    }
95

    
96
    public static Double toDouble(String number){
97
        if(Objects.equals(number, "null"))
98
            return null;
99
        else
100
            return Double.valueOf(number);
101
    }
102

    
103
    public static List<Repository> jsonToRepositoryList(JSONArray rs) throws JSONException {
104

    
105
        List<Repository> resultSet = new ArrayList<>();
106
        for(int i=0;i<rs.length();i++)
107
            resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
108
        return resultSet;
109
    }
110

    
111
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
112

    
113
        RepositoryInterface repositoryInterface = new RepositoryInterface();
114

    
115
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
116
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
117
        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
118
        repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
119
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
120
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
121
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
122
        repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
123
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
124

    
125
        return repositoryInterface;
126
    }
127

    
128
    public static String repositoryObjectToJson(Repository repository) throws JSONException {
129

    
130
        JSONObject jsonObject = new JSONObject();
131
        jsonObject.put("activationId",repository.getActivationId());
132
        jsonObject.put("aggregator",repository.getAggregator());
133
        jsonObject.put("certificates",repository.getCertificates());
134
        jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
135
        jsonObject.put("collectedfrom",repository.getCollectedFrom());
136
        jsonObject.put("contactemail",repository.getContactEmail());
137
        jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
138
        jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
139
        jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
140
        jsonObject.put("datauploadtype",repository.getDataUploadType());
141
        jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
142
        jsonObject.put("dateofvalidation",convertDateToString(repository.getDateOfValidation()));
143
        jsonObject.put("description",repository.getDescription());
144
        jsonObject.put("eissn",repository.getEissn());
145
        jsonObject.put("englishname",repository.getEnglishName());
146
        jsonObject.put("id",repository.getId());
147
        jsonObject.put("issn",repository.getIssn());
148
        jsonObject.put("languages",repository.getOdLanguages());
149
        jsonObject.put("latitude",repository.getLatitude().toString());
150
        jsonObject.put("lissn",repository.getLissn());
151
        jsonObject.put("logourl",repository.getLogoUrl());
152
        jsonObject.put("longitude",repository.getLongitude().toString());
153
        jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
154
        jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
155
        jsonObject.put("od_contenttypes",repository.getOdContentTypes());
156
        jsonObject.put("officialname",repository.getOfficialName());
157
        jsonObject.put("pidsystems",repository.getPidSystems());
158
        jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
159
        jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
160
        jsonObject.put("registeredby",repository.getRegisteredBy());
161
        jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
162
        jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
163
        jsonObject.put("serviceprovider",repository.getServiceProvider());
164
        jsonObject.put("timezone",repository.getTimezone());
165
        jsonObject.put("typology",repository.getTypology());
166
        jsonObject.put("versioning",repository.getVersioning());
167
        jsonObject.put("websiteurl",repository.getWebsiteUrl());
168

    
169
        //datasource.get("managed");
170
        //datasource.get("platform");
171
        //datasource.get("subjects");
172
        return jsonObject.toString();
173
    }
174

    
175
    public static String repositoryInterfaceObjectToJson(RepositoryInterface repositoryInterface) throws JSONException {
176

    
177
        JSONObject jsonObject = new JSONObject();
178

    
179
        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
180
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
181
        jsonObject.put("id",repositoryInterface.getId());
182
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
183
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
184
        jsonObject.put("typology",repositoryInterface.getTypology());
185
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
186
        //jsonObject.put("removable",repositoryInterface.getRemovable());
187
        //jsonObject.put("active",repositoryInterface.getActive());
188
        return jsonObject.toString();
189
    }
190

    
191
    public static ArrayList<String> readFile(String filename) {
192
        String line;
193
        ArrayList<String> list = new ArrayList<String>();
194
        try {
195
            //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
196
            InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
197
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
198
            while((line = br.readLine()) != null) {
199
                list.add(line.trim());
200
            }
201
            br.close();
202
        } catch (IOException e) {
203
            LOGGER.debug("Error opening file!");
204
            e.printStackTrace();
205
        }
206
        return list;
207
    }
208

    
209
    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject repositoryObject) throws JSONException {
210

    
211
        if( repositoryObject.get("aggregationHistory").toString().equals("[]"))
212
            return null;
213

    
214
        JSONArray rs = new JSONArray(repositoryObject.get("aggregationHistory").toString());
215
        List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
216
        for(int i=0;i<rs.length();i++)
217
            aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
218
        return aggregationDetailsList;
219
    }
220

    
221
    private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {
222

    
223
        AggregationDetails aggregationDetails = new AggregationDetails();
224
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
225
        aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
226
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
227
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
228
        return aggregationDetails;
229
    }
230

    
231
    public static AggregationDetails getLastCollectionFromJson(JSONObject repositoryObject) throws JSONException {
232

    
233
        if( repositoryObject.get("lastCollection").equals(null))
234
            return null;
235

    
236
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastCollection"));
237
    }
238

    
239
    public static AggregationDetails getLastTransformationFromJson(JSONObject repositoryObject) throws JSONException {
240

    
241
        if( repositoryObject.get("lastTransformation").equals(null))
242
            return null;
243

    
244
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastTransformation"));
245
    }
246

    
247
    public static List<Timezone> toTimezones(List<String> timezones) {
248

    
249
        List<Timezone> tmz = new ArrayList<>();
250
        for(String t : timezones){
251
            String[] s = t.split("\t");
252
            tmz.add(new Timezone(s[1],Double.parseDouble(s[0])));
253
        }
254
        return tmz;
255
    }
256
}
(1-1/2)