Coverage Report - org.jtheque.films.services.impl.utils.file.exports.XMLExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLExporter
0 %
0/49
0 %
0/14
1.857
 
 1  
 package org.jtheque.films.services.impl.utils.file.exports;
 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.utils.file.XMLWriter;
 21  
 import org.jtheque.films.persistence.od.able.Film;
 22  
 import org.jtheque.films.utils.Constants;
 23  
 import org.jtheque.films.utils.Constants.Files.FileType;
 24  
 import org.jtheque.primary.od.able.Kind;
 25  
 import org.jtheque.primary.od.able.Person;
 26  
 import org.jtheque.utils.bean.IntDate;
 27  
 
 28  
 /**
 29  
  * An exporter to XML.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  0
 public final class XMLExporter extends AbstractExporter<Film> {
 34  
     @Override
 35  
     public boolean canExportTo(FileType fileType) {
 36  0
         return fileType == FileType.XML;
 37  
     }
 38  
 
 39  
     @Override
 40  
     public void export(String path) {
 41  0
         XMLWriter writer = new XMLWriter("films");
 42  
 
 43  0
         writeHeader(writer);
 44  
 
 45  0
         for (Film film : getDatas()) {
 46  0
             writer.add("film");
 47  
 
 48  0
             writer.addOnly("title", film.getTitle());
 49  
 
 50  0
             exportActors(film, writer);
 51  0
             exportKinds(film, writer);
 52  
 
 53  0
             writer.addOnly("year", film.getYear());
 54  
 
 55  0
             exportRealizer(film, writer);
 56  
 
 57  0
             if (film.hasType()) {
 58  0
                 writer.add("type", film.getTheType().getDisplayableText());
 59  
             }
 60  
 
 61  0
             if (film.hasLanguage()) {
 62  0
                 writer.add("language", film.getTheLanguage().getDisplayableText());
 63  
             }
 64  
 
 65  0
             writer.addOnly("duration", film.getDuration());
 66  0
             writer.addOnly("note", film.getNote().getValue().intValue());
 67  0
             writer.addOnly("resume", film.getResume());
 68  0
             writer.addOnly("comment", film.getComment());
 69  
 
 70  0
             writer.switchToParent();
 71  
         }
 72  
 
 73  0
         writer.switchToParent();
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Write the header to the XML file.
 78  
      *
 79  
      * @param writer The XML writer.
 80  
      */
 81  
     private static void writeHeader(XMLWriter writer) {
 82  0
         writer.add("header");
 83  
 
 84  0
         writer.addOnly("date", IntDate.today().intValue());
 85  0
         writer.addOnly("file-version", Constants.Files.Versions.XML.THIRD);
 86  0
         writer.addOnly("jtheque-version", Managers.getCore().getApplication().getVersion().getVersion());
 87  
 
 88  0
         writer.switchToParent();
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Write the actors of the film to the XML file.
 93  
      *
 94  
      * @param film   The film to get the informations from.
 95  
      * @param writer The XML writer.
 96  
      */
 97  
     private static void exportActors(Film film, XMLWriter writer) {
 98  0
         writer.add("actors");
 99  
 
 100  0
         for (Person actor : film.getActors()) {
 101  0
             writePerson(writer, actor, "actor");
 102  
         }
 103  
 
 104  0
         writer.switchToParent();
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Write the realizer of the film to the XML file.
 109  
      *
 110  
      * @param film   The film to get the informations from.
 111  
      * @param writer The XML writer.
 112  
      */
 113  
     private static void exportRealizer(Film film, XMLWriter writer) {
 114  0
         if (film.hasRealizer()) {
 115  0
             writePerson(writer, film.getTheRealizer(), "realizer");
 116  
         }
 117  0
     }
 118  
 
 119  
     /**
 120  
      * Write a person.
 121  
      *
 122  
      * @param writer   The XML Writer.
 123  
      * @param person   The person to write.
 124  
      * @param category The category id.
 125  
      */
 126  
     private static void writePerson(XMLWriter writer, Person person, String category) {
 127  0
         writer.add(category);
 128  
 
 129  0
         writer.addOnly("name", person.getName());
 130  0
         writer.addOnly("firstname", person.getFirstName());
 131  0
         writer.addOnly("country", person.getTheCountry().getDisplayableText());
 132  
 
 133  0
         writer.switchToParent();
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Write the kinds of the film to the XML file.
 138  
      *
 139  
      * @param film   The film to get the informations from.
 140  
      * @param writer The XML writer.
 141  
      */
 142  
     private static void exportKinds(Film film, XMLWriter writer) {
 143  0
         writer.add("kinds");
 144  
 
 145  0
         for (Kind kind : film.getKinds()) {
 146  0
             writer.add("kind");
 147  
 
 148  0
             writer.addOnly("name", kind.getName());
 149  
 
 150  0
             writer.switchToParent();
 151  
         }
 152  
 
 153  0
         writer.switchToParent();
 154  0
     }
 155  
 }