Skip to content

Commit

Permalink
[DSC-1357] ORCID integration not working correctly with versions
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGiamminonni committed Nov 21, 2023
1 parent 18cc1aa commit 30f4d12
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

Expand All @@ -30,6 +31,7 @@
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.core.CrisConstants;
import org.dspace.core.exception.SQLRuntimeException;
import org.dspace.event.Consumer;
import org.dspace.event.Event;
import org.dspace.orcid.OrcidHistory;
Expand Down Expand Up @@ -73,7 +75,7 @@ public class OrcidQueueConsumer implements Consumer {

private ConfigurationService configurationService;

private List<UUID> alreadyConsumedItems = new ArrayList<>();
private Set<UUID> itemsToConsume = new HashSet<>();

@Override
public void initialize() throws Exception {
Expand Down Expand Up @@ -107,16 +109,26 @@ public void consume(Context context, Event event) throws Exception {
return;
}

if (alreadyConsumedItems.contains(item.getID())) {
return;
}
itemsToConsume.add(item.getID());
}

@Override
public void end(Context context) throws Exception {

for (UUID itemId : itemsToConsume) {

Item item = itemService.find(context, itemId);

context.turnOffAuthorisationSystem();
try {
consumeItem(context, item);
} finally {
context.restoreAuthSystemState();
}

context.turnOffAuthorisationSystem();
try {
consumeItem(context, item);
} finally {
context.restoreAuthSystemState();
}

itemsToConsume.clear();
}

private void consumeItem(Context context, Item item) throws SQLException {
Expand All @@ -132,7 +144,7 @@ private void consumeItem(Context context, Item item) throws SQLException {
consumeProfile(context, item);
}

alreadyConsumedItems.add(item.getID());
itemsToConsume.add(item.getID());

}

Expand Down Expand Up @@ -162,6 +174,10 @@ private void consumeEntity(Context context, Item entity) throws SQLException {
continue;
}

if (isNotLatestVersion(context, entity)) {
continue;
}

orcidQueueService.create(context, relatedItem, entity);

}
Expand Down Expand Up @@ -297,6 +313,14 @@ private boolean isNotProfileItem(Item profileItemItem) {
return !getProfileType().equals(itemService.getEntityTypeLabel(profileItemItem));
}

private boolean isNotLatestVersion(Context context, Item entity) {
try {
return !itemService.isLatestVersion(context, entity);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

private boolean isNestedMetadataPlaceholder(MetadataValue metadata) {
return StringUtils.equals(metadata.getValue(), CrisConstants.PLACEHOLDER_PARENT_METADATA_VALUE);
}
Expand All @@ -322,11 +346,6 @@ private boolean isOrcidSynchronizationDisabled() {
return !configurationService.getBooleanProperty("orcid.synchronization-enabled", true);
}

@Override
public void end(Context context) throws Exception {
alreadyConsumedItems.clear();
}

@Override
public void finish(Context context) throws Exception {
// nothing to do
Expand Down
10 changes: 10 additions & 0 deletions dspace-api/src/main/java/org/dspace/orcid/dao/OrcidQueueDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public List<OrcidQueue> findByProfileItemAndEntity(Context context, Item profile
*/
public List<OrcidQueue> findByProfileItemOrEntity(Context context, Item item) throws SQLException;

/**
* Get the OrcidQueue records where the given item is the entity.
*
* @param context DSpace context object
* @param item the item to search for
* @return the found OrcidQueue entities
* @throws SQLException if database error
*/
public List<OrcidQueue> findByEntity(Context context, Item item) throws SQLException;

/**
* Find all the OrcidQueue records with the given entity and record type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public List<OrcidQueue> findByProfileItemOrEntity(Context context, Item item) th
return query.getResultList();
}

@Override
public List<OrcidQueue> findByEntity(Context context, Item item) throws SQLException {
Query query = createQuery(context, "FROM OrcidQueue WHERE entity.id = :itemId");
query.setParameter("itemId", item.getID());
return query.getResultList();
}

@Override
public List<OrcidQueue> findByEntityAndRecordType(Context context, Item entity, String type) throws SQLException {
Query query = createQuery(context, "FROM OrcidQueue WHERE entity = :entity AND recordType = :type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public List<OrcidQueue> findByProfileItemAndEntity(Context context, Item profile
*/
public List<OrcidQueue> findByProfileItemOrEntity(Context context, Item item) throws SQLException;

/**
* Get the OrcidQueue records where the given item is the entity.
*
* @param context DSpace context object
* @param item the item to search for
* @return the found OrcidQueue records
* @throws SQLException if database error
*/
public List<OrcidQueue> findByEntity(Context context, Item item) throws SQLException;

/**
* Get all the OrcidQueue records with attempts less than the given attempts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public List<OrcidQueue> findByProfileItemOrEntity(Context context, Item item) th
return orcidQueueDAO.findByProfileItemOrEntity(context, item);
}

@Override
public List<OrcidQueue> findByEntity(Context context, Item item) throws SQLException {
return orcidQueueDAO.findByEntity(context, item);
}

@Override
public long countByProfileItemId(Context context, UUID profileItemId) throws SQLException {
return orcidQueueDAO.countByProfileItemId(context, profileItemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.exception.SQLRuntimeException;
import org.dspace.discovery.IndexEventConsumer;
import org.dspace.event.Consumer;
import org.dspace.event.Event;
import org.dspace.orcid.OrcidHistory;
import org.dspace.orcid.OrcidQueue;
import org.dspace.orcid.factory.OrcidServiceFactory;
import org.dspace.orcid.service.OrcidHistoryService;
import org.dspace.orcid.service.OrcidQueueService;
import org.dspace.utils.DSpace;
import org.dspace.versioning.factory.VersionServiceFactory;
import org.dspace.versioning.service.VersionHistoryService;
Expand Down Expand Up @@ -61,6 +67,8 @@ public class VersioningConsumer implements Consumer {
private RelationshipService relationshipService;
private RelationshipVersioningUtils relationshipVersioningUtils;
private DedupService dedupService;
private OrcidQueueService orcidQueueService;
private OrcidHistoryService orcidHistoryService;

@Override
public void initialize() throws Exception {
Expand All @@ -72,6 +80,8 @@ public void initialize() throws Exception {
relationshipVersioningUtils = VersionServiceFactory.getInstance().getRelationshipVersioningUtils();
dedupService = new DSpace().getServiceManager().getServiceByName(DedupService.class.getName(),
DedupService.class);
this.orcidQueueService = OrcidServiceFactory.getInstance().getOrcidQueueService();
this.orcidHistoryService = OrcidServiceFactory.getInstance().getOrcidHistoryService();
}

@Override
Expand Down Expand Up @@ -138,6 +148,8 @@ public void consume(Context ctx, Event event) throws Exception {
// unarchive previous item
unarchiveItem(ctx, previousItem);

handleOrcidSynchronization(ctx, previousItem, latestItem);

updateDuplicateDetection(ctx, latestItem, previousItem);

// update relationships
Expand All @@ -155,6 +167,29 @@ protected void unarchiveItem(Context ctx, Item item) {
));
}

private void handleOrcidSynchronization(Context ctx, Item previousItem, Item latestItem) {
try {
replaceOrcidHistoryEntities(ctx, previousItem, latestItem);
removeOrcidQueueEntries(ctx, previousItem);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

private void removeOrcidQueueEntries(Context ctx, Item previousItem) throws SQLException {
List<OrcidQueue> queueEntries = orcidQueueService.findByEntity(ctx, previousItem);
for (OrcidQueue queueEntry : queueEntries) {
orcidQueueService.delete(ctx, queueEntry);
}
}

private void replaceOrcidHistoryEntities(Context ctx, Item previousItem, Item latestItem) throws SQLException {
List<OrcidHistory> entries = orcidHistoryService.findByEntity(ctx, previousItem);
for (OrcidHistory entry : entries) {
entry.setEntity(latestItem);
}
}

private void updateDuplicateDetection(Context ctx, Item latestItem, Item previousItem) throws Exception {
dedupService.inheritDecisions(ctx, previousItem, latestItem);
dedupService.removeMatch(previousItem);
Expand Down
Loading

0 comments on commit 30f4d12

Please sign in to comment.