Skip to content

Commit

Permalink
[DSC-1319] Improved RorOrgUnitAuthority
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGiamminonni committed Nov 3, 2023
1 parent 01673c4 commit 9f7e3ee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
package org.dspace.content.authority;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.dspace.content.authority.factory.ItemAuthorityServiceFactory;
import org.dspace.importer.external.datamodel.ImportRecord;
import org.dspace.importer.external.exception.MetadataSourceException;
import org.dspace.importer.external.metadatamapping.MetadatumDTO;
import org.dspace.importer.external.ror.service.RorImportMetadataSourceServiceImpl;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
Expand Down Expand Up @@ -81,7 +83,51 @@ private String getName(ImportRecord orgUnit) {
}

private Map<String, String> buildExtras(ImportRecord orgUnit) {
return new HashMap<>();

Map<String, String> extras = new LinkedHashMap<String, String>();

addExtra(extras, getIdentifier(orgUnit), "id");

orgUnit.getSingleValue("dc", "type", null)
.ifPresent(type -> addExtra(extras, type, "type"));

String acronym = orgUnit.getValue("oairecerif", "acronym", null).stream()
.map(MetadatumDTO::getValue)
.collect(Collectors.joining(", "));

if (StringUtils.isNotBlank(acronym)) {
addExtra(extras, acronym, "acronym");
}

return extras;
}

private void addExtra(Map<String, String> extras, String value, String extraType) {

String key = getKey(extraType);

if (useAsData(extraType)) {
extras.put("data-" + key, value);
}
if (useForDisplaying(extraType)) {
extras.put(key, value);
}

}

private boolean useForDisplaying(String extraType) {
return configurationService.getBooleanProperty("cris.OrcidAuthority."
+ getPluginInstanceName() + "." + extraType + ".display", true);
}

private boolean useAsData(String extraType) {
return configurationService.getBooleanProperty("cris.OrcidAuthority."
+ getPluginInstanceName() + "." + extraType + ".as-data", true);
}

private String getKey(String extraType) {
return configurationService.getProperty("cris.OrcidAuthority."
+ getPluginInstanceName() + "." + extraType + ".key", "ror_orgunit_" + extraType);
}

private String composeAuthorityValue(String rorId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.dspace.importer.external.metadatamapping.MetadatumDTO;

Expand Down Expand Up @@ -94,6 +95,12 @@ public Collection<MetadatumDTO> getValue(String schema, String element, String q
return values;
}

public Optional<String> getSingleValue(String schema, String element, String qualifier) {
return getValue(schema, element, qualifier).stream()
.map(MetadatumDTO::getValue)
.findFirst();
}

/**
* Add a value to the valueList
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import javax.el.MethodNotFoundException;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -69,7 +70,7 @@ public int getRecordsCount(Query query) throws MetadataSourceException {

@Override
public Collection<ImportRecord> getRecords(String query, int start, int count) throws MetadataSourceException {
return retry(new SearchByQueryCallable(query));
return retry(new SearchByQueryCallable(query, start));
}

@Override
Expand Down Expand Up @@ -110,9 +111,10 @@ private class SearchByQueryCallable implements Callable<List<ImportRecord>> {

private Query query;

private SearchByQueryCallable(String queryString) {
private SearchByQueryCallable(String queryString, int start) {
query = new Query();
query.addParameter("query", queryString);
query.addParameter("start", start);
}

private SearchByQueryCallable(Query query) {
Expand All @@ -121,7 +123,8 @@ private SearchByQueryCallable(Query query) {

@Override
public List<ImportRecord> call() throws Exception {
return search(query.getParameterAsClass("query", String.class));
return search(query.getParameterAsClass("query", String.class),
query.getParameterAsClass("start", Integer.class));
}
}

Expand Down Expand Up @@ -220,13 +223,16 @@ private List<ImportRecord> searchById(String id) {
return adsResults;
}

private List<ImportRecord> search(String query) {
private List<ImportRecord> search(String query, Integer start) {
List<ImportRecord> adsResults = new ArrayList<>();
try {
Map<String, Map<String, String>> params = new HashMap<String, Map<String, String>>();

URIBuilder uriBuilder = new URIBuilder(this.url);
uriBuilder.addParameter("query", query);
if (start != null) {
uriBuilder.addParameter("page", String.valueOf((start / 20) + 1));
}

String resp = liveImportClient.executeHttpGetRequest(timeout, uriBuilder.toString(), params);
if (StringUtils.isEmpty(resp)) {
Expand All @@ -247,7 +253,20 @@ private List<ImportRecord> search(String query) {
} catch (URISyntaxException e) {
e.printStackTrace();
}
return adsResults;

if (start == null) {
return adsResults;
}

if (start % 20 == 0) {
return adsResults.stream()
.limit(10)
.collect(Collectors.toList());
} else {
return adsResults.stream()
.skip(10)
.collect(Collectors.toList());
}
}

private JsonNode convertStringJsonToJsonNode(String json) {
Expand Down

0 comments on commit 9f7e3ee

Please sign in to comment.