diff --git a/dspace-api/src/main/java/org/dspace/orcid/consumer/OrcidQueueConsumer.java b/dspace-api/src/main/java/org/dspace/orcid/consumer/OrcidQueueConsumer.java index d177e61607f..1cc9b43f0c0 100644 --- a/dspace-api/src/main/java/org/dspace/orcid/consumer/OrcidQueueConsumer.java +++ b/dspace-api/src/main/java/org/dspace/orcid/consumer/OrcidQueueConsumer.java @@ -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; @@ -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 orcidqueueentries = this.orcidQueueService.findByProfileItemOrEntity(context, entity); + + for (OrcidQueue orcidqueueentry : orcidqueueentries) { + List 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 findAllRelatedItems(Context context, Item entity) throws SQLException { diff --git a/dspace-api/src/test/java/org/dspace/orcid/OrcidQueueConsumerIT.java b/dspace-api/src/test/java/org/dspace/orcid/OrcidQueueConsumerIT.java index f2e528d78cd..d6bf522267b 100644 --- a/dspace-api/src/test/java/org/dspace/orcid/OrcidQueueConsumerIT.java +++ b/dspace-api/src/test/java/org/dspace/orcid/OrcidQueueConsumerIT.java @@ -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; @@ -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 @@ -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 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 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 orcidQueueRecordsAfterDeletion = orcidQueueService.findAll(context); + assertThat(orcidQueueRecordsAfterDeletion, hasSize(1)); + assertThat(orcidQueueRecords.get(0), matches(profile, publication, "Publication", "123456", UPDATE)); + } + @Test public void testNoOrcidQueueRecordCreationOccursIfPublicationSynchronizationIsDisabled() throws Exception { @@ -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 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 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 orcidQueueRecordsAfterDeletion = orcidQueueService.findAll(context); + assertThat(orcidQueueRecordsAfterDeletion, hasSize(1)); + assertThat(orcidQueueRecordsAfterDeletion.get(0), matches(profile, project, "Project", "123456", UPDATE)); + + } + @Test public void testNoOrcidQueueRecordCreationOccursForNotConfiguredEntities() throws Exception {