Skip to content

Commit

Permalink
Merge pull request DSpace#9238 from 4Science/main_CST-12825
Browse files Browse the repository at this point in the history
ROR Integration - Live Import
  • Loading branch information
tdonohue authored Feb 14, 2024
2 parents dfe951b + b678e91 commit 20c8b03
Show file tree
Hide file tree
Showing 13 changed files with 3,250 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

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

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

/**
* Returns an {@code Optional<String>} representing the value
* of the metadata {@code field} found inside the {@code valueList}.
* @param field String of the MetadataField to search
* @return {@code Optional<String>} non empty if found.
*/
public Optional<String> getSingleValue(String field) {
MetadataFieldName metadataFieldName = new MetadataFieldName(field);
return getSingleValue(metadataFieldName.schema, metadataFieldName.element, metadataFieldName.qualifier);
}

/**
* Retrieves a single value for the given schema, element, and qualifier.
*
* @param schema the schema for the value
* @param element the element for the value
* @param qualifier the qualifier for the value
* @return an optional containing the single value, if present
*/
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
@@ -0,0 +1,130 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.importer.external.metadatamapping.contributor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.importer.external.metadatamapping.MetadatumDTO;

/**
* A ROR JsonPath Metadata processor that should be configured inside the {@code ror-integration.xml} file.
* This allows the extraction of a given contributor with a specific mappings from the ROR JSON response.
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
*/
public class RorParentOrgUnitMetadataContributor extends SimpleJsonPathMetadataContributor {

/**
* Determines which field of the JSON detains the {@code type} of this
* specific node (that needs to be mapped).
*
*/
private String typeField;

/**
* Determines which is the type of the main parent node that needs to be mapped.
* It should match the value of the {@code typeField} of the JSON node.
*
*/
private String parentType;

/**
* Determines which is the field of the JSON that contains the value
* that needs to be mapped into a {@code MetadatumDTO}.
*/
private String labelField;

/**
* Creates a {@code MetadatumDTO} for each correctly mapped JSON node
* of the ROR response.
* Partial / Unmatched parent-type metadatum will be ignored from this mapping.
*
* @param fullJson ROR response
* @return a collection of read ROR metadata.
*/
@Override
public Collection<MetadatumDTO> contributeMetadata(String fullJson) {

Collection<MetadatumDTO> metadata = new ArrayList<>();
Collection<String> metadataValue = new ArrayList<>();

JsonNode jsonNode = convertStringJsonToJsonNode(fullJson);
JsonNode array = jsonNode.at(getQuery());
if (!array.isArray()) {
return metadata;
}

Iterator<JsonNode> nodes = array.iterator();
while (nodes.hasNext()) {
JsonNode node = nodes.next();

if (!node.has(labelField)) {
continue;
}

String type = node.has(typeField) ? node.get(typeField).asText() : null;
String label = node.get(labelField).asText();

if (parentType.equalsIgnoreCase(type)) {
metadataValue.add(label);
}

}

for (String value : metadataValue) {
MetadatumDTO metadatumDto = new MetadatumDTO();
metadatumDto.setValue(value);
metadatumDto.setElement(getField().getElement());
metadatumDto.setQualifier(getField().getQualifier());
metadatumDto.setSchema(getField().getSchema());
metadata.add(metadatumDto);
}
return metadata;
}

private JsonNode convertStringJsonToJsonNode(String json) {
ObjectMapper mapper = new ObjectMapper();
JsonNode body = null;
try {
body = mapper.readTree(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return body;
}

public String getTypeField() {
return typeField;
}

public void setTypeField(String typeField) {
this.typeField = typeField;
}

public String getLabelField() {
return labelField;
}

public void setLabelField(String labelField) {
this.labelField = labelField;
}

public String getParentType() {
return parentType;
}

public void setParentType(String parentType) {
this.parentType = parentType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.importer.external.ror.service;

import java.util.Map;
import javax.annotation.Resource;

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


/**
* An implementation of {@link AbstractMetadataFieldMapping}
* Responsible for defining the mapping of the ROR metadatum fields on the DSpace metadatum fields
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
*/
public class RorFieldMapping extends AbstractMetadataFieldMapping {

/**
* Defines which metadatum is mapped on which metadatum. Note that while the key must be unique it
* only matters here for postprocessing of the value. The mapped MetadatumContributor has full control over
* what metadatafield is generated.
*
* @param metadataFieldMap The map containing the link between retrieve metadata and metadata that will be set to
* the item.
*/
@Override
@Resource(name = "rorMetadataFieldMap")
public void setMetadataFieldMap(Map metadataFieldMap) {
super.setMetadataFieldMap(metadataFieldMap);
}

}
Loading

0 comments on commit 20c8b03

Please sign in to comment.