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 org.json.JSONArray;
|
6
|
import org.json.JSONException;
|
7
|
import org.json.JSONObject;
|
8
|
|
9
|
import java.io.BufferedReader;
|
10
|
import java.io.File;
|
11
|
import java.io.FileReader;
|
12
|
import java.io.IOException;
|
13
|
import java.text.ParseException;
|
14
|
import java.text.SimpleDateFormat;
|
15
|
import java.util.*;
|
16
|
|
17
|
public class Converter {
|
18
|
|
19
|
public static Repository jsonToRepositoryObject(JSONObject jsonObject) throws JSONException {
|
20
|
|
21
|
Repository repository = new Repository();
|
22
|
JSONObject datasource = (JSONObject) jsonObject.get("datasource");
|
23
|
|
24
|
repository.setActivationId(datasource.get("activationId").toString());
|
25
|
repository.setAggregator(datasource.get("aggregator").toString());
|
26
|
repository.setCertificates(datasource.get("certificates").toString());
|
27
|
repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString());
|
28
|
repository.setCollectedFrom( datasource.get("collectedfrom").toString());
|
29
|
repository.setContactEmail(datasource.get("contactemail").toString());
|
30
|
repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString());
|
31
|
repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString());
|
32
|
repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString());
|
33
|
repository.setDataUploadType(datasource.get("datauploadtype").toString());
|
34
|
repository.setDateOfCollection(convertDate( datasource.get("dateofcollection").toString()));
|
35
|
repository.setDateOfValidation(convertDate( datasource.get("dateofvalidation").toString()));
|
36
|
repository.setDescription(datasource.get("description").toString());
|
37
|
repository.setEissn(datasource.get("eissn").toString());
|
38
|
repository.setEnglishName( datasource.get("englishname").toString());
|
39
|
repository.setId(datasource.get("id").toString());
|
40
|
repository.setIssn(datasource.get("issn").toString());
|
41
|
repository.setOdLanguages(datasource.get("languages").toString());
|
42
|
repository.setLatitude( toDouble(datasource.get("latitude").toString()));
|
43
|
repository.setLissn(datasource.get("lissn").toString());
|
44
|
repository.setLogoUrl(datasource.get("logourl").toString());
|
45
|
repository.setLongitude(toDouble(datasource.get("longitude").toString()));
|
46
|
//datasource.get("managed");
|
47
|
repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString());
|
48
|
repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
|
49
|
repository.setOdContentTypes(datasource.get("od_contenttypes").toString());
|
50
|
repository.setOfficialName(datasource.get("officialname").toString());
|
51
|
repository.setPidSystems(datasource.get("pidsystems").toString());
|
52
|
//datasource.get("platform");
|
53
|
repository.setProvenanceActionClass( datasource.get("provenanceaction").toString());
|
54
|
repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString());
|
55
|
repository.setRegisteredBy(datasource.get("registeredby").toString());
|
56
|
repository.setReleaseEndDate(convertDate(datasource.get("releaseenddate").toString()));
|
57
|
repository.setReleaseStartDate(convertDate(datasource.get("releasestartdate").toString()));
|
58
|
repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString()));
|
59
|
//datasource.get("subjects");
|
60
|
Double timezone = toDouble(datasource.get("timezone").toString());
|
61
|
repository.setTimezone(timezone!=null?timezone:0.0);
|
62
|
repository.setTypology(datasource.get("typology").toString());
|
63
|
repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString()));
|
64
|
repository.setWebsiteUrl(datasource.get("websiteurl").toString());
|
65
|
return repository;
|
66
|
}
|
67
|
|
68
|
public static Date convertDate(String date){
|
69
|
|
70
|
if(Objects.equals(date, "null"))
|
71
|
return null;
|
72
|
|
73
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
74
|
try {
|
75
|
return formatter.parse(date);
|
76
|
} catch (ParseException e) {
|
77
|
e.printStackTrace();
|
78
|
}
|
79
|
return null;
|
80
|
}
|
81
|
|
82
|
public static String convertDateToString(String date){
|
83
|
|
84
|
if(Objects.equals(date, "null"))
|
85
|
return null;
|
86
|
|
87
|
SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
|
88
|
|
89
|
Date d = null;
|
90
|
try {
|
91
|
d = (Date)formatter.parse(date);
|
92
|
Calendar cal = Calendar.getInstance();
|
93
|
cal.setTime(d);
|
94
|
return cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR);
|
95
|
|
96
|
} catch (ParseException e) {
|
97
|
e.printStackTrace();
|
98
|
}
|
99
|
return null;
|
100
|
}
|
101
|
|
102
|
public static Double toDouble(String number){
|
103
|
if(Objects.equals(number, "null"))
|
104
|
return null;
|
105
|
else
|
106
|
return Double.valueOf(number);
|
107
|
}
|
108
|
|
109
|
public static List<Repository> jsonToRepositoryList(JSONArray rs) throws JSONException {
|
110
|
|
111
|
List<Repository> resultSet = new ArrayList<>();
|
112
|
for(int i=0;i<rs.length();i++)
|
113
|
resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
|
114
|
return resultSet;
|
115
|
}
|
116
|
|
117
|
public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject jsonObject) throws JSONException {
|
118
|
|
119
|
RepositoryInterface repositoryInterface = new RepositoryInterface();
|
120
|
|
121
|
repositoryInterface.setBaseUrl(jsonObject.get("baseurl").toString());
|
122
|
repositoryInterface.setContentDescription(jsonObject.get("contentdescription").toString());
|
123
|
repositoryInterface.setId(jsonObject.get("id").toString());
|
124
|
repositoryInterface.setMetadataIdentifierPath(jsonObject.get("metadataIdentifierPath").toString());
|
125
|
repositoryInterface.setAccessProtocol(jsonObject.get("protocol").toString());
|
126
|
repositoryInterface.setTypology(jsonObject.get("typology").toString());
|
127
|
repositoryInterface.setDesiredCompatibilityLevel(jsonObject.get("compatibility").toString());
|
128
|
repositoryInterface.setActive(Boolean.parseBoolean(jsonObject.get("active").toString()));
|
129
|
repositoryInterface.setRemovable(Boolean.parseBoolean(jsonObject.get("removable").toString()));
|
130
|
|
131
|
return repositoryInterface;
|
132
|
}
|
133
|
|
134
|
public static String repositoryObjectToJson(Repository repository) throws JSONException {
|
135
|
|
136
|
JSONObject jsonObject = new JSONObject();
|
137
|
jsonObject.put("activationId",repository.getActivationId());
|
138
|
jsonObject.put("aggregator",repository.getAggregator());
|
139
|
jsonObject.put("certificates",repository.getCertificates());
|
140
|
jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
|
141
|
jsonObject.put("collectedfrom",repository.getCollectedFrom());
|
142
|
jsonObject.put("contactemail",repository.getContactEmail());
|
143
|
jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
|
144
|
jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
|
145
|
jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
|
146
|
jsonObject.put("datauploadtype",repository.getDataUploadType());
|
147
|
jsonObject.put("dateofcollection",convertDateToString(String.valueOf(repository.getDateOfCollection())));
|
148
|
jsonObject.put("dateofvalidation",convertDateToString(String.valueOf(repository.getDateOfValidation())));
|
149
|
jsonObject.put("description",repository.getDescription());
|
150
|
jsonObject.put("eissn",repository.getEissn());
|
151
|
jsonObject.put("englishname",repository.getEnglishName());
|
152
|
jsonObject.put("id",repository.getId());
|
153
|
jsonObject.put("issn",repository.getIssn());
|
154
|
jsonObject.put("languages",repository.getOdLanguages());
|
155
|
jsonObject.put("latitude",repository.getLatitude().toString());
|
156
|
jsonObject.put("lissn",repository.getLissn());
|
157
|
jsonObject.put("logourl",repository.getLogoUrl());
|
158
|
jsonObject.put("longitude",repository.getLongitude().toString());
|
159
|
jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
|
160
|
jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
|
161
|
jsonObject.put("od_contenttypes",repository.getOdContentTypes());
|
162
|
jsonObject.put("officialname",repository.getOfficialName());
|
163
|
jsonObject.put("pidsystems",repository.getPidSystems());
|
164
|
jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
|
165
|
jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
|
166
|
jsonObject.put("registeredby",repository.getRegisteredBy());
|
167
|
jsonObject.put("releaseenddate",convertDateToString(String.valueOf(repository.getReleaseEndDate())));
|
168
|
jsonObject.put("releasestartdate",convertDateToString(String.valueOf(repository.getReleaseStartDate())));
|
169
|
jsonObject.put("serviceprovider",repository.getServiceProvider());
|
170
|
jsonObject.put("timezone",repository.getTimezone());
|
171
|
jsonObject.put("typology",repository.getTypology());
|
172
|
jsonObject.put("versioning",repository.getVersioning());
|
173
|
jsonObject.put("websiteurl",repository.getWebsiteUrl());
|
174
|
|
175
|
//datasource.get("managed");
|
176
|
//datasource.get("platform");
|
177
|
//datasource.get("subjects");
|
178
|
return jsonObject.toString();
|
179
|
}
|
180
|
|
181
|
public static String repositoryInterfaceObjectToJson(RepositoryInterface repositoryInterface) throws JSONException {
|
182
|
|
183
|
JSONObject jsonObject = new JSONObject();
|
184
|
|
185
|
jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
|
186
|
jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
|
187
|
jsonObject.put("id",repositoryInterface.getId());
|
188
|
jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
|
189
|
jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
|
190
|
jsonObject.put("typology",repositoryInterface.getTypology());
|
191
|
jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
|
192
|
//jsonObject.put("removable",repositoryInterface.getRemovable());
|
193
|
//jsonObject.put("active",repositoryInterface.getActive());
|
194
|
return jsonObject.toString();
|
195
|
}
|
196
|
|
197
|
public static ArrayList<String> readFile(String filename) {
|
198
|
String line;
|
199
|
ArrayList<String> list = new ArrayList<String>();
|
200
|
try {
|
201
|
File file = new File(Converter.class.getResource("/eu/dnetlib/repo/manager/service/utils/"+filename).getFile());
|
202
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
203
|
while((line = br.readLine()) != null) {
|
204
|
list.add(line.trim());
|
205
|
}
|
206
|
br.close();
|
207
|
} catch (IOException e) {
|
208
|
e.printStackTrace();
|
209
|
}
|
210
|
return list;
|
211
|
}
|
212
|
}
|