Example #1
0
  /**
   * Method to import lists of episodes from a CSV file.
   *
   * @param file The file to import from.
   */
  public void importFromCSV(File file) {

    ShowsService showsService = serviceContext.newService(ShowsService.class);
    EpisodesService episodesService = serviceContext.newService(EpisodesService.class);
    SeasonsService seasonsService = serviceContext.newService(SeasonsService.class);

    try {
      CSVReader reader = new CSVReader(new FileReader(file), ';');
      List<String[]> myEntries = reader.readAll();

      // remove the headers
      myEntries.remove(0);

      for (String[] episode : myEntries) {
        Show show = null;
        Season season = null;
        Episode newEpisode = null;
        log.debug("Importing episode : " + episode[0] + " - " + episode[2]);
        if (showsService.showExists(episode[0])) {
          log.debug("The show exists, getting it from the database");
          show = showsService.getShowByName(episode[0]);
        } else {
          log.debug("The show does not exist, creating it");
          try {
            show = showsService.createShow(episode[0]);
          } catch (AlreadyExistException aee) {
            log.error("Show already exists, not normal", aee);
          }
        }

        log.debug("Using show : " + show.getTitle());

        if (seasonsService.seasonExists(show, Integer.parseInt(episode[1]))) {
          log.debug("Season exist, getting it from the database");
          season = seasonsService.getSeasonByNumber(show, Integer.parseInt(episode[1]));
        } else {
          log.debug("The season does not exist, creating it");
          try {
            season = seasonsService.createSeason(show, Integer.parseInt(episode[1]));
          } catch (AlreadyExistException aee) {
            log.error("The season " + episode[1] + " already exists, it shouldn't");
          }
        }

        log.debug("Using season " + season.getNumber() + ";");

        if (episodesService.episodeExistsByNumber(season, Integer.parseInt(episode[6]))) {
          log.debug("Episode already exists, updating it");
          Collection<Episode> episodes = episodesService.getAllEpisodes(season);
          for (Episode tempEpisode : episodes) {
            if (tempEpisode.getTitle().equals(episode[2])) {
              newEpisode = tempEpisode;
            }
          }
        } else {
          log.debug("Episode does not exist, creating it");
          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
          try {
            newEpisode =
                episodesService.createEpisode(season, Integer.parseInt(episode[6]), episode[2]);
          } catch (AlreadyExistException aee) {
            log.error(episode[3] + "already exists but it should not");
          }
          newEpisode.setAcquired(Boolean.parseBoolean(episode[4]));
          if (!"null".equals(episode[3])) {
            newEpisode.setAiringDate(formatter.parse(episode[3]));
          }
          newEpisode.setViewed(Boolean.parseBoolean(episode[5]));
        }
        showsService.updateShow(show);
        seasonsService.updateSeason(season);
        episodesService.updateEpisode(newEpisode);
      }
    } catch (IOException ioe) {
      log.error("An error occured while trying to import database from CSV", ioe);
    } catch (ParseException pe) {
      log.error("An error occured while trying to import database from CSV", pe);
    }
  }