Coverage Report - org.jtheque.films.services.impl.utils.file.restore.JTDBackupReader
 
Classes in this File Line Coverage Branch Coverage Complexity
JTDBackupReader
0 %
0/223
0 %
0/106
4.857
 
 1  
 package org.jtheque.films.services.impl.utils.file.restore;
 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.file.able.BackupReader;
 22  
 import org.jtheque.core.managers.log.ILoggingManager;
 23  
 import org.jtheque.core.utils.file.jt.able.JTNotZippedFile;
 24  
 import org.jtheque.core.utils.file.jt.impl.JTDFile;
 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.imports.ImporterUtils;
 30  
 import org.jtheque.primary.od.able.Country;
 31  
 import org.jtheque.primary.od.able.Kind;
 32  
 import org.jtheque.primary.od.able.Language;
 33  
 import org.jtheque.primary.od.able.Lending;
 34  
 import org.jtheque.primary.od.able.Person;
 35  
 import org.jtheque.primary.od.able.Type;
 36  
 import org.jtheque.primary.services.able.IBorrowersService;
 37  
 import org.jtheque.primary.services.able.ICountriesService;
 38  
 import org.jtheque.primary.services.able.IKindsService;
 39  
 import org.jtheque.primary.services.able.ILanguagesService;
 40  
 import org.jtheque.primary.services.able.ILendingsService;
 41  
 import org.jtheque.primary.services.able.ITypesService;
 42  
 import org.jtheque.utils.bean.IntDate;
 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  
 
 51  
 /**
 52  
  * A reader for JTD Backup.
 53  
  *
 54  
  * @author Baptiste Wicht
 55  
  */
 56  0
 public final class JTDBackupReader implements BackupReader {
 57  
     private Collection<Film> films;
 58  
     private Collection<Person> actors;
 59  
     private Collection<Person> realizers;
 60  
     private Collection<Kind> kinds;
 61  
     private Collection<Type> types;
 62  
     private Collection<Language> languages;
 63  
     private Collection<Country> countries;
 64  
     private Collection<Person> borrowers;
 65  
     private Collection<Lending> lendings;
 66  
 
 67  
     @Resource
 68  
     private IBorrowersService borrowersService;
 69  
 
 70  
     @Resource
 71  
     private ICountriesService countriesService;
 72  
 
 73  
     @Resource
 74  
     private ILendingsService lendingsService;
 75  
 
 76  
     @Resource
 77  
     private ILanguagesService languagesService;
 78  
 
 79  
     @Resource
 80  
     private ITypesService typesService;
 81  
 
 82  
     @Resource
 83  
     private IKindsService kindsService;
 84  
 
 85  
     @Resource
 86  
     private IActorService actorService;
 87  
 
 88  
     @Resource
 89  
     private IFilmsService filmsService;
 90  
 
 91  
     @Resource
 92  
     private IRealizersService realizersService;
 93  
 
 94  
     @Override
 95  
     public void readBackup(Object object) {
 96  0
         JTDFile file = (JTDFile) object;
 97  0
         DataInputStream stream = file.getStream();
 98  
 
 99  
         try {
 100  0
             readFilms(stream, file);
 101  0
             readActors(stream, file);
 102  0
             readRealizers(stream, file);
 103  0
             readLanguages(stream, file);
 104  0
             readKinds(stream, file);
 105  0
             readTypes(stream, file);
 106  0
             readCountries(stream, file);
 107  0
             readLendings(stream, file);
 108  0
             readBorrowers(stream, file);
 109  0
         } catch (IOException e) {
 110  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 111  0
         }
 112  0
     }
 113  
 
 114  
     @Override
 115  
     public void persistTheData() {
 116  0
         ImporterUtils.persistDataOfImport(films, actors,
 117  
                 realizers, kinds, types,
 118  
                 languages, countries,
 119  
                 borrowers, lendings);
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Read the borrowers.
 124  
      *
 125  
      * @param stream The input stream.
 126  
      * @param file   The file.
 127  
      * @throws IOException Thrown when an error occurs during the reading process.
 128  
      */
 129  
     private void readBorrowers(DataInputStream stream, JTNotZippedFile file) throws IOException {
 130  0
         if (stream.readInt() == IFileManager.CONTENT) {
 131  0
             borrowers = new ArrayList<Person>(10);
 132  
 
 133  0
             boolean endOfBorrowersList = false;
 134  0
             while (!endOfBorrowersList) {
 135  0
                 Person borrower = borrowersService.getEmptyBorrower();
 136  
 
 137  0
                 borrower.getTemporaryContext().setId(stream.readInt());
 138  0
                 borrower.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 139  0
                 borrower.setFirstName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 140  0
                 borrower.setEmail(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 141  
 
 142  0
                 borrowers.add(borrower);
 143  
 
 144  0
                 long separator = stream.readLong();
 145  
 
 146  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 147  0
                     endOfBorrowersList = true;
 148  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 149  0
                     file.setCorrectSeparators(false);
 150  
                 }
 151  0
             }
 152  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 153  0
             file.setCorrectSeparators(false);
 154  
         }
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Read the lendings.
 159  
      *
 160  
      * @param stream The input stream.
 161  
      * @param file   The file.
 162  
      * @throws IOException Thrown when an error occurs during the reading process.
 163  
      */
 164  
     private void readLendings(DataInputStream stream, JTNotZippedFile file) throws IOException {
 165  0
         if (stream.readInt() == IFileManager.CONTENT) {
 166  0
             boolean endOfLendingsList = false;
 167  0
             lendings = new ArrayList<Lending>(10);
 168  
 
 169  0
             while (!endOfLendingsList) {
 170  0
                 Lending lending = lendingsService.getEmptyLending();
 171  
 
 172  0
                 lending.getTemporaryContext().setId(stream.readInt());
 173  0
                 lending.setDate(new IntDate(stream.readInt()));
 174  0
                 lending.getTemporaryContext().setBorrower(stream.readInt());
 175  0
                 lending.getTemporaryContext().setFilm(stream.readInt());
 176  
 
 177  0
                 lendings.add(lending);
 178  
 
 179  0
                 long separator = stream.readLong();
 180  
 
 181  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 182  0
                     endOfLendingsList = true;
 183  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 184  0
                     file.setCorrectSeparators(false);
 185  
                 }
 186  0
             }
 187  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 188  0
             file.setCorrectSeparators(false);
 189  
         }
 190  0
     }
 191  
 
 192  
     /**
 193  
      * Read the countries.
 194  
      *
 195  
      * @param stream The input stream.
 196  
      * @param file   The file.
 197  
      * @throws IOException Thrown when an error occurs during the reading process.
 198  
      */
 199  
     private void readCountries(DataInputStream stream, JTNotZippedFile file) throws IOException {
 200  0
         if (stream.readInt() == IFileManager.CONTENT) {
 201  0
             boolean endOfCountries = false;
 202  0
             countries = new ArrayList<Country>(20);
 203  
 
 204  0
             while (!endOfCountries) {
 205  0
                 Country country = countriesService.getEmptyCountry();
 206  
 
 207  0
                 country.getTemporaryContext().setId(stream.readInt());
 208  0
                 country.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 209  
 
 210  0
                 countries.add(country);
 211  
 
 212  0
                 long separator = stream.readLong();
 213  
 
 214  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 215  0
                     endOfCountries = true;
 216  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 217  0
                     file.setCorrectSeparators(false);
 218  
                 }
 219  0
             }
 220  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 221  0
             file.setCorrectSeparators(false);
 222  
         }
 223  0
     }
 224  
 
 225  
     /**
 226  
      * Read the types.
 227  
      *
 228  
      * @param stream The input stream.
 229  
      * @param file   The file.
 230  
      * @throws IOException Thrown when an error occurs during the reading process.
 231  
      */
 232  
     private void readTypes(DataInputStream stream, JTNotZippedFile file) throws IOException {
 233  0
         if (stream.readInt() == IFileManager.CONTENT) {
 234  0
             boolean endOfKindsList = false;
 235  0
             types = new ArrayList<Type>(10);
 236  
 
 237  0
             while (!endOfKindsList) {
 238  0
                 Type type = typesService.getEmptyType();
 239  
 
 240  0
                 type.getTemporaryContext().setId(stream.readInt());
 241  0
                 type.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 242  
 
 243  0
                 types.add(type);
 244  
 
 245  0
                 long separator = stream.readLong();
 246  
 
 247  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 248  0
                     endOfKindsList = true;
 249  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 250  0
                     file.setCorrectSeparators(false);
 251  
                 }
 252  0
             }
 253  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 254  0
             file.setCorrectSeparators(false);
 255  
         }
 256  0
     }
 257  
 
 258  
     /**
 259  
      * Read the kinds.
 260  
      *
 261  
      * @param stream The input stream.
 262  
      * @param file   The file.
 263  
      * @throws IOException Thrown when an error occurs during the reading process.
 264  
      */
 265  
     private void readKinds(DataInputStream stream, JTNotZippedFile file) throws IOException {
 266  
         //Si y a des genres
 267  0
         if (stream.readInt() == IFileManager.CONTENT) {
 268  0
             boolean endOfKindsList = false;
 269  0
             kinds = new ArrayList<Kind>(10);
 270  
 
 271  0
             while (!endOfKindsList) {
 272  0
                 Kind genre = kindsService.getEmptyKind();
 273  
 
 274  0
                 genre.getTemporaryContext().setId(stream.readInt());
 275  0
                 genre.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 276  
 
 277  0
                 kinds.add(genre);
 278  
 
 279  0
                 long separator = stream.readLong();
 280  
 
 281  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 282  0
                     endOfKindsList = true;
 283  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 284  0
                     file.setCorrectSeparators(false);
 285  
                 }
 286  0
             }
 287  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 288  0
             file.setCorrectSeparators(false);
 289  
         }
 290  0
     }
 291  
 
 292  
     /**
 293  
      * Read the languages.
 294  
      *
 295  
      * @param stream The input stream.
 296  
      * @param file   The file.
 297  
      * @throws IOException Thrown when an error occurs during the reading process.
 298  
      */
 299  
     private void readLanguages(DataInputStream stream, JTNotZippedFile file) throws IOException {
 300  0
         if (stream.readInt() == IFileManager.CONTENT) {
 301  0
             boolean endOfLanguagesList = false;
 302  0
             languages = new ArrayList<Language>(10);
 303  
 
 304  0
             while (!endOfLanguagesList) {
 305  0
                 Language language = languagesService.getEmptyLanguage();
 306  
 
 307  0
                 language.getTemporaryContext().setId(stream.readInt());
 308  0
                 language.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 309  
 
 310  0
                 languages.add(language);
 311  
 
 312  0
                 long separator = stream.readLong();
 313  
 
 314  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 315  0
                     endOfLanguagesList = true;
 316  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 317  0
                     file.setCorrectSeparators(false);
 318  
                 }
 319  0
             }
 320  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 321  0
             file.setCorrectSeparators(false);
 322  
         }
 323  0
     }
 324  
 
 325  
     /**
 326  
      * Read the realizers.
 327  
      *
 328  
      * @param stream The input stream.
 329  
      * @param file   The file.
 330  
      * @throws IOException Thrown when an error occurs during the reading process.
 331  
      */
 332  
     private void readRealizers(DataInputStream stream, JTNotZippedFile file) throws IOException {
 333  0
         if (stream.readInt() == IFileManager.CONTENT) {
 334  0
             boolean endOfRealizersList = false;
 335  0
             realizers = new ArrayList<Person>(20);
 336  
 
 337  0
             while (!endOfRealizersList) {
 338  0
                 Person realizer = realizersService.getEmptyRealizer();
 339  
 
 340  0
                 realizer.getTemporaryContext().setId(stream.readInt());
 341  0
                 realizer.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 342  0
                 realizer.setFirstName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 343  0
                 realizer.getTemporaryContext().setCountry(stream.readInt());
 344  0
                 realizer.getTemporaryContext().setIntNote(stream.readInt());
 345  
 
 346  0
                 realizers.add(realizer);
 347  
 
 348  0
                 long separator = stream.readLong();
 349  
 
 350  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 351  0
                     endOfRealizersList = true;
 352  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 353  0
                     file.setCorrectSeparators(false);
 354  
                 }
 355  0
             }
 356  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 357  0
             file.setCorrectSeparators(false);
 358  
         }
 359  0
     }
 360  
 
 361  
     /**
 362  
      * Read the actors.
 363  
      *
 364  
      * @param stream The input stream.
 365  
      * @param file   The file.
 366  
      * @throws IOException Thrown when an error occurs during the reading process.
 367  
      */
 368  
     private void readActors(DataInputStream stream, JTNotZippedFile file) throws IOException {
 369  0
         if (stream.readInt() == IFileManager.CONTENT) {
 370  0
             boolean endOfActorsList = false;
 371  0
             actors = new ArrayList<Person>(50);
 372  
 
 373  0
             while (!endOfActorsList) {
 374  0
                 Person actor = actorService.getEmptyActor();
 375  
 
 376  0
                 actor.getTemporaryContext().setId(stream.readInt());
 377  0
                 actor.setName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 378  0
                 actor.setFirstName(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 379  0
                 actor.getTemporaryContext().setCountry(stream.readInt());
 380  0
                 actor.getTemporaryContext().setIntNote(stream.readInt());
 381  
 
 382  0
                 actors.add(actor);
 383  
 
 384  0
                 long separator = stream.readLong();
 385  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 386  0
                     endOfActorsList = true;
 387  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 388  0
                     file.setCorrectSeparators(false);
 389  
                 }
 390  0
             }
 391  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 392  0
             file.setCorrectSeparators(false);
 393  
         }
 394  0
     }
 395  
 
 396  
     /**
 397  
      * Read the films.
 398  
      *
 399  
      * @param stream The input stream.
 400  
      * @param file   The file.
 401  
      * @throws IOException Thrown when an error occurs during the reading process.
 402  
      */
 403  
     private void readFilms(DataInputStream stream, JTNotZippedFile file) throws IOException {
 404  0
         if (stream.readInt() == IFileManager.CONTENT) {
 405  0
             boolean endOfFilmsList = false;
 406  0
             films = new ArrayList<Film>(25);
 407  
 
 408  0
             while (!endOfFilmsList) {
 409  0
                 Film film = filmsService.getEmptyFilm();
 410  
 
 411  0
                 film.getTemporaryContext().setId(stream.readInt());
 412  0
                 film.setTitle(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 413  
 
 414  0
                 readKinds(stream, file, film);
 415  0
                 readFilm(stream, film);
 416  0
                 readActors(stream, file, film);
 417  
 
 418  0
                 long separator = stream.readLong();
 419  
 
 420  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 421  0
                     endOfFilmsList = true;
 422  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 423  0
                     file.setCorrectSeparators(false);
 424  
                 }
 425  
 
 426  0
                 films.add(film);
 427  0
             }
 428  0
         } else if (stream.readLong() != IFileManager.JT_CATEGORY_SEPARATOR) {
 429  0
             file.setCorrectSeparators(false);
 430  
         }
 431  0
     }
 432  
 
 433  
     /**
 434  
      * Read the kinds from the input stream.
 435  
      *
 436  
      * @param stream The stream to read.
 437  
      * @param file   The file to read from.
 438  
      * @param film   The film to fill.
 439  
      * @throws IOException If an error occurs during the stream reading.
 440  
      */
 441  
     private static void readKinds(DataInputStream stream, JTNotZippedFile file, Film film) throws IOException {
 442  0
         if (stream.readInt() == IFileManager.CONTENT) {
 443  0
             boolean endOfKindsList = false;
 444  0
             Collection<Integer> kindsFilm = new ArrayList<Integer>(12);
 445  
 
 446  0
             while (!endOfKindsList) {
 447  0
                 kindsFilm.add(stream.readInt());
 448  
 
 449  0
                 long separator = stream.readLong();
 450  
 
 451  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 452  0
                     endOfKindsList = true;
 453  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 454  0
                     file.setCorrectSeparators(false);
 455  
                 }
 456  0
             }
 457  
 
 458  0
             film.getTemporaryContext().setKinds(kindsFilm);
 459  0
         } else {
 460  0
             film.getTemporaryContext().setKinds(Collections.<Integer>emptyList());
 461  
         }
 462  0
     }
 463  
 
 464  
     /**
 465  
      * Fill the film object with the data of the stream.
 466  
      *
 467  
      * @param stream The stream to read from.
 468  
      * @param film   The film to fill.
 469  
      * @throws IOException Thrown when an error occurs during the reading process.
 470  
      */
 471  
     private static void readFilm(DataInputStream stream, Film film) throws IOException {
 472  0
         film.getTemporaryContext().setType(stream.readInt());
 473  0
         film.getTemporaryContext().setRealizer(stream.readInt());
 474  0
         film.getTemporaryContext().setLanguage(stream.readInt());
 475  0
         film.setYear(stream.readInt());
 476  0
         film.setDuration(stream.readInt());
 477  0
         film.getTemporaryContext().setLending(stream.readInt());
 478  0
         film.setResume(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 479  0
         film.setComment(Managers.getManager(IFileManager.class).formatUTFToRead(stream.readUTF()));
 480  0
     }
 481  
 
 482  
     /**
 483  
      * Read the actors from the input stream.
 484  
      *
 485  
      * @param stream The stream to read.
 486  
      * @param file   The file to read from.
 487  
      * @param film   The film to fill.
 488  
      * @throws IOException If an error occurs during the stream reading.
 489  
      */
 490  
     private static void readActors(DataInputStream stream, JTNotZippedFile file, Film film) throws IOException {
 491  0
         if (stream.readInt() == IFileManager.CONTENT) {
 492  0
             boolean endOfActorsList = false;
 493  0
             Collection<Integer> actors = new ArrayList<Integer>(25);
 494  
 
 495  0
             while (!endOfActorsList) {
 496  0
                 actors.add(stream.readInt());
 497  
 
 498  0
                 long separator = stream.readLong();
 499  
 
 500  0
                 if (separator == IFileManager.JT_CATEGORY_SEPARATOR) {
 501  0
                     endOfActorsList = true;
 502  0
                 } else if (separator != IFileManager.JT_INTERN_SEPARATOR) {
 503  0
                     file.setCorrectSeparators(false);
 504  
                 }
 505  0
             }
 506  
 
 507  0
             film.getTemporaryContext().setActors(actors);
 508  0
         } else {
 509  0
             film.getTemporaryContext().setActors(new ArrayList<Integer>(0));
 510  
         }
 511  0
     }
 512  
 }