Coverage Report - org.jtheque.films.services.impl.utils.file.jt.reader.JTFFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
JTFFileReader
0 %
0/135
0 %
0/54
3.636
 
 1  
 package org.jtheque.films.services.impl.utils.file.jt.reader;
 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.file.IFileManager;
 21  
 import org.jtheque.core.managers.language.ILanguageManager;
 22  
 import org.jtheque.core.utils.file.jt.AbstractJTFileHeader;
 23  
 import org.jtheque.core.utils.file.jt.JTFileReader;
 24  
 import org.jtheque.core.utils.file.jt.able.JTNotZippedFile;
 25  
 import org.jtheque.films.persistence.od.able.Film;
 26  
 import org.jtheque.films.services.able.IActorService;
 27  
 import org.jtheque.films.services.able.IFilmsService;
 28  
 import org.jtheque.films.services.able.IRealizersService;
 29  
 import org.jtheque.films.services.impl.utils.file.jt.JTFFile;
 30  
 import org.jtheque.films.utils.Constants;
 31  
 import org.jtheque.primary.od.able.Country;
 32  
 import org.jtheque.primary.od.able.Kind;
 33  
 import org.jtheque.primary.od.able.Language;
 34  
 import org.jtheque.primary.od.able.Person;
 35  
 import org.jtheque.primary.od.able.Type;
 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.bean.Version;
 41  
 import org.jtheque.utils.io.FileException;
 42  
 import org.jtheque.utils.io.FileUtils;
 43  
 
 44  
 import javax.annotation.Resource;
 45  
 import java.io.DataInputStream;
 46  
 import java.io.IOException;
 47  
 import java.util.ArrayList;
 48  
 import java.util.Collection;
 49  
 import java.util.Collections;
 50  
 import java.util.HashSet;
 51  
 import java.util.Set;
 52  
 
 53  
 /**
 54  
  * A reader for JTF File.
 55  
  *
 56  
  * @author Baptiste Wicht
 57  
  */
 58  0
 public final class JTFFileReader extends JTFileReader {
 59  
     private boolean correctSeparators;
 60  
 
 61  
     @Resource
 62  
     private ICountriesService countriesService;
 63  
 
 64  
     @Resource
 65  
     private ILanguagesService languagesService;
 66  
 
 67  
     @Resource
 68  
     private ITypesService typesService;
 69  
 
 70  
     @Resource
 71  
     private IKindsService kindsService;
 72  
 
 73  
     @Resource
 74  
     private IActorService actorsService;
 75  
 
 76  
     @Resource
 77  
     private IFilmsService filmsService;
 78  
 
 79  
     @Resource
 80  
     private IRealizersService realizersService;
 81  
 
 82  
     @Override
 83  
     public JTFFile readFile(DataInputStream stream) throws FileException {
 84  0
         JTFFile file = new JTFFile();
 85  
 
 86  0
         correctSeparators = true;
 87  
 
 88  
         try {
 89  0
             readHeader(stream, file);
 90  
 
 91  0
             if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 92  0
                 correctSeparators = false;
 93  
             }
 94  
 
 95  0
             readFilm(stream, file);
 96  
 
 97  0
             if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 98  0
                 correctSeparators = false;
 99  
             }
 100  
 
 101  0
             readActors(stream, file);
 102  0
             readRealizer(stream, file);
 103  0
             readLanguage(stream, file);
 104  0
             readKinds(stream, file);
 105  0
             readType(stream, file);
 106  0
             readCountries(stream, file);
 107  0
         } catch (IOException e) {
 108  0
             throw new FileException(Managers.getManager(ILanguageManager.class).getMessage("errors.file.ioexception"), e);
 109  
         } finally {
 110  0
             FileUtils.close(stream);
 111  0
         }
 112  
 
 113  0
         file.setCorrectSeparators(correctSeparators);
 114  
 
 115  0
         return file;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Read the header of the file.
 120  
      *
 121  
      * @param stream The stream to read from.
 122  
      * @param file   The file to read.
 123  
      * @throws IOException Thrown if an exception occurs during the write process.
 124  
      */
 125  
     private static void readHeader(DataInputStream stream, JTNotZippedFile file) throws IOException {
 126  0
         AbstractJTFileHeader header = file.getHeader();
 127  
 
 128  0
         header.setKey(stream.readUTF());
 129  0
         header.setVersionJTheque(new Version(stream.readUTF()));
 130  0
         header.setFileVersion(stream.readInt());
 131  0
         header.setDate(stream.readInt());
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Read the film in the file.
 136  
      *
 137  
      * @param stream The stream to read from.
 138  
      * @param file   The file to read.
 139  
      * @throws IOException Thrown if an exception occurs during the write process.
 140  
      */
 141  
     private void readFilm(DataInputStream stream, JTFFile file) throws IOException {
 142  0
         Film film = filmsService.getEmptyFilm();
 143  
 
 144  0
         film.setTitle(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 145  0
         film.setYear(stream.readInt());
 146  0
         film.setDuration(stream.readInt());
 147  0
         film.getTemporaryContext().setTemporaryIntNote(stream.readInt());
 148  0
         film.setResume(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 149  0
         film.setComment(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 150  
 
 151  0
         file.setFilm(film);
 152  0
     }
 153  
 
 154  
     /**
 155  
      * Read the actors in the file.
 156  
      *
 157  
      * @param stream The stream to read from.
 158  
      * @param file   The file to read.
 159  
      * @throws IOException Thrown if an exception occurs during the write process.
 160  
      */
 161  
     private void readActors(DataInputStream stream, JTFFile file) throws IOException {
 162  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 163  0
             boolean endOfActorsList = false;
 164  0
             Set<Person> actors = new HashSet<Person>(12);
 165  
 
 166  0
             while (!endOfActorsList) {
 167  0
                 Person actor = actorsService.getEmptyActor();
 168  
 
 169  0
                 actor.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 170  0
                 actor.setFirstName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 171  0
                 actor.getTemporaryContext().setCountry(stream.readInt());
 172  0
                 actor.getTemporaryContext().setIntNote(stream.readInt());
 173  
 
 174  0
                 actors.add(actor);
 175  
 
 176  0
                 long separator = stream.readLong();
 177  
 
 178  0
                 if (separator == Constants.Files.JT.JTCATEGORYSEPARATOR) {
 179  0
                     endOfActorsList = true;
 180  0
                 } else if (separator != Constants.Files.JT.JTINTERNSEPARATOR) {
 181  0
                     correctSeparators = false;
 182  
                 }
 183  0
             }
 184  
 
 185  0
             file.setActors(actors);
 186  0
         } else {
 187  0
             file.setActors(Collections.<Person>emptySet());
 188  
 
 189  0
             if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 190  0
                 correctSeparators = false;
 191  
             }
 192  
         }
 193  0
     }
 194  
 
 195  
     /**
 196  
      * Read the realizer in the file.
 197  
      *
 198  
      * @param stream The stream to read from.
 199  
      * @param file   The file to read.
 200  
      * @throws IOException Thrown if an exception occurs during the write process.
 201  
      */
 202  
     private void readRealizer(DataInputStream stream, JTFFile file) throws IOException {
 203  
         //Si y a un réalisateur
 204  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 205  0
             Person realizer = realizersService.getEmptyRealizer();
 206  
 
 207  0
             realizer.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 208  0
             realizer.setFirstName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 209  0
             realizer.getTemporaryContext().setCountry(stream.readInt());
 210  0
             realizer.getTemporaryContext().setIntNote(stream.readInt());
 211  
 
 212  0
             file.setRealizer(realizer);
 213  
         }
 214  
 
 215  0
         if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 216  0
             correctSeparators = false;
 217  
         }
 218  0
     }
 219  
 
 220  
     /**
 221  
      * Read the language in the file.
 222  
      *
 223  
      * @param stream The stream to read from.
 224  
      * @param file   The file to read.
 225  
      * @throws IOException Thrown if an exception occurs during the write process.
 226  
      */
 227  
     private void readLanguage(DataInputStream stream, JTFFile file) throws IOException {
 228  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 229  0
             Language language = languagesService.getEmptyLanguage();
 230  
 
 231  0
             language.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 232  
 
 233  0
             file.setLanguage(language);
 234  
         }
 235  
 
 236  0
         if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 237  0
             correctSeparators = false;
 238  
         }
 239  0
     }
 240  
 
 241  
     /**
 242  
      * Read the kinds in the file.
 243  
      *
 244  
      * @param stream The stream to read from.
 245  
      * @param file   The file to read.
 246  
      * @throws IOException Thrown if an exception occurs during the write process.
 247  
      */
 248  
     private void readKinds(DataInputStream stream, JTFFile file) throws IOException {
 249  0
         if (file.getHeader().getFileVersion() == Constants.Files.Versions.JTF.FIRST) {
 250  0
             readOneKind(stream, file);
 251  0
         } else if (file.getHeader().getFileVersion() == Constants.Files.Versions.JTF.SECOND) {
 252  0
             readMultipleKinds(stream, file);
 253  
         }
 254  0
     }
 255  
 
 256  
     /**
 257  
      * Read one kind in the file.
 258  
      *
 259  
      * @param stream The stream to read from.
 260  
      * @param file   The file to read.
 261  
      * @throws IOException Thrown if an exception occurs during the write process.
 262  
      */
 263  
     private void readOneKind(DataInputStream stream, JTFFile file) throws IOException {
 264  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 265  0
             Kind kind = kindsService.getEmptyKind();
 266  
 
 267  0
             kind.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 268  
 
 269  0
             Set<Kind> kinds = new HashSet<Kind>(1);
 270  
 
 271  0
             kinds.add(kind);
 272  
 
 273  0
             file.setKinds(kinds);
 274  
         }
 275  
 
 276  0
         if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 277  0
             correctSeparators = false;
 278  
         }
 279  0
     }
 280  
 
 281  
     /**
 282  
      * Read some kinds in the file.
 283  
      *
 284  
      * @param stream The stream to read from.
 285  
      * @param file   The file to read.
 286  
      * @throws IOException Thrown if an exception occurs during the write process.
 287  
      */
 288  
     private void readMultipleKinds(DataInputStream stream, JTFFile file) throws IOException {
 289  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 290  0
             boolean endOfKindsList = false;
 291  0
             Set<Kind> kinds = new HashSet<Kind>(5);
 292  
 
 293  0
             while (!endOfKindsList) {
 294  0
                 Kind kind = kindsService.getEmptyKind();
 295  
 
 296  0
                 kind.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 297  
 
 298  0
                 kinds.add(kind);
 299  
 
 300  0
                 long separator = stream.readLong();
 301  
 
 302  0
                 if (separator == Constants.Files.JT.JTCATEGORYSEPARATOR) {
 303  0
                     endOfKindsList = true;
 304  0
                 } else if (separator != Constants.Files.JT.JTINTERNSEPARATOR) {
 305  0
                     correctSeparators = false;
 306  
                 }
 307  0
             }
 308  
 
 309  0
             file.setKinds(kinds);
 310  0
         } else {
 311  0
             file.setKinds(Collections.<Kind>emptySet());
 312  
 
 313  0
             if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 314  0
                 correctSeparators = false;
 315  
             }
 316  
         }
 317  0
     }
 318  
 
 319  
     /**
 320  
      * Read the type in the file.
 321  
      *
 322  
      * @param stream The stream to read from.
 323  
      * @param file   The file to read.
 324  
      * @throws IOException Thrown if an exception occurs during the write process.
 325  
      */
 326  
     private void readType(DataInputStream stream, JTFFile file) throws IOException {
 327  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 328  0
             Type type = typesService.getEmptyType();
 329  
 
 330  0
             type.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 331  
 
 332  0
             file.setType(type);
 333  
         }
 334  
 
 335  0
         if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 336  0
             correctSeparators = false;
 337  
         }
 338  0
     }
 339  
 
 340  
     /**
 341  
      * Read the countries in the file.
 342  
      *
 343  
      * @param stream The stream to read from.
 344  
      * @param file   The file to read.
 345  
      * @throws IOException Thrown if an exception occurs during the write process.
 346  
      */
 347  
     private void readCountries(DataInputStream stream, JTFFile file) throws IOException {
 348  0
         if (stream.readInt() == Constants.Files.JT.CONTENT) {
 349  0
             boolean endOfCountries = false;
 350  0
             Collection<Country> countries = new ArrayList<Country>(12);
 351  
 
 352  0
             while (!endOfCountries) {
 353  0
                 Country country = countriesService.getEmptyCountry();
 354  
 
 355  0
                 country.getTemporaryContext().setId(stream.readInt());
 356  0
                 country.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 357  
 
 358  0
                 countries.add(country);
 359  
 
 360  0
                 long separator = stream.readLong();
 361  
 
 362  0
                 if (separator == Constants.Files.JT.JTCATEGORYSEPARATOR) {
 363  0
                     endOfCountries = true;
 364  0
                 } else if (separator != Constants.Files.JT.JTINTERNSEPARATOR) {
 365  0
                     correctSeparators = false;
 366  
                 }
 367  0
             }
 368  
 
 369  0
             file.setCountries(countries);
 370  0
         } else {
 371  0
             file.setCountries(Collections.<Country>emptyList());
 372  
 
 373  0
             if (stream.readLong() != Constants.Files.JT.JTCATEGORYSEPARATOR) {
 374  0
                 correctSeparators = false;
 375  
             }
 376  
         }
 377  0
     }
 378  
 }