99
1010import java .sql .SQLException ;
1111import java .util .List ;
12+ import java .util .Map ;
13+ import java .util .Optional ;
1214
1315import org .apache .logging .log4j .LogManager ;
1416import org .apache .logging .log4j .Logger ;
15- import org .dspace .content .Collection ;
1617import org .dspace .content .DSpaceObject ;
1718import org .dspace .content .Item ;
1819import org .dspace .content .logic .LogicalStatementException ;
20+ import org .dspace .content .logic .supplier .HandleSupplierFactory ;
1921import org .dspace .core .Context ;
2022
2123/**
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