Revision 49963
Added by Panagiotis Kanakakis over 6 years ago
- Organize code for storing/updating repository/interface.
RepositoryApiImpl.java | ||
---|---|---|
22 | 22 |
import org.springframework.web.util.UriComponentsBuilder; |
23 | 23 |
import javax.annotation.PostConstruct; |
24 | 24 |
import java.sql.Timestamp; |
25 |
import java.text.Normalizer; |
|
25 | 26 |
import java.util.*; |
26 | 27 |
import java.util.concurrent.ConcurrentHashMap; |
27 | 28 |
import eu.dnetlib.repo.manager.shared.*; |
... | ... | |
186 | 187 |
@PathVariable("size") String size) throws JSONException { |
187 | 188 |
|
188 | 189 |
LOGGER.debug("Retreiving repositories of user : " + userEmail + " with params: " + baseAddress + "-" + page + "/" + size); |
190 |
//TODO update with correct method |
|
189 | 191 |
UriComponents uriComponents = UriComponentsBuilder |
190 | 192 |
.fromHttpUrl(baseAddress + "/ds/search/email/") |
191 | 193 |
.path("/{page}/{size}/") |
... | ... | |
280 | 282 |
repository = mapper.readValue(json_repository, Repository.class); |
281 | 283 |
} catch (Exception e) { |
282 | 284 |
LOGGER.debug("Error parsing repository ", e); |
283 |
e.printStackTrace(); |
|
284 | 285 |
} |
285 | 286 |
|
287 |
repository = this.setRepositoryFeatures(datatype,repository); |
|
286 | 288 |
|
287 | 289 |
LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId()); |
288 |
String retMessage; |
|
289 |
try { |
|
290 |
Date utilDate = new Date(); |
|
291 |
Timestamp date = new Timestamp(utilDate.getTime()); |
|
292 |
if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) { |
|
293 |
if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) { |
|
294 |
LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources"); |
|
295 |
if (getRepositoryById(repository.getId()) != null) { |
|
296 |
retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources."; |
|
297 |
repository.getInterfaces().clear(); |
|
298 |
LOGGER.debug(retMessage); |
|
299 |
} else { |
|
300 |
LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting.."); |
|
301 |
repository.setDateOfCollection(date); |
|
302 |
repository.setAggregator("OPENAIRE"); |
|
303 |
try { |
|
304 |
storeRepository(repository); |
|
305 |
} catch (JSONException e) { |
|
306 |
LOGGER.debug("Error parsing repository", e); |
|
307 |
throw e; |
|
308 |
} |
|
309 |
} |
|
290 |
if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) { |
|
291 |
if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) { |
|
292 |
LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources"); |
|
293 |
if (getRepositoryById(repository.getId()) != null) { |
|
294 |
String retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources."; |
|
295 |
repository.getInterfaces().clear(); |
|
296 |
LOGGER.debug(retMessage); |
|
297 |
} else { |
|
298 |
LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting.."); |
|
299 |
storeRepository(repository); |
|
310 | 300 |
} |
311 |
} else { |
|
312 |
// TODO call update method for register |
|
313 | 301 |
} |
314 |
} catch (Exception e) { |
|
315 |
LOGGER.debug("Error on storing repository", e); |
|
316 |
throw e; |
|
302 |
} else { |
|
303 |
// TODO call update method for register |
|
317 | 304 |
} |
318 | 305 |
|
319 |
storeInterfaces(datatype,repository); |
|
320 |
|
|
321 |
} |
|
322 |
|
|
323 |
private void storeInterfaces(String datatype,Repository repo) { |
|
324 |
|
|
325 | 306 |
LOGGER.debug("Inserting Interfaces"); |
326 |
Iterator var11 = repo.getInterfaces().iterator(); |
|
307 |
Iterator var11 = repository.getInterfaces().iterator();
|
|
327 | 308 |
|
328 | 309 |
while (var11.hasNext()) { |
329 | 310 |
RepositoryInterface iFace = (RepositoryInterface) var11.next(); |
330 | 311 |
if (!iFace.getBaseUrl().isEmpty() && !iFace.getDesiredCompatibilityLevel().isEmpty()) { |
331 | 312 |
if (iFace.getId() != null && !iFace.getId().isEmpty()) { |
332 | 313 |
LOGGER.debug("updating iface.."); |
333 |
//TODO call update base url |
|
334 |
//((DatasourceManagerService) this.dmService.getService()).updateBaseUrl(repo.getId(), iFace.getId(), iFace.getBaseUrl()); |
|
335 |
|
|
336 |
if (!iFace.getAccessSet().isEmpty()) { |
|
337 |
LOGGER.debug("set not empty: " + iFace.getAccessSet()); |
|
338 |
//TODO call update method for access params |
|
339 |
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "set", iFace.getAccessSet(), false); |
|
340 |
} else { |
|
341 |
//TODO call deleteAccessParamOrExtraField |
|
342 |
//((DatasourceManagerService) this.dmService.getService()).deleteAccessParamOrExtraField(repo.getId(), iFace.getId(), "set"); |
|
343 |
} |
|
344 |
//TODO update content description |
|
345 |
//((DatasourceManagerService) this.dmService.getService()).updateContentDescription(repo.getId(), iFace.getId(), "metadata"); |
|
346 |
if (datatype.equals("re3data")) { |
|
347 |
//TODO call update access params |
|
348 |
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_datacite", false); |
|
349 |
iFace.setAccessFormat("oai_datacite"); |
|
350 |
} else { |
|
351 |
//TODO call update access params |
|
352 |
//((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_dc", false); |
|
353 |
iFace.setAccessFormat("oai_dc"); |
|
354 |
} |
|
355 |
|
|
314 |
this.updateInterface(datatype,iFace); |
|
356 | 315 |
LOGGER.debug("updated successfully"); |
357 | 316 |
} else { |
358 | 317 |
LOGGER.debug("adding new iface.."); |
359 |
iFace.setContentDescription("metadata"); |
|
360 |
iFace.setCompliance("UNKNOWN"); |
|
361 |
if (datatype.equals("re3data")) { |
|
362 |
iFace.setAccessFormat("oai_datacite"); |
|
363 |
} else { |
|
364 |
iFace.setAccessFormat("oai_dc"); |
|
365 |
} |
|
318 |
registerRepositoryInterface(repository.getId(),iFace,datatype); |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
} |
|
366 | 323 |
|
367 |
if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) { |
|
368 |
iFace.setTypology(repo.getDatasourceClass()); |
|
369 |
} else if (datatype.equalsIgnoreCase("journal")) { |
|
370 |
iFace.setTypology("pubsrepository::journal"); |
|
371 |
} else if (datatype.equalsIgnoreCase("aggregator")) { |
|
372 |
iFace.setTypology("aggregator::pubsrepository::unknown"); |
|
373 |
} else if (datatype.equalsIgnoreCase("opendoar")) { |
|
374 |
iFace.setTypology("pubsrepository::unknown"); |
|
375 |
} else if (datatype.equalsIgnoreCase("re3data")) { |
|
376 |
iFace.setTypology("datarepository::unknown"); |
|
377 |
} |
|
324 |
private Repository setRepositoryFeatures(String datatype, Repository repository) { |
|
378 | 325 |
|
379 |
iFace.setRemovable(true); |
|
380 |
iFace.setAccessProtocol("oai"); |
|
381 |
iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); |
|
382 |
iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); |
|
383 |
if (iFace.getAccessSet().isEmpty()) { |
|
384 |
LOGGER.debug("set is empty: " + iFace.getAccessSet()); |
|
385 |
iFace.removeAccessSet(); |
|
386 |
} |
|
326 |
//TODO update map |
|
327 |
repository.setCountryCode(countriesMap.get(repository.getCountryName())); |
|
387 | 328 |
|
388 |
LOGGER.debug("new ifaceId :" + iFace.getId()); |
|
389 |
try { |
|
390 |
addRepositoryInterface(repo,iFace); |
|
391 |
LOGGER.debug("added successfully"); |
|
392 |
} catch (JSONException e) { |
|
393 |
LOGGER.error("error while adding iface",e); |
|
394 |
e.printStackTrace(); |
|
395 |
} |
|
396 |
} |
|
329 |
repository.setActivationId(UUID.randomUUID().toString()); |
|
330 |
// repo.setRegisteredBy((String) session.get(LocalVocabularies.loggedInField)); |
|
331 |
|
|
332 |
if (datatype.equals("opendoar") || datatype.equals("re3data")) { |
|
333 |
repository.setProvenanceActionClass("sysimport:crosswalk:entityregistry"); |
|
334 |
} else if (datatype.equals("journal")) { |
|
335 |
repository.setProvenanceActionClass("user:insert"); |
|
336 |
repository.setCollectedFrom("infrastruct_::openaire"); |
|
337 |
if (repository.getIssn() != null && repository.getIssn().length() == 0) |
|
338 |
repository.setIssn(com.unboundid.util.Base64.encode(repository.getOfficialName()).substring(0, 8)); |
|
339 |
repository.setId("openaire____::issn" + repository.getIssn()); |
|
340 |
repository.setNamespacePrefix("issn" + repository.getIssn()); |
|
341 |
} else if (datatype.equals("aggregator")) { |
|
342 |
repository.setProvenanceActionClass("user:insert"); |
|
343 |
repository.setCollectedFrom("infrastruct_::openaire"); |
|
344 |
repository.setId("openaire____::" + com.unboundid.util.Base64.encode(repository.getOfficialName())); |
|
345 |
repository.setNamespacePrefix(Normalizer.normalize(repository.getOfficialName().toLowerCase().replace(" ", "_"), Normalizer.Form.NFD).replaceAll("[^a-zA-Z0-9]", "")); |
|
346 |
if (repository.getNamespacePrefix().length() > 12) { |
|
347 |
repository.setNamespacePrefix(repository.getNamespacePrefix().substring(0, 12)); |
|
348 |
} else { |
|
349 |
while (repository.getNamespacePrefix().length() < 12) |
|
350 |
repository.setNamespacePrefix(repository.getNamespacePrefix().concat("_")); |
|
397 | 351 |
} |
398 | 352 |
} |
353 |
return repository; |
|
399 | 354 |
} |
400 | 355 |
|
356 |
private void updateInterface(String datatype,RepositoryInterface iFace) { |
|
357 |
//TODO call update base url |
|
358 |
//((DatasourceManagerService) this.dmService.getService()).updateBaseUrl(repo.getId(), iFace.getId(), iFace.getBaseUrl()); |
|
359 |
|
|
360 |
if (!iFace.getAccessSet().isEmpty()) { |
|
361 |
LOGGER.debug("set not empty: " + iFace.getAccessSet()); |
|
362 |
//TODO call update method for access params |
|
363 |
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "set", iFace.getAccessSet(), false); |
|
364 |
} else { |
|
365 |
//TODO call deleteAccessParamOrExtraField |
|
366 |
//((DatasourceManagerService) this.dmService.getService()).deleteAccessParamOrExtraField(repo.getId(), iFace.getId(), "set"); |
|
367 |
} |
|
368 |
//TODO update content description |
|
369 |
//((DatasourceManagerService) this.dmService.getService()).updateContentDescription(repo.getId(), iFace.getId(), "metadata"); |
|
370 |
if (datatype.equals("re3data")) { |
|
371 |
//TODO call update access params |
|
372 |
// ((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_datacite", false); |
|
373 |
iFace.setAccessFormat("oai_datacite"); |
|
374 |
} else { |
|
375 |
//TODO call update access params |
|
376 |
//((DatasourceManagerService) this.dmService.getService()).updateAccessParam(repo.getId(), iFace.getId(), "format", "oai_dc", false); |
|
377 |
iFace.setAccessFormat("oai_dc"); |
|
378 |
} |
|
379 |
|
|
380 |
} |
|
381 |
|
|
382 |
private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) { |
|
383 |
|
|
384 |
iFace.setContentDescription("metadata"); |
|
385 |
iFace.setCompliance("UNKNOWN"); |
|
386 |
if (datatype.equals("re3data")) { |
|
387 |
iFace.setAccessFormat("oai_datacite"); |
|
388 |
} else { |
|
389 |
iFace.setAccessFormat("oai_dc"); |
|
390 |
} |
|
391 |
|
|
392 |
if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) { |
|
393 |
iFace.setTypology(repo.getDatasourceClass()); |
|
394 |
} else if (datatype.equalsIgnoreCase("journal")) { |
|
395 |
iFace.setTypology("pubsrepository::journal"); |
|
396 |
} else if (datatype.equalsIgnoreCase("aggregator")) { |
|
397 |
iFace.setTypology("aggregator::pubsrepository::unknown"); |
|
398 |
} else if (datatype.equalsIgnoreCase("opendoar")) { |
|
399 |
iFace.setTypology("pubsrepository::unknown"); |
|
400 |
} else if (datatype.equalsIgnoreCase("re3data")) { |
|
401 |
iFace.setTypology("datarepository::unknown"); |
|
402 |
} |
|
403 |
|
|
404 |
iFace.setRemovable(true); |
|
405 |
iFace.setAccessProtocol("oai"); |
|
406 |
iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); |
|
407 |
iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); |
|
408 |
if (iFace.getAccessSet().isEmpty()) { |
|
409 |
LOGGER.debug("set is empty: " + iFace.getAccessSet()); |
|
410 |
iFace.removeAccessSet(); |
|
411 |
} |
|
412 |
return iFace; |
|
413 |
} |
|
414 |
|
|
401 | 415 |
private void storeRepository(Repository repository) throws JSONException { |
402 | 416 |
|
403 |
LOGGER.debug("Adding repository with name : " + repository.getOfficialName()); |
|
417 |
Date utilDate = new Date(); |
|
418 |
Timestamp date = new Timestamp(utilDate.getTime()); |
|
419 |
repository.setDateOfCollection(date); |
|
420 |
repository.setAggregator("OPENAIRE"); |
|
421 |
|
|
404 | 422 |
UriComponents uriComponents = UriComponentsBuilder |
405 | 423 |
.fromHttpUrl(baseAddress + "/ds/add/") |
406 | 424 |
.build() |
... | ... | |
424 | 442 |
} |
425 | 443 |
|
426 | 444 |
@Override |
427 |
public RepositoryInterface addRepositoryInterface(Repository repository,RepositoryInterface iFace) throws JSONException {
|
|
445 |
public RepositoryInterface addRepositoryInterface(@RequestBody String params) throws JSONException {
|
|
428 | 446 |
|
447 |
JSONObject json_params = new JSONObject(params); |
|
448 |
String datatype = json_params.getString("datatype"); |
|
449 |
String repoId = json_params.getString("repoId"); |
|
450 |
String json_iFace = json_params.getString("iFace"); |
|
451 |
RepositoryInterface iFace = null; |
|
452 |
|
|
453 |
ObjectMapper mapper = new ObjectMapper(); |
|
429 | 454 |
try { |
430 |
LOGGER.debug("Repository iface equals : " + Converter.repositoryInterfaceObjectToJson(repository,iFace)); |
|
431 |
}catch (Exception e){ |
|
432 |
LOGGER.debug("JSON exception ", e); |
|
433 |
throw e; |
|
455 |
iFace = mapper.readValue(json_iFace, RepositoryInterface.class); |
|
456 |
} catch (Exception e1) { |
|
457 |
LOGGER.debug("Error parsing repository interface ", e1); |
|
434 | 458 |
} |
435 | 459 |
|
436 |
UriComponents uriComponents = UriComponentsBuilder |
|
437 |
.fromHttpUrl(baseAddress + "/ds/api/add/") |
|
438 |
.build() |
|
439 |
.encode(); |
|
440 |
String rs = restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(repository,iFace),String.class); |
|
441 |
return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0)); |
|
460 |
return registerRepositoryInterface(repoId,iFace,datatype); |
|
442 | 461 |
} |
443 | 462 |
|
463 |
public RepositoryInterface registerRepositoryInterface(String repoId, RepositoryInterface iFace, String datatype) { |
|
464 |
Repository e = null; |
|
465 |
try { |
|
466 |
e = this.getRepositoryById(repoId); |
|
467 |
iFace = createRepositoryInterface(e,iFace,datatype); |
|
468 |
|
|
469 |
UriComponents uriComponents = UriComponentsBuilder |
|
470 |
.fromHttpUrl(baseAddress + "/ds/api/add/") |
|
471 |
.build() |
|
472 |
.encode(); |
|
473 |
String rs = restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(e,iFace),String.class); |
|
474 |
return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0)); |
|
475 |
|
|
476 |
} catch (JSONException e1) { |
|
477 |
LOGGER.debug("Error parsing json ",e1); |
|
478 |
} |
|
479 |
return null; |
|
480 |
} |
|
481 |
|
|
444 | 482 |
@Override |
445 | 483 |
public List<String> getDnetCountries() { |
446 | 484 |
LOGGER.debug("Getting dnet-countries!"); |
Also available in: Unified diff