forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DSC-1459 Allows multiple entity types for RelatedEntityItemEnhancer, …
…provide a bridge between the ReferCrosswalk virtual and the ItemEnhancer
- Loading branch information
Showing
6 changed files
with
170 additions
and
83 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
...ce-api/src/main/java/org/dspace/content/enhancer/impl/RelatedEntityItemEnhancerUtils.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,72 @@ | ||
/** | ||
* 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.enhancer.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.dspace.content.Item; | ||
import org.dspace.content.MetadataValue; | ||
import org.dspace.content.service.ItemService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* Utility methods used by {@link RelatedEntityItemEnhancer} | ||
* | ||
* @author Andrea Bollini (andrea.bollini at 4science.com) | ||
* | ||
*/ | ||
public class RelatedEntityItemEnhancerUtils { | ||
|
||
@Autowired | ||
private ItemService itemService; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(RelatedEntityItemEnhancerUtils.class); | ||
|
||
public Map<String, List<MetadataValue>> getCurrentVirtualsMap(Item item, String virtualQualifier) { | ||
Map<String, List<MetadataValue>> currentVirtualsMap = new HashMap<String, List<MetadataValue>>(); | ||
List<MetadataValue> sources = itemService.getMetadata(item, RelatedEntityItemEnhancer.VIRTUAL_METADATA_SCHEMA, | ||
RelatedEntityItemEnhancer.VIRTUAL_SOURCE_METADATA_ELEMENT, virtualQualifier, Item.ANY); | ||
List<MetadataValue> generated = itemService.getMetadata(item, | ||
RelatedEntityItemEnhancer.VIRTUAL_METADATA_SCHEMA, RelatedEntityItemEnhancer.VIRTUAL_METADATA_ELEMENT, | ||
virtualQualifier, Item.ANY); | ||
|
||
if (sources.size() != generated.size()) { | ||
LOGGER.error( | ||
"inconsistent virtual metadata for the item {} got {} sources and {} generated virtual metadata", | ||
item.getID().toString(), sources.size(), generated.size()); | ||
} | ||
|
||
for (int i = 0; i < Integer.max(sources.size(), generated.size()); i++) { | ||
String authority; | ||
if (i < sources.size()) { | ||
authority = sources.get(i).getValue(); | ||
} else { | ||
// we have less source than virtual metadata let's generate a random uuid to | ||
// associate with these extra metadata so that they will be managed as obsolete | ||
// value | ||
authority = UUID.randomUUID().toString(); | ||
} | ||
List<MetadataValue> mvalues = currentVirtualsMap.get(authority); | ||
if (mvalues == null) { | ||
mvalues = new ArrayList<MetadataValue>(); | ||
} | ||
if (i < generated.size()) { | ||
mvalues.add(generated.get(i)); | ||
} | ||
currentVirtualsMap.put(authority, mvalues); | ||
} | ||
return currentVirtualsMap; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...g/dspace/content/integration/crosswalks/virtualfields/VirtualFieldToEnhancedMetadata.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,62 @@ | ||
/** | ||
* 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.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.dspace.content.Item; | ||
import org.dspace.content.MetadataValue; | ||
import org.dspace.content.enhancer.impl.RelatedEntityItemEnhancerUtils; | ||
import org.dspace.content.factory.ContentServiceFactory; | ||
import org.dspace.content.service.ItemService; | ||
import org.dspace.core.Context; | ||
import org.dspace.core.CrisConstants; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* Implementation of {@link VirtualField} that returns the values from the a | ||
* cris.virtual.<element> metadata using the <qualifier> provided in the form of | ||
* <schema>-<element>-<qualifier> as source metadata. | ||
* Source metadata that are not found in the cris.virtualsource.<element> leads to a PLACEHOLDER | ||
* | ||
* @author Andrea Bollini at 4science.comm | ||
* | ||
*/ | ||
public class VirtualFieldToEnhancedMetadata implements VirtualField { | ||
|
||
@Autowired | ||
private ItemService itemService; | ||
|
||
@Autowired | ||
private RelatedEntityItemEnhancerUtils relatedEntityItemEnhancerUtils; | ||
|
||
@Override | ||
public String[] getMetadata(Context context, Item item, String fieldName) { | ||
ItemService itemService = ContentServiceFactory.getInstance().getItemService(); | ||
String[] fieldBits = fieldName.split("\\."); | ||
if (fieldBits.length != 3) { | ||
throw new IllegalArgumentException( | ||
"VirtualFieldToEnhancedMetadata must be used specifying the EnhancedMetadata qualifier as " | ||
+ "element and the source metadata as qualifier, i.e. virtual.department.dc-contributor-author"); | ||
} | ||
String virtualQualifier = fieldBits[1]; | ||
String metadata = fieldBits[2].replaceAll("-", "."); | ||
Map<String, List<MetadataValue>> map = relatedEntityItemEnhancerUtils.getCurrentVirtualsMap(item, | ||
virtualQualifier); | ||
List<String> values = itemService.getMetadataByMetadataString(item, metadata).stream() | ||
.map(mv -> mv.getAuthority() != null && map.containsKey(mv.getAuthority()) | ||
? map.get(mv.getAuthority()).get(0).getValue() | ||
: CrisConstants.PLACEHOLDER_PARENT_METADATA_VALUE) | ||
.collect(Collectors.toList()); | ||
String[] resultValues = values.toArray(new String[0]); | ||
return resultValues; | ||
} | ||
|
||
} |
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
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