From 6920c38b37efcafa4cb64da28d36e81c02a1629d Mon Sep 17 00:00:00 2001 From: "Gantner, Florian Klaus" Date: Thu, 9 Nov 2023 15:58:57 +0100 Subject: [PATCH] use relationship approach for orcid sync setting check if relationship exist using the relationshipservice rather than the search, becaude the requested item might not have been indexed yet if it is recent archived --- .../impl/OrcidSynchronizationServiceImpl.java | 99 +++++++++---------- 1 file changed, 45 insertions(+), 54 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/orcid/service/impl/OrcidSynchronizationServiceImpl.java b/dspace-api/src/main/java/org/dspace/orcid/service/impl/OrcidSynchronizationServiceImpl.java index bbbf99b9c09..d309427f12e 100644 --- a/dspace-api/src/main/java/org/dspace/orcid/service/impl/OrcidSynchronizationServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/orcid/service/impl/OrcidSynchronizationServiceImpl.java @@ -28,7 +28,6 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.content.Item; import org.dspace.content.MetadataValue; -import org.dspace.content.Relationship; import org.dspace.content.service.ItemService; import org.dspace.content.service.RelationshipService; import org.dspace.core.Context; @@ -200,28 +199,34 @@ public boolean isSynchronizationAllowed(Context context, Item profile, Item item Optional option; option = getEntityPreference(profile, OrcidEntityType.fromEntityType(entityType)); if (option.isPresent()) { - switch (option.get()) { - case DISABLED: - return false; - case ALL: - return true; - case MINE: - case MY_SELECTED: - String relationname = configurationService.getProperty( - "orcid.relation." + entityType + "." + option.get().name()); - boolean exclusion = configurationService.getBooleanProperty("orcid.relation." - + entityType + "." + option.get().name() + ".exclusion", false); - if (isBlank(relationname)) { + try { + switch (option.get()) { + case DISABLED: return false; - } - Iterator entities = checkRelation(context, profile, item, relationname, exclusion); - if (entities.hasNext()) { + case ALL: return true; - } else { + case MINE: + case MY_SELECTED: + String relationname = configurationService.getProperty("orcid.relation." + entityType + "." + + option.get().name()); + if (isBlank(relationname)) { + return false; + } + if (configurationService.getBooleanProperty("orcid.relation." + entityType + "." + + option.get().name() + ".exclusion", false)) { + //check if no relationship exists between profile and item (e.g. MINE) + return relationShipMatchForOrcidSync(context, profile, item, relationname, false); + } else { + //check, if any relationship exists between profile and item(e.g. MY_SELECTED) + + return relationShipMatchForOrcidSync(context, profile, item, relationname, true); + + } + default: return false; - } - default: - return false; + } + } catch (SQLException e) { + throw new RuntimeException(e); } } else { return false; @@ -249,30 +254,20 @@ public boolean isSyncSettingsBasedOnRelationshipCriteriaNotValid(Context context switch (option.get()) { case MINE: case MY_SELECTED: - // Consider MINE and MY_SELECTED Settings - String relationname = configurationService.getProperty( - "orcid.relation." + entityType + "." + option.get().name()); + String relationname = configurationService.getProperty("orcid.relation." + entityType + "." + + option.get().name()); if (isBlank(relationname)) { return false; } - boolean exclusion = configurationService.getBooleanProperty("orcid.relation." - + entityType + "." + option.get().name() + ".exclusion", false); - if (exclusion) { - //check, if relationship exists - List rels = relationshipService.findByItem(context, profile); - boolean val = rels.stream() - .anyMatch(relationship -> - relationship.getLeftItem().getID().toString().equals(item.getID().toString()) && - relationship.getLeftwardValue().contentEquals(relationname)); - return val; + + //because we check the criteria is not valid we have to check the opposite + if (configurationService.getBooleanProperty("orcid.relation." + entityType + "." + + option.get().name() + ".exclusion", false)) { + //check, if any relationship exists between profile and item (e.g. MINE) + return relationShipMatchForOrcidSync(context, profile, item, relationname, true); } else { - //check if no relationship exists - List rels = relationshipService.findByItem(context, profile); - boolean val = rels.stream() - .noneMatch(relationship -> - relationship.getLeftItem().getID().toString().equals(item.getID().toString()) && - relationship.getLeftwardValue().contentEquals(relationname)); - return val; + //check if none relationship exist between profile and item (e.g. MY_SELECT) + return relationShipMatchForOrcidSync(context, profile, item, relationname, false); } default: @@ -285,21 +280,17 @@ public boolean isSyncSettingsBasedOnRelationshipCriteriaNotValid(Context context return false; } - private Iterator checkRelation(Context context, Item profile, Item item, String filterrelationname, - boolean exclusion) { - StringBuilder sb = new StringBuilder(); - if (exclusion) { - sb.append("-"); + protected boolean relationShipMatchForOrcidSync(Context context, Item profile, Item item, String relationname, + boolean any) throws SQLException { + if (any) { + return relationshipService.findByItem(context, profile).stream().anyMatch(relationship -> + relationship.getLeftItem().getID().toString().equals(item.getID().toString()) && + relationship.getLeftwardValue().contentEquals(relationname)); + } else { + return relationshipService.findByItem(context, profile).stream().noneMatch(relationship -> + relationship.getLeftItem().getID().toString().equals(item.getID().toString()) && + relationship.getLeftwardValue().contentEquals(relationname)); } - sb.append("relation." + filterrelationname + ":"); - sb.append(profile.getID().toString()); - - DiscoverQuery discoverQuery = new DiscoverQuery(); - discoverQuery.setDSpaceObjectFilter(IndexableItem.TYPE); - discoverQuery.addFilterQueries("search.resourceid:" + item.getID()); - discoverQuery.addFilterQueries(sb.toString()); - discoverQuery.setMaxResults(1); - return new DiscoverResultItemIterator(context, discoverQuery); } @Override