Search in sources :

Example 1 with UpdatePlaylistItemIndicesTask

use of net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask in project aiode by robinfriedli.

the class VerifyPlaylistInterceptor method preFlush.

@Override
public void preFlush(Iterator entities) {
    @SuppressWarnings("unchecked") Iterable<Object> iterable = () -> entities;
    Set<Playlist> playlistsToUpdate = StreamSupport.stream(iterable.spliterator(), false).filter(entity -> entity instanceof PlaylistItem).map(entity -> ((PlaylistItem) entity).getPlaylist()).collect(Collectors.toSet());
    if (!playlistsToUpdate.isEmpty()) {
        playlistsToUpdate.forEach(this::checkPlaylistSize);
        UpdatePlaylistItemIndicesTask task = new UpdatePlaylistItemIndicesTask(playlistsToUpdate, Comparator.comparing(PlaylistItem::getOrdinal));
        task.perform();
    }
    super.preFlush(entities);
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Song(net.robinfriedli.aiode.entities.Song) Set(java.util.Set) Interceptor(org.hibernate.Interceptor) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Transaction(org.hibernate.Transaction) Playlist(net.robinfriedli.aiode.entities.Playlist) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) StreamSupport(java.util.stream.StreamSupport) Video(net.robinfriedli.aiode.entities.Video) Comparator(java.util.Comparator) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Type(org.hibernate.type.Type) Playlist(net.robinfriedli.aiode.entities.Playlist) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 2 with UpdatePlaylistItemIndicesTask

use of net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask in project aiode by robinfriedli.

the class SetPlaylistItemIndexTask method perform.

@Override
public void perform(@Nullable JDA shard) {
    try (Session session = sessionFactory.openSession()) {
        Query<Playlist> relevantPlaylistQuery = session.createQuery("from " + Playlist.class.getName() + " as p where " + "exists(from " + Song.class.getName() + " where item_index = null and playlist_pk = p.pk) or " + "exists(from " + Video.class.getName() + " where item_index = null and playlist_pk = p.pk) or " + "exists(from " + UrlTrack.class.getName() + " where item_index = null and playlist_pk = p.pk)", Playlist.class);
        List<Playlist> relevantPlaylists = relevantPlaylistQuery.getResultList();
        session.beginTransaction();
        UpdatePlaylistItemIndicesTask task = new UpdatePlaylistItemIndicesTask(relevantPlaylists);
        task.perform();
        session.getTransaction().commit();
    }
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) Song(net.robinfriedli.aiode.entities.Song) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Session(org.hibernate.Session)

Aggregations

Playlist (net.robinfriedli.aiode.entities.Playlist)2 Song (net.robinfriedli.aiode.entities.Song)2 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)2 UpdatePlaylistItemIndicesTask (net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask)2 Serializable (java.io.Serializable)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 SpringPropertiesConfig (net.robinfriedli.aiode.boot.SpringPropertiesConfig)1 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)1 Video (net.robinfriedli.aiode.entities.Video)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 Interceptor (org.hibernate.Interceptor)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 Type (org.hibernate.type.Type)1 Logger (org.slf4j.Logger)1