mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
[Task 57103] implemented the support for related beans and added the option for config to supply virtual metadata throughout many nested relations
This commit is contained in:
@@ -41,6 +41,7 @@ import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.dspace.content.service.RelationshipService;
|
||||
import org.dspace.content.service.WorkspaceItemService;
|
||||
import org.dspace.content.virtual.Concatenate;
|
||||
import org.dspace.content.virtual.VirtualBean;
|
||||
import org.dspace.content.virtual.VirtualMetadataPopulator;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
@@ -1327,54 +1328,56 @@ prevent the generation of resource policy entry values with null dspace_object a
|
||||
if (StringUtils.isNotBlank(entityType)) {
|
||||
List<Relationship> relationships = relationshipService.findByItem(context, item);
|
||||
for (Relationship relationship : relationships) {
|
||||
fullMetadataValueList.addAll(handleItemRelationship(item, entityType, relationship, extra));
|
||||
fullMetadataValueList.addAll(handleItemRelationship(context, item, entityType, relationship, extra));
|
||||
}
|
||||
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.error(e, e);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
return fullMetadataValueList;
|
||||
}
|
||||
|
||||
private List<MetadataValue> handleItemRelationship(Item item, String entityType,
|
||||
Relationship relationship, boolean extra) {
|
||||
private List<MetadataValue> handleItemRelationship(Context context, Item item, String entityType,
|
||||
Relationship relationship, boolean extra) throws SQLException {
|
||||
List<MetadataValue> resultingMetadataValueList = new LinkedList<>();
|
||||
RelationshipType relationshipType = relationship.getRelationshipType();
|
||||
HashMap<String, Concatenate> hashMaps = new HashMap<>();
|
||||
HashMap<String, VirtualBean> hashMaps = new HashMap<>();
|
||||
String relationName = "";
|
||||
Item otherItem = null;
|
||||
if (StringUtils.equals(relationshipType.getLeftType().getLabel(), entityType)) {
|
||||
hashMaps = (HashMap<String, Concatenate>) virtualMetadataPopulator
|
||||
hashMaps = (HashMap<String, VirtualBean>) virtualMetadataPopulator
|
||||
.getMap().get(relationshipType.getLeftLabel());
|
||||
otherItem = relationship.getRightItem();
|
||||
relationName = relationship.getRelationshipType().getLeftLabel();
|
||||
} else if (StringUtils.equals(relationshipType.getRightType().getLabel(), entityType)) {
|
||||
hashMaps = (HashMap<String, Concatenate>) virtualMetadataPopulator
|
||||
hashMaps = (HashMap<String, VirtualBean>) virtualMetadataPopulator
|
||||
.getMap().get(relationshipType.getRightLabel());
|
||||
otherItem = relationship.getLeftItem();
|
||||
relationName = relationship.getRelationshipType().getRightLabel();
|
||||
}
|
||||
|
||||
if (hashMaps != null && extra) {
|
||||
resultingMetadataValueList.addAll(handleRelationshipTypeMetadataMappping(item, hashMaps,
|
||||
resultingMetadataValueList.addAll(handleRelationshipTypeMetadataMappping(context, item, hashMaps,
|
||||
otherItem, relationName));
|
||||
}
|
||||
resultingMetadataValueList.add(getRelationMetadataFromOtherItem(otherItem, relationName));
|
||||
return resultingMetadataValueList;
|
||||
}
|
||||
|
||||
private List<MetadataValue> handleRelationshipTypeMetadataMappping(Item item,
|
||||
HashMap<String, Concatenate> hashMaps,
|
||||
private List<MetadataValue> handleRelationshipTypeMetadataMappping(Context context, Item item,
|
||||
HashMap<String, VirtualBean> hashMaps,
|
||||
Item otherItem,
|
||||
String relationName) {
|
||||
String relationName) throws SQLException {
|
||||
List<MetadataValue> resultingMetadataValueList = new LinkedList<>();
|
||||
for (Map.Entry<String, Concatenate> entry : hashMaps.entrySet()) {
|
||||
for (Map.Entry<String, VirtualBean> entry : hashMaps.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Concatenate concatenate = entry.getValue();
|
||||
VirtualBean virtualBean = entry.getValue();
|
||||
|
||||
MetadataValue metadataValue = constructMetadataValue(key);
|
||||
metadataValue = constructResultingMetadataValue(item, otherItem, concatenate, metadataValue);
|
||||
metadataValue = constructResultingMetadataValue(context, item, otherItem, virtualBean, metadataValue);
|
||||
if (StringUtils.isNotBlank(metadataValue.getValue())) {
|
||||
resultingMetadataValueList.add(metadataValue);
|
||||
}
|
||||
@@ -1403,36 +1406,9 @@ prevent the generation of resource policy entry values with null dspace_object a
|
||||
return entityType;
|
||||
}
|
||||
|
||||
private MetadataValue constructResultingMetadataValue(Item item, Item otherItem, Concatenate concatenate,
|
||||
MetadataValue metadataValue) {
|
||||
List<String> resultValues = new LinkedList<>();
|
||||
List<String> value = concatenate.getFields();
|
||||
for (String s : value) {
|
||||
String[] splittedString = s.split("\\.");
|
||||
|
||||
List<MetadataValue> resultList = this.getMetadata(otherItem,
|
||||
splittedString.length > 0 ? splittedString[0] : null,
|
||||
splittedString.length > 1 ? splittedString[1] : null,
|
||||
splittedString.length > 2 ? splittedString[2] : null,
|
||||
Item.ANY);
|
||||
|
||||
String resultString = "";
|
||||
for (int i = 0; i < resultList.size(); i++) {
|
||||
String metadataValueString = resultList.get(i).getValue();
|
||||
if (StringUtils.isNotBlank(metadataValueString)) {
|
||||
if (StringUtils.isNotBlank(resultString)) {
|
||||
resultString += concatenate.getSeparator();
|
||||
}
|
||||
resultString += metadataValueString;
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(resultString)) {
|
||||
resultValues.add(resultString);
|
||||
}
|
||||
}
|
||||
|
||||
String result = StringUtils.join(resultValues, concatenate.getSeparator());
|
||||
metadataValue.setValue(result);
|
||||
private MetadataValue constructResultingMetadataValue(Context context, Item item, Item otherItem, VirtualBean virtualBean,
|
||||
MetadataValue metadataValue) throws SQLException {
|
||||
metadataValue.setValue(virtualBean.getValue(context, otherItem));
|
||||
metadataValue.setAuthority("virtual");
|
||||
metadataValue.setConfidence(-1);
|
||||
metadataValue.setDSpaceObject(item);
|
||||
|
@@ -7,9 +7,20 @@
|
||||
*/
|
||||
package org.dspace.content.virtual;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Concatenate {
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.MetadataValue;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class Concatenate implements VirtualBean {
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
private List<String> fields;
|
||||
private String separator;
|
||||
@@ -29,4 +40,40 @@ public class Concatenate {
|
||||
public void setSeparator(String separator) {
|
||||
this.separator = separator;
|
||||
}
|
||||
|
||||
public String getValue(Context context, Item item) {
|
||||
|
||||
List<String> resultValues = new LinkedList<>();
|
||||
List<String> value = this.getFields();
|
||||
for (String s : value) {
|
||||
String[] splittedString = s.split("\\.");
|
||||
|
||||
List<MetadataValue> resultList = itemService.getMetadata(item,
|
||||
splittedString.length > 0 ? splittedString[0] :
|
||||
null,
|
||||
splittedString.length > 1 ? splittedString[1] :
|
||||
null,
|
||||
splittedString.length > 2 ? splittedString[2] :
|
||||
null,
|
||||
Item.ANY);
|
||||
|
||||
String resultString = "";
|
||||
for (int i = 0; i < resultList.size(); i++) {
|
||||
String metadataValueString = resultList.get(i).getValue();
|
||||
if (StringUtils.isNotBlank(metadataValueString)) {
|
||||
if (StringUtils.isNotBlank(resultString)) {
|
||||
resultString += this.getSeparator();
|
||||
}
|
||||
resultString += metadataValueString;
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(resultString)) {
|
||||
resultValues.add(resultString);
|
||||
}
|
||||
}
|
||||
|
||||
String result = StringUtils.join(resultValues, this.getSeparator());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,96 @@
|
||||
package org.dspace.content.virtual;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.dspace.content.Entity;
|
||||
import org.dspace.content.EntityType;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.Relationship;
|
||||
import org.dspace.content.RelationshipType;
|
||||
import org.dspace.content.service.EntityService;
|
||||
import org.dspace.content.service.EntityTypeService;
|
||||
import org.dspace.content.service.RelationshipService;
|
||||
import org.dspace.content.service.RelationshipTypeService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class Related implements VirtualBean {
|
||||
|
||||
@Autowired
|
||||
private RelationshipTypeService relationshipTypeService;
|
||||
|
||||
@Autowired
|
||||
private RelationshipService relationshipService;
|
||||
|
||||
@Autowired
|
||||
private EntityTypeService entityTypeService;
|
||||
|
||||
@Autowired
|
||||
private EntityService entityService;
|
||||
|
||||
private String relationshipTypeString;
|
||||
private Integer place;
|
||||
private VirtualBean virtualBean;
|
||||
|
||||
public String getRelationshipTypeString() {
|
||||
return relationshipTypeString;
|
||||
}
|
||||
|
||||
public void setRelationshipTypeString(String relationshipTypeString) {
|
||||
this.relationshipTypeString = relationshipTypeString;
|
||||
}
|
||||
|
||||
public Integer getPlace() {
|
||||
return place;
|
||||
}
|
||||
|
||||
public void setPlace(Integer place) {
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
public VirtualBean getVirtualBean() {
|
||||
return virtualBean;
|
||||
}
|
||||
|
||||
public void setVirtualBean(VirtualBean virtualBean) {
|
||||
this.virtualBean = virtualBean;
|
||||
}
|
||||
|
||||
public String getValue(Context context, Item item) throws SQLException {
|
||||
Entity entity = entityService.findByItemId(context, item.getID());
|
||||
EntityType entityType = entityService.getType(context, entity);
|
||||
|
||||
List<RelationshipType> relationshipTypes = entityService.getAllRelationshipTypes(context, entity);
|
||||
List<RelationshipType> possibleRelationshipTypes = new LinkedList<>();
|
||||
for (RelationshipType relationshipType : relationshipTypes) {
|
||||
if (StringUtils.equals(relationshipType.getLeftLabel(), relationshipTypeString) || StringUtils
|
||||
.equals(relationshipType.getRightLabel(), relationshipTypeString)) {
|
||||
possibleRelationshipTypes.add(relationshipType);
|
||||
}
|
||||
}
|
||||
|
||||
List<Relationship> relationships = new LinkedList<>();
|
||||
for (RelationshipType relationshipType : possibleRelationshipTypes) {
|
||||
relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType));
|
||||
}
|
||||
|
||||
for (Relationship relationship : relationships) {
|
||||
if (relationship.getRelationshipType().getLeftType() == entityType) {
|
||||
if (relationship.getLeftPlace() == place) {
|
||||
Item otherItem = relationship.getRightItem();
|
||||
return virtualBean.getValue(context, otherItem);
|
||||
}
|
||||
} else if (relationship.getRelationshipType().getRightType() == entityType) {
|
||||
if (relationship.getRightPlace() == place) {
|
||||
Item otherItem = relationship.getLeftItem();
|
||||
return virtualBean.getValue(context, otherItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package org.dspace.content.virtual;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.core.Context;
|
||||
|
||||
public interface VirtualBean {
|
||||
|
||||
String getValue(Context context, Item item) throws SQLException;
|
||||
}
|
@@ -128,6 +128,7 @@
|
||||
<entry key="isJournalOfVolume" value-ref="isJournalOfVolumeMap"/>
|
||||
<entry key="isVolumeOfJournal" value-ref="isVolumeOfJournalMap"/>
|
||||
<entry key="isIssueOfJournalVolume" value-ref="isIssueOfJournalVolumeMap"/>
|
||||
<entry key="isJournalIssueOfPublication" value-ref="isJournalIssueOfPublicationMap"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
@@ -215,7 +216,8 @@
|
||||
</bean>
|
||||
|
||||
<util:map id="isJournalVolumeOfIssueMap">
|
||||
<entry key="journalvolume_identifier_volume" value-ref="issueVolume_volume"/>
|
||||
<entry key="journalvolume.identifier.volume" value-ref="issueVolume_volume"/>
|
||||
<entry key="journal.identifier.issn" value-ref="volumeJournal_issn_related"/>
|
||||
</util:map>
|
||||
<bean class="org.dspace.content.virtual.Concatenate" id="issueVolume_volume">
|
||||
<property name="fields">
|
||||
@@ -227,6 +229,11 @@
|
||||
<value>, </value>
|
||||
</property>
|
||||
</bean>
|
||||
<bean class="org.dspace.content.virtual.Related" id="volumeJournal_issn_related">
|
||||
<property name="relationshipTypeString" value="isJournalOfVolume"/>
|
||||
<property name="place" value="1"/>
|
||||
<property name="virtualBean" ref="volumeJournal_issn"/>
|
||||
</bean>
|
||||
|
||||
<util:map id="isJournalOfVolumeMap">
|
||||
<entry key="journal.identifier.issn" value-ref="volumeJournal_issn"/>
|
||||
@@ -269,5 +276,15 @@
|
||||
<value>, </value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<util:map id="isJournalIssueOfPublicationMap">
|
||||
<entry key="journal.identifier.issn" value-ref="issueVolumeJournal_issn_related"/>
|
||||
</util:map>
|
||||
|
||||
<bean class="org.dspace.content.virtual.Related" id="issueVolumeJournal_issn_related">
|
||||
<property name="relationshipTypeString" value="isJournalVolumeOfIssue"/>
|
||||
<property name="place" value="1"/>
|
||||
<property name="virtualBean" ref="volumeJournal_issn_related"/>
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
|
Reference in New Issue
Block a user