Coverage Report - org.jtheque.films.services.impl.utils.file.imports.ImporterUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ImporterUtils
0 %
0/46
0 %
0/18
2.5
 
 1  
 package org.jtheque.films.services.impl.utils.file.imports;
 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.core.utils.db.DaoNotes;
 22  
 import org.jtheque.core.utils.db.DaoNotes.NoteType;
 23  
 import org.jtheque.films.persistence.od.able.Film;
 24  
 import org.jtheque.films.services.able.IActorService;
 25  
 import org.jtheque.films.services.able.IFilmsService;
 26  
 import org.jtheque.films.services.able.IRealizersService;
 27  
 import org.jtheque.films.services.impl.utils.DataUtils;
 28  
 import org.jtheque.primary.od.able.Country;
 29  
 import org.jtheque.primary.od.able.Kind;
 30  
 import org.jtheque.primary.od.able.Language;
 31  
 import org.jtheque.primary.od.able.Lending;
 32  
 import org.jtheque.primary.od.able.Person;
 33  
 import org.jtheque.primary.od.able.Type;
 34  
 import org.jtheque.primary.services.able.IBorrowersService;
 35  
 import org.jtheque.primary.services.able.ICountriesService;
 36  
 import org.jtheque.primary.services.able.IKindsService;
 37  
 import org.jtheque.primary.services.able.ILanguagesService;
 38  
 import org.jtheque.primary.services.able.ILendingsService;
 39  
 import org.jtheque.primary.services.able.ITypesService;
 40  
 
 41  
 import javax.annotation.Resource;
 42  
 
 43  
 /**
 44  
  * Utils for the importer.
 45  
  *
 46  
  * @author Baptiste Wicht
 47  
  */
 48  
 public final class ImporterUtils {
 49  
     @Resource
 50  
     private static ICountriesService countriesService;
 51  
 
 52  
     @Resource
 53  
     private static ILendingsService lendingsService;
 54  
 
 55  
     @Resource
 56  
     private static ITypesService typesService;
 57  
 
 58  
     @Resource
 59  
     private static ILanguagesService languagesService;
 60  
 
 61  
     @Resource
 62  
     private static IBorrowersService borrowersService;
 63  
 
 64  
     @Resource
 65  
     private static IFilmsService filmsService;
 66  
 
 67  
     @Resource
 68  
     private static IActorService actorService;
 69  
 
 70  
     @Resource
 71  
     private static IRealizersService realizersService;
 72  
 
 73  
     @Resource
 74  
     private static IKindsService kindsService;
 75  
 
 76  0
     private static final DaoNotes DAO_NOTES = DaoNotes.getInstance();
 77  
 
 78  
     /**
 79  
      * Construct a new ImporterUtils.
 80  
      */
 81  
     private ImporterUtils() {
 82  0
         super();
 83  
 
 84  0
         Managers.getManager(IBeansManager.class).inject(this);
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Persist the data of an import.
 89  
      *
 90  
      * @param films     The films.
 91  
      * @param actors    The actors.
 92  
      * @param realizers The realizers.
 93  
      * @param kinds     The kinds.
 94  
      * @param types     The types.
 95  
      * @param languages The languages.
 96  
      * @param countries The countries.
 97  
      * @param borrowers The borrowers.
 98  
      * @param lendings  The lendings.
 99  
      */
 100  
     public static void persistDataOfImport(Iterable<Film> films, Iterable<Person> actors,
 101  
                                            Iterable<Person> realizers, Iterable<Kind> kinds,
 102  
                                            Iterable<Type> types, Iterable<Language> languages,
 103  
                                            Iterable<Country> countries,
 104  
                                            Iterable<Person> borrowers, Iterable<Lending> lendings) {
 105  
 
 106  0
         countriesService.createAll(countries);
 107  0
         borrowersService.createAll(borrowers);
 108  0
         languagesService.createAll(languages);
 109  0
         typesService.createAll(types);
 110  0
         kindsService.createAll(kinds);
 111  
 
 112  0
         persistRealizers(realizers, countries);
 113  0
         persistActors(actors, countries);
 114  0
         persistFilms(films, actors, realizers, kinds, types, languages);
 115  0
         persistLendings(lendings, films, borrowers);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Persist the realizers.
 120  
      *
 121  
      * @param realizers The realizers to persist.
 122  
      * @param countries The countries.
 123  
      */
 124  
     private static void persistRealizers(Iterable<Person> realizers, Iterable<Country> countries) {
 125  0
         for (Person realizer : realizers) {
 126  0
             realizer.setTheCountry(DataUtils.getDataByTemporaryId(realizer.getTemporaryContext().getCountry(), countries));
 127  0
             realizer.setNote(DAO_NOTES.getNote(NoteType.getEnum(realizer.getTemporaryContext().getIntNote())));
 128  
 
 129  0
             realizersService.create(realizer);
 130  
         }
 131  0
     }
 132  
 
 133  
     /**
 134  
      * Persist the actors.
 135  
      *
 136  
      * @param actors    The actors to persist.
 137  
      * @param countries The countries.
 138  
      */
 139  
     private static void persistActors(Iterable<Person> actors, Iterable<Country> countries) {
 140  0
         for (Person actor : actors) {
 141  0
             actor.setTheCountry(DataUtils.getDataByTemporaryId(actor.getTemporaryContext().getCountry(), countries));
 142  0
             actor.setNote(DAO_NOTES.getNote(NoteType.getEnum(actor.getTemporaryContext().getIntNote())));
 143  
 
 144  0
             actorService.create(actor);
 145  
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Persist the films.
 150  
      *
 151  
      * @param films     The films to persist
 152  
      * @param actors    The actors.
 153  
      * @param realizers The realizers.
 154  
      * @param kinds     The kinds.
 155  
      * @param types     The types.
 156  
      * @param languages The languages.
 157  
      */
 158  
     private static void persistFilms(Iterable<Film> films, Iterable<Person> actors, Iterable<Person> realizers, Iterable<Kind> kinds, Iterable<Type> types, Iterable<Language> languages) {
 159  0
         for (Film film : films) {
 160  0
             film.setTheLanguage(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getLanguage(), languages));
 161  0
             film.setTheType(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getType(), types));
 162  0
             film.setTheRealizer(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getRealizer(), realizers));
 163  
 
 164  0
             for (Integer i : film.getTemporaryContext().getActors()) {
 165  0
                 Person actor = DataUtils.getDataByTemporaryId(i, actors);
 166  
 
 167  0
                 if (actor != null) {
 168  0
                     film.addActor(actor);
 169  
                 }
 170  0
             }
 171  
 
 172  0
             for (Integer i : film.getTemporaryContext().getKinds()) {
 173  0
                 Kind kind = DataUtils.getDataByTemporaryId(i, kinds);
 174  
 
 175  0
                 if (kind != null) {
 176  0
                     film.addKind(kind);
 177  
                 }
 178  0
             }
 179  
 
 180  0
             filmsService.create(film);
 181  
         }
 182  0
     }
 183  
 
 184  
     /**
 185  
      * Persist the lendings.
 186  
      *
 187  
      * @param lendings  The lendings to persist.
 188  
      * @param films     The films.
 189  
      * @param borrowers The borrowers.
 190  
      */
 191  
     private static void persistLendings(Iterable<Lending> lendings, Iterable<Film> films, Iterable<Person> borrowers) {
 192  0
         if (lendings != null) {
 193  0
             for (Lending lending : lendings) {
 194  0
                 lending.setThePerson(DataUtils.getDataByTemporaryId(lending.getTemporaryContext().getBorrower(), borrowers));
 195  0
                 lending.setTheOther(DataUtils.getDataByTemporaryId(lending.getTemporaryContext().getFilm(), films).getId());
 196  
 
 197  0
                 lendingsService.create(lending);
 198  
             }
 199  
         }
 200  0
     }
 201  
 }