Coverage Report - org.jtheque.films.services.impl.utils.file.restore.XMLBackupReader
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLBackupReader
0 %
0/145
0 %
0/24
1.867
 
 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.jdom.Element;
 20  
 import org.jdom.JDOMException;
 21  
 import org.jdom.xpath.XPath;
 22  
 import org.jtheque.core.managers.Managers;
 23  
 import org.jtheque.core.managers.beans.IBeansManager;
 24  
 import org.jtheque.core.managers.file.able.BackupReader;
 25  
 import org.jtheque.core.managers.log.ILoggingManager;
 26  
 import org.jtheque.core.utils.db.DaoNotes;
 27  
 import org.jtheque.core.utils.db.DaoNotes.NoteType;
 28  
 import org.jtheque.films.persistence.od.able.Film;
 29  
 import org.jtheque.films.services.able.IActorService;
 30  
 import org.jtheque.films.services.able.IFilmsService;
 31  
 import org.jtheque.films.services.able.IRealizersService;
 32  
 import org.jtheque.films.services.impl.utils.DataUtils;
 33  
 import org.jtheque.primary.od.able.Country;
 34  
 import org.jtheque.primary.od.able.Kind;
 35  
 import org.jtheque.primary.od.able.Language;
 36  
 import org.jtheque.primary.od.able.Lending;
 37  
 import org.jtheque.primary.od.able.Person;
 38  
 import org.jtheque.primary.od.able.Type;
 39  
 import org.jtheque.primary.services.able.IBorrowersService;
 40  
 import org.jtheque.primary.services.able.ICountriesService;
 41  
 import org.jtheque.primary.services.able.IKindsService;
 42  
 import org.jtheque.primary.services.able.ILanguagesService;
 43  
 import org.jtheque.primary.services.able.ILendingsService;
 44  
 import org.jtheque.primary.services.able.ITypesService;
 45  
 import org.jtheque.utils.bean.IntDate;
 46  
 
 47  
 import javax.annotation.Resource;
 48  
 import java.util.ArrayList;
 49  
 import java.util.Collection;
 50  
 
 51  
 /**
 52  
  * A reader for JTD Backup.
 53  
  *
 54  
  * @author Baptiste Wicht
 55  
  */
 56  
 public final class XMLBackupReader implements BackupReader {
 57  0
     private final Collection<Language> languages = new ArrayList<Language>(10);
 58  0
     private final Collection<Country> countries = new ArrayList<Country>(10);
 59  0
     private final Collection<Kind> kinds = new ArrayList<Kind>(10);
 60  0
     private final Collection<Type> types = new ArrayList<Type>(10);
 61  0
     private final Collection<Lending> lendings = new ArrayList<Lending>(10);
 62  0
     private final Collection<Person> borrowers = new ArrayList<Person>(10);
 63  0
     private final Collection<Person> realizers = new ArrayList<Person>(20);
 64  0
     private final Collection<Person> actors = new ArrayList<Person>(50);
 65  0
     private final Collection<Film> films = new ArrayList<Film>(25);
 66  
 
 67  
     @Resource
 68  
     private ICountriesService countriesService;
 69  
 
 70  
     @Resource
 71  
     private ILendingsService lendingsService;
 72  
 
 73  
     @Resource
 74  
     private ITypesService typesService;
 75  
 
 76  
     @Resource
 77  
     private ILanguagesService languagesService;
 78  
 
 79  
     @Resource
 80  
     private IBorrowersService borrowersService;
 81  
 
 82  
     @Resource
 83  
     private IFilmsService filmsService;
 84  
 
 85  
     @Resource
 86  
     private IActorService actorService;
 87  
 
 88  
     @Resource
 89  
     private IRealizersService realizersService;
 90  
 
 91  
     @Resource
 92  
     private IKindsService kindsService;
 93  
 
 94  0
     private static final DaoNotes DAO_NOTES = DaoNotes.getInstance();
 95  
 
 96  
     /**
 97  
      * Construct a new XMLBackupReader.
 98  
      */
 99  
     public XMLBackupReader() {
 100  0
         super();
 101  
 
 102  0
         Managers.getManager(IBeansManager.class).inject(this);
 103  0
     }
 104  
 
 105  
     @Override
 106  
     public void readBackup(Object object) {
 107  0
         Element root = (Element) object;
 108  
 
 109  
         try {
 110  0
             readLanguages(root);
 111  0
             readCountries(root);
 112  0
             readKinds(root);
 113  0
             readTypes(root);
 114  0
             readBorrowers(root);
 115  0
             readRealizers(root);
 116  0
             readActors(root);
 117  0
             readFilms(root);
 118  0
             readLendings(root);
 119  
 
 120  0
             for (Film film : films) {
 121  0
                 film.setTheLending(DataUtils.getLendingForFilm(film, lendings));
 122  
             }
 123  0
         } catch (JDOMException e) {
 124  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 125  0
         }
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Read all the languages in the root element.
 130  
      *
 131  
      * @param root The root element.
 132  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 133  
      */
 134  
     private void readLanguages(Element root) throws JDOMException {
 135  0
         XPath xpa = XPath.newInstance("//languages/language");
 136  
 
 137  0
         for (Object languageElement : xpa.selectNodes(root)) {
 138  0
             Language language = languagesService.getEmptyLanguage();
 139  0
             language.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(languageElement)));
 140  0
             language.setName(XPath.newInstance("./name").valueOf(languageElement));
 141  
 
 142  0
             languages.add(language);
 143  
 
 144  0
             languagesService.create(language);
 145  0
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Read all the countries in the root element.
 150  
      *
 151  
      * @param root The root element.
 152  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 153  
      */
 154  
     private void readCountries(Element root) throws JDOMException {
 155  0
         XPath xpa = XPath.newInstance("//countries/country");
 156  
 
 157  0
         for (Object countryElement : xpa.selectNodes(root)) {
 158  0
             Country country = countriesService.getEmptyCountry();
 159  0
             country.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(countryElement)));
 160  0
             country.setName(XPath.newInstance("./name").valueOf(countryElement));
 161  
 
 162  0
             countries.add(country);
 163  
 
 164  0
             countriesService.create(country);
 165  0
         }
 166  0
     }
 167  
 
 168  
     /**
 169  
      * Read all the kinds in the root element.
 170  
      *
 171  
      * @param root The root element.
 172  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 173  
      */
 174  
     private void readKinds(Element root) throws JDOMException {
 175  0
         XPath xpa = XPath.newInstance("//kinds/kinds");
 176  
 
 177  0
         for (Object kindElement : xpa.selectNodes(root)) {
 178  0
             Kind kind = kindsService.getEmptyKind();
 179  0
             kind.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(kindElement)));
 180  0
             kind.setName(XPath.newInstance("./name").valueOf(kindElement));
 181  
 
 182  0
             kinds.add(kind);
 183  
 
 184  0
             kindsService.create(kind);
 185  0
         }
 186  0
     }
 187  
 
 188  
     /**
 189  
      * Read all the types in the root element.
 190  
      *
 191  
      * @param root The root element.
 192  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 193  
      */
 194  
     private void readTypes(Element root) throws JDOMException {
 195  0
         XPath xpa = XPath.newInstance("//types/type");
 196  
 
 197  0
         for (Object typeElement : xpa.selectNodes(root)) {
 198  0
             Type type = typesService.getEmptyType();
 199  0
             type.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(typeElement)));
 200  0
             type.setName(XPath.newInstance("./id").valueOf(typeElement));
 201  
 
 202  0
             types.add(type);
 203  
 
 204  0
             typesService.create(type);
 205  0
         }
 206  0
     }
 207  
 
 208  
     /**
 209  
      * Read all the borrowers in the root element.
 210  
      *
 211  
      * @param root The root element.
 212  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 213  
      */
 214  
     private void readBorrowers(Element root) throws JDOMException {
 215  0
         XPath xpa = XPath.newInstance("//borrowers/borrower");
 216  
 
 217  0
         for (Object borrowerElement : xpa.selectNodes(root)) {
 218  0
             Person borrower = borrowersService.getEmptyBorrower();
 219  0
             borrower.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(borrowerElement)));
 220  0
             borrower.setName(XPath.newInstance("./name").valueOf(borrowerElement));
 221  0
             borrower.setFirstName(XPath.newInstance("./firstname").valueOf(borrowerElement));
 222  0
             borrower.setEmail(XPath.newInstance("./email").valueOf(borrowerElement));
 223  
 
 224  0
             borrowers.add(borrower);
 225  
 
 226  0
             borrowersService.create(borrower);
 227  0
         }
 228  0
     }
 229  
 
 230  
     /**
 231  
      * Read all the realizers in the root element.
 232  
      *
 233  
      * @param root The root element.
 234  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 235  
      */
 236  
     private void readRealizers(Element root) throws JDOMException {
 237  0
         XPath xpa = XPath.newInstance("//realizers/realizer");
 238  
 
 239  0
         for (Object realizerElement : xpa.selectNodes(root)) {
 240  0
             Person realizer = realizersService.getEmptyRealizer();
 241  0
             realizer.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(realizerElement)));
 242  0
             realizer.setName(XPath.newInstance("./name").valueOf(realizerElement));
 243  0
             realizer.setFirstName(XPath.newInstance("./firstname").valueOf(realizerElement));
 244  0
             realizer.setTheCountry(DataUtils.getDataByTemporaryId(
 245  
                     Integer.parseInt(XPath.newInstance("./country").valueOf(realizerElement)), countries));
 246  0
             realizer.setNote(DAO_NOTES.getNote(NoteType.getEnum(Integer.parseInt(XPath.newInstance("./note").valueOf(realizerElement)))));
 247  
 
 248  0
             realizers.add(realizer);
 249  
 
 250  0
             realizersService.create(realizer);
 251  0
         }
 252  0
     }
 253  
 
 254  
     /**
 255  
      * Read all the actors in the root element.
 256  
      *
 257  
      * @param root The root element.
 258  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 259  
      */
 260  
     private void readActors(Element root) throws JDOMException {
 261  0
         XPath xpa = XPath.newInstance("//actors/actor");
 262  
 
 263  0
         for (Object actorElement : xpa.selectNodes(root)) {
 264  0
             Person actor = actorService.getEmptyActor();
 265  0
             actor.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(actorElement)));
 266  0
             actor.setName(XPath.newInstance("./name").valueOf(actorElement));
 267  0
             actor.setFirstName(XPath.newInstance("./firstname").valueOf(actorElement));
 268  0
             actor.setTheCountry(DataUtils.getDataByTemporaryId(
 269  
                     Integer.parseInt(XPath.newInstance("./country").valueOf(actorElement)), countries));
 270  0
             actor.setNote(DAO_NOTES.getNote(NoteType.getEnum(Integer.parseInt(XPath.newInstance("./note").valueOf(actorElement)))));
 271  
 
 272  0
             actors.add(actor);
 273  
 
 274  0
             actorService.create(actor);
 275  0
         }
 276  0
     }
 277  
 
 278  
     /**
 279  
      * Read all the films in the root element.
 280  
      *
 281  
      * @param root The root element.
 282  
      * @throws JDOMException         This exception is throwed when an error occurs during the xml reading process.
 283  
      * @throws NumberFormatException This exception is throwed when an error occurs during the parse process.
 284  
      */
 285  
     private void readFilms(Element root) throws NumberFormatException, JDOMException {
 286  0
         XPath xpa = XPath.newInstance("//films/film");
 287  
 
 288  0
         for (Object filmElement : xpa.selectNodes(root)) {
 289  0
             Film film = filmsService.getEmptyFilm();
 290  
 
 291  0
             readValuesOfFilm(filmElement, film);
 292  0
             readActorsOfFilm(filmElement, film);
 293  0
             readKindsOfFilm(filmElement, film);
 294  
 
 295  0
             films.add(film);
 296  
 
 297  0
             filmsService.create(film);
 298  0
         }
 299  0
     }
 300  
 
 301  
     /**
 302  
      * Read the values of the film from the XML.
 303  
      *
 304  
      * @param filmElement The XML film element.
 305  
      * @param film        The film to fill.
 306  
      * @throws JDOMException If an error occurs during the XML reading.
 307  
      */
 308  
     private void readValuesOfFilm(Object filmElement, Film film) throws JDOMException {
 309  0
         film.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(filmElement)));
 310  0
         film.setTitle(XPath.newInstance("./title").valueOf(filmElement));
 311  0
         film.setTheRealizer(DataUtils.getDataByTemporaryId(
 312  
                 Integer.parseInt(XPath.newInstance("./realizer").valueOf(filmElement)), realizers));
 313  0
         film.setTheLanguage(DataUtils.getDataByTemporaryId(
 314  
                 Integer.parseInt(XPath.newInstance("./language").valueOf(filmElement)), languages));
 315  0
         film.setTheType(DataUtils.getDataByTemporaryId(
 316  
                 Integer.parseInt(XPath.newInstance("./type").valueOf(filmElement)), types));
 317  0
         film.setYear(Integer.parseInt(XPath.newInstance("./year").valueOf(filmElement)));
 318  0
         film.setDuration(Integer.parseInt(XPath.newInstance("./duration").valueOf(filmElement)));
 319  0
         film.setNote(DAO_NOTES.getNote(NoteType.getEnum(Integer.parseInt(XPath.newInstance("./note").valueOf(filmElement)))));
 320  0
         film.setResume(XPath.newInstance("./resume").valueOf(filmElement));
 321  0
         film.setComment(XPath.newInstance("./comment").valueOf(filmElement));
 322  0
     }
 323  
 
 324  
     /**
 325  
      * Read the actors of the film from the XML.
 326  
      *
 327  
      * @param filmElement The XML film element.
 328  
      * @param film        The film to fill.
 329  
      * @throws JDOMException If an error occurs during the XML reading.
 330  
      */
 331  
     private void readActorsOfFilm(Object filmElement, Film film) throws JDOMException {
 332  0
         XPath xpa = XPath.newInstance(".//actors/actor");
 333  
 
 334  0
         for (Object actorElement : xpa.selectNodes(filmElement)) {
 335  0
             Person actor = DataUtils.getDataByTemporaryId(
 336  
                     Integer.parseInt(XPath.newInstance(".").valueOf(actorElement)), actors);
 337  
 
 338  0
             film.addActor(actor);
 339  0
         }
 340  0
     }
 341  
 
 342  
     /**
 343  
      * Read the kinds of the film from the XML.
 344  
      *
 345  
      * @param filmElement The XML film element.
 346  
      * @param film        The film to fill.
 347  
      * @throws JDOMException If an error occurs during the XML reading.
 348  
      */
 349  
     private void readKindsOfFilm(Object filmElement, Film film) throws JDOMException {
 350  0
         XPath xpa = XPath.newInstance(".//kinds/kind");
 351  
 
 352  0
         for (Object kindElement : xpa.selectNodes(filmElement)) {
 353  0
             Kind kind = DataUtils.getDataByTemporaryId(
 354  
                     Integer.parseInt(XPath.newInstance(".").valueOf(kindElement)), kinds);
 355  
 
 356  0
             film.addKind(kind);
 357  0
         }
 358  0
     }
 359  
 
 360  
     /**
 361  
      * Read all the lendings in the root element.
 362  
      *
 363  
      * @param root The root element.
 364  
      * @throws JDOMException This exception is throwed when an error occurs during the xml reading process.
 365  
      */
 366  
     private void readLendings(Element root) throws JDOMException {
 367  0
         XPath xpa = XPath.newInstance("//lendings/lending");
 368  
 
 369  0
         for (Object lendingElement : xpa.selectNodes(root)) {
 370  0
             Lending lending = lendingsService.getEmptyLending();
 371  0
             lending.getTemporaryContext().setId(Integer.parseInt(XPath.newInstance("./id").valueOf(lendingElement)));
 372  0
             lending.setDate(new IntDate(Integer.parseInt(XPath.newInstance("./date").valueOf(lendingElement))));
 373  0
             lending.setThePerson(DataUtils.getDataByTemporaryId(
 374  
                     Integer.parseInt(XPath.newInstance("./borrower").valueOf(lendingElement)), borrowers));
 375  0
             lending.setTheOther(DataUtils.getDataByTemporaryId(
 376  
                     Integer.parseInt(XPath.newInstance("./film").valueOf(lendingElement)), films).getId());
 377  
 
 378  0
             lendings.add(lending);
 379  
 
 380  0
             lendingsService.create(lending);
 381  0
         }
 382  0
     }
 383  
 
 384  
     @Override
 385  
     public void persistTheData() {
 386  0
     }
 387  
 }