Revision 60747
Added by Antonis Koulalis about 3 years ago
Converter.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.utils; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.core.JsonProcessingException; |
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
5 |
import eu.dnetlib.domain.data.Repository; |
|
6 |
import eu.dnetlib.domain.data.RepositoryInterface; |
|
7 |
import eu.dnetlib.repo.manager.domain.AggregationDetails; |
|
8 |
import eu.dnetlib.repo.manager.domain.RepositorySnippet; |
|
9 |
import eu.dnetlib.repo.manager.domain.Timezone; |
|
10 |
import org.apache.commons.codec.digest.DigestUtils; |
|
11 |
import org.apache.log4j.Logger; |
|
12 |
import org.json.JSONArray; |
|
13 |
import org.json.JSONException; |
|
14 |
import org.json.JSONObject; |
|
15 |
|
|
16 |
import java.io.BufferedReader; |
|
17 |
import java.io.IOException; |
|
18 |
import java.io.InputStream; |
|
19 |
import java.io.InputStreamReader; |
|
20 |
import java.text.ParseException; |
|
21 |
import java.text.SimpleDateFormat; |
|
22 |
import java.util.*; |
|
23 |
|
|
24 |
public class Converter { |
|
25 |
|
|
26 |
private static final Logger LOGGER = Logger.getLogger(Converter.class); |
|
27 |
|
|
28 |
public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException { |
|
29 |
|
|
30 |
Repository repository = new Repository(); |
|
31 |
|
|
32 |
JSONObject datasource = repositoryObject.getJSONObject("datasource"); |
|
33 |
|
|
34 |
//if( datasource.equals(null)) |
|
35 |
// return null; |
|
36 |
|
|
37 |
repository.setId(datasource.get("id").toString()); |
|
38 |
repository.setOfficialName(datasource.get("officialname").toString()); |
|
39 |
|
|
40 |
repository.setEnglishName( datasource.get("englishname").toString()); |
|
41 |
if(repository.getEnglishName().equals("null")) |
|
42 |
repository.setEnglishName(""); |
|
43 |
|
|
44 |
repository.setWebsiteUrl(datasource.get("websiteurl").toString()); |
|
45 |
if(repository.getWebsiteUrl().equals("null")) |
|
46 |
repository.setWebsiteUrl(""); |
|
47 |
|
|
48 |
repository.setLogoUrl(datasource.get("logourl").toString()); |
|
49 |
if(repository.getLogoUrl().equals("null")) |
|
50 |
repository.setLogoUrl(""); |
|
51 |
|
|
52 |
repository.setContactEmail(datasource.get("contactemail").toString()); |
|
53 |
if(repository.getContactEmail().equals("null")) |
|
54 |
repository.setContactEmail(""); |
|
55 |
|
|
56 |
|
|
57 |
repository.setLatitude( toDouble(datasource.get("latitude").toString())); |
|
58 |
repository.setLongitude(toDouble(datasource.get("longitude").toString())); |
|
59 |
Double timezone = toDouble(datasource.get("timezone").toString()); |
|
60 |
repository.setTimezone(timezone!=null?timezone:0.0); |
|
61 |
repository.setNamespacePrefix(datasource.get("namespaceprefix").toString()); |
|
62 |
repository.setOdLanguages(datasource.get("languages").toString()); |
|
63 |
repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString())); |
|
64 |
|
|
65 |
/* typology -> platform |
|
66 |
* datasource class -> typology */ |
|
67 |
repository.setTypology(datasource.get("platform").toString()); |
|
68 |
if(repository.getTypology().equals("null")) |
|
69 |
repository.setTypology(""); |
|
70 |
repository.setDatasourceClass(datasource.get("typology").toString()); |
|
71 |
|
|
72 |
repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString())); |
|
73 |
repository.setActivationId(datasource.get("activationId").toString()); |
|
74 |
|
|
75 |
repository.setDescription(datasource.get("description").toString()); |
|
76 |
if(repository.getDescription().equals("null")) |
|
77 |
repository.setDescription(""); |
|
78 |
|
|
79 |
repository.setIssn(datasource.get("issn").toString()); |
|
80 |
repository.setLissn(datasource.get("lissn").toString()); |
|
81 |
if(repository.getLissn().equals("null")) |
|
82 |
repository.setLissn(""); |
|
83 |
repository.setEissn(datasource.get("eissn").toString()); |
|
84 |
if(repository.getEissn().equals("null")) |
|
85 |
repository.setEissn(""); |
|
86 |
repository.setRegisteredBy(datasource.get("registeredby").toString()); |
|
87 |
|
|
88 |
/* managed field */ |
|
89 |
repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString())); |
|
90 |
|
|
91 |
//subjects |
|
92 |
|
|
93 |
repository.setAggregator(datasource.get("aggregator").toString()); |
|
94 |
repository.setCollectedFrom(datasource.get("collectedfrom").toString()); |
|
95 |
|
|
96 |
//TODO change organization to list |
|
97 |
JSONArray organizations = ((JSONArray)datasource.get("organizations")); |
|
98 |
if(organizations.length() != 0) { |
|
99 |
repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString()); |
|
100 |
String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString(); |
|
101 |
repository.setCountryCode(countryCode); |
|
102 |
} |
|
103 |
|
|
104 |
/* identities field */ |
|
105 |
|
|
106 |
return repository; |
|
107 |
} |
|
108 |
|
|
109 |
public static Date convertStringToDate(String date){ |
|
110 |
|
|
111 |
if(Objects.equals(date, "null")) |
|
112 |
return null; |
|
113 |
|
|
114 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
|
115 |
try { |
|
116 |
return formatter.parse(date); |
|
117 |
} catch (ParseException e) { |
|
118 |
LOGGER.error(e); |
|
119 |
} |
|
120 |
return null; |
|
121 |
} |
|
122 |
|
|
123 |
public static String convertDateToString(Date date){ |
|
124 |
|
|
125 |
if(Objects.equals(date, null)) |
|
126 |
return null; |
|
127 |
|
|
128 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
|
129 |
return formatter.format(date); |
|
130 |
} |
|
131 |
|
|
132 |
public static Double toDouble(String number){ |
|
133 |
if(Objects.equals(number, "null")) |
|
134 |
return 0.0; |
|
135 |
else |
|
136 |
return Double.valueOf(number); |
|
137 |
} |
|
138 |
|
|
139 |
public static List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException { |
|
140 |
|
|
141 |
List<RepositorySnippet> resultSet = new ArrayList<>(); |
|
142 |
JSONArray rs = json.getJSONArray("datasourceInfo"); |
|
143 |
for(int i=0;i<rs.length();i++) |
|
144 |
resultSet.add(jsonToRepositorySnippetObject( rs.getJSONObject(i)) ); |
|
145 |
return resultSet; |
|
146 |
} |
|
147 |
|
|
148 |
public static RepositorySnippet jsonToRepositorySnippetObject(JSONObject repositorySnippetObject) throws JSONException { |
|
149 |
|
|
150 |
|
|
151 |
RepositorySnippet repositorySnippet = new RepositorySnippet(); |
|
152 |
|
|
153 |
// JSONObject datasource = repositorySnippetObject.getJSONObject("datasource"); |
|
154 |
|
|
155 |
|
|
156 |
repositorySnippet.setId(repositorySnippetObject.get("id").toString()); |
|
157 |
repositorySnippet.setOfficialname(repositorySnippetObject.get("officialname").toString()); |
|
158 |
|
|
159 |
repositorySnippet.setEnglishname( repositorySnippetObject.get("englishname").toString()); |
|
160 |
if(repositorySnippet.getEnglishname().equals("null")) |
|
161 |
repositorySnippet.setEnglishname(""); |
|
162 |
|
|
163 |
repositorySnippet.setWebsiteurl(repositorySnippetObject.get("websiteurl").toString()); |
|
164 |
if(repositorySnippet.getWebsiteurl().equals("null")) |
|
165 |
repositorySnippet.setWebsiteurl(""); |
|
166 |
|
|
167 |
repositorySnippet.setRegisteredby(repositorySnippetObject.get("registeredby").toString()); |
|
168 |
if(repositorySnippet.getRegisteredby().equals("null")) |
|
169 |
repositorySnippet.setRegisteredby(""); |
|
170 |
return repositorySnippet; |
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 |
public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException { |
|
175 |
|
|
176 |
List<Repository> resultSet = new ArrayList<>(); |
|
177 |
JSONArray rs = json.getJSONArray("datasourceInfo"); |
|
178 |
for(int i=0;i<rs.length();i++) |
|
179 |
resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) ); |
|
180 |
return resultSet; |
|
181 |
} |
|
182 |
|
|
183 |
public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException { |
|
184 |
|
|
185 |
List<RepositoryInterface> resultSet = new ArrayList<>(); |
|
186 |
JSONArray rs = json.getJSONArray("api"); |
|
187 |
for(int i=0;i<rs.length();i++) |
|
188 |
resultSet.add(jsonToRepositoryInterfaceObject( rs.getJSONObject(i)) ); |
|
189 |
return resultSet; |
|
190 |
} |
|
191 |
|
|
192 |
public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException { |
|
193 |
|
|
194 |
RepositoryInterface repositoryInterface = new RepositoryInterface(); |
|
195 |
|
|
196 |
repositoryInterface.setId(repositoryInterfaceObject.get("id").toString()); |
|
197 |
repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString()); |
|
198 |
repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString()); |
|
199 |
repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString()); |
|
200 |
repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString()); |
|
201 |
repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString()); |
|
202 |
|
|
203 |
repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString()); |
|
204 |
repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString())); |
|
205 |
|
|
206 |
|
|
207 |
// repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString()); |
|
208 |
repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString()); |
|
209 |
//repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString())); |
|
210 |
|
|
211 |
|
|
212 |
Map<String, String> accessParams = new HashMap<>(); |
|
213 |
Map<String, String> extraFields = new HashMap<>(); |
|
214 |
|
|
215 |
ObjectMapper mapper = new ObjectMapper(); |
|
216 |
JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiParams"); |
|
217 |
|
|
218 |
for(int i=0;i<apiparams.length();i++) |
|
219 |
accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value")); |
|
220 |
|
|
221 |
repositoryInterface.setAccessParams(accessParams); |
|
222 |
|
|
223 |
return repositoryInterface; |
|
224 |
} |
|
225 |
|
|
226 |
public static String repositoryObjectToJson(Repository repository) throws JSONException, JsonProcessingException { |
|
227 |
|
|
228 |
HashMap<String,Object> repositoryMap = new HashMap<>(); |
|
229 |
ObjectMapper mapper = new ObjectMapper(); |
|
230 |
|
|
231 |
repositoryMap.put("id",repository.getId()); |
|
232 |
repositoryMap.put("openaireId",getOpenaireId(repository.getId())); |
|
233 |
repositoryMap.put("officialname",repository.getOfficialName()); |
|
234 |
repositoryMap.put("englishname",repository.getEnglishName()); |
|
235 |
repositoryMap.put("websiteurl",repository.getWebsiteUrl()); |
|
236 |
repositoryMap.put("logourl",repository.getLogoUrl()); |
|
237 |
repositoryMap.put("contactemail",repository.getContactEmail()); |
|
238 |
repositoryMap.put("longitude",repository.getLongitude().toString()); |
|
239 |
repositoryMap.put("latitude",repository.getLatitude().toString()); |
|
240 |
repositoryMap.put("timezone",repository.getTimezone()); |
|
241 |
|
|
242 |
repositoryMap.put("namespaceprefix",repository.getNamespacePrefix()!=null?repository.getNamespacePrefix():""); |
|
243 |
repositoryMap.put("languages",repository.getOdLanguages()!=null?repository.getOdLanguages():""); |
|
244 |
|
|
245 |
repositoryMap.put("dateofcollection",repository.getDateOfCollection()!=null?convertDateToString(repository.getDateOfCollection()):""); |
|
246 |
|
|
247 |
/* |
|
248 |
* typology -> platform |
|
249 |
* datasource class -> typology |
|
250 |
* */ |
|
251 |
repositoryMap.put("typology",repository.getDatasourceClass()); |
|
252 |
repositoryMap.put("platform",repository.getTypology()); |
|
253 |
|
|
254 |
repositoryMap.put("dateofvalidation",repository.getDateOfCollection()!=null?convertDateToString(repository.getDateOfCollection()):""); |
|
255 |
repositoryMap.put("activationId",repository.getActivationId()!=null?repository.getActivationId():""); |
|
256 |
|
|
257 |
repositoryMap.put("description",repository.getDescription()); |
|
258 |
|
|
259 |
repositoryMap.put("eissn",repository.getEissn()!=null?repository.getEissn():""); |
|
260 |
repositoryMap.put("issn",repository.getIssn()!=null?repository.getIssn():""); |
|
261 |
repositoryMap.put("lissn",repository.getLissn()!=null?repository.getLissn():""); |
|
262 |
|
|
263 |
repositoryMap.put("registeredby",repository.getRegisteredBy()); |
|
264 |
|
|
265 |
repositoryMap.put("aggregator",repository.getAggregator()!=null?repository.getAggregator():""); |
|
266 |
repositoryMap.put("collectedfrom",repository.getCollectedFrom()!=null?repository.getCollectedFrom():""); |
|
267 |
|
|
268 |
repositoryMap.put("managed",repository.isRegistered()); |
|
269 |
|
|
270 |
Map<String,String> organization = new HashMap<>(); |
|
271 |
organization.put("legalname",repository.getOrganization()); |
|
272 |
organization.put("country",repository.getCountryCode()); |
|
273 |
organization.put("legalshortname",""); |
|
274 |
organization.put("websiteurl",""); |
|
275 |
organization.put("logourl",""); |
|
276 |
|
|
277 |
List organizations = new ArrayList(); |
|
278 |
organizations.add(organization); |
|
279 |
repositoryMap.put("organizations",organizations); |
|
280 |
|
|
281 |
//TODO check identitites |
|
282 |
//Map<String,String> identity = new HashMap<>(); |
|
283 |
|
|
284 |
if (repository.getPiwikInfo() != null) { |
|
285 |
Map<String, Object> identity = new HashMap<>(); |
|
286 |
HashSet<Map<String, Object>> identities = new HashSet<>(); |
|
287 |
|
|
288 |
identity.put("issuertype", "piwik"); |
|
289 |
identity.put("pid", "piwik:" + repository.getPiwikInfo().getSiteId()); |
|
290 |
|
|
291 |
identities.add(identity); |
|
292 |
|
|
293 |
repositoryMap.put("identities", identities); |
|
294 |
} |
|
295 |
|
|
296 |
repositoryMap.put("subjects",""); |
|
297 |
|
|
298 |
return mapper.writeValueAsString(repositoryMap); |
|
299 |
} |
|
300 |
|
|
301 |
public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException { |
|
302 |
|
|
303 |
JSONObject jsonObject = new JSONObject(); |
|
304 |
|
|
305 |
jsonObject.put("id",repositoryInterface.getId()); |
|
306 |
jsonObject.put("protocol",repositoryInterface.getAccessProtocol()); |
|
307 |
jsonObject.put("datasource",repository.getId()); |
|
308 |
jsonObject.put("contentdescription",repositoryInterface.getContentDescription()); |
|
309 |
jsonObject.put("typology",repositoryInterface.getTypology()); |
|
310 |
jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel()); |
|
311 |
jsonObject.put("compatibilityOverride",repositoryInterface.getDesiredCompatibilityLevel()); |
|
312 |
|
|
313 |
jsonObject.put("lastCollectionTotal",""); |
|
314 |
|
|
315 |
jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate()); |
|
316 |
jsonObject.put("lastAggregationTotal",""); |
|
317 |
jsonObject.put("lastAggregationDate",""); |
|
318 |
jsonObject.put("lastDownloadTotal",""); |
|
319 |
jsonObject.put("lastDownloadDate",""); |
|
320 |
|
|
321 |
jsonObject.put("baseurl",repositoryInterface.getBaseUrl()); |
|
322 |
jsonObject.put("removable",repositoryInterface.isRemovable()); |
|
323 |
|
|
324 |
|
|
325 |
JSONArray apiparams = new JSONArray(); |
|
326 |
for(String param: repositoryInterface.getAccessParams().keySet()){ |
|
327 |
JSONObject jo = new JSONObject(); |
|
328 |
jo.put("param",param); |
|
329 |
jo.put("value",repositoryInterface.getAccessParams().get(param)); |
|
330 |
apiparams.put(jo); |
|
331 |
} |
|
332 |
jsonObject.put("apiParams",apiparams); |
|
333 |
|
|
334 |
|
|
335 |
// jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath()); |
|
336 |
|
|
337 |
|
|
338 |
return jsonObject.toString(); |
|
339 |
} |
|
340 |
|
|
341 |
public static ArrayList<String> readFile(String filename) { |
|
342 |
String line; |
|
343 |
ArrayList<String> list = new ArrayList<String>(); |
|
344 |
try { |
|
345 |
//InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename); |
|
346 |
InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename); |
|
347 |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
|
348 |
while((line = br.readLine()) != null) { |
|
349 |
list.add(line.trim()); |
|
350 |
} |
|
351 |
br.close(); |
|
352 |
} catch (IOException e) { |
|
353 |
LOGGER.debug("Error opening file!"); |
|
354 |
LOGGER.error(e); |
|
355 |
} |
|
356 |
return list; |
|
357 |
} |
|
358 |
|
|
359 |
public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject datasourceInfo) throws JSONException { |
|
360 |
JSONArray rs; |
|
361 |
List<AggregationDetails> aggregationDetailsList = new ArrayList<>(); |
|
362 |
|
|
363 |
if (datasourceInfo.get("aggregationHistory") != null && !datasourceInfo.get("aggregationHistory").toString().equals("null")) { |
|
364 |
rs = new JSONArray(datasourceInfo.get("aggregationHistory").toString()); |
|
365 |
|
|
366 |
for (int i = 0; i < rs.length(); i++) |
|
367 |
aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i))); |
|
368 |
} |
|
369 |
|
|
370 |
return aggregationDetailsList; |
|
371 |
} |
|
372 |
|
|
373 |
private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException { |
|
374 |
|
|
375 |
AggregationDetails aggregationDetails = new AggregationDetails(); |
|
376 |
|
|
377 |
aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString()); |
|
378 |
if(aggregationObject.has("collectionMode")) |
|
379 |
aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString()); |
|
380 |
if(aggregationObject.has("indexedVersion")) |
|
381 |
aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString())); |
|
382 |
aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString())); |
|
383 |
aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString())); |
|
384 |
return aggregationDetails; |
|
385 |
} |
|
386 |
|
|
387 |
|
|
388 |
public static List<Timezone> toTimezones(List<String> timezones) { |
|
389 |
|
|
390 |
List<Timezone> tmz = new ArrayList<>(); |
|
391 |
for(String t : timezones){ |
|
392 |
String[] s = t.split("\t"); |
|
393 |
tmz.add(new Timezone(s[1],Double.parseDouble(s[0]))); |
|
394 |
} |
|
395 |
return tmz; |
|
396 |
} |
|
397 |
|
|
398 |
private static String getOpenaireId(String repositoryId) { |
|
399 |
if (repositoryId != null && repositoryId.contains("::")) |
|
400 |
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]); |
|
401 |
return null; |
|
402 |
} |
|
403 |
|
|
404 |
public static String convertRepoIdToRoleId(String repoId) { |
|
405 |
if ( repoId != null ) { |
|
406 |
return "datasource." + repoId.replaceAll(":", "$"); |
|
407 |
} |
|
408 |
return null; |
|
409 |
} |
|
410 |
|
|
411 |
} |
Also available in: Unified diff