Coverage Report - org.jtheque.films.services.impl.utils.file.jt.writer.JTFFileWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
JTFFileWriter
0 %
0/91
0 %
0/26
2.75
 
 1  
 package org.jtheque.films.services.impl.utils.file.jt.writer;
 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.BasicDataSource;
 22  
 import org.jtheque.core.managers.log.ILoggingManager;
 23  
 import org.jtheque.core.utils.file.jt.JTFileWriter;
 24  
 import org.jtheque.films.persistence.od.able.Film;
 25  
 import org.jtheque.films.services.impl.utils.file.jt.sources.JTFDataSource;
 26  
 import org.jtheque.films.utils.Constants;
 27  
 import org.jtheque.primary.od.able.Country;
 28  
 import org.jtheque.primary.od.able.Kind;
 29  
 import org.jtheque.primary.od.able.Person;
 30  
 import org.jtheque.utils.bean.IntDate;
 31  
 import org.jtheque.utils.io.FileUtils;
 32  
 
 33  
 import java.io.DataOutputStream;
 34  
 import java.io.IOException;
 35  
 import java.util.Collection;
 36  
 import java.util.HashSet;
 37  
 
 38  
 /**
 39  
  * A writer for JTF file.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  0
 public final class JTFFileWriter extends JTFileWriter {
 44  
     @Override
 45  
     public void writeFile(DataOutputStream stream, BasicDataSource source) {
 46  0
         JTFDataSource dataSource = (JTFDataSource) source;
 47  
 
 48  
         try {
 49  0
             writeHeader(stream, dataSource);
 50  
 
 51  0
             Film film = dataSource.getFilm();
 52  
 
 53  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTitle()));
 54  0
             stream.writeInt(film.getYear());
 55  0
             stream.writeInt(film.getDuration());
 56  0
             stream.writeInt(film.getNote().getValue().intValue());
 57  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getResume()));
 58  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getComment()));
 59  
 
 60  0
             stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 61  
 
 62  0
             Collection<Country> countries = new HashSet<Country>(10);
 63  
 
 64  0
             writeActors(stream, film, countries);
 65  0
             writeRealizer(stream, film, countries);
 66  0
             writeLanguage(stream, film);
 67  0
             writeKinds(stream, film);
 68  0
             writeType(stream, film);
 69  0
             writeCountries(stream, countries);
 70  0
         } catch (IOException e) {
 71  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 72  
         } finally {
 73  0
             FileUtils.close(stream);
 74  0
         }
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Write the header of the file.
 79  
      *
 80  
      * @param stream     The stream to write to.
 81  
      * @param dataSource The data source.
 82  
      * @throws IOException Thrown if an error occurs during the stream writing.
 83  
      */
 84  
     private static void writeHeader(DataOutputStream stream, BasicDataSource dataSource) throws IOException {
 85  0
         stream.writeUTF(FileUtils.encryptKey(Constants.Files.JT.JTKEY));
 86  0
         stream.writeUTF(dataSource.getVersion().getVersion());
 87  0
         stream.writeInt(dataSource.getFileVersion());
 88  0
         stream.writeInt(IntDate.today().intValue());
 89  
 
 90  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Write the actors to the file.
 95  
      *
 96  
      * @param stream    The stream to write to.
 97  
      * @param film      The film.
 98  
      * @param countries The countries.
 99  
      * @throws IOException Thrown if an error occurs during the stream writing.
 100  
      */
 101  
     private static void writeActors(DataOutputStream stream, Film film, Collection<Country> countries) throws IOException {
 102  0
         if (film.hasActors()) {
 103  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 104  
         } else {
 105  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 106  
 
 107  0
             boolean first = true;
 108  
 
 109  0
             for (Person actor : film.getActors()) {
 110  0
                 if (first) {
 111  0
                     first = false;
 112  
                 } else {
 113  0
                     stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR);
 114  
                 }
 115  
 
 116  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getName()));
 117  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getFirstName()));
 118  0
                 stream.writeInt(actor.getTheCountry().getId());
 119  0
                 stream.writeInt(actor.getNote().getValue().intValue());
 120  
 
 121  0
                 countries.add(actor.getTheCountry());
 122  
             }
 123  
         }
 124  
 
 125  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Write the realizer to the file.
 130  
      *
 131  
      * @param stream    The stream to write to.
 132  
      * @param film      The film.
 133  
      * @param countries The countries.
 134  
      * @throws IOException Thrown if an error occurs during the stream writing.
 135  
      */
 136  
     private static void writeRealizer(DataOutputStream stream, Film film, Collection<Country> countries) throws IOException {
 137  0
         if (film.hasRealizer()) {
 138  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 139  
         } else {
 140  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 141  
 
 142  0
             Person realizer = film.getTheRealizer();
 143  
 
 144  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getName()));
 145  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getFirstName()));
 146  0
             stream.writeInt(realizer.getTheCountry().getId());
 147  0
             stream.writeInt(realizer.getNote().getValue().intValue());
 148  
 
 149  0
             if (realizer.hasCountry()) {
 150  0
                 countries.add(realizer.getTheCountry());
 151  
             }
 152  
         }
 153  
 
 154  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Write the language to the file.
 159  
      *
 160  
      * @param stream The stream to write to.
 161  
      * @param film   The film.
 162  
      * @throws IOException Thrown if an error occurs during the stream writing.
 163  
      */
 164  
     private static void writeLanguage(DataOutputStream stream, Film film) throws IOException {
 165  0
         if (film.hasLanguage()) {
 166  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 167  
         } else {
 168  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 169  
 
 170  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTheLanguage().getName()));
 171  
         }
 172  
 
 173  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 174  0
     }
 175  
 
 176  
     /**
 177  
      * Write the kinds to the file.
 178  
      *
 179  
      * @param stream The stream to write to.
 180  
      * @param film   The film.
 181  
      * @throws IOException Thrown if an error occurs during the stream writing.
 182  
      */
 183  
     private static void writeKinds(DataOutputStream stream, Film film) throws IOException {
 184  0
         if (film.hasKinds()) {
 185  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 186  
         } else {
 187  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 188  
 
 189  0
             boolean first = true;
 190  
 
 191  0
             for (Kind kind : film.getKinds()) {
 192  0
                 if (first) {
 193  0
                     first = false;
 194  
                 } else {
 195  0
                     stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR);
 196  
                 }
 197  
 
 198  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(kind.getName()));
 199  
             }
 200  
         }
 201  
 
 202  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 203  0
     }
 204  
 
 205  
     /**
 206  
      * Write the type to the file.
 207  
      *
 208  
      * @param stream The stream to write to.
 209  
      * @param film   The film.
 210  
      * @throws IOException Thrown if an error occurs during the stream writing.
 211  
      */
 212  
     private static void writeType(DataOutputStream stream, Film film) throws IOException {
 213  0
         if (film.getTheType() == null) {
 214  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 215  
         } else {
 216  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 217  
 
 218  0
             stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTheType().getName()));
 219  
         }
 220  
 
 221  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 222  0
     }
 223  
 
 224  
     /**
 225  
      * Write the countries to the file.
 226  
      *
 227  
      * @param stream    The stream to write to.
 228  
      * @param countries The countries to write.
 229  
      * @throws IOException Thrown if an error occurs during the stream writing.
 230  
      */
 231  
     private static void writeCountries(DataOutputStream stream, Collection<Country> countries) throws IOException {
 232  0
         if (countries.isEmpty()) {
 233  0
             stream.writeInt(Constants.Files.JT.NO_CONTENT);
 234  
         } else {
 235  0
             stream.writeInt(Constants.Files.JT.CONTENT);
 236  
 
 237  0
             boolean first = true;
 238  
 
 239  0
             for (Country country : countries) {
 240  0
                 if (first) {
 241  0
                     first = false;
 242  
                 } else {
 243  0
                     stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR);
 244  
                 }
 245  
 
 246  0
                 stream.writeInt(country.getId());
 247  0
                 stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(country.getName()));
 248  
             }
 249  
         }
 250  
 
 251  0
         stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR);
 252  0
     }
 253  
 }