forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in DSC-1243 (pull request DSpace#1139)
[DSC-1243] added virtualField generator for primary and alt dois
- Loading branch information
Showing
14 changed files
with
331 additions
and
3 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...src/main/java/org/dspace/content/integration/crosswalks/virtualfields/ItemDOIService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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.content.integration.crosswalks.virtualfields; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import org.dspace.content.Item; | ||
import org.dspace.content.MetadataValue; | ||
import org.dspace.content.service.ItemService; | ||
import org.dspace.services.ConfigurationService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
|
||
public class ItemDOIService { | ||
static final String CFG_PREFIX = "identifier.doi.prefix"; | ||
|
||
static final String DOI_METADATA = "dc.identifier.doi"; | ||
|
||
@Autowired | ||
protected ItemService itemService; | ||
@Autowired | ||
private ConfigurationService configurationService; | ||
|
||
public String[] getAlternativeDOIFromItem(Item item) { | ||
List<MetadataValue> metadataValueList = itemService.getMetadataByMetadataString(item, DOI_METADATA); | ||
return getAlternativeDOI(metadataValueList, getPrimaryDOI(metadataValueList)); | ||
} | ||
private String[] getAlternativeDOI(List<MetadataValue> metadataValueList, String primaryValue) { | ||
return metadataValueList.stream().map(MetadataValue::getValue) | ||
.filter(value -> !value.equals(primaryValue)).toArray(String[]::new); | ||
} | ||
|
||
public String getPrimaryDOIFromItem(Item item) { | ||
return getPrimaryDOI(itemService.getMetadataByMetadataString(item, DOI_METADATA)); | ||
} | ||
|
||
private String getPrimaryDOI(List<MetadataValue> metadataValueList) { | ||
return metadataValueList.stream().filter(metadata -> metadata.getValue().contains(getPrefix())) | ||
.min(Comparator.comparingInt(MetadataValue::getPlace)).map(MetadataValue::getValue) | ||
.orElse(!metadataValueList.isEmpty() ? metadataValueList.get(0).getValue() : null); | ||
} | ||
|
||
protected String getPrefix() { | ||
String prefix; | ||
prefix = this.configurationService.getProperty(CFG_PREFIX); | ||
if (null == prefix) { | ||
throw new RuntimeException("Unable to load DOI prefix from " | ||
+ "configuration. Cannot find property " + | ||
CFG_PREFIX + "."); | ||
} | ||
return prefix; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...a/org/dspace/content/integration/crosswalks/virtualfields/VirtualFieldAlternativeDOI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.content.integration.crosswalks.virtualfields; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.dspace.content.Item; | ||
import org.dspace.core.Context; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
|
||
public class VirtualFieldAlternativeDOI implements VirtualField { | ||
|
||
@Autowired | ||
private ItemDOIService itemDOIService; | ||
|
||
@Override | ||
public String[] getMetadata(Context context, Item item, String fieldName) { | ||
String[] qualifiers = StringUtils.split(fieldName, "."); | ||
if (qualifiers.length != 3) { | ||
throw new IllegalArgumentException("Invalid field name " + fieldName); | ||
} | ||
|
||
return itemDOIService.getAlternativeDOIFromItem(item); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../java/org/dspace/content/integration/crosswalks/virtualfields/VirtualFieldPrimaryDOI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.content.integration.crosswalks.virtualfields; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.dspace.content.Item; | ||
import org.dspace.core.Context; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
|
||
public class VirtualFieldPrimaryDOI implements VirtualField { | ||
|
||
@Autowired | ||
private ItemDOIService itemDOIService; | ||
|
||
@Override | ||
public String[] getMetadata(Context context, Item item, String fieldName) { | ||
String[] qualifiers = StringUtils.split(fieldName, "."); | ||
if (qualifiers.length != 3) { | ||
throw new IllegalArgumentException("Invalid field name " + fieldName); | ||
} | ||
|
||
return new String[] {itemDOIService.getPrimaryDOIFromItem(item)}; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...api/src/test/data/dspaceFolder/config/crosswalks/template/virtual-field-doi-json.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"primary-doi": "@virtual.primary-doi.dc-identifier-doi@", | ||
"alternative-doi": "@virtual.alternative-doi.dc-identifier-doi@", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
dspace-oai/src/main/java/org/dspace/xoai/app/DataciteDOIItemCompilePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* 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.xoai.app; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.lyncode.xoai.dataprovider.xml.xoai.Element; | ||
import com.lyncode.xoai.dataprovider.xml.xoai.Metadata; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.dspace.content.Item; | ||
import org.dspace.content.integration.crosswalks.virtualfields.ItemDOIService; | ||
import org.dspace.core.Context; | ||
import org.dspace.xoai.util.ItemUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
|
||
/** | ||
* XOAIExtensionItemCompilePlugin aims to add structured information about the | ||
* DOIs of the item (if any). | ||
* The xoai document will be enriched with a structure like that | ||
* <code> | ||
* <element name="other"> | ||
* <element name="datacite"> | ||
* <element name="primary"> | ||
* <field name="doi"></field> | ||
* </element> | ||
* <element name="alternative"> | ||
* <field name="doi"></field> | ||
* ... | ||
* <field name="doi"></field> | ||
* </element> | ||
* </element> | ||
* </element> | ||
* </code> | ||
* | ||
*/ | ||
public class DataciteDOIItemCompilePlugin implements XOAIExtensionItemCompilePlugin { | ||
|
||
@Autowired | ||
private ItemDOIService itemDOIService; | ||
|
||
@Override | ||
public Metadata additionalMetadata(Context context, Metadata metadata, Item item) { | ||
String primaryDoiValue = itemDOIService.getPrimaryDOIFromItem(item); | ||
String[] alternativeDoiValue = itemDOIService.getAlternativeDOIFromItem(item); | ||
Element datacite = ItemUtils.create("datacite"); | ||
if (StringUtils.isNotBlank(primaryDoiValue)) { | ||
Element primary = ItemUtils.create("primary"); | ||
datacite.getElement().add(primary); | ||
primary.getField().add(ItemUtils.createValue("doi", primaryDoiValue)); | ||
if (alternativeDoiValue != null && alternativeDoiValue.length != 0) { | ||
Element alternative = ItemUtils.create("alternative"); | ||
datacite.getElement().add(alternative); | ||
Arrays.stream(alternativeDoiValue) | ||
.forEach(value -> alternative.getField().add(ItemUtils.createValue("doi", value))); | ||
} | ||
Element other; | ||
List<Element> elements = metadata.getElement(); | ||
if (ItemUtils.getElement(elements, "others") != null) { | ||
other = ItemUtils.getElement(elements, "others"); | ||
} else { | ||
other = ItemUtils.create("others"); | ||
} | ||
other.getElement().add(datacite); | ||
} | ||
return metadata; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.