com.deltachi.videotex.daos.UpdaterForMovies.java Source code

Java tutorial

Introduction

Here is the source code for com.deltachi.videotex.daos.UpdaterForMovies.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.deltachi.videotex.daos;

import com.deltachi.videotex.grabers.IMDBGraber;
import com.deltachi.videotex.entities.Genre;
import com.deltachi.videotex.entities.Language;
import com.deltachi.videotex.entities.Movie;
import com.deltachi.videotex.entities.MovieTorrent;
import com.deltachi.videotex.entities.Screenshot;
import com.deltachi.videotex.entities.Torrent;
import com.deltachi.videotex.entities.Video;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;

/**
 *
 * @author dimitrios1988
 */
public class UpdaterForMovies extends Thread {
    private List<Language> languages;
    private Torrent t;
    private String imdbCode;
    private boolean completedSuccessfuly;

    public UpdaterForMovies(Torrent t, String imdbCode) {
        this.t = t;
        this.imdbCode = imdbCode;
        this.completedSuccessfuly = false;
    }

    public void run() {
        if (DAOSingleton.getInstance().torrentExists(t.getChecksum()) == false) {
            languages = DAOSingleton.getInstance().getLanguages();
            Video retrievedVideo = DAOSingleton.getInstance().findVideoByImdbCode(imdbCode);
            if (retrievedVideo == null) {
                Video video = null;
                try {
                    video = new IMDBGraber().getVideo(imdbCode);
                } catch (JSONException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                    return;
                } catch (IOException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                    return;
                }
                Collection<Genre> genreCollection = video.getGenreCollection();
                GenreDAO[] genreUpdateThread = new GenreDAO[genreCollection.size()];
                int i = 0;
                for (Genre g : genreCollection) {
                    genreUpdateThread[i] = new GenreDAO(g);
                    i++;
                }
                for (GenreDAO genreUpdateThread1 : genreUpdateThread) {
                    genreUpdateThread1.start();
                }
                for (GenreDAO genreUpdateThread1 : genreUpdateThread) {
                    try {
                        genreUpdateThread1.join();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                VideoDAO videoUpdater = new VideoDAO(video);
                videoUpdater.start();
                try {
                    videoUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                video = DAOSingleton.getInstance().findVideoByImdbCode(video.getImdbCode());
                MovieDAO movieUpdater = new MovieDAO(video);
                movieUpdater.start();
                try {
                    movieUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                Collection<Genre> genreCollection2 = new ArrayList<>();
                genreCollection.stream().forEach((g) -> {
                    genreCollection2.add(DAOSingleton.getInstance().findGenreByName(g.getName()));
                });
                genreCollection = genreCollection2;
                VideoAndGenresAssociater vaga = new VideoAndGenresAssociater(video, genreCollection);
                vaga.start();
                Collection<Screenshot> screenshotCollection = t.getScreenshotCollection();
                int numberOfScreenshots = 0;
                if (screenshotCollection.isEmpty() == false) {
                    numberOfScreenshots = screenshotCollection.size();
                }
                t.setScreenshotCollection(new ArrayList<>());
                TorrentDAO torrentUpdater = new TorrentDAO(t);
                torrentUpdater.start();
                try {
                    torrentUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                t = DAOSingleton.getInstance().findTorrentByChecksum(t.getChecksum());
                Thread[] screenshotsThread = new Thread[numberOfScreenshots];
                int j = 0;
                if (numberOfScreenshots > 0) {
                    for (Screenshot s : screenshotCollection) {
                        s.setTorrentID(t);
                        screenshotsThread[j] = new ScreenshotDAO(s);
                        j++;
                    }
                }
                for (Thread t : screenshotsThread) {
                    t.start();
                }
                for (Thread t : screenshotsThread) {
                    try {
                        t.join();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                Movie movie = DAOSingleton.getInstance().findVideoByImdbCode(video.getImdbCode()).getMovie();
                MovieTorrent mt = new MovieTorrent();
                t = DAOSingleton.getInstance().findTorrentByChecksum(t.getChecksum());
                mt.setMovieID(movie);
                mt.setTorrent(t);
                mt.setTorrentID(t.getId());
                MovieTorrentDAO mtUpdater = new MovieTorrentDAO(mt);
                mtUpdater.start();
                try {
                    mtUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                t.setMovieTorrent(mt);
                torrentUpdater = new TorrentDAO(t);
                torrentUpdater.start();
                try {
                    torrentUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }

                Collection<MovieTorrent> movieTorrentCollection = movie.getMovieTorrentCollection();
                if ((movieTorrentCollection == null) || (movieTorrentCollection.isEmpty())) {
                    movieTorrentCollection = new ArrayList<>();
                }
                movieTorrentCollection.add(mt);
                movie.setMovieTorrentCollection(movieTorrentCollection);
                movieUpdater = new MovieDAO(video, movie);
                movieUpdater.start();
                try {
                    movieUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.completedSuccessfuly = true;
            } else {
                Collection<Screenshot> screenshotCollection = t.getScreenshotCollection();
                int numberOfScreenshots = 0;
                if (screenshotCollection.isEmpty() == false) {
                    numberOfScreenshots = screenshotCollection.size();
                }
                t.setScreenshotCollection(new ArrayList<>());
                TorrentDAO torrentUpdater = new TorrentDAO(t);
                torrentUpdater.start();
                try {
                    torrentUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                t = DAOSingleton.getInstance().findTorrentByChecksum(t.getChecksum());
                Thread[] screenshotsThread = new Thread[numberOfScreenshots];
                int j = 0;
                if (numberOfScreenshots > 0) {
                    for (Screenshot s : screenshotCollection) {
                        s.setTorrentID(t);
                        screenshotsThread[j] = new ScreenshotDAO(s);
                        j++;
                    }
                }
                for (Thread t : screenshotsThread) {
                    t.start();
                }
                for (Thread t : screenshotsThread) {
                    try {
                        t.join();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                Movie movie = DAOSingleton.getInstance().findVideoByImdbCode(retrievedVideo.getImdbCode())
                        .getMovie();
                MovieTorrent mt = new MovieTorrent();
                t = DAOSingleton.getInstance().findTorrentByChecksum(t.getChecksum());
                mt.setMovieID(movie);
                mt.setTorrent(t);
                mt.setTorrentID(t.getId());
                MovieTorrentDAO mtUpdater = new MovieTorrentDAO(mt);
                mtUpdater.start();
                try {
                    mtUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                t.setMovieTorrent(mt);
                torrentUpdater = new TorrentDAO(t);
                torrentUpdater.start();
                try {
                    torrentUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }

                Collection<MovieTorrent> movieTorrentCollection = movie.getMovieTorrentCollection();
                if ((movieTorrentCollection == null) || (movieTorrentCollection.isEmpty())) {
                    movieTorrentCollection = new ArrayList<>();
                }
                movieTorrentCollection.add(mt);
                movie.setMovieTorrentCollection(movieTorrentCollection);
                MovieDAO movieUpdater = new MovieDAO(retrievedVideo, movie);
                movieUpdater.start();
                try {
                    movieUpdater.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdaterForMovies.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.completedSuccessfuly = true;
            }
        }
    }

    public boolean isCompletedSuccessfuly() {
        return this.completedSuccessfuly;
    }
}