Coverage Report - org.jtheque.films.services.impl.utils.file.backup.JTDBackupWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
JTDBackupWriter
0 %
0/169
0 %
0/76
3.786
 
 1  
 package org.jtheque.films.services.impl.utils.file.backup;
 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.file.IFileManager;
 22  
 import org.jtheque.core.managers.file.able.BackupWriter;
 23  
 import org.jtheque.core.managers.log.ILoggingManager;
 24  
 import org.jtheque.films.persistence.od.able.Film;
 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.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  
 import java.io.DataOutputStream;
 43  
 import java.io.IOException;
 44  
 
 45  
 /**
 46  
  * A JTD Backup Writer.
 47  
  *
 48  
  * @author Baptiste Wicht
 49  
  */
 50  
 public final class JTDBackupWriter implements BackupWriter {
 51  
     @Resource
 52  
     private IFilmsService filmsService;
 53  
 
 54  
     @Resource
 55  
     private IKindsService kindsService;
 56  
 
 57  
     @Resource
 58  
     private ITypesService typesService;
 59  
 
 60  
     @Resource
 61  
     private ILanguagesService languagesService;
 62  
 
 63  
     @Resource
 64  
     private IActorService actorService;
 65  
 
 66  
     @Resource
 67  
     private IRealizersService realizersService;
 68  
 
 69  
     @Resource
 70  
     private ILendingsService lendingsService;
 71  
 
 72  
     @Resource
 73  
     private IBorrowersService borrowersService;
 74  
 
 75  
     @Resource
 76  
     private ICountriesService countriesService;
 77  
 
 78  
     /**
 79  
      * Construct a new JTDBackupWriter.
 80  
      */
 81  
     public JTDBackupWriter() {
 82  0
         super();
 83  
 
 84  0
         Managers.getManager(IBeansManager.class).inject(this);
 85  0
     }
 86  
 
 87  
     @Override
 88  
     public void write(Object object) {
 89  0
         DataOutputStream stream = (DataOutputStream) object;
 90  
 
 91  
         try {
 92  0
             writeFilms(stream);
 93  0
             writeActors(stream);
 94  0
             writeRealizers(stream);
 95  0
             writeLanguages(stream);
 96  0
             writeKinds(stream);
 97  0
             writeTypes(stream);
 98  0
             writeCountries(stream);
 99  0
             writeLendings(stream);
 100  0
             writeBorrowers(stream);
 101  0
         } catch (IOException e) {
 102  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 103  0
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Write the borrowers to the stream.
 108  
      *
 109  
      * @param stream The stream to write in.
 110  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 111  
      */
 112  
     private void writeBorrowers(DataOutputStream stream) throws IOException {
 113  0
         if (borrowersService.hasNoBorrowers()) {
 114  0
             stream.writeInt(IFileManager.NO_CONTENT);
 115  
         } else {
 116  0
             stream.writeInt(IFileManager.CONTENT);
 117  
 
 118  0
             boolean firstBorrower = true;
 119  0
             for (Person borrower : borrowersService.getBorrowers()) {
 120  0
                 if (firstBorrower) {
 121  0
                     firstBorrower = false;
 122  
                 } else {
 123  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 124  
                 }
 125  
 
 126  0
                 stream.writeInt(borrower.getId());
 127  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(borrower.getName()));
 128  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(borrower.getFirstName()));
 129  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(borrower.getEmail()));
 130  
             }
 131  
         }
 132  
 
 133  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Write the lendings to the stream.
 138  
      *
 139  
      * @param stream The stream to write in.
 140  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 141  
      */
 142  
     private void writeLendings(DataOutputStream stream) throws IOException {
 143  0
         if (lendingsService.hasNoLendings()) {
 144  0
             stream.writeInt(IFileManager.NO_CONTENT);
 145  
         } else {
 146  0
             stream.writeInt(IFileManager.CONTENT);
 147  
 
 148  0
             boolean firstLending = true;
 149  0
             for (Lending lending : lendingsService.getLendings()) {
 150  0
                 if (firstLending) {
 151  0
                     firstLending = false;
 152  
                 } else {
 153  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 154  
                 }
 155  
 
 156  0
                 stream.writeInt(lending.getId());
 157  0
                 stream.writeInt(lending.getDate().intValue());
 158  0
                 stream.writeInt(lending.getThePerson().getId());
 159  0
                 stream.writeInt(lending.getTheOther());
 160  
             }
 161  
         }
 162  
 
 163  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Write the countries to the stream.
 168  
      *
 169  
      * @param stream The stream to write in.
 170  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 171  
      */
 172  
     private void writeCountries(DataOutputStream stream) throws IOException {
 173  0
         if (countriesService.getCountries() == null || countriesService.getCountries().isEmpty()) {
 174  0
             stream.writeInt(IFileManager.NO_CONTENT);
 175  
         } else {
 176  0
             stream.writeInt(IFileManager.CONTENT);
 177  
 
 178  0
             boolean firstCountry = true;
 179  0
             for (Country country : countriesService.getCountries()) {
 180  0
                 if (firstCountry) {
 181  0
                     firstCountry = false;
 182  
                 } else {
 183  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 184  
                 }
 185  
 
 186  0
                 stream.writeInt(country.getId());
 187  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(country.getName()));
 188  
             }
 189  
         }
 190  
 
 191  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Write the types to the stream.
 196  
      *
 197  
      * @param stream The stream to write in.
 198  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 199  
      */
 200  
     private void writeTypes(DataOutputStream stream) throws IOException {
 201  0
         if (typesService.hasNoTypes()) {
 202  0
             stream.writeInt(IFileManager.NO_CONTENT);
 203  
         } else {
 204  0
             stream.writeInt(IFileManager.CONTENT);
 205  
 
 206  0
             boolean firstType = true;
 207  0
             for (Type type : typesService.getTypes()) {
 208  0
                 if (firstType) {
 209  0
                     firstType = false;
 210  
                 } else {
 211  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 212  
                 }
 213  
 
 214  0
                 stream.writeInt(type.getId());
 215  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(type.getName()));
 216  
             }
 217  
         }
 218  
 
 219  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 220  0
     }
 221  
 
 222  
     /**
 223  
      * Write the kinds to the stream.
 224  
      *
 225  
      * @param stream The stream to write in.
 226  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 227  
      */
 228  
     private void writeKinds(DataOutputStream stream) throws IOException {
 229  0
         if (kindsService.hasNoKinds()) {
 230  0
             stream.writeInt(IFileManager.NO_CONTENT);
 231  
         } else {
 232  0
             stream.writeInt(IFileManager.CONTENT);
 233  
 
 234  0
             boolean firstKind = true;
 235  0
             for (Kind kind : kindsService.getKinds()) {
 236  0
                 if (firstKind) {
 237  0
                     firstKind = false;
 238  
                 } else {
 239  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 240  
                 }
 241  
 
 242  0
                 stream.writeInt(kind.getId());
 243  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(kind.getName()));
 244  
             }
 245  
         }
 246  
 
 247  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 248  0
     }
 249  
 
 250  
     /**
 251  
      * Write the languages to the stream.
 252  
      *
 253  
      * @param stream The stream to write in.
 254  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 255  
      */
 256  
     private void writeLanguages(DataOutputStream stream) throws IOException {
 257  0
         if (languagesService.getLanguages() == null || languagesService.getLanguages().isEmpty()) {
 258  0
             stream.writeInt(IFileManager.NO_CONTENT);
 259  
         } else {
 260  0
             stream.writeInt(IFileManager.CONTENT);
 261  
 
 262  0
             boolean firstLanguage = true;
 263  0
             for (Language language : languagesService.getLanguages()) {
 264  0
                 if (firstLanguage) {
 265  0
                     firstLanguage = false;
 266  
                 } else {
 267  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 268  
                 }
 269  
 
 270  0
                 stream.writeInt(language.getId());
 271  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(language.getName()));
 272  
             }
 273  
         }
 274  
 
 275  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 276  0
     }
 277  
 
 278  
     /**
 279  
      * Write the realizers to the stream.
 280  
      *
 281  
      * @param stream The stream to write in.
 282  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 283  
      */
 284  
     private void writeRealizers(DataOutputStream stream) throws IOException {
 285  0
         if (realizersService.hasNoRealizers()) {
 286  0
             stream.writeInt(IFileManager.NO_CONTENT);
 287  
         } else {
 288  0
             stream.writeInt(IFileManager.CONTENT);
 289  
 
 290  0
             boolean firstRealizer = true;
 291  0
             for (Person realizer : realizersService.getRealizers()) {
 292  0
                 if (firstRealizer) {
 293  0
                     firstRealizer = false;
 294  
                 } else {
 295  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 296  
                 }
 297  
 
 298  0
                 stream.writeInt(realizer.getId());
 299  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getName()));
 300  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getFirstName()));
 301  0
                 stream.writeInt(realizer.getTheCountry().getId());
 302  0
                 stream.writeInt(realizer.getNote().getValue().intValue());
 303  
             }
 304  
         }
 305  
 
 306  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 307  0
     }
 308  
 
 309  
     /**
 310  
      * Write the actors to the stream.
 311  
      *
 312  
      * @param stream The stream to write in.
 313  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 314  
      */
 315  
     private void writeActors(DataOutputStream stream) throws IOException {
 316  0
         if (actorService.hasNoActor()) {
 317  0
             stream.writeInt(IFileManager.NO_CONTENT);
 318  
         } else {
 319  0
             stream.writeInt(IFileManager.CONTENT);
 320  
 
 321  0
             boolean firstActor = true;
 322  0
             for (Person actor : actorService.getActors()) {
 323  0
                 if (firstActor) {
 324  0
                     firstActor = false;
 325  
                 } else {
 326  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 327  
                 }
 328  
 
 329  0
                 stream.writeInt(actor.getId());
 330  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getName()));
 331  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getFirstName()));
 332  0
                 stream.writeInt(actor.getTheCountry().getId());
 333  0
                 stream.writeInt(actor.getNote().getValue().intValue());
 334  
             }
 335  
         }
 336  
 
 337  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 338  0
     }
 339  
 
 340  
     /**
 341  
      * Write the films to the stream.
 342  
      *
 343  
      * @param stream The stream to write in.
 344  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 345  
      */
 346  
     private void writeFilms(DataOutputStream stream) throws IOException {
 347  0
         if (filmsService.isNoFilms()) {
 348  0
             stream.writeInt(IFileManager.NO_CONTENT);
 349  
         } else {
 350  0
             stream.writeInt(IFileManager.CONTENT);
 351  
 
 352  0
             boolean firstFilm = true;
 353  0
             for (Film film : filmsService.getFilms()) {
 354  0
                 if (firstFilm) {
 355  0
                     firstFilm = false;
 356  
                 } else {
 357  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 358  
                 }
 359  
 
 360  0
                 writeFilm(stream, film);
 361  
             }
 362  
         }
 363  
 
 364  0
         stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 365  0
     }
 366  
 
 367  
     /**
 368  
      * Write the film.
 369  
      *
 370  
      * @param stream The stream to write to.
 371  
      * @param film   The film to write.
 372  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 373  
      */
 374  
     private static void writeFilm(DataOutputStream stream, Film film) throws IOException {
 375  0
         stream.writeInt(film.getId());
 376  0
         stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTitle()));
 377  
 
 378  0
         writeKindsOfFilm(stream, film);
 379  
 
 380  0
         stream.writeInt(film.hasType() ? film.getTheType().getId() : -1);
 381  0
         stream.writeInt(film.hasRealizer() ? film.getTheRealizer().getId() : -1);
 382  0
         stream.writeInt(film.hasLanguage() ? film.getTheLanguage().getId() : -1);
 383  
 
 384  0
         stream.writeInt(film.getYear());
 385  0
         stream.writeInt(film.getDuration());
 386  0
         stream.writeInt(film.getNote().getValue().intValue());
 387  0
         stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getResume()));
 388  0
         stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getComment()));
 389  
 
 390  0
         writeActorsOfFilm(stream, film);
 391  0
     }
 392  
 
 393  
     /**
 394  
      * Write the kinds of the film.
 395  
      *
 396  
      * @param stream The stream to write to.
 397  
      * @param film   The film to write.
 398  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 399  
      */
 400  
     private static void writeKindsOfFilm(DataOutputStream stream, Film film) throws IOException {
 401  0
         if (film.hasKinds()) {
 402  0
             stream.writeInt(IFileManager.NO_CONTENT);
 403  
         } else {
 404  0
             stream.writeInt(IFileManager.CONTENT);
 405  
 
 406  0
             boolean firstKind = true;
 407  0
             for (Kind kind : film.getKinds()) {
 408  0
                 if (firstKind) {
 409  0
                     firstKind = false;
 410  
                 } else {
 411  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 412  
                 }
 413  
 
 414  0
                 stream.writeInt(kind.getId());
 415  
             }
 416  
 
 417  0
             stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 418  
         }
 419  0
     }
 420  
 
 421  
     /**
 422  
      * Write the actors of the film.
 423  
      *
 424  
      * @param stream The stream to write to.
 425  
      * @param film   The film to write.
 426  
      * @throws IOException Thrown when an exception occurs during the writing of data.
 427  
      */
 428  
     private static void writeActorsOfFilm(DataOutputStream stream, Film film) throws IOException {
 429  0
         if (film.hasActors()) {
 430  0
             stream.writeInt(IFileManager.NO_CONTENT);
 431  
         } else {
 432  0
             stream.writeInt(IFileManager.CONTENT);
 433  
 
 434  0
             boolean firstActor = true;
 435  0
             for (Person actor : film.getActors()) {
 436  0
                 if (firstActor) {
 437  0
                     firstActor = false;
 438  
                 } else {
 439  0
                     stream.writeLong(IFileManager.JT_INTERN_SEPARATOR);
 440  
                 }
 441  
 
 442  0
                 stream.writeInt(actor.getId());
 443  
             }
 444  
 
 445  0
             stream.writeLong(IFileManager.JT_CATEGORY_SEPARATOR);
 446  
         }
 447  0
     }
 448  
 }