Skip to content

Commit

Permalink
internal/epic-pid-service-never-called (#431)
Browse files Browse the repository at this point in the history
* The item is created with configured prefix.

* The Configuration property configMap is not shared. The right prefix is added during creating the handle.

* The handle is correctly composed and the tests are created

* epic changed to local because the local handle should be used by default.

* Refactoring

* Refactoring

* Fixed one failing test

* Commented out unwanted community configuration.

* Fixed failing tests. Instead of changing owningCollection I used Context events.

* Fixed checkstyle

* Fixed failing IT. It was needed to update configuraition property.

* Fixed failing integration tests - the community configuration service wasn't restored.
  • Loading branch information
milanmajchrak authored Sep 12, 2023
1 parent 80face6 commit 1eb7971
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 79 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 @@ -73,6 +75,13 @@ public Item installItem(Context c, InProgressSubmission is,
AuthorizeException {
Item item = is.getItem();
Collection collection = is.getCollection();

// CLARIN
// The owning collection is needed for getting owning community and creating configured handle.
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.
Map<Class<? extends Identifier>, Filter> filters = FilterUtils.getIdentifierFilters(false);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.content.Collection;
import org.dspace.content.Community;
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;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -25,11 +33,90 @@
*/
public class ClarinItemServiceImpl implements ClarinItemService {

private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinItemServiceImpl.class);
@Autowired
ClarinItemDAO clarinItemDAO;

@Autowired
CollectionService collectionService;

@Override
public List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException {
return clarinItemDAO.findByBitstreamUUID(context, bitstreamUUID);
}

@Override
public Community getOwningCommunity(Context context, DSpaceObject dso) {
if (Objects.isNull(dso)) {
return null;
}
int type = dso.getType();
if (Objects.equals(type, Constants.COMMUNITY)) {
return (Community) dso;
}

Collection owningCollection = null;
if (Objects.equals(type, Constants.COLLECTION)) {
owningCollection = (Collection) dso;
}

if (Objects.equals(type, Constants.ITEM)) {
owningCollection = ((Item) dso).getOwningCollection();
}

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 Item: " + dso.getID() + ", because: " + e.getSQLState());
}

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 @@ -11,6 +11,8 @@
import java.util.List;
import java.util.UUID;

import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Context;

Expand All @@ -30,4 +32,21 @@ public interface ClarinItemService {
* @throws SQLException database error
*/
List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException;

/**
* Get item/collection/community's owning community
* @param context DSpace context object
* @param dso item/collection/community
* @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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
*/
package org.dspace.discovery;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.Logger;
import org.apache.solr.common.SolrInputDocument;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.service.clarin.ClarinItemService;
import org.dspace.core.Context;
import org.dspace.discovery.indexobject.IndexableItem;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Plugin for indexing the Items community. It helps search the Item by the community.
Expand All @@ -30,40 +28,18 @@ public class ClarinSolrItemsCommunityIndexPlugin implements SolrServiceIndexPlug
private static final Logger log = org.apache.logging.log4j.LogManager
.getLogger(ClarinSolrItemsCommunityIndexPlugin.class);

@Autowired(required = true)
protected ClarinItemService clarinItemService;

@Override
public void additionalIndex(Context context, IndexableObject indexableObject, SolrInputDocument document) {
if (indexableObject instanceof IndexableItem) {
Item item = ((IndexableItem) indexableObject).getIndexedObject();

String owningCommunity = this.getOwningCommunity(context, item);
document.addField("items_owning_community", owningCommunity);
}
}
Community owningCommunity = clarinItemService.getOwningCommunity(context, item);
String communityName = Objects.isNull(owningCommunity) ? " " : owningCommunity.getName();

private String getOwningCommunity(Context context, Item item) {
if (Objects.isNull(item)) {
return " ";
document.addField("items_owning_community", communityName);
}
Collection owningCollection = item.getOwningCollection();
try {
List<Community> communities = owningCollection.getCommunities();
if (CollectionUtils.isEmpty(communities)) {
log.error("Community list of the owning collection is empty.");
return " ";
}

// First community is the owning community.
Community owningCommunity = communities.get(0);
if (Objects.isNull(owningCommunity)) {
log.error("Owning community is null.");
return " ";
}

return owningCommunity.getName();
} catch (SQLException e) {
log.error("Cannot getOwningCommunity for the Item: " + item.getID() + ", because: " + e.getSQLState());
}

return " ";
}
}
66 changes: 60 additions & 6 deletions dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
*/
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -21,11 +25,14 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.api.DSpaceApi;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.service.SiteService;
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 @@ -62,6 +69,8 @@ public class HandleServiceImpl implements HandleService {

@Autowired
protected SiteService siteService;
@Autowired
protected ClarinItemService clarinItemService;

private static final Pattern[] IDENTIFIER_PATTERNS = {
Pattern.compile("^hdl:(.*)$"),
Expand Down Expand Up @@ -143,8 +152,8 @@ public String getCanonicalForm(String handle) {
public String createHandle(Context context, DSpaceObject dso)
throws SQLException {
Handle handle = handleDAO.create(context, new Handle());
// String handleId = createId(context, dso);
String handleId = createId(context);
String handleId = createId(context, dso);
// String handleId = createId(context);
handle.setHandle(handleId);
handle.setDSpaceObject(dso);
dso.addHandle(handle);
Expand Down Expand Up @@ -387,10 +396,12 @@ protected String createId(Context context, DSpaceObject dso) throws SQLException
//create handle for another type of dspace objects
return createdId;
}
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(dso.getID());
.getPIDCommunityConfiguration(owningCommunityId);
//Which type is pis community configuration?
if (pidCommunityConfiguration.isEpic()) {
String handleId;
Expand All @@ -413,10 +424,11 @@ protected String createId(Context context, DSpaceObject dso) throws SQLException
}
return handleId;
} else if (pidCommunityConfiguration.isLocal()) {
String prefix = pidCommunityConfiguration.getPrefix();
String handleSubprefix = pidCommunityConfiguration.getSubprefix();
if (Objects.nonNull(handleSubprefix) && !handleSubprefix.isEmpty()) {
//create local handle
return handlePrefix + (handlePrefix.endsWith("/") ? "" : "/")
// create local handle
return prefix + (handlePrefix.endsWith("/") ? "" : "/")
+ handleSubprefix + "-" + handleSuffix.toString();
}
} else {
Expand Down Expand Up @@ -468,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
Loading

0 comments on commit 1eb7971

Please sign in to comment.