Skip to content

Commit dcfca14

Browse files
vins01-4scienceatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1075 (pull request DSpace#2116)
DSC-1075 Approved-by: Giuseppe Digilio
2 parents 747a74d + 7c09d79 commit dcfca14

25 files changed

+1020
-243
lines changed

dspace-api/src/main/java/org/dspace/content/logic/InCollectionFilter.java

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.content.logic.condition;
9+
10+
11+
import java.util.Collection;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import org.dspace.content.Item;
16+
import org.dspace.content.logic.LogicalStatementException;
17+
import org.dspace.content.logic.supplier.HandleSupplier;
18+
import org.dspace.content.service.ItemService;
19+
import org.dspace.core.Context;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
22+
/**
23+
* This is an {@link AbstractCondition} that checks if a given {@link Collection} of {@code handles}, contains at least
24+
* one handle provided by the {@link AbstractInHandlesCondition#handleSupplier}.
25+
*
26+
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
27+
**/
28+
public abstract class AbstractInHandlesCondition implements Condition {
29+
30+
public static Boolean isInHandles(
31+
Context context, Item item, HandleSupplier handleSupplier, Collection<String> handles
32+
) {
33+
return handleSupplier.getHandles(context, item)
34+
.flatMap(col ->
35+
col.stream()
36+
.map(handle -> handles.contains(handle))
37+
.filter(Boolean::booleanValue)
38+
.findFirst()
39+
)
40+
.orElse(false);
41+
}
42+
43+
@Autowired
44+
protected ItemService itemService;
45+
46+
protected Map<String, Object> parameters = new HashMap<>();
47+
protected HandleSupplier handleSupplier;
48+
49+
private AbstractInHandlesCondition() {
50+
}
51+
52+
public AbstractInHandlesCondition(HandleSupplier handleSupplier) {
53+
this.handleSupplier = handleSupplier;
54+
}
55+
56+
public AbstractInHandlesCondition(
57+
HandleSupplier handleSupplier,
58+
Map<String, Object> parameters
59+
) {
60+
this(handleSupplier);
61+
this.parameters = parameters;
62+
}
63+
64+
/**
65+
* Abstract method that retrieves the handles for the condition.
66+
*
67+
* @return Collection of handles in String format
68+
* @param <T>
69+
*/
70+
protected abstract <T extends Collection<String>> T getHandles();
71+
72+
@Override
73+
public Boolean getResult(Context context, Item item) throws LogicalStatementException {
74+
return isInHandles(context, item, handleSupplier, getHandles());
75+
}
76+
77+
@Override
78+
public Map<String, Object> getParameters() {
79+
return parameters;
80+
}
81+
82+
@Override
83+
public void setParameters(Map<String, Object> parameters) {
84+
this.parameters = parameters;
85+
}
86+
87+
@Override
88+
public void setItemService(ItemService itemService) {
89+
this.itemService = itemService;
90+
}
91+
}

dspace-api/src/main/java/org/dspace/content/logic/condition/InCollectionCondition.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
import java.sql.SQLException;
1111
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Optional;
1214

1315
import org.apache.logging.log4j.LogManager;
1416
import org.apache.logging.log4j.Logger;
15-
import org.dspace.content.Collection;
1617
import org.dspace.content.DSpaceObject;
1718
import org.dspace.content.Item;
1819
import org.dspace.content.logic.LogicalStatementException;
20+
import org.dspace.content.logic.supplier.HandleSupplierFactory;
1921
import org.dspace.core.Context;
2022

2123
/**
@@ -24,39 +26,50 @@
2426
*
2527
* @author Kim Shepherd
2628
*/
27-
public class InCollectionCondition extends AbstractCondition {
29+
public class InCollectionCondition extends AbstractInHandlesCondition {
2830

2931
private static Logger log = LogManager.getLogger(InCollectionCondition.class);
3032

33+
public InCollectionCondition() {
34+
super(HandleSupplierFactory.getInstance().collectionHandleSupplier());
35+
}
36+
37+
public InCollectionCondition(Map<String, Object> parameters) {
38+
super(HandleSupplierFactory.getInstance().collectionHandleSupplier(), parameters);
39+
}
40+
41+
@Override
42+
public List<String> getHandles() {
43+
return Optional.ofNullable(getParameters().get("collections"))
44+
.map(handles -> (List<String>)handles)
45+
.orElse(List.of());
46+
}
47+
3148
/**
3249
* Return true if item is in one of the specified collections
3350
* Return false if not
34-
* @param context DSpace context
35-
* @param item Item to evaluate
51+
*
52+
* @param context DSpace context
53+
* @param item Item to evaluate
3654
* @return boolean result of evaluation
3755
* @throws LogicalStatementException
3856
*/
3957
@Override
4058
public Boolean getResult(Context context, Item item) throws LogicalStatementException {
59+
return super.getResult(context, item) ||
60+
isParentObjectInHandles(context, item) ||
61+
notFound(item);
62+
}
4163

42-
List<String> collectionHandles = (List<String>)getParameters().get("collections");
43-
List<Collection> itemCollections = item.getCollections();
44-
for (Collection collection : itemCollections) {
45-
if (collectionHandles.contains(collection.getHandle())) {
46-
log.debug("item " + item.getHandle() + " is in collection "
47-
+ collection.getHandle() + ", returning true");
48-
return true;
49-
}
50-
}
51-
64+
private boolean isParentObjectInHandles(Context context, Item item) {
5265
// Look for the parent object of the item. This is important as the item.getOwningCollection method
5366
// may return null, even though the item itself does have a parent object, at the point of archival
5467
try {
5568
DSpaceObject parent = itemService.getParentObject(context, item);
5669
if (parent != null) {
5770
log.debug("Got parent DSO for item: " + parent.getID().toString());
5871
log.debug("Parent DSO handle: " + parent.getHandle());
59-
if (collectionHandles.contains(parent.getHandle())) {
72+
if (getHandles().contains(parent.getHandle())) {
6073
log.debug("item " + item.getHandle() + " is in collection "
6174
+ parent.getHandle() + ", returning true");
6275
return true;
@@ -68,10 +81,11 @@ public Boolean getResult(Context context, Item item) throws LogicalStatementExce
6881
log.error("Error obtaining parent DSO", e);
6982
throw new LogicalStatementException(e);
7083
}
84+
return false;
85+
}
7186

72-
// If we reach this statement, the item did not appear in any of the collections from the parameters
87+
protected boolean notFound(Item item) {
7388
log.debug("item " + item.getHandle() + " not found in the passed collection handle list");
74-
7589
return false;
7690
}
7791
}

0 commit comments

Comments
 (0)