Coverage Report - org.jtheque.films.services.impl.utils.file.imports.XMLImporter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLImporter
0 %
0/92
0 %
0/28
2.333
 
 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.XMLException;
 25  
 import org.jtheque.core.utils.file.XMLOverReader;
 26  
 import org.jtheque.films.persistence.od.able.Film;
 27  
 import org.jtheque.films.services.able.IActorService;
 28  
 import org.jtheque.films.services.able.IFilmsService;
 29  
 import org.jtheque.films.services.able.INotesService;
 30  
 import org.jtheque.films.services.able.IRealizersService;
 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.Language;
 36  
 import org.jtheque.primary.od.able.Person;
 37  
 import org.jtheque.primary.od.able.Type;
 38  
 import org.jtheque.primary.services.able.ICountriesService;
 39  
 import org.jtheque.primary.services.able.IKindsService;
 40  
 import org.jtheque.primary.services.able.ILanguagesService;
 41  
 import org.jtheque.primary.services.able.ITypesService;
 42  
 import org.jtheque.utils.io.FileException;
 43  
 import org.jtheque.utils.io.FileUtils;
 44  
 
 45  
 import javax.annotation.Resource;
 46  
 
 47  
 /**
 48  
  * Import films from XML file.
 49  
  *
 50  
  * @author Baptiste Wicht
 51  
  */
 52  
 public final class XMLImporter implements Importer {
 53  0
     private final ILanguageManager resources = Managers.getManager(ILanguageManager.class);
 54  
 
 55  
     @Resource
 56  
     private ICountriesService countriesService;
 57  
 
 58  
     @Resource
 59  
     private ITypesService typesService;
 60  
 
 61  
     @Resource
 62  
     private ILanguagesService languagesService;
 63  
 
 64  
     @Resource
 65  
     private IFilmsService filmsService;
 66  
 
 67  
     @Resource
 68  
     private IActorService actorService;
 69  
 
 70  
     @Resource
 71  
     private IRealizersService realizersService;
 72  
 
 73  
     @Resource
 74  
     private INotesService notesService;
 75  
 
 76  0
     private final DaoNotes daoNotes = DaoNotes.getInstance();
 77  
 
 78  
     @Resource
 79  
     private IKindsService kindsService;
 80  
 
 81  
     /**
 82  
      * Construct a new XMLImporter.
 83  
      */
 84  
     public XMLImporter() {
 85  0
         super();
 86  
 
 87  0
         Managers.getManager(IBeansManager.class).inject(this);
 88  0
     }
 89  
 
 90  
     @Override
 91  
     public boolean canImportFrom(FileType fileType) {
 92  0
         return fileType == FileType.XML;
 93  
     }
 94  
 
 95  
     @Override
 96  
     public void importFrom(String filePath) throws FileException {
 97  0
         XMLOverReader reader = new XMLOverReader();
 98  
 
 99  
         try {
 100  0
             reader.openFile(filePath);
 101  
 
 102  0
             int version = threatVersion(reader);
 103  
 
 104  0
             while (reader.next("//film")) {
 105  0
                 Film film = filmsService.getEmptyFilm();
 106  
 
 107  0
                 importPropertiesOfFilm(reader, film);
 108  0
                 importLanguage(reader, film);
 109  0
                 importType(reader, film);
 110  0
                 importRealizer(reader, film);
 111  0
                 importKinds(reader, version, film);
 112  0
                 importActors(reader, film);
 113  
 
 114  0
                 filmsService.create(film);
 115  0
             }
 116  0
         } catch (XMLException e) {
 117  0
             throw new FileException(resources.getMessage("errors.file.ioexception"), e);
 118  
         } finally {
 119  0
             FileUtils.close(reader);
 120  0
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * Import the informations of the film from the XML file.
 125  
      *
 126  
      * @param reader The XML Reader.
 127  
      * @param film   The film to get the informations from.
 128  
      * @throws XMLException If an error occurs during the XML reading.
 129  
      */
 130  
     private void importPropertiesOfFilm(XMLOverReader reader, Film film) throws XMLException {
 131  0
         film.setTitle(reader.readString("./title"));
 132  0
         film.setDuration(reader.readInt("./duration"));
 133  0
         film.setYear(reader.readInt("./year"));
 134  0
         film.setNote(daoNotes.getNote(NoteType.getEnum(reader.readInt("./note"))));
 135  0
         film.setComment(reader.readString("./comment"));
 136  0
         film.setResume(reader.readString("./resume"));
 137  0
     }
 138  
 
 139  
     /**
 140  
      * Read the version of the XML file.
 141  
      *
 142  
      * @param reader The XML reader
 143  
      * @return The version.
 144  
      * @throws XMLException  If an error occurs during the XML reading.
 145  
      * @throws FileException If the version of the
 146  
      */
 147  
     private int threatVersion(XMLOverReader reader) throws XMLException, FileException {
 148  0
         int version = reader.readInt("./header/file-version");
 149  
 
 150  0
         if (version != Constants.Files.Versions.XML.FIRST && version != Constants.Files.Versions.XML.SECOND) {
 151  0
             throw new FileException(resources.getMessage("errors.file.unsupportedversion"));
 152  
         }
 153  
 
 154  0
         return version;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Import the language of the film.
 159  
      *
 160  
      * @param reader The XML reader
 161  
      * @param film   The film to fill.
 162  
      * @throws XMLException If an error occurs during the XML reading.
 163  
      */
 164  
     private void importLanguage(XMLOverReader reader, Film film) throws XMLException {
 165  0
         Language language = languagesService.getEmptyLanguage();
 166  0
         language.setName(reader.readString("./language"));
 167  
 
 168  0
         if (languagesService.exist(language)) {
 169  0
             language.setId(languagesService.getLanguage(language.getName()).getId());
 170  
         } else {
 171  0
             languagesService.create(language);
 172  
         }
 173  
 
 174  0
         film.setTheLanguage(language);
 175  0
     }
 176  
 
 177  
     /**
 178  
      * Import the type of the film.
 179  
      *
 180  
      * @param reader The XML reader
 181  
      * @param film   The film to fill.
 182  
      * @throws XMLException If an error occurs during the XML reading.
 183  
      */
 184  
     private void importType(XMLOverReader reader, Film film) throws XMLException {
 185  0
         Type type = typesService.getEmptyType();
 186  0
         type.setName(reader.readString("./type"));
 187  
 
 188  0
         if (typesService.exists(type)) {
 189  0
             type.setId(typesService.getType(type.getName()).getId());
 190  
         } else {
 191  0
             typesService.create(type);
 192  
         }
 193  
 
 194  0
         film.setTheType(type);
 195  0
     }
 196  
 
 197  
     /**
 198  
      * Import the realizer of the film.
 199  
      *
 200  
      * @param reader The XML reader
 201  
      * @param film   The film to fill.
 202  
      * @throws XMLException If an error occurs during the XML reading.
 203  
      */
 204  
     private void importRealizer(XMLOverReader reader, Film film) throws XMLException {
 205  0
         if (reader.readNode("./realizer")) {
 206  0
             Person realizer = realizersService.getEmptyRealizer();
 207  0
             realizer.setName(reader.readString("./name"));
 208  0
             realizer.setFirstName(reader.readString("./firstname"));
 209  0
             realizer.setNote(notesService.getDefaultNote());
 210  
 
 211  0
             addCountryToPerson(realizer, reader.readString("./country"));
 212  
 
 213  0
             if (realizersService.exists(realizer)) {
 214  0
                 film.setTheRealizer(realizersService.getRealizer(realizer.getFirstName(), realizer.getName()));
 215  
             } else {
 216  0
                 realizersService.create(realizer);
 217  0
                 film.setTheRealizer(realizer);
 218  
             }
 219  
 
 220  0
             reader.switchToParent();
 221  
         }
 222  0
     }
 223  
 
 224  
     /**
 225  
      * Import the kinds of the film.
 226  
      *
 227  
      * @param reader  The XML reader
 228  
      * @param version The version of the file.
 229  
      * @param film    The film to fill.
 230  
      * @throws XMLException If an error occurs during the XML reading.
 231  
      */
 232  
     private void importKinds(XMLOverReader reader, int version, Film film) throws XMLException {
 233  0
         if (version == Constants.Files.Versions.XML.FIRST) {
 234  0
             addKindToFilm(film, reader.readString("./kind"));
 235  
         } else {
 236  0
             while (reader.next("//kinds/kind")) {
 237  0
                 addKindToFilm(film, reader.readString("./name"));
 238  
             }
 239  
         }
 240  0
     }
 241  
 
 242  
     /**
 243  
      * Add a kind to the film.
 244  
      *
 245  
      * @param film     The film to add kind to.
 246  
      * @param kindName The name of the kind.
 247  
      */
 248  
     private void addKindToFilm(Film film, String kindName) {
 249  0
         if (kindsService.exists(kindName)) {
 250  0
             film.addKind(kindsService.getKind(kindName));
 251  
         } else {
 252  0
             Kind kind = kindsService.getEmptyKind();
 253  0
             kind.setName(kindName);
 254  
 
 255  0
             kindsService.create(kind);
 256  0
             film.addKind(kind);
 257  
         }
 258  0
     }
 259  
 
 260  
     /**
 261  
      * Import the actors of the film.
 262  
      *
 263  
      * @param reader The XML reader
 264  
      * @param film   The film to fill.
 265  
      * @throws XMLException If an error occurs during the XML reading.
 266  
      */
 267  
     private void importActors(XMLOverReader reader, Film film) throws XMLException {
 268  0
         while (reader.next("//actors/actor")) {
 269  0
             Person actor = actorService.getEmptyActor();
 270  0
             actor.setName(reader.readString("./name"));
 271  0
             actor.setFirstName(reader.readString("./firstname"));
 272  0
             actor.setNote(notesService.getDefaultNote());
 273  
 
 274  0
             addCountryToPerson(actor, reader.readString("./country"));
 275  
 
 276  0
             if (actorService.exist(actor)) {
 277  0
                 film.addActor(actorService.getActor(actor.getFirstName(), actor.getName()));
 278  
             } else {
 279  0
                 actorService.create(actor);
 280  0
                 film.addActor(actor);
 281  
             }
 282  0
         }
 283  0
     }
 284  
 
 285  
     /**
 286  
      * Add a country the person.
 287  
      *
 288  
      * @param person      The person to add country to.
 289  
      * @param countryName The name of the country.
 290  
      */
 291  
     private void addCountryToPerson(Person person, String countryName) {
 292  0
         if (countriesService.exist(countryName)) {
 293  0
             person.setTheCountry(countriesService.getCountry(countryName));
 294  
         } else {
 295  0
             Country country = countriesService.getEmptyCountry();
 296  0
             country.setName(countryName);
 297  
 
 298  0
             countriesService.create(country);
 299  0
             person.setTheCountry(country);
 300  
         }
 301  0
     }
 302  
 }