Coverage Report - org.jtheque.films.services.impl.utils.file.jt.writer.JTFEFileWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
JTFEFileWriter
0 %
0/22
0 %
0/6
5
 
 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.error.IErrorManager;
 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.core.utils.file.jt.JTZippedFileWriter;
 25  
 import org.jtheque.films.persistence.od.able.Film;
 26  
 import org.jtheque.films.services.impl.utils.file.jt.sources.JTFDataSource;
 27  
 import org.jtheque.films.services.impl.utils.file.jt.sources.JTFEDataSource;
 28  
 import org.jtheque.utils.io.FileException;
 29  
 import org.jtheque.utils.io.FileUtils;
 30  
 
 31  
 import java.io.BufferedOutputStream;
 32  
 import java.io.File;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Collection;
 35  
 
 36  
 /**
 37  
  * A writer for JTFE file.
 38  
  *
 39  
  * @author Baptiste Wicht
 40  
  */
 41  0
 public final class JTFEFileWriter extends JTZippedFileWriter {
 42  
     @Override
 43  
     public void writeFile(BufferedOutputStream stream, BasicDataSource source) {
 44  0
         JTFEDataSource dataSource = (JTFEDataSource) source;
 45  
 
 46  0
         Collection<File> files = new ArrayList<File>(dataSource.getFilms().size());
 47  0
         JTFileWriter writer = new JTFFileWriter();
 48  
 
 49  0
         int count = 0;
 50  0
         for (Film film : dataSource.getFilms()) {
 51  0
             ++count;
 52  
 
 53  0
             File f = new File(Managers.getCore().getFolders().getTempFolderPath() + File.separator + count + ".jtf");
 54  
 
 55  
             try {
 56  0
                 BasicDataSource filmDataSource = new JTFDataSource(film);
 57  0
                 filmDataSource.setFileVersion(dataSource.getFileVersion());
 58  0
                 filmDataSource.setVersion(dataSource.getVersion());
 59  
 
 60  0
                 writer.writeFile(f, filmDataSource);
 61  0
             } catch (FileException e) {
 62  0
                 Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 63  0
             }
 64  
 
 65  0
             files.add(f);
 66  0
         }
 67  
 
 68  0
         FileUtils.zip(files, stream);
 69  
 
 70  0
         for (File f : files) {
 71  0
             if (!f.delete()) {
 72  0
                 Managers.getManager(IErrorManager.class).addInternationalizedError("errors.jtfe.delete", f.getAbsolutePath());
 73  
             }
 74  
         }
 75  0
     }
 76  
 }