35
35
import org .apereo .openequella .tools .toolbox .Config ;
36
36
import org .apereo .openequella .tools .toolbox .utils .Check ;
37
37
import org .apereo .openequella .tools .toolbox .utils .CheckFilesUtils ;
38
+ import org .apereo .openequella .tools .toolbox .utils .Pair ;
38
39
import org .apereo .openequella .tools .toolbox .utils .WhereClauseExpression ;
39
40
40
41
/**
@@ -104,39 +105,47 @@ public boolean execute() {
104
105
return false ;
105
106
}
106
107
107
- // Cache Items
108
- if ( Config .get ( Config . CF_MODE ). contains ( "ALL_ITEMS" ) ) {
109
- if (!cacheItemsAll ( )) {
108
+ if ( Config . CheckFilesType . valueOf ( Config . get ( Config . CF_MODE ))
109
+ == Config .CheckFilesType . DB_BATCH_ITEMS_PER_ITEM_ATTS_CONFIRM_INLINE ) {
110
+ if (!cacheItemsBatched ( true )) {
110
111
closeConnection ();
111
112
return false ;
112
113
}
113
114
} else {
114
- if (!cacheItemsBatched ()) {
115
- closeConnection ();
116
- return false ;
115
+ // Cache Items
116
+ if (Config .get (Config .CF_MODE ).contains ("ALL_ITEMS" )) {
117
+ if (!cacheItemsAll ()) {
118
+ closeConnection ();
119
+ return false ;
120
+ }
121
+ } else {
122
+ if (!cacheItemsBatched (false )) {
123
+ closeConnection ();
124
+ return false ;
125
+ }
117
126
}
118
- }
119
127
120
- // Cache Attachments
121
- if (Config .get (Config .CF_MODE ).endsWith ("ALL_ATTACHMENTS" )) {
122
- if (!cacheAttachmentsAll ()) {
123
- closeConnection ();
124
- return false ;
125
- }
126
- } else {
127
- if (!cacheAttachmentsPerItem ()) {
128
- closeConnection ();
129
- return false ;
128
+ // Cache Attachments
129
+ if (Config .get (Config .CF_MODE ).endsWith ("ALL_ATTACHMENTS" )) {
130
+ if (!cacheAttachmentsAll ()) {
131
+ closeConnection ();
132
+ return false ;
133
+ }
134
+ } else {
135
+ if (!cacheAttachmentsPerItem ()) {
136
+ closeConnection ();
137
+ return false ;
138
+ }
130
139
}
131
- }
132
140
133
- // Check attachments one item at a time
134
- int counter = 0 ;
135
- for (Integer itemId : attachmentsCache .keySet ()) {
136
- counter ++;
137
- if (!processItem (itemId , counter )) {
138
- closeConnection ();
139
- return false ;
141
+ // Check attachments one item at a time
142
+ int counter = 0 ;
143
+ for (Integer itemId : attachmentsCache .keySet ()) {
144
+ counter ++;
145
+ if (!processItem (itemId , counter )) {
146
+ closeConnection ();
147
+ return false ;
148
+ }
140
149
}
141
150
}
142
151
@@ -443,33 +452,11 @@ private boolean cacheAttachmentsPerItem() {
443
452
try {
444
453
// For each item cached, query for the associated attachments.
445
454
for (List <ResultsRow > itemRowSet : attachmentsCache .values ()) {
446
- long batchStart = System .currentTimeMillis ();
447
-
448
- int itemId = itemRowSet .get (0 ).getItemId ();
449
- PreparedStatement stmt =
450
- con .prepareStatement (
451
- "select a.type, a.url, a.value1, a.uuid, a.id "
452
- + "from attachment a where a.item_id = ?" );
453
- stmt .setInt (1 , itemId );
454
- ResultSet rs = stmt .executeQuery ();
455
- while (rs .next ()) {
456
- counter ++;
457
- ResultsRow attRow =
458
- inflateAttRow (
459
- rs .getString (1 ),
460
- rs .getString (2 ),
461
- rs .getString (3 ),
462
- rs .getString (4 ),
463
- rs .getInt (5 ),
464
- itemId );
465
- // Note - at this point, attRow will not be null.
466
- attachmentsCache .get (attRow .getItemId ()).add (attRow );
467
- logger .info ("Attachment ({}) gathered: {}" , counter , attRow .toString ());
455
+ Pair <Boolean , Integer > result = cacheAttachmentsPerItem (itemRowSet .get (0 ).getItemId ());
456
+ if (!result .getFirst ()) {
457
+ return false ;
468
458
}
469
- rs .close ();
470
- long batchDur = System .currentTimeMillis () - batchStart ;
471
- ReportManager .getInstance ().getStats ().queryRan (batchDur );
472
- logger .debug ("Cached a batch of attachments. Duration {} ms." , batchDur );
459
+ counter += result .getSecond ();
473
460
}
474
461
} catch (Exception e ) {
475
462
logger .error ("Unable to cache attachments (query per item) - {}" , e .getMessage (), e );
@@ -481,6 +468,46 @@ private boolean cacheAttachmentsPerItem() {
481
468
return true ;
482
469
}
483
470
471
+ private Pair <Boolean , Integer > cacheAttachmentsPerItem (int itemId ) {
472
+ long start = System .currentTimeMillis ();
473
+ int counter = 0 ;
474
+ try {
475
+ long batchStart = System .currentTimeMillis ();
476
+
477
+ PreparedStatement stmt =
478
+ con .prepareStatement (
479
+ "select a.type, a.url, a.value1, a.uuid, a.id "
480
+ + "from attachment a where a.item_id = ?" );
481
+ stmt .setInt (1 , itemId );
482
+ ResultSet rs = stmt .executeQuery ();
483
+ while (rs .next ()) {
484
+ counter ++;
485
+ ResultsRow attRow =
486
+ inflateAttRow (
487
+ rs .getString (1 ),
488
+ rs .getString (2 ),
489
+ rs .getString (3 ),
490
+ rs .getString (4 ),
491
+ rs .getInt (5 ),
492
+ itemId );
493
+ // Note - at this point, attRow will not be null.
494
+ attachmentsCache .get (attRow .getItemId ()).add (attRow );
495
+ logger .info ("Attachment ({}) gathered: {}" , counter , attRow .toString ());
496
+ }
497
+ rs .close ();
498
+ long batchDur = System .currentTimeMillis () - batchStart ;
499
+ ReportManager .getInstance ().getStats ().queryRan (batchDur );
500
+ logger .debug ("Cached a batch of attachments. Duration {} ms." , batchDur );
501
+ } catch (Exception e ) {
502
+ logger .error ("Unable to cache attachments (query per item) - {}" , e .getMessage (), e );
503
+ return Pair .pair (false , counter );
504
+ }
505
+ long dur = System .currentTimeMillis () - start ;
506
+ logger .info ("Cached {} attachments (query per item). Duration {} ms." , counter , dur );
507
+ ReportManager .getInstance ().getStats ().incNumGrandTotalAttachments (counter );
508
+ return Pair .pair (true , counter );
509
+ }
510
+
484
511
private boolean cacheAttachmentsAll () {
485
512
long start = System .currentTimeMillis ();
486
513
int counter = 0 ;
@@ -704,7 +731,7 @@ private boolean cacheItemsAll() {
704
731
*
705
732
* @return
706
733
*/
707
- private boolean cacheItemsBatched () {
734
+ private boolean cacheItemsBatched (boolean confirmInline ) {
708
735
long start = System .currentTimeMillis ();
709
736
try {
710
737
String sql =
@@ -755,6 +782,20 @@ private boolean cacheItemsBatched() {
755
782
attachmentsCache .put (itemRow .getItemId (), seeder );
756
783
757
784
logger .info ("Found the [{}]th item: {}" , (attachmentsCache .size ()), itemRow .toString ());
785
+ if (confirmInline ) {
786
+ Pair <Boolean , Integer > result = cacheAttachmentsPerItem (itemRow .getItemId ());
787
+ if (result .getFirst ()) {
788
+ if (!processItem (itemRow .getItemId (), attachmentsCache .size ())) {
789
+ rs .close ();
790
+ throw new Exception (
791
+ "Unable to check attachments for item [" + itemRow .getItemId () + "]" );
792
+ }
793
+ } else {
794
+ rs .close ();
795
+ throw new Exception (
796
+ "Unable to cache attachments for item [" + itemRow .getItemId () + "]" );
797
+ }
798
+ }
758
799
}
759
800
rs .close ();
760
801
offsetCounter ++;
0 commit comments