Search in sources :

Example 1 with HibernatePlaylistMigrator

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

the class MigratePlaylistsTask method migrateFile.

private void migrateFile(Session session, Context context, Guild guild, SpotifyApi spotifyApi) throws Exception {
    HibernatePlaylistMigrator hibernatePlaylistMigrator = new HibernatePlaylistMigrator(context, guild, spotifyApi, session);
    Map<Playlist, List<PlaylistItem>> playlistMap = hibernatePlaylistMigrator.perform();
    for (Playlist playlist : playlistMap.keySet()) {
        playlistMap.get(playlist).forEach(item -> {
            item.add();
            session.persist(item);
        });
        session.persist(playlist);
    }
    File directory = new File("./resources/archive");
    if (!directory.exists()) {
        directory.mkdir();
    }
    File file = context.getFile();
    Objects.requireNonNull(file);
    Files.move(file, new File(directory.getPath() + "/" + file.getName()));
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) List(java.util.List) File(java.io.File)

Example 2 with HibernatePlaylistMigrator

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

the class GuildManager method handleNewGuild.

private void handleNewGuild(Guild guild, GuildContext guildContext) {
    Aiode aiode = Aiode.get();
    MessageService messageService = aiode.getMessageService();
    try {
        EmbedDocumentContribution embedDocumentContribution = embedDocumentContext.query(attribute("name").is("getting-started"), EmbedDocumentContribution.class).getOnlyResult();
        if (embedDocumentContribution != null) {
            EmbedBuilder embedBuilder = embedDocumentContribution.buildEmbed();
            TextChannel defaultTextChannelForGuild = getDefaultTextChannelForGuild(guild, guildContext);
            if (defaultTextChannelForGuild != null) {
                messageService.sendWithLogo(embedBuilder, defaultTextChannelForGuild);
            }
        }
    } catch (Exception e) {
        logger.error("Error sending getting started message", e);
    }
    SessionFactory sessionFactory = hibernateComponent.getSessionFactory();
    SpotifyApi.Builder spotifyApiBuilder = aiode.getSpotifyApiBuilder();
    if (defaultPlaylistContext != null) {
        try (Session session = sessionFactory.withOptions().interceptor(InterceptorChain.of(PlaylistItemTimestampInterceptor.class, VerifyPlaylistInterceptor.class)).openSession()) {
            HibernatePlaylistMigrator hibernatePlaylistMigrator = new HibernatePlaylistMigrator(defaultPlaylistContext, guild, spotifyApiBuilder.build(), session);
            Map<Playlist, List<PlaylistItem>> playlistMap = hibernatePlaylistMigrator.perform();
            Mode mode = getMode();
            HibernateInvoker.create(session).invokeConsumer(currentSession -> {
                for (Playlist playlist : playlistMap.keySet()) {
                    Playlist existingList = SearchEngine.searchLocalList(currentSession, playlist.getName(), mode == GuildManager.Mode.PARTITIONED, guild.getId());
                    if (existingList == null) {
                        playlistMap.get(playlist).forEach(item -> {
                            item.add();
                            currentSession.persist(item);
                        });
                        currentSession.persist(playlist);
                    }
                }
            });
        } catch (Exception e) {
            logger.error("Exception while setting up default playlists", e);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) Aiode(net.robinfriedli.aiode.Aiode) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Playlist(net.robinfriedli.aiode.entities.Playlist) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) EmbedDocumentContribution(net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution) List(java.util.List) Session(org.hibernate.Session)

Aggregations

List (java.util.List)2 Playlist (net.robinfriedli.aiode.entities.Playlist)2 HibernatePlaylistMigrator (net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator)2 File (java.io.File)1 IOException (java.io.IOException)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 Aiode (net.robinfriedli.aiode.Aiode)1 EmbedDocumentContribution (net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 SpotifyApi (se.michaelthelin.spotify.SpotifyApi)1