Coverage Report - org.jtheque.films.services.impl.utils.AutoManager
 
Classes in this File Line Coverage Branch Coverage Complexity
AutoManager
0 %
0/33
0 %
0/18
2.8
 
 1  
 package org.jtheque.films.services.impl.utils;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.beans.IBeansManager;
 21  
 import org.jtheque.films.persistence.od.able.Film;
 22  
 import org.jtheque.films.services.able.IFilmAutoService;
 23  
 import org.jtheque.films.services.able.IFilmsService;
 24  
 import org.jtheque.films.services.impl.utils.web.FilmResult;
 25  
 import org.jtheque.films.utils.Constants.Site;
 26  
 
 27  
 import javax.annotation.Resource;
 28  
 import java.io.File;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Collection;
 31  
 
 32  
 /**
 33  
  * A manager to extract the films of a folder.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  
 public final class AutoManager {
 38  0
     private static final AutoManager INSTANCE = new AutoManager();
 39  
 
 40  
     @Resource
 41  
     private IFilmsService filmsService;
 42  
 
 43  
     @Resource
 44  
     private IFilmAutoService filmAutoService;
 45  
 
 46  
     /**
 47  
      * Create a new AutoManager.
 48  
      */
 49  
     private AutoManager() {
 50  0
         super();
 51  
 
 52  0
         Managers.getManager(IBeansManager.class).inject(this);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Return the unique instance of the class.
 57  
      *
 58  
      * @return The unique instance of the class.
 59  
      */
 60  
     public static AutoManager getInstance() {
 61  0
         return INSTANCE;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Fill all films.
 66  
      *
 67  
      * @param titles  The films titles.
 68  
      * @param webMode The mode to fill the films.
 69  
      * @param site    The site to get the informations from.
 70  
      * @return A list containing all the films.
 71  
      */
 72  
     public Collection<Film> getFilmsOfFolder(Collection<String> titles, boolean webMode, Site site) {
 73  0
         Collection<Film> films = new ArrayList<Film>(titles.size());
 74  
 
 75  0
         for (String title : titles) {
 76  0
             if (webMode) {
 77  0
                 Collection<FilmResult> results = filmAutoService.getFilms(site, title);
 78  
 
 79  0
                 if (results.isEmpty()) {
 80  0
                     addSimpleFilm(films, title);
 81  
                 } else {
 82  0
                     Film film = filmAutoService.getFilm(results.iterator().next());
 83  
 
 84  0
                     films.add(film);
 85  
                 }
 86  0
             } else {
 87  0
                 addSimpleFilm(films, title);
 88  
             }
 89  
         }
 90  
 
 91  0
         return films;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Add a simple film to the list.
 96  
      *
 97  
      * @param films The films list.
 98  
      * @param title The title of the film.
 99  
      */
 100  
     private void addSimpleFilm(Collection<Film> films, String title) {
 101  0
         Film film = filmsService.getDefaultFilm();
 102  
 
 103  0
         film.setTitle(title);
 104  
 
 105  0
         films.add(film);
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Return all the film titles from a folder.
 110  
      *
 111  
      * @param folder   The folder
 112  
      * @param fileMode The file mode.
 113  
      * @return A List containing all the titles of the film.
 114  
      */
 115  
     public Collection<String> getFilmTitles(File folder, boolean fileMode) {
 116  0
         Collection<String> titles = new ArrayList<String>(50);
 117  
 
 118  0
         for (File f : folder.listFiles()) {
 119  0
             if (!f.isHidden()) {
 120  0
                 if (f.isFile() && fileMode) {
 121  0
                     String title = f.getName();
 122  
 
 123  0
                     title = title.substring(0, title.lastIndexOf('.'));
 124  0
                     title = title.trim();
 125  
 
 126  0
                     titles.add(title);
 127  
                 }
 128  
 
 129  0
                 if (f.isDirectory() && !fileMode) {
 130  0
                     String title = f.getName();
 131  
 
 132  0
                     title = title.trim();
 133  
 
 134  0
                     titles.add(title);
 135  
                 }
 136  
             }
 137  
         }
 138  
 
 139  0
         return titles;
 140  
     }
 141  
 }