From 3fb5326ab83ccf2c205f56fb6ed9fe2dc3d840eb Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 7 Sep 2023 11:00:52 +0200 Subject: [PATCH] Fixed failing tests. Instead of changing owningCollection I used Context events. --- .../content/InstallItemServiceImpl.java | 5 +- .../content/clarin/ClarinItemServiceImpl.java | 34 ++++++++++++ .../service/clarin/ClarinItemService.java | 9 ++++ .../org/dspace/handle/HandleServiceImpl.java | 52 +++++++++++++++++-- 4 files changed, 95 insertions(+), 5 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java index beab2bb7190b..dff1790c7657 100644 --- a/dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java @@ -42,6 +42,8 @@ */ public class InstallItemServiceImpl implements InstallItemService { + public static final String SET_OWNING_COLLECTION_EVENT_DETAIL = "setCollection:"; + @Autowired(required = true) protected ContentServiceFactory contentServiceFactory; @Autowired(required = true) @@ -76,7 +78,8 @@ public Item installItem(Context c, InProgressSubmission is, // CLARIN // The owning collection is needed for getting owning community and creating configured handle. - item.setOwningCollection(collection); + c.addEvent(new Event(Event.MODIFY, Constants.ITEM, item.getID(), + SET_OWNING_COLLECTION_EVENT_DETAIL + collection.getID())); // CLARIN // Get map of filters to use for identifier types. diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinItemServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinItemServiceImpl.java index 0f219bc9024b..936d1e136545 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinItemServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinItemServiceImpl.java @@ -19,6 +19,7 @@ import org.dspace.content.DSpaceObject; import org.dspace.content.Item; import org.dspace.content.dao.clarin.ClarinItemDAO; +import org.dspace.content.service.CollectionService; import org.dspace.content.service.clarin.ClarinItemService; import org.dspace.core.Constants; import org.dspace.core.Context; @@ -36,6 +37,9 @@ public class ClarinItemServiceImpl implements ClarinItemService { @Autowired ClarinItemDAO clarinItemDAO; + @Autowired + CollectionService collectionService; + @Override public List findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException { return clarinItemDAO.findByBitstreamUUID(context, bitstreamUUID); @@ -85,4 +89,34 @@ public Community getOwningCommunity(Context context, DSpaceObject dso) { return null; } + + @Override + public Community getOwningCommunity(Context context, UUID owningCollectionId) throws SQLException { + Collection owningCollection = collectionService.find(context, owningCollectionId); + + if (Objects.isNull(owningCollection)) { + return null; + } + + try { + List communities = owningCollection.getCommunities(); + if (CollectionUtils.isEmpty(communities)) { + log.error("Community list of the owning collection is empty."); + return null; + } + + // First community is the owning community. + Community owningCommunity = communities.get(0); + if (Objects.isNull(owningCommunity)) { + log.error("Owning community is null."); + return null; + } + + return owningCommunity; + } catch (SQLException e) { + log.error("Cannot getOwningCommunity for the Collection: " + owningCollectionId + + ", because: " + e.getSQLState()); + } + return null; + } } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinItemService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinItemService.java index d096ffaac6cb..029b251571d3 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinItemService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinItemService.java @@ -40,4 +40,13 @@ public interface ClarinItemService { * @return owning community or null */ Community getOwningCommunity(Context context, DSpaceObject dso); + + /** + * Get owning community from the collection with UUID which is passed to the method. + * @param context DSpace context object + * @param owningCollectionId UUID of the collection to get the owning community + * @return owning community or null + * @throws SQLException + */ + Community getOwningCommunity(Context context, UUID owningCollectionId) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java index a07e2916bcdf..24166d42f3db 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java @@ -7,10 +7,13 @@ */ package org.dspace.handle; +import static org.dspace.content.InstallItemServiceImpl.SET_OWNING_COLLECTION_EVENT_DETAIL; + import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.UUID; @@ -29,6 +32,7 @@ import org.dspace.content.service.clarin.ClarinItemService; import org.dspace.core.Constants; import org.dspace.core.Context; +import org.dspace.event.Event; import org.dspace.handle.dao.HandleDAO; import org.dspace.handle.service.HandleService; import org.dspace.services.ConfigurationService; @@ -392,12 +396,10 @@ protected String createId(Context context, DSpaceObject dso) throws SQLException //create handle for another type of dspace objects return createdId; } - - // Get owning communityID from the item - Community owningCommunity = clarinItemService.getOwningCommunity(context, dso); + Community owningCommunity = getOwningCommunity(context, dso); UUID owningCommunityId = Objects.isNull(owningCommunity) ? null : owningCommunity.getID(); - //add subprefix for item handle + // add subprefix for item handle PIDCommunityConfiguration pidCommunityConfiguration = PIDConfiguration .getPIDCommunityConfiguration(owningCommunityId); //Which type is pis community configuration? @@ -478,6 +480,48 @@ public String parseHandle(String identifier) { return null; } + /** + * + * @param context DSpace context + * @param dso DSpaceObject + * @return dso owning community + * @throws SQLException + */ + private Community getOwningCommunity(Context context, DSpaceObject dso) throws SQLException { + // There is stored event with dso collection UUID in the context + Event setOwningCollectionEvent = getClarinSetOwningCollectionEvent(context); + + String detail = Objects.isNull(setOwningCollectionEvent) ? "" : setOwningCollectionEvent.getDetail(); + if (StringUtils.isNotBlank(detail)) { + int searchingCharIndex = detail.indexOf(":"); + detail = detail.substring(searchingCharIndex + 1); + return clarinItemService.getOwningCommunity(context, UUID.fromString(detail)); + } + + return clarinItemService.getOwningCommunity(context, dso); + } + + /** + * Context has a lot of events stored in the list. Fetch just that one with the special detail prefix. + * @param context DSpace context + * @return event with owningCollection UUID + */ + private Event getClarinSetOwningCollectionEvent(Context context) { + int index = -1; + LinkedList allEvents = context.getEvents(); + for (Event event: allEvents) { + index++; + if (StringUtils.isBlank(event.getDetail())) { + continue; + } + if (StringUtils.startsWith(event.getDetail(), SET_OWNING_COLLECTION_EVENT_DETAIL)) { + context.getEvents().remove(index); + return event; + } + } + return null; + } + @Override public String[] getAdditionalPrefixes() { return configurationService.getArrayProperty("handle.additional.prefixes");