Coverage Report - org.jtheque.films.services.impl.utils.file.imports.JTFImporter
 
Classes in this File Line Coverage Branch Coverage Complexity
JTFImporter
0 %
0/61
0 %
0/32
2.7
 
 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.managers.language.ILanguageManager;
 22  
 import org.jtheque.core.utils.db.DaoNotes;
 23  
 import org.jtheque.core.utils.db.DaoNotes.NoteType;
 24  
 import org.jtheque.core.utils.file.jt.JTFileReader;
 25  
 import org.jtheque.films.services.able.IActorService;
 26  
 import org.jtheque.films.services.able.IFilmsService;
 27  
 import org.jtheque.films.services.able.IRealizersService;
 28  
 import org.jtheque.films.services.impl.utils.DataUtils;
 29  
 import org.jtheque.films.services.impl.utils.file.jt.JTFFile;
 30  
 import org.jtheque.films.services.impl.utils.file.jt.reader.JTFFileReader;
 31  
 import org.jtheque.films.utils.Constants;
 32  
 import org.jtheque.films.utils.Constants.Files.FileType;
 33  
 import org.jtheque.primary.od.able.Country;
 34  
 import org.jtheque.primary.od.able.Kind;
 35  
 import org.jtheque.primary.od.able.Person;
 36  
 import org.jtheque.primary.services.able.ICountriesService;
 37  
 import org.jtheque.primary.services.able.IKindsService;
 38  
 import org.jtheque.primary.services.able.ILanguagesService;
 39  
 import org.jtheque.primary.services.able.ITypesService;
 40  
 import org.jtheque.utils.io.FileException;
 41  
 
 42  
 import javax.annotation.Resource;
 43  
 
 44  
 /**
 45  
  * An importer for JTF file.
 46  
  *
 47  
  * @author Baptiste Wicht
 48  
  */
 49  
 public final class JTFImporter implements Importer {
 50  0
     private final ILanguageManager resources = Managers.getManager(ILanguageManager.class);
 51  
 
 52  
     @Resource
 53  
     private ICountriesService countriesService;
 54  
 
 55  
     @Resource
 56  
     private ITypesService typesService;
 57  
 
 58  
     @Resource
 59  
     private ILanguagesService languagesService;
 60  
 
 61  
     @Resource
 62  
     private IFilmsService filmsService;
 63  
 
 64  
     @Resource
 65  
     private IActorService actorService;
 66  
 
 67  
     @Resource
 68  
     private IRealizersService realizersService;
 69  
 
 70  0
     private final DaoNotes daoNotes = DaoNotes.getInstance();
 71  
 
 72  
     @Resource
 73  
     private IKindsService kindsService;
 74  
 
 75  
     /**
 76  
      * Construct a new JTFImporter.
 77  
      */
 78  
     public JTFImporter() {
 79  0
         super();
 80  
 
 81  0
         Managers.getManager(IBeansManager.class).inject(this);
 82  0
     }
 83  
 
 84  
     @Override
 85  
     public boolean canImportFrom(FileType fileType) {
 86  0
         return fileType == FileType.JTF;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public void importFrom(String filePath) throws FileException {
 91  0
         JTFileReader reader = new JTFFileReader();
 92  
 
 93  0
         importFrom((JTFFile) reader.readFile(filePath));
 94  0
     }
 95  
 
 96  
     /**
 97  
      * Import from a JTFFile.
 98  
      *
 99  
      * @param file The JTF File.
 100  
      * @throws FileException If there is a problem during the import.
 101  
      */
 102  
     public void importFrom(JTFFile file) throws FileException {
 103  0
         if (file.getHeader().getFileVersion() != Constants.Files.Versions.JTF.FIRST ||
 104  
                 file.getHeader().getFileVersion() != Constants.Files.Versions.JTF.SECOND) {
 105  0
             throw new FileException(resources.getMessage("errors.file.unsupportedversion"));
 106  0
         } else if (file.isValid()) {
 107  
 
 108  0
             importLanguage(file);
 109  0
             importCountries(file);
 110  0
             importKinds(file);
 111  0
             importType(file);
 112  0
             importRealizer(file);
 113  0
             importActors(file);
 114  
 
 115  0
             file.getFilm().setNote(daoNotes.getNote(NoteType.getEnum(file.getFilm().getTemporaryContext().getIntNote())));
 116  
 
 117  0
             filmsService.create(file.getFilm());
 118  
         } else {
 119  0
             throw new FileException(resources.getMessage("errors.file.structureerror"));
 120  
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * Import the language from the file.
 125  
      *
 126  
      * @param file The file to import the language from.
 127  
      */
 128  
     private void importLanguage(JTFFile file) {
 129  0
         if (file.getLanguage() != null) {
 130  0
             if (languagesService.exist(file.getLanguage())) {
 131  0
                 file.setLanguage(languagesService.getLanguage(file.getLanguage().getName()));
 132  
             } else {
 133  0
                 languagesService.create(file.getLanguage());
 134  
             }
 135  
 
 136  0
             file.getFilm().setTheLanguage(file.getLanguage());
 137  
         }
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Import the countries from the file.
 142  
      *
 143  
      * @param file The file to import the countries from.
 144  
      */
 145  
     private void importCountries(JTFFile file) {
 146  0
         for (Country country : file.getCountries()) {
 147  0
             if (countriesService.exist(country)) {
 148  0
                 country.setId(countriesService.getCountry(country.getName()).getId());
 149  
             } else {
 150  0
                 countriesService.create(country);
 151  
             }
 152  
         }
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Import the kinds from the file.
 157  
      *
 158  
      * @param file The file to import the kinds from.
 159  
      */
 160  
     private void importKinds(JTFFile file) {
 161  0
         for (Kind kind : file.getKinds()) {
 162  0
             if (kindsService.exists(kind)) {
 163  0
                 kind.setId(kindsService.getKind(kind.getName()).getId());
 164  
             } else {
 165  0
                 kindsService.create(kind);
 166  
             }
 167  
 
 168  0
             file.getFilm().addKind(kind);
 169  
         }
 170  0
     }
 171  
 
 172  
     /**
 173  
      * Import the type from the file.
 174  
      *
 175  
      * @param file The file to import the type from.
 176  
      */
 177  
     private void importType(JTFFile file) {
 178  0
         if (file.getType() != null) {
 179  0
             if (typesService.exists(file.getType())) {
 180  0
                 file.getType().setId(typesService.getType(file.getType().getName()).getId());
 181  
             } else {
 182  0
                 typesService.create(file.getType());
 183  
             }
 184  
 
 185  0
             file.getFilm().setTheType(file.getType());
 186  
         }
 187  0
     }
 188  
 
 189  
     /**
 190  
      * Import the realizer from the file.
 191  
      *
 192  
      * @param file The file to import the realizer from.
 193  
      */
 194  
     private void importRealizer(JTFFile file) {
 195  0
         if (file.getRealizer() != null) {
 196  0
             file.getRealizer().setNote(daoNotes.getNote(NoteType.getEnum(file.getRealizer().getTemporaryContext().getIntNote())));
 197  0
             file.getRealizer().setTheCountry(DataUtils.getDataByTemporaryId(
 198  
                     file.getRealizer().getTemporaryContext().getCountry(), file.getCountries()));
 199  
 
 200  0
             if (realizersService.exists(file.getRealizer())) {
 201  0
                 file.getRealizer().setId(realizersService.getRealizer(file.getRealizer().getFirstName(), file.getRealizer().getName()).getId());
 202  
             } else {
 203  0
                 realizersService.create(file.getRealizer());
 204  
             }
 205  
 
 206  0
             file.getFilm().setTheRealizer(file.getRealizer());
 207  
         }
 208  0
     }
 209  
 
 210  
     /**
 211  
      * Import the actors from the file.
 212  
      *
 213  
      * @param file The file to import the actors from.
 214  
      */
 215  
     private void importActors(JTFFile file) {
 216  0
         for (Person actor : file.getActors()) {
 217  0
             actor.setNote(daoNotes.getNote(NoteType.getEnum(actor.getTemporaryContext().getIntNote())));
 218  0
             actor.setTheCountry(DataUtils.getDataByTemporaryId(
 219  
                     actor.getTemporaryContext().getCountry(), file.getCountries()));
 220  
 
 221  0
             if (actorService.exist(actor)) {
 222  0
                 actor.setId(actorService.getActor(actor.getFirstName(), actor.getName()).getId());
 223  
             } else {
 224  0
                 actorService.create(actor);
 225  
             }
 226  
 
 227  0
             file.getFilm().addActor(actor);
 228  
         }
 229  0
     }
 230  
 }