Skip to content

Commit

Permalink
consider removed relationships in orcidqueueconsumer
Browse files Browse the repository at this point in the history
consider removed relationships which might still exist in orcid queue entries
DSpace#9298
  • Loading branch information
floriangantner committed Jan 30, 2024
1 parent 068b1bd commit caf9f4e
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.dspace.event.Event;
import org.dspace.orcid.OrcidHistory;
import org.dspace.orcid.OrcidOperation;
import org.dspace.orcid.OrcidQueue;
import org.dspace.orcid.factory.OrcidServiceFactory;
import org.dspace.orcid.model.OrcidEntityType;
import org.dspace.orcid.model.factory.OrcidProfileSectionFactory;
Expand Down Expand Up @@ -173,6 +174,38 @@ private void consumeEntity(Context context, Item entity) throws SQLException {

}

deleteOrcidQueueEntriesNotInRelationship(context, entity);
}

/**
* Loop through all orcid queue entries of entity and check if there is some relation between the entity and
* the profileitem of the orcidqueue entry. If no relation is found and it the record is some insert action
* delete the entry
*/
private void deleteOrcidQueueEntriesNotInRelationship(Context context, Item entity) throws SQLException {
List<OrcidQueue> orcidqueueentries = this.orcidQueueService.findByProfileItemOrEntity(context, entity);

for (OrcidQueue orcidqueueentry : orcidqueueentries) {
List<Item> relatedEntityItems = findAllRelatedItems(context, orcidqueueentry.getEntity());
boolean relationshipexist = false;
for (Item relatedItem : relatedEntityItems) {

if (isNotProfileItem(relatedItem) || isNotLinkedToOrcid(context, relatedItem)) {
continue;
}

if (shouldNotBeSynchronized(relatedItem, entity)) {
continue;
}
if (relatedItem.getID().equals(orcidqueueentry.getProfileItem().getID())) {
relationshipexist = true;
}
}
if (!relationshipexist && orcidqueueentry.isInsertAction()) {
orcidQueueService.delete(context, orcidqueueentry);
}
}

}

private List<Item> findAllRelatedItems(Context context, Item entity) throws SQLException {
Expand Down
140 changes: 140 additions & 0 deletions dspace-api/src/test/java/org/dspace/orcid/OrcidQueueConsumerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
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;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.RelationshipService;
import org.dspace.orcid.consumer.OrcidQueueConsumer;
import org.dspace.orcid.factory.OrcidServiceFactory;
import org.dspace.orcid.service.OrcidQueueService;
Expand All @@ -66,6 +68,8 @@ public class OrcidQueueConsumerIT extends AbstractIntegrationTestWithDatabase {

private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();

protected RelationshipService relationshipService = ContentServiceFactory.getInstance().getRelationshipService();

private Collection profileCollection;

@Before
Expand Down Expand Up @@ -536,6 +540,75 @@ public void testOrcidQueueRecordCreationToUpdatePublication() throws Exception {
assertThat(orcidQueueRecords.get(0), matches(profile, publication, "Publication", "123456", UPDATE));
}

@Test
public void testOrcidQueueRecordCreationToUpdatePublicationAndNotInRelationship() throws Exception {

context.turnOffAuthorisationSystem();

Item profile = ItemBuilder.createItem(context, profileCollection)
.withTitle("Test User")
.withOrcidIdentifier("0000-1111-2222-3333")
.withOrcidAccessToken("ab4d18a0-8d9a-40f1-b601-a417255c8d20", eperson)
.withOrcidSynchronizationPublicationsPreference(ALL)
.build();

Collection publicationCollection = createCollection("Publications", "Publication");

Item publication = ItemBuilder.createItem(context, publicationCollection)
.withTitle("Test publication")
.withAuthor("Test User")
.build();

Item publication1 = ItemBuilder.createItem(context, publicationCollection)
.withTitle("Test publication2")
.withAuthor("Test User")
.build();

createOrcidHistory(context, profile, publication)
.withPutCode("123456")
.withOperation(INSERT)
.build();

EntityType publicationType = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication").build();
EntityType personType = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();

RelationshipType isAuthorOfPublication = createRelationshipTypeBuilder(context, personType, publicationType,
"isAuthorOfPublication", "isPublicationOfAuthor", 0, null, 0, null).build();

RelationshipBuilder.createRelationshipBuilder(context, profile, publication, isAuthorOfPublication).build();

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecords = orcidQueueService.findAll(context);
assertThat(orcidQueueRecords, hasSize(1));
assertThat(orcidQueueRecords.get(0), matches(profile, publication, "Publication", "123456", UPDATE));

context.turnOffAuthorisationSystem();

Relationship rs = RelationshipBuilder
.createRelationshipBuilder(context, profile, publication1, isAuthorOfPublication).build();

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecordsMore = orcidQueueService.findAll(context);
assertThat(orcidQueueRecordsMore, hasSize(2));
assertThat(orcidQueueRecordsMore.get(0), matches(profile, publication, "Publication", "123456", UPDATE));
assertThat(orcidQueueRecordsMore.get(1), matches(profile, publication1, "Publication", INSERT));

context.turnOffAuthorisationSystem();

RelationshipBuilder.deleteRelationship(rs.getID());

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecordsAfterDeletion = orcidQueueService.findAll(context);
assertThat(orcidQueueRecordsAfterDeletion, hasSize(1));
assertThat(orcidQueueRecords.get(0), matches(profile, publication, "Publication", "123456", UPDATE));
}

@Test
public void testNoOrcidQueueRecordCreationOccursIfPublicationSynchronizationIsDisabled() throws Exception {

Expand Down Expand Up @@ -612,6 +685,73 @@ public void testOrcidQueueRecordCreationToUpdateProject() throws Exception {
assertThat(orcidQueueRecords.get(0), matches(profile, project, "Project", "123456", UPDATE));
}

@Test
public void testOrcidQueueRecordCreationToUpdateProjectAndNotInRelationship() throws Exception {

context.turnOffAuthorisationSystem();

Item profile = ItemBuilder.createItem(context, profileCollection)
.withTitle("Test User")
.withOrcidIdentifier("0000-1111-2222-3333")
.withOrcidAccessToken("ab4d18a0-8d9a-40f1-b601-a417255c8d20", eperson)
.withOrcidSynchronizationFundingsPreference(ALL)
.build();

Collection projectCollection = createCollection("Projects", "Project");

Item project = ItemBuilder.createItem(context, projectCollection)
.withTitle("Test project")
.build();

Item project1 = ItemBuilder.createItem(context, projectCollection)
.withTitle("Test project1")
.build();

createOrcidHistory(context, profile, project)
.withPutCode("123456")
.build();

EntityType projectType = EntityTypeBuilder.createEntityTypeBuilder(context, "Project").build();
EntityType personType = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();

RelationshipType isProjectOfPerson = createRelationshipTypeBuilder(context, projectType, personType,
"isProjectOfPerson", "isPersonOfProject", 0, null, 0, null).build();

RelationshipBuilder.createRelationshipBuilder(context, project, profile, isProjectOfPerson).build();

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecords = orcidQueueService.findAll(context);
assertThat(orcidQueueRecords, hasSize(1));
assertThat(orcidQueueRecords.get(0), matches(profile, project, "Project", "123456", UPDATE));

context.turnOffAuthorisationSystem();

Relationship rs =
RelationshipBuilder.createRelationshipBuilder(context, project1, profile, isProjectOfPerson).build();

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecordsMore = orcidQueueService.findAll(context);
assertThat(orcidQueueRecordsMore, hasSize(2));
assertThat(orcidQueueRecordsMore.get(0), matches(profile, project, "Project", "123456", UPDATE));
assertThat(orcidQueueRecordsMore.get(1), matches(profile, project1, "Project", INSERT));

context.turnOffAuthorisationSystem();

RelationshipBuilder.deleteRelationship(rs.getID());

context.restoreAuthSystemState();
context.commit();

List<OrcidQueue> orcidQueueRecordsAfterDeletion = orcidQueueService.findAll(context);
assertThat(orcidQueueRecordsAfterDeletion, hasSize(1));
assertThat(orcidQueueRecordsAfterDeletion.get(0), matches(profile, project, "Project", "123456", UPDATE));

}

@Test
public void testNoOrcidQueueRecordCreationOccursForNotConfiguredEntities() throws Exception {

Expand Down

0 comments on commit caf9f4e

Please sign in to comment.