From 5e40130458ddd8713831229bebdbada51dfd0461 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Thu, 25 Jan 2024 09:42:53 +0100 Subject: [PATCH 1/2] [DSC-1503] Add in-collection item filter --- .../content/logic/InCollectionFilter.java | 125 ++++++++++++++++++ dspace/config/spring/api/item-filters.xml | 16 +++ 2 files changed, 141 insertions(+) create mode 100644 dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java diff --git a/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java b/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java new file mode 100644 index 000000000000..6fdc53460f82 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java @@ -0,0 +1,125 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.content.logic; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.dspace.content.Collection; +import org.dspace.content.DSpaceObject; +import org.dspace.content.Item; +import org.dspace.content.logic.LogicalStatementException; +import org.dspace.content.service.CollectionService; +import org.dspace.content.service.ItemService; +import org.dspace.core.Context; +import org.dspace.handle.service.HandleService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * A condition that accepts a list of collection handles and returns true + * if the item belongs to any of them. + * + * @author Kim Shepherd + * @author Giuseppe Digilio + */ +public class InCollectionFilter implements Filter { + + @Autowired(required = true) + protected ItemService itemService; + @Autowired(required = true) + protected CollectionService collectionService; + @Autowired(required = true) + protected HandleService handleService; + + private String name; + private Map parameters = new HashMap<>(); + private static Logger log = LogManager.getLogger(InCollectionFilter.class); + + /** + * Get parameters set by spring configuration in item-filters.xml + * These could be any kind of map that the extending condition class needs for evaluation + * @return map of parameters + * @throws LogicalStatementException + */ + public Map getParameters() throws LogicalStatementException { + return this.parameters; + } + + /** + * Set parameters - used by Spring when creating beans from item-filters.xml + * These could be any kind of map that the extending condition class needs for evaluation + * @param parameters + * @throws LogicalStatementException + */ + @Autowired(required = true) + public void setParameters(Map parameters) throws LogicalStatementException { + this.parameters = parameters; + } + + /** + * Return true if item is in one of the specified collections + * Return false if not + * @param context DSpace context + * @param item Item to evaluate + * @return boolean result of evaluation + * @throws LogicalStatementException + */ + @Override + public Boolean getResult(Context context, Item item) throws LogicalStatementException { + + List collectionHandles = (List)getParameters().get("collections"); + List itemCollections = item.getCollections(); + for (Collection collection : itemCollections) { + if (collectionHandles.contains(collection.getHandle())) { + log.debug("item " + item.getHandle() + " is in collection " + + collection.getHandle() + ", returning true"); + return true; + } + } + + // Look for the parent object of the item. This is important as the item.getOwningCollection method + // may return null, even though the item itself does have a parent object, at the point of archival + try { + DSpaceObject parent = itemService.getParentObject(context, item); + if (parent != null) { + log.debug("Got parent DSO for item: " + parent.getID().toString()); + log.debug("Parent DSO handle: " + parent.getHandle()); + if (collectionHandles.contains(parent.getHandle())) { + log.debug("item " + item.getHandle() + " is in collection " + + parent.getHandle() + ", returning true"); + return true; + } + } else { + log.debug("Parent DSO is null..."); + } + } catch (SQLException e) { + log.error("Error obtaining parent DSO", e); + throw new LogicalStatementException(e); + } + + // If we reach this statement, the item did not appear in any of the collections from the parameters + log.debug("item " + item.getHandle() + " not found in the passed collection handle list"); + + return false; + } + + @Override + public void setBeanName(String name) { + log.debug("Initialize bean " + name); + this.name = name; + } + + @Override + public String getName() { + return name; + } +} diff --git a/dspace/config/spring/api/item-filters.xml b/dspace/config/spring/api/item-filters.xml index d494d0c1c2ed..2514304151ae 100644 --- a/dspace/config/spring/api/item-filters.xml +++ b/dspace/config/spring/api/item-filters.xml @@ -408,4 +408,20 @@ + + + From 9cae03593a0918d599ede39f27582c9df1f9ad11 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Thu, 25 Jan 2024 11:09:34 +0100 Subject: [PATCH 2/2] [DSC-1503] Fix checkstyle --- .../main/java/org/dspace/content/logic/InCollectionFilter.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java b/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java index 6fdc53460f82..c7697ce82fa1 100644 --- a/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java +++ b/dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java @@ -17,7 +17,6 @@ import org.dspace.content.Collection; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; -import org.dspace.content.logic.LogicalStatementException; import org.dspace.content.service.CollectionService; import org.dspace.content.service.ItemService; import org.dspace.core.Context; @@ -43,7 +42,7 @@ public class InCollectionFilter implements Filter { private String name; private Map parameters = new HashMap<>(); private static Logger log = LogManager.getLogger(InCollectionFilter.class); - + /** * Get parameters set by spring configuration in item-filters.xml * These could be any kind of map that the extending condition class needs for evaluation