Exemplo n.º 1
0
  @PostConstruct
  public void init() {
    String genreFileName = PropertyTools.getProperty("yamj3.genre.fileName");
    if (StringUtils.isBlank(genreFileName)) {
      LOG.trace("No valid genre file name configured");
      return;
    }
    if (!StringUtils.endsWithIgnoreCase(genreFileName, "xml")) {
      LOG.warn("Invalid genre file name specified: {}", genreFileName);
      return;
    }

    File xmlFile;
    if (StringUtils.isBlank(FilenameUtils.getPrefix(genreFileName))) {
      // relative path given
      String path = System.getProperty("yamj3.home");
      if (StringUtils.isEmpty(path)) {
        path = ".";
      }
      xmlFile = new File(FilenameUtils.concat(path, genreFileName));
    } else {
      // absolute path given
      xmlFile = new File(genreFileName);
    }

    if (!xmlFile.exists() || !xmlFile.isFile()) {
      LOG.warn("Genres file does not exist: {}", xmlFile.getPath());
      return;
    }
    if (!xmlFile.canRead()) {
      LOG.warn("Genres file not readble: {}", xmlFile.getPath());
      return;
    }

    LOG.debug("Initialize genres from file: {}", xmlFile.getPath());

    try {
      XMLConfiguration c = new XMLConfiguration(xmlFile);

      List<HierarchicalConfiguration> genres = c.configurationsAt("genre");
      for (HierarchicalConfiguration genre : genres) {
        String masterGenre = genre.getString("[@name]");
        List<Object> subGenres = genre.getList("subgenre");
        for (Object subGenre : subGenres) {
          LOG.debug("New genre added to map: {} -> {}", subGenre, masterGenre);
          GENRES_MAP.put(((String) subGenre).toLowerCase(), masterGenre);
        }
      }

      try {
        this.commonStorageService.updateGenresXml(GENRES_MAP);
      } catch (Exception ex) {
        LOG.warn("Failed update genres xml in database", ex);
      }
    } catch (Exception ex) {
      LOG.error("Failed parsing genre input file: " + xmlFile.getPath(), ex);
    }
  }