Skip to content

Commit

Permalink
Fixed failing tests. Instead of changing owningCollection I used Cont…
Browse files Browse the repository at this point in the history
…ext events.
  • Loading branch information
milanmajchrak committed Sep 7, 2023
1 parent 47b8a77 commit 3fb5326
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,9 @@ public class ClarinItemServiceImpl implements ClarinItemService {
@Autowired
ClarinItemDAO clarinItemDAO;

@Autowired
CollectionService collectionService;

@Override
public List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException {
return clarinItemDAO.findByBitstreamUUID(context, bitstreamUUID);
Expand Down Expand Up @@ -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<Community> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
52 changes: 48 additions & 4 deletions dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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<Event> 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");
Expand Down

0 comments on commit 3fb5326

Please sign in to comment.