From f17fcc2f42bf3a11ff6cac6023cb038c55b6bde1 Mon Sep 17 00:00:00 2001 From: Raf Ponsaerts Date: Tue, 6 Aug 2019 08:41:27 +0200 Subject: [PATCH] intermediary commit --- .../content/RelationshipMetadataService.java | 7 +++ .../RelationshipMetadataServiceImpl.java | 58 +++++++++++++++---- .../org/dspace/content/virtual/Collected.java | 6 ++ .../dspace/content/virtual/Concatenate.java | 9 +++ .../org/dspace/content/virtual/Related.java | 6 ++ .../org/dspace/content/virtual/UUIDValue.java | 6 ++ .../virtual/VirtualMetadataConfiguration.java | 4 ++ .../RelationshipMetadataServiceTest.java | 7 +++ dspace/config/spring/api/virtual-metadata.xml | 1 + 9 files changed, 92 insertions(+), 12 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataService.java b/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataService.java index f691cff8da..e11a8e54d7 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataService.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataService.java @@ -1,3 +1,10 @@ +/** + * 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; import java.util.List; diff --git a/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataServiceImpl.java index a18fb6d7e6..3391eb5386 100644 --- a/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/RelationshipMetadataServiceImpl.java @@ -1,3 +1,10 @@ +/** + * 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; import java.sql.SQLException; @@ -78,16 +85,19 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ String relationName; Item otherItem; int place = 0; + boolean isLeft; if (StringUtils.equals(relationshipType.getLeftType().getLabel(), entityType)) { hashMaps = virtualMetadataPopulator.getMap().get(relationshipType.getLeftLabel()); otherItem = relationship.getRightItem(); relationName = relationship.getRelationshipType().getLeftLabel(); place = relationship.getLeftPlace(); + isLeft = true; } else if (StringUtils.equals(relationshipType.getRightType().getLabel(), entityType)) { hashMaps = virtualMetadataPopulator.getMap().get(relationshipType.getRightLabel()); otherItem = relationship.getLeftItem(); relationName = relationship.getRelationshipType().getRightLabel(); place = relationship.getRightPlace(); + isLeft = false; } else { //No virtual metadata can be created return resultingMetadataValueList; @@ -96,7 +106,7 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ if (hashMaps != null && enableVirtualMetadata) { resultingMetadataValueList.addAll(handleRelationshipTypeMetadataMapping(context, item, hashMaps, otherItem, relationName, - relationship.getID(), place)); + relationship, place, isLeft)); } RelationshipMetadataValue relationMetadataFromOtherItem = getRelationMetadataFromOtherItem(context, otherItem, relationName, relationship.getID(), place); @@ -110,29 +120,53 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ //hashmaps parameter. The beans will be used to retrieve the values for the RelationshipMetadataValue objects //and the keys of the hashmap will be used to construct the RelationshipMetadataValue object. private List handleRelationshipTypeMetadataMapping(Context context, Item item, - HashMap hashMaps, Item otherItem, String relationName, - Integer relationshipId, int place) throws SQLException { + HashMap hashMaps, + Item otherItem, String relationName, + Relationship relationship, int place, + boolean isLeft) throws SQLException { List resultingMetadataValueList = new LinkedList<>(); for (Map.Entry entry : hashMaps.entrySet()) { String key = entry.getKey(); VirtualMetadataConfiguration virtualBean = entry.getValue(); - for (String value : virtualBean.getValues(context, otherItem)) { - RelationshipMetadataValue metadataValue = constructMetadataValue(context, key); - if (metadataValue != null) { - metadataValue = constructResultingMetadataValue(item, value, metadataValue, relationshipId); - metadataValue.setUseForPlace(virtualBean.getUseForPlace()); - metadataValue.setPlace(place); - if (StringUtils.isNotBlank(metadataValue.getValue())) { - resultingMetadataValueList.add(metadataValue); - } + if (virtualBean.getPopulateWithNameVariant()) { + String wardLabel = isLeft ? relationship.getLeftwardLabel() : relationship.getRightwardLabel(); + if (wardLabel != null) { + resultingMetadataValueList.add(constructRelationshipMetadataValue(context, item, relationship.getID(), place, key, virtualBean, wardLabel)); + } else { + handleVirtualBeanValues(context, item, otherItem, relationship, place, resultingMetadataValueList, key, virtualBean); } + } else { + handleVirtualBeanValues(context, item, otherItem, relationship, place, resultingMetadataValueList, key, virtualBean); } } return resultingMetadataValueList; } + private void handleVirtualBeanValues(Context context, Item item, Item otherItem, Relationship relationship, + int place, List resultingMetadataValueList, + String key, VirtualMetadataConfiguration virtualBean) throws SQLException { + for (String value : virtualBean.getValues(context, otherItem)) { + resultingMetadataValueList.add(constructRelationshipMetadataValue(context, item, relationship.getID(), place, key, virtualBean, value)); + } + } + + private RelationshipMetadataValue constructRelationshipMetadataValue(Context context, Item item, Integer relationshipId, int place, + String key, VirtualMetadataConfiguration virtualBean, + String value) { + RelationshipMetadataValue metadataValue = constructMetadataValue(context, key); + if (metadataValue != null) { + metadataValue = constructResultingMetadataValue(item, value, metadataValue, relationshipId); + metadataValue.setUseForPlace(virtualBean.getUseForPlace()); + metadataValue.setPlace(place); + if (StringUtils.isNotBlank(metadataValue.getValue())) { + return metadataValue; + } + } + return null; + } + //This method will construct a RelationshipMetadataValue object with proper schema, element and qualifier based //on the key String parameter passed along to it private RelationshipMetadataValue constructMetadataValue(Context context, String key) { diff --git a/dspace-api/src/main/java/org/dspace/content/virtual/Collected.java b/dspace-api/src/main/java/org/dspace/content/virtual/Collected.java index 4820e34e4e..6853454d2a 100644 --- a/dspace-api/src/main/java/org/dspace/content/virtual/Collected.java +++ b/dspace-api/src/main/java/org/dspace/content/virtual/Collected.java @@ -77,6 +77,12 @@ public class Collected implements VirtualMetadataConfiguration { return useForPlace; } + public void setPopulateWithNameVariant(boolean populateWithNameVariant) { } + + public boolean getPopulateWithNameVariant() { + return false; + } + /** * this method will retrieve the metadata values from the given item for all the metadata fields listed * in the fields property and it'll return all those values as a list diff --git a/dspace-api/src/main/java/org/dspace/content/virtual/Concatenate.java b/dspace-api/src/main/java/org/dspace/content/virtual/Concatenate.java index 04d3e911ec..ec4fcd9240 100644 --- a/dspace-api/src/main/java/org/dspace/content/virtual/Concatenate.java +++ b/dspace-api/src/main/java/org/dspace/content/virtual/Concatenate.java @@ -44,6 +44,7 @@ public class Concatenate implements VirtualMetadataConfiguration { */ private boolean useForPlace = false; + private boolean populateWithNameVariant = false; /** * Generic getter for the fields property * @return The list of fields to be used in this bean @@ -92,6 +93,14 @@ public class Concatenate implements VirtualMetadataConfiguration { return useForPlace; } + public void setPopulateWithNameVariant(boolean populateWithNameVariant) { + this.populateWithNameVariant = populateWithNameVariant; + } + + public boolean getPopulateWithNameVariant() { + return populateWithNameVariant; + } + /** * this method will retrieve the metadata values from the given item for all the metadata fields listed * in the fields property and it'll concatenate all those values together with the separator specified diff --git a/dspace-api/src/main/java/org/dspace/content/virtual/Related.java b/dspace-api/src/main/java/org/dspace/content/virtual/Related.java index 09eb951f06..eee13b7d58 100644 --- a/dspace-api/src/main/java/org/dspace/content/virtual/Related.java +++ b/dspace-api/src/main/java/org/dspace/content/virtual/Related.java @@ -132,6 +132,12 @@ public class Related implements VirtualMetadataConfiguration { return useForPlace; } + public void setPopulateWithNameVariant(boolean populateWithNameVariant) { } + + public boolean getPopulateWithNameVariant() { + return false; + } + /** * This method will find the correct Relationship from the given item to retrieve the other item from it * and pass this along to the next VirtualBean that's stored in this class. diff --git a/dspace-api/src/main/java/org/dspace/content/virtual/UUIDValue.java b/dspace-api/src/main/java/org/dspace/content/virtual/UUIDValue.java index c5c45cac8c..b8768bb75d 100644 --- a/dspace-api/src/main/java/org/dspace/content/virtual/UUIDValue.java +++ b/dspace-api/src/main/java/org/dspace/content/virtual/UUIDValue.java @@ -38,4 +38,10 @@ public class UUIDValue implements VirtualMetadataConfiguration { public boolean getUseForPlace() { return useForPlace; } + + public void setPopulateWithNameVariant(boolean populateWithNameVariant) { } + + public boolean getPopulateWithNameVariant() { + return false; + } } diff --git a/dspace-api/src/main/java/org/dspace/content/virtual/VirtualMetadataConfiguration.java b/dspace-api/src/main/java/org/dspace/content/virtual/VirtualMetadataConfiguration.java index 0cbb685c96..a84efd91e8 100644 --- a/dspace-api/src/main/java/org/dspace/content/virtual/VirtualMetadataConfiguration.java +++ b/dspace-api/src/main/java/org/dspace/content/virtual/VirtualMetadataConfiguration.java @@ -42,4 +42,8 @@ public interface VirtualMetadataConfiguration { * @return The useForPlace to be used by this bean */ boolean getUseForPlace(); + + void setPopulateWithNameVariant(boolean populateWithNameVariant); + + boolean getPopulateWithNameVariant(); } diff --git a/dspace-api/src/test/java/org/dspace/content/RelationshipMetadataServiceTest.java b/dspace-api/src/test/java/org/dspace/content/RelationshipMetadataServiceTest.java index c5fff5086b..f30f270268 100644 --- a/dspace-api/src/test/java/org/dspace/content/RelationshipMetadataServiceTest.java +++ b/dspace-api/src/test/java/org/dspace/content/RelationshipMetadataServiceTest.java @@ -1,3 +1,10 @@ +/** + * 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; import static org.mockito.Mockito.when; diff --git a/dspace/config/spring/api/virtual-metadata.xml b/dspace/config/spring/api/virtual-metadata.xml index bf15c955c6..5c82de1e0b 100644 --- a/dspace/config/spring/api/virtual-metadata.xml +++ b/dspace/config/spring/api/virtual-metadata.xml @@ -59,6 +59,7 @@ , +