91680: Refactor: move entity type getters to itemService

This commit is contained in:
Bruno Roemers
2022-05-24 17:13:01 +02:00
committed by Ben Bosman
parent a028b4130f
commit 3a5766a5d2
7 changed files with 68 additions and 70 deletions

View File

@@ -40,6 +40,7 @@ import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataSchemaService;
@@ -120,6 +121,9 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
@Autowired(required = true)
private RelationshipMetadataService relationshipMetadataService;
@Autowired(required = true)
private EntityTypeService entityTypeService;
protected ItemServiceImpl() {
super();
}
@@ -1529,5 +1533,37 @@ prevent the generation of resource policy entry values with null dspace_object a
.stream().findFirst().orElse(null);
}
@Override
public String getEntityTypeLabel(Item item) {
List<MetadataValue> mdvs = getMetadata(item, "dspace", "entity", "type", Item.ANY, false);
if (mdvs.isEmpty()) {
return null;
}
if (mdvs.size() > 1) {
log.warn(
"Item with uuid {}, handle {} has {} entity types ({}), expected 1 entity type",
item.getID(), item.getHandle(), mdvs.size(),
mdvs.stream().map(MetadataValue::getValue).collect(Collectors.toUnmodifiableList())
);
}
String entityType = mdvs.get(0).getValue();
if (StringUtils.isBlank(entityType)) {
return null;
}
return entityType;
}
@Override
public EntityType getEntityType(Context context, Item item) throws SQLException {
String entityTypeString = getEntityTypeLabel(item);
if (StringUtils.isBlank(entityTypeString)) {
return null;
}
return entityTypeService.findByEntityType(context, entityTypeString);
}
}

View File

@@ -56,14 +56,9 @@ public interface RelationshipMetadataService {
* This method will retrieve the EntityType String from an item
* @param item The Item for which the entityType String will be returned
* @return A String value indicating the entityType
* @deprecated use {@link org.dspace.content.service.ItemService#getEntityTypeLabel(Item)} instead.
*/
@Deprecated
public String getEntityTypeStringFromMetadata(Item item);
/**
* This method will retrieve the EntityType from an item
* @param item The Item for which the entityType will be returned
* @return The entity type
*/
public EntityType getEntityTypeFromMetadata(Context context, Item item) throws SQLException;
}

View File

@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.content.dao.pojo.ItemUuidAndRelationshipId;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
@@ -45,7 +45,7 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ
protected RelationshipTypeService relationshipTypeService;
@Autowired(required = true)
protected EntityTypeService entityTypeService;
protected ItemService itemService;
@Autowired(required = true)
protected VirtualMetadataPopulator virtualMetadataPopulator;
@@ -58,7 +58,7 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ
Context context = new Context();
List<RelationshipMetadataValue> fullMetadataValueList = new LinkedList<>();
try {
EntityType entityType = getEntityTypeFromMetadata(context, item);
EntityType entityType = itemService.getEntityType(context, item);
if (entityType != null) {
// NOTE: The following code will add metadata fields of type relation.*.latestForDiscovery
// (e.g. relation.isAuthorOfPublication.latestForDiscovery).
@@ -167,25 +167,10 @@ public class RelationshipMetadataServiceImpl implements RelationshipMetadataServ
.collect(Collectors.toUnmodifiableList());
}
@Override
@Deprecated
public String getEntityTypeStringFromMetadata(Item item) {
List<MetadataValue> list = item.getMetadata();
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getMetadataField().getMetadataSchema().getName(), "dspace")
&& StringUtils.equals(mdv.getMetadataField().getElement(), "entity")
&& StringUtils.equals(mdv.getMetadataField().getQualifier(), "type")) {
return mdv.getValue();
}
}
return null;
}
public EntityType getEntityTypeFromMetadata(Context context, Item item) throws SQLException {
String entityTypeString = getEntityTypeStringFromMetadata(item);
if (StringUtils.isBlank(entityTypeString)) {
return null;
}
return entityTypeService.findByEntityType(context, entityTypeString);
return itemService.getEntityTypeLabel(item);
}
@Override

View File

@@ -792,7 +792,7 @@ public class RelationshipServiceImpl implements RelationshipService {
+ item.getID() + " due to " + currentDepth + " depth");
return;
}
String entityTypeStringFromMetadata = relationshipMetadataService.getEntityTypeStringFromMetadata(item);
String entityTypeStringFromMetadata = itemService.getEntityTypeLabel(item);
EntityType actualEntityType = entityTypeService.findByEntityType(context, entityTypeStringFromMetadata);
// Get all types of relations for the current item
List<RelationshipType> relationshipTypes = relationshipTypeService.findByEntityType(context, actualEntityType);
@@ -865,8 +865,7 @@ public class RelationshipServiceImpl implements RelationshipService {
boolean copyToRightItem)
throws SQLException, AuthorizeException {
if (copyToLeftItem) {
String entityTypeString = relationshipMetadataService
.getEntityTypeStringFromMetadata(relationship.getLeftItem());
String entityTypeString = itemService.getEntityTypeLabel(relationship.getLeftItem());
List<RelationshipMetadataValue> relationshipMetadataValues =
relationshipMetadataService.findRelationshipMetadataValueForItemRelationship(context,
relationship.getLeftItem(), entityTypeString, relationship, true);
@@ -892,8 +891,7 @@ public class RelationshipServiceImpl implements RelationshipService {
itemService.update(context, relationship.getLeftItem());
}
if (copyToRightItem) {
String entityTypeString = relationshipMetadataService
.getEntityTypeStringFromMetadata(relationship.getRightItem());
String entityTypeString = itemService.getEntityTypeLabel(relationship.getRightItem());
List<RelationshipMetadataValue> relationshipMetadataValues =
relationshipMetadataService.findRelationshipMetadataValueForItemRelationship(context,
relationship.getRightItem(), entityTypeString, relationship, true);

View File

@@ -21,6 +21,7 @@ import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataValue;
@@ -783,4 +784,19 @@ public interface ItemService
public List<MetadataValue> getMetadata(Item item, String schema, String element, String qualifier,
String lang, boolean enableVirtualMetadata);
/**
* Retrieve the label of the entity type of the given item.
* @param item the item.
* @return the label of the entity type, taken from the item metadata, or null if not found.
*/
public String getEntityTypeLabel(Item item);
/**
* Retrieve the entity type of the given item.
* @param context the DSpace context.
* @param item the item.
* @return the entity type of the given item, or null if not found.
*/
public EntityType getEntityType(Context context, Item item) throws SQLException;
}

View File

@@ -21,7 +21,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.content.factory.ContentServiceFactory;
@@ -299,8 +298,8 @@ public class VersioningConsumer implements Consumer {
* @return true if the entity types of both items are non-null and equal, false otherwise.
*/
protected boolean doEntityTypesMatch(Item latestItem, Item previousItem) {
String latestItemEntityType = getEntityType(latestItem);
String previousItemEntityType = getEntityType(previousItem);
String latestItemEntityType = itemService.getEntityTypeLabel(latestItem);
String previousItemEntityType = itemService.getEntityTypeLabel(previousItem);
// check if both items have an entity type
if (latestItemEntityType == null || previousItemEntityType == null) {
@@ -338,49 +337,18 @@ public class VersioningConsumer implements Consumer {
return true;
}
/**
* Get the entity type (stored in metadata field dspace.entity.type) of any item.
* @param item the item.
* @return the label of the entity type.
*/
protected String getEntityType(Item item) {
List<MetadataValue> mdvs = itemService.getMetadata(item, "dspace", "entity", "type", Item.ANY, false);
if (mdvs.isEmpty()) {
return null;
}
if (mdvs.size() > 1) {
log.warn(
"Item with uuid {}, handle {} has {} entity types ({}), expected 1 entity type",
item.getID(), item.getHandle(), mdvs.size(),
mdvs.stream().map(MetadataValue::getValue).collect(Collectors.toUnmodifiableList())
);
}
String entityType = mdvs.get(0).getValue();
if (StringUtils.isBlank(entityType)) {
return null;
}
return entityType;
}
/**
* Get the entity type (stored in metadata field dspace.entity.type) of any item.
* @param item the item.
* @return the entity type.
*/
protected EntityType getEntityType(Context ctx, Item item) {
String entityTypeStr = getEntityType(item);
if (entityTypeStr == null) {
return null;
}
try {
return entityTypeService.findByEntityType(ctx, entityTypeStr);
return itemService.getEntityType(ctx, item);
} catch (SQLException e) {
log.error(
"Exception occurred when trying to obtain entity type with label {} of item with uuid {}, handle {}",
entityTypeStr, item.getID(), item.getHandle(), e
itemService.getEntityTypeLabel(item), item.getID(), item.getHandle(), e
);
return null;
}

View File

@@ -2753,7 +2753,7 @@ public class RelationshipServiceImplPlaceTest extends AbstractUnitTest {
}
private String getRelationshipTypeStringForEntity(RelationshipType relationshipType, Item item) {
String entityType = relationshipMetadataService.getEntityTypeStringFromMetadata(item);
String entityType = itemService.getEntityTypeLabel(item);
if (StringUtils.equals(entityType, relationshipType.getLeftType().getLabel())) {
return relationshipType.getLeftwardType();