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.BufferedReader;
12
import java.io.File;
13
import java.io.FileReader;
14
import java.io.IOException;
15
import java.text.ParseException;
16
import java.text.SimpleDateFormat;
17
import java.util.*;
18

    
19
public class Converter {
20

    
21
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
22

    
23
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
24

    
25
        Repository repository = new Repository();
26

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

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

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

    
76
    public static Date convertStringToDate(String date){
77

    
78
        if(Objects.equals(date, "null"))
79
            return null;
80

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

    
90
    public static String convertDateToString(Date date){
91

    
92
        if(Objects.equals(date, "null"))
93
            return null;
94

    
95
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
96
        return formatter.format(date);
97
    }
98

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

    
106
    public static List<Repository> jsonToRepositoryList(JSONArray rs) throws JSONException {
107

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

    
114
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
115

    
116
        RepositoryInterface repositoryInterface = new RepositoryInterface();
117

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

    
128
        return repositoryInterface;
129
    }
130

    
131
    public static String repositoryObjectToJson(Repository repository) throws JSONException {
132

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

    
172
        //datasource.get("managed");
173
        //datasource.get("platform");
174
        //datasource.get("subjects");
175
        return jsonObject.toString();
176
    }
177

    
178
    public static String repositoryInterfaceObjectToJson(RepositoryInterface repositoryInterface) throws JSONException {
179

    
180
        JSONObject jsonObject = new JSONObject();
181

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

    
194
    public static ArrayList<String> readFile(String filename) {
195
        String line;
196
        ArrayList<String> list = new ArrayList<String>();
197
        try {
198
            File file = new File(Converter.class.getResource("/eu/dnetlib/repo/manager/service/utils/"+filename).getFile());
199
            BufferedReader br = new BufferedReader(new FileReader(file));
200
            while((line = br.readLine()) != null) {
201
                list.add(line.trim());
202
            }
203
            br.close();
204
        } catch (IOException e) {
205
            e.printStackTrace();
206
        }
207
        return list;
208
    }
209

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

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

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

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

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

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

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

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

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

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

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

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

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