Skip to content

Commit

Permalink
Merged in CST-11738 (pull request DSpace#1129)
Browse files Browse the repository at this point in the history
[CST-11738] TUHH: ORCID lookup with more data

Approved-by: Stefano Maffei
  • Loading branch information
eskander17 authored and steph-ieffam committed Oct 25, 2023
2 parents 1fcd95e + eeadc46 commit c060a18
Show file tree
Hide file tree
Showing 13 changed files with 691 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
package org.dspace.authority.filler;

import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.removeStart;
import static org.apache.commons.lang3.StringUtils.startsWith;
Expand Down Expand Up @@ -110,7 +109,11 @@ private void enrichItemWithExternalData(Context context, Item item, ExternalData
}

private boolean notAlreadyPresent(Item item, MetadataValueDTO value) {
return isEmpty(itemService.getMetadata(item, value.getSchema(), value.getElement(), value.getQualifier(), ANY));
List<MetadataValue> metadataValues = itemService.getMetadata(item, value.getSchema(),
value.getElement(), value.getQualifier(), ANY);

return metadataValues.stream().noneMatch(metadataValue ->
metadataValue.getValue().equals(value.getValue()));
}

private boolean isTitleNotSet(Item item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -27,6 +28,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.content.MetadataFieldName;
import org.dspace.content.dto.MetadataValueDTO;
import org.dspace.external.OrcidRestConnector;
import org.dspace.external.model.ExternalDataObject;
Expand All @@ -35,6 +37,7 @@
import org.json.JSONObject;
import org.orcid.jaxb.model.v3.release.common.OrcidIdentifier;
import org.orcid.jaxb.model.v3.release.record.Person;
import org.orcid.jaxb.model.v3.release.record.Record;
import org.orcid.jaxb.model.v3.release.search.Result;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -60,6 +63,8 @@ public class OrcidV3AuthorDataProvider extends AbstractExternalDataProvider {

private XMLtoBio converter;

private Map<String, String> externalIdentifiers;

public static final String ORCID_ID_SYNTAX = "\\d{4}-\\d{4}-\\d{4}-(\\d{3}X|\\d{4})";
private static final int MAX_INDEX = 10000;

Expand Down Expand Up @@ -113,12 +118,13 @@ public void init() throws IOException {

@Override
public Optional<ExternalDataObject> getExternalDataObject(String id) {
Person person = getBio(id);
ExternalDataObject externalDataObject = convertToExternalDataObject(person);
Record record = getBio(id);
ExternalDataObject externalDataObject = convertToExternalDataObject(record);
return Optional.of(externalDataObject);
}

protected ExternalDataObject convertToExternalDataObject(Person person) {
protected ExternalDataObject convertToExternalDataObject(Record record) {
Person person = record.getPerson();
ExternalDataObject externalDataObject = new ExternalDataObject(sourceIdentifier);
if (person.getName() != null) {
String lastName = "";
Expand All @@ -141,6 +147,12 @@ protected ExternalDataObject convertToExternalDataObject(Person person) {
externalDataObject
.addMetadata(new MetadataValueDTO("dc", "identifier", "uri", null,
orcidUrl + '/' + person.getName().getPath()));

appendOtherNames(externalDataObject, person);
appendResearcherUrls(externalDataObject, person);
appendExternalIdentifiers(externalDataObject, person);
appendAffiliations(externalDataObject, record);

if (!StringUtils.isBlank(lastName) && !StringUtils.isBlank(firstName)) {
externalDataObject.setDisplayValue(lastName + ", " + firstName);
externalDataObject.setValue(lastName + ", " + firstName);
Expand All @@ -157,24 +169,64 @@ protected ExternalDataObject convertToExternalDataObject(Person person) {
return externalDataObject;
}

private void appendOtherNames(ExternalDataObject externalDataObject, Person person) {
person.getOtherNames().getOtherNames().forEach(otherName ->
externalDataObject.addMetadata(new MetadataValueDTO("crisrp", "name", "variant", null,
otherName.getContent())));
}

private void appendResearcherUrls(ExternalDataObject externalDataObject, Person person) {
person.getResearcherUrls().getResearcherUrls().forEach(researcherUrl ->
externalDataObject.addMetadata(new MetadataValueDTO("oairecerif", "identifier", "url", null,
researcherUrl.getUrl().getValue())));
}

private void appendExternalIdentifiers(ExternalDataObject externalDataObject, Person person) {
if (getExternalIdentifiers() != null) {
person.getExternalIdentifiers()
.getExternalIdentifiers()
.forEach(externalIdentifier -> {
String metadataField = externalIdentifiers.get(externalIdentifier.getType());
if (StringUtils.isNotEmpty(metadataField)) {
MetadataFieldName field = new MetadataFieldName(metadataField);
externalDataObject.addMetadata(
new MetadataValueDTO(field.schema, field.element, field.qualifier, null,
externalIdentifier.getValue()));
}
});
}
}

private void appendAffiliations(ExternalDataObject externalDataObject, Record record) {
record.getActivitiesSummary()
.getEmployments()
.getEmploymentGroups()
.stream()
.flatMap(affiliationGroup ->
affiliationGroup.getActivities().stream())
.forEach(employmentSummary ->
externalDataObject.addMetadata(new MetadataValueDTO("person", "affiliation", "name",
null, employmentSummary.getOrganization().getName())));
}

/**
* Retrieve a Person object based on a given orcid identifier.
* Retrieve a Record object based on a given orcid identifier.
* @param id orcid identifier
* @return Person
* @return Record
*/
public Person getBio(String id) {
public Record getBio(String id) {
log.debug("getBio called with ID=" + id);
if (!isValid(id)) {
return null;
}
InputStream bioDocument = orcidRestConnector.get(id + ((id.endsWith("/person")) ? "" : "/person"), accessToken);
Person person = converter.convertSinglePerson(bioDocument);
InputStream bioDocument = orcidRestConnector.get(id, accessToken);
Record record = converter.convertToRecord(bioDocument);
try {
bioDocument.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return person;
return record;
}

/**
Expand All @@ -201,13 +253,13 @@ public List<ExternalDataObject> searchExternalDataObjects(String query, int star
log.debug("queryBio searchPath=" + searchPath + " accessToken=" + accessToken);
InputStream bioDocument = orcidRestConnector.get(searchPath, accessToken);
List<Result> results = converter.convert(bioDocument);
List<Person> bios = new LinkedList<>();
List<Record> bios = new LinkedList<>();
for (Result result : results) {
OrcidIdentifier orcidIdentifier = result.getOrcidIdentifier();
if (orcidIdentifier != null) {
log.debug("Found OrcidId=" + orcidIdentifier.toString());
String orcid = orcidIdentifier.getPath();
Person bio = getBio(orcid);
Record bio = getBio(orcid);
if (bio != null) {
bios.add(bio);
}
Expand Down Expand Up @@ -298,4 +350,11 @@ public void setOrcidRestConnector(OrcidRestConnector orcidRestConnector) {
this.orcidRestConnector = orcidRestConnector;
}

public Map<String, String> getExternalIdentifiers() {
return externalIdentifiers;
}

public void setExternalIdentifiers(Map<String, String> externalIdentifiers) {
this.externalIdentifiers = externalIdentifiers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.apache.logging.log4j.Logger;
import org.orcid.jaxb.model.v3.release.record.Person;
import org.orcid.jaxb.model.v3.release.record.Record;
import org.orcid.jaxb.model.v3.release.search.Result;
import org.orcid.jaxb.model.v3.release.search.Search;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -64,4 +65,15 @@ public Person convertSinglePerson(InputStream xml) {
}
return null;
}

public Record convertToRecord(InputStream xml) {
Record record = null;
try {
record = (Record) unmarshall(xml, Record.class);
return record;
} catch (SAXException | URISyntaxException e) {
log.error(e);
}
return record;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public InputStream answer(InvocationOnMock invocation) {
}
});

when(orcidRestConnector.get(ArgumentMatchers.matches("^\\d{4}-\\d{4}-\\d{4}-\\d{4}$"), ArgumentMatchers.any()))
.thenAnswer(new Answer<InputStream>() {
@Override
public InputStream answer(InvocationOnMock invocation) {
return this.getClass().getResourceAsStream("orcid-record.xml");
}
});

setOrcidRestConnector(orcidRestConnector);
}

Expand Down
Loading

0 comments on commit c060a18

Please sign in to comment.