1616import java .util .Objects ;
1717import java .util .UUID ;
1818
19- import com .amazonaws .util .CollectionUtils ;
2019import org .dspace .content .Bitstream ;
2120import org .dspace .content .Bundle ;
2221import org .dspace .content .Item ;
@@ -38,92 +37,85 @@ public class LicenseCheck extends Check {
3837 private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService =
3938 ClarinServiceFactory .getInstance ().getClarinLicenseResourceMappingService ();
4039
41- private Map <String , Integer > licensesCount = new HashMap <>();
42- private Map <String , List <UUID >> problemItems = new HashMap <>();
4340
4441 @ Override
4542 protected String run (ReportInfo ri ) {
4643 Context context = new Context ();
4744 StringBuilder sb = new StringBuilder ();
48-
49- Iterator <Item > items ;
5045 ItemService itemService = ContentServiceFactory .getInstance ().getItemService ();
46+ Map <String , Integer > licensesCount = new HashMap <>();
47+ Map <String , List <UUID >> problemItems = new HashMap <>();
48+ List <UUID > bitstreamUUIDs = new ArrayList <>();
49+ Iterator <Item > items ;
5150 try {
5251 items = itemService .findAll (context );
53- } catch (SQLException e ) {
54- throw new RuntimeException ("Error while fetching items. " , e );
55- }
56-
57- for (Iterator <Item > it = items ; it .hasNext (); ) {
58- Item item = it .next ();
59-
60- List <Bundle > bundles = item .getBundles (Constants .DEFAULT_BUNDLE_NAME );
61- if (bundles .isEmpty ()) {
62- licensesCount .put ("no bundle" , licensesCount .getOrDefault ("no bundle" , 0 ) + 1 );
63- continue ;
64- }
65-
66- if (item .getBundles (Constants .LICENSE_BUNDLE_NAME ).isEmpty ()) {
67- problemItems .computeIfAbsent (
68- "UUIDs of items without license bundle" , k -> new ArrayList <>()).add (item .getID ());
52+ while (items .hasNext ()) {
53+ Item item = items .next ();
54+ List <Bundle > bundles = item .getBundles (Constants .DEFAULT_BUNDLE_NAME );
55+ if (bundles .isEmpty ()) {
56+ licensesCount .put ("no bundle" , licensesCount .getOrDefault ("no bundle" , 0 ) + 1 );
57+ continue ;
58+ }
59+ if (item .getBundles (Constants .LICENSE_BUNDLE_NAME ).isEmpty ()) {
60+ problemItems .computeIfAbsent (
61+ "UUIDs of items without license bundle" , k -> new ArrayList <>()).add (item .getID ());
62+ }
63+ List <Bitstream > bitstreams = bundles .get (0 ).getBitstreams ();
64+ if (bitstreams .isEmpty ()) {
65+ problemItems .computeIfAbsent (
66+ "UUIDs of items without bitstreams" , k -> new ArrayList <>()).add (item .getID ());
67+ continue ;
68+ }
69+ Bitstream firstBitstream = bitstreams .get (0 );
70+ UUID uuid = firstBitstream .getID ();
71+ bitstreamUUIDs .add (uuid );
6972 }
70-
71- List <Bitstream > bitstreams = bundles .get (0 ).getBitstreams ();
72- if (bitstreams .isEmpty ()) {
73- problemItems .computeIfAbsent (
74- "UUIDs of items without bitstreams" , k -> new ArrayList <>()).add (item .getID ());
75- continue ;
73+ // Batch fetch all mappings for the collected UUIDs
74+ List <ClarinLicenseResourceMapping > mappingList =
75+ clarinLicenseResourceMappingService .findByBitstreamUUIDs (context , bitstreamUUIDs );
76+ Map <UUID , ClarinLicenseResourceMapping > mappingByUUID = new HashMap <>();
77+ for (ClarinLicenseResourceMapping mapping : mappingList ) {
78+ if (mapping .getBitstream () != null ) {
79+ mappingByUUID .put (mapping .getBitstream ().getID (), mapping );
80+ }
7681 }
77-
78- // one bitstream is enough as there is only one license for all bitstreams in item
79- Bitstream firstBitstream = bitstreams .get (0 );
80- UUID uuid = firstBitstream .getID ();
81- try {
82- List <ClarinLicenseResourceMapping > clarinLicenseResourceMappingList =
83- clarinLicenseResourceMappingService .findByBitstreamUUID (context , uuid );
84-
85- if (CollectionUtils .isNullOrEmpty (clarinLicenseResourceMappingList )) {
86- log .error ("No license mapping found for bitstream with uuid {}" , uuid );
82+ // Process results in memory
83+ for (UUID uuid : bitstreamUUIDs ) {
84+ ClarinLicenseResourceMapping mapping = mappingByUUID .get (uuid );
85+ if (mapping == null ) {
8786 problemItems .computeIfAbsent (
88- "UUIDs of bitstreams without license mappings" , k -> new ArrayList <>()).add (uuid );
87+ "UUIDs of bitstreams without license mappings" , k -> new ArrayList <>()).add (uuid );
8988 continue ;
9089 }
91-
9290 // Every resource mapping between license and the bitstream has only one record,
9391 // because the bitstream has unique UUID, so get the first record from the List
94- ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingList .get (0 );
95-
96- ClarinLicenseLabel nonExtendedLabel =
97- clarinLicenseResourceMapping .getLicense ().getNonExtendedClarinLicenseLabel ();
98-
92+ ClarinLicenseLabel nonExtendedLabel = mapping .getLicense ().getNonExtendedClarinLicenseLabel ();
9993 if (Objects .isNull (nonExtendedLabel )) {
100- log .error ("Item {} with id {} does not have non extended license label." ,
101- item .getName (), item .getID ());
94+ problemItems .computeIfAbsent (
95+ "UUIDs of bitstreams without non-extended license labels" ,
96+ k -> new ArrayList <>()).add (uuid );
10297 } else {
10398 licensesCount .put (nonExtendedLabel .getLabel (),
10499 licensesCount .getOrDefault (nonExtendedLabel .getLabel (), 0 ) + 1 );
105100 }
106- } catch (SQLException e ) {
107- throw new RuntimeException ("Error while fetching ClarinLicenseResourceMapping by Bitstream UUID: " +
108- uuid , e );
109101 }
102+ } catch (SQLException e ) {
103+ throw new RuntimeException ("Error while fetching items or license mappings." , e );
104+ } finally {
105+ context .close ();
110106 }
111-
112107 for (Map .Entry <String , Integer > result : licensesCount .entrySet ()) {
113108 sb .append (String .format ("%-20s: %d\n " , result .getKey (), result .getValue ()));
114109 }
115-
116110 if (!problemItems .isEmpty ()) {
117- for (Map .Entry <String , List <UUID >> problemItems : problemItems .entrySet ()) {
118- List <UUID > uuids = problemItems .getValue ();
119- sb .append (String .format ("\n %s: %d\n " , problemItems .getKey (), uuids .size ()));
111+ for (Map .Entry <String , List <UUID >> problemEntry : problemItems .entrySet ()) {
112+ List <UUID > uuids = problemEntry .getValue ();
113+ sb .append (String .format ("\n %s: %d\n " , problemEntry .getKey (), uuids .size ()));
120114 for (UUID uuid : uuids ) {
121115 sb .append (String .format (" %s\n " , uuid ));
122116 }
123117 }
124118 }
125-
126- context .close ();
127119 return sb .toString ();
128120 }
129121}
0 commit comments