1818
1919import java .util .Date ;
2020import java .util .List ;
21+ import java .util .stream .Collectors ;
2122
2223
24+ import org .apache .commons .collections .CollectionUtils ;
2325import org .springframework .stereotype .Component ;
2426
2527import com .cloud .event .Event .State ;
2931import com .cloud .utils .db .SearchBuilder ;
3032import com .cloud .utils .db .SearchCriteria ;
3133import com .cloud .utils .db .SearchCriteria .Op ;
32- import com .cloud .utils .db .TransactionLegacy ;
34+ import com .cloud .utils .db .UpdateBuilder ;
3335
3436@ Component
3537public class EventDaoImpl extends GenericDaoBase <EventVO , Long > implements EventDao {
3638 protected final SearchBuilder <EventVO > CompletedEventSearch ;
3739 protected final SearchBuilder <EventVO > ToArchiveOrDeleteEventSearch ;
40+ protected final SearchBuilder <EventVO > ArchiveByIdsSearch ;
3841
3942 public EventDaoImpl () {
4043 CompletedEventSearch = createSearchBuilder ();
@@ -51,6 +54,10 @@ public EventDaoImpl() {
5154 ToArchiveOrDeleteEventSearch .and ("createdDateL" , ToArchiveOrDeleteEventSearch .entity ().getCreateDate (), Op .LTEQ );
5255 ToArchiveOrDeleteEventSearch .and ("archived" , ToArchiveOrDeleteEventSearch .entity ().getArchived (), Op .EQ );
5356 ToArchiveOrDeleteEventSearch .done ();
57+
58+ ArchiveByIdsSearch = createSearchBuilder ();
59+ ArchiveByIdsSearch .and ("id" , ArchiveByIdsSearch .entity ().getId (), Op .IN );
60+ ArchiveByIdsSearch .done ();
5461 }
5562
5663 @ Override
@@ -100,16 +107,16 @@ public List<EventVO> listToArchiveOrDeleteEvents(List<Long> ids, String type, Da
100107
101108 @ Override
102109 public void archiveEvents (List <EventVO > events ) {
103- if (events != null && !events .isEmpty ()) {
104- TransactionLegacy txn = TransactionLegacy .currentTxn ();
105- txn .start ();
106- for (EventVO event : events ) {
107- event = lockRow (event .getId (), true );
108- event .setArchived (true );
109- update (event .getId (), event );
110- txn .commit ();
111- }
112- txn .close ();
110+ if (CollectionUtils .isEmpty (events )) {
111+ return ;
113112 }
113+
114+ List <Long > ids = events .stream ().map (EventVO ::getId ).collect (Collectors .toList ());
115+ SearchCriteria <EventVO > sc = ArchiveByIdsSearch .create ();
116+ sc .setParameters ("id" , ids .toArray (new Object [ids .size ()]));
117+ EventVO eventForUpdate = createForUpdate ();
118+ eventForUpdate .setArchived (true );
119+ UpdateBuilder ub = getUpdateBuilder (eventForUpdate );
120+ update (ub , sc , null );
114121 }
115122}
0 commit comments