2626import org .labkey .api .audit .AuditLogService ;
2727import org .labkey .api .audit .TransactionAuditProvider ;
2828import org .labkey .api .collections .CaseInsensitiveHashMap ;
29+ import org .labkey .api .collections .CsvSet ;
2930import org .labkey .api .data .ColumnInfo ;
3031import org .labkey .api .data .CompareType ;
3132import org .labkey .api .data .Container ;
9091import java .util .HashMap ;
9192import java .util .List ;
9293import java .util .Map ;
94+ import java .util .stream .Collectors ;
9395
9496import static org .labkey .api .util .IntegerUtils .isIntegral ;
9597
@@ -759,45 +761,39 @@ protected Map<String, Object> deleteRow(User user, Container container, Map<Stri
759761 return result ;
760762 }
761763
764+ record ListRow (Container container , String entityId ){}
762765
763- // Deletes attachments & discussions, and removes list documents from full-text search index.
764- public void deleteRelatedListData (final User user , final Container container )
766+ // Delete attachments and remove documents from the full-text search index. If Container is non-null, delete only
767+ // from that container (truncate case). If container is null, delete from all containers (delete list case).
768+ public void deleteRelatedListData (final @ Nullable Container container )
765769 {
766770 // Unindex all item docs and the entire list doc
767771 ListManager .get ().deleteIndexedList (_list );
768772
769- // Delete attachments and discussions associated with a list in batches of 1,000
770- new TableSelector (getDbTable (), Collections .singleton ("entityId" ), SimpleFilter .createContainerFilter (container ), null ).forEachBatch (String .class , 1000 , new ForEachBatchBlock <>()
773+ if (hasAttachmentProperties ())
771774 {
772- @ Override
773- public boolean accept (String entityId )
774- {
775- return null != entityId ;
776- }
775+ SimpleFilter filter = null != container ? SimpleFilter .createContainerFilter (container ) : null ;
777776
778- @ Override
779- public void exec ( List < String > entityIds )
777+ // Delete attachments associated with a list in batches of 1,000
778+ new TableSelector ( getDbTable (), new CsvSet ( "Container, EntityId" ), filter , null ). forEachBatch ( ListRow . class , 1000 , new ForEachBatchBlock <>( )
780779 {
781- // delete the related list data for this block
782- deleteRelatedListData (user , container , entityIds );
783- }
784- });
785- }
786-
787- // delete the related list data for this block of entityIds
788- private void deleteRelatedListData (User user , Container container , List <String > entityIds )
789- {
790- // Build up set of entityIds and AttachmentParents
791- List <AttachmentParent > attachmentParents = new ArrayList <>();
780+ @ Override
781+ public boolean accept (ListRow row )
782+ {
783+ return null != row .entityId ();
784+ }
792785
793- // Delete Attachments
794- if (hasAttachmentProperties ())
795- {
796- for (String entityId : entityIds )
797- {
798- attachmentParents .add (new ListItemAttachmentParent (entityId , container ));
799- }
800- AttachmentService .get ().deleteAttachments (attachmentParents );
786+ @ Override
787+ public void exec (List <ListRow > rows )
788+ {
789+ // delete the related attachments for this block
790+ AttachmentService .get ().deleteAttachments (
791+ rows .stream ()
792+ .map (row -> new ListItemAttachmentParent (row .entityId (), row .container ()))
793+ .collect (Collectors .toList ())
794+ );
795+ }
796+ });
801797 }
802798 }
803799
@@ -807,7 +803,7 @@ protected int truncateRows(User user, Container container) throws QueryUpdateSer
807803 int result ;
808804 try (DbScope .Transaction transaction = getDbTable ().getSchema ().getScope ().ensureTransaction ())
809805 {
810- deleteRelatedListData (user , container );
806+ deleteRelatedListData (container );
811807 result = super .truncateRows (getListUser (user , container ), container );
812808 transaction .addCommitTask (() -> ListManager .get ().addAuditEvent (_list , user , "Deleted " + result + " rows from list." ), DbScope .CommitTaskOption .POSTCOMMIT );
813809 transaction .commit ();
0 commit comments