Coverage Report - org.jtheque.films.services.impl.utils.file.restore.DBV3BackupReader
 
Classes in this File Line Coverage Branch Coverage Complexity
DBV3BackupReader
0 %
0/164
0 %
0/14
3
 
 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.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.beans.IBeansManager;
 21  
 import org.jtheque.core.managers.file.able.BackupReader;
 22  
 import org.jtheque.core.managers.log.ILoggingManager;
 23  
 import org.jtheque.films.persistence.od.able.Film;
 24  
 import org.jtheque.films.services.able.IActorService;
 25  
 import org.jtheque.films.services.able.IFilmsService;
 26  
 import org.jtheque.films.services.able.INotesService;
 27  
 import org.jtheque.films.services.able.IRealizersService;
 28  
 import org.jtheque.films.services.impl.utils.file.imports.ImporterUtils;
 29  
 import org.jtheque.primary.od.able.Country;
 30  
 import org.jtheque.primary.od.able.Kind;
 31  
 import org.jtheque.primary.od.able.Language;
 32  
 import org.jtheque.primary.od.able.Person;
 33  
 import org.jtheque.primary.od.able.Type;
 34  
 import org.jtheque.primary.services.able.ICountriesService;
 35  
 import org.jtheque.primary.services.able.IKindsService;
 36  
 import org.jtheque.primary.services.able.ILanguagesService;
 37  
 import org.jtheque.primary.services.able.ITypesService;
 38  
 import org.jtheque.utils.DatabaseException;
 39  
 import org.jtheque.utils.DatabaseUtils;
 40  
 
 41  
 import javax.annotation.Resource;
 42  
 import java.sql.Connection;
 43  
 import java.sql.PreparedStatement;
 44  
 import java.sql.ResultSet;
 45  
 import java.sql.SQLException;
 46  
 import java.sql.Statement;
 47  
 import java.util.ArrayList;
 48  
 import java.util.Arrays;
 49  
 import java.util.Collection;
 50  
 
 51  
 /**
 52  
  * A reader for a database V4 backup.
 53  
  *
 54  
  * @author Baptiste Wicht
 55  
  */
 56  
 public final class DBV3BackupReader implements BackupReader {
 57  
     @Resource
 58  
     private INotesService notesService;
 59  
 
 60  
     @Resource
 61  
     private ICountriesService countriesService;
 62  
 
 63  
     @Resource
 64  
     private ILanguagesService languagesService;
 65  
 
 66  
     @Resource
 67  
     private ITypesService typesService;
 68  
 
 69  
     @Resource
 70  
     private IKindsService kindsService;
 71  
 
 72  
     @Resource
 73  
     private IActorService actorService;
 74  
 
 75  
     @Resource
 76  
     private IFilmsService filmsService;
 77  
 
 78  
     @Resource
 79  
     private IRealizersService realizersService;
 80  
 
 81  
     /**
 82  
      * Construct a new DBV3BackupReader.
 83  
      */
 84  
     public DBV3BackupReader() {
 85  0
         super();
 86  
 
 87  0
         Managers.getManager(IBeansManager.class).inject(this);
 88  0
     }
 89  
 
 90  
     @Override
 91  
     public void readBackup(Object object) {
 92  0
         Connection connection = (Connection) object;
 93  
 
 94  
         try {
 95  0
             Collection<Film> films = importFilms(connection);
 96  0
             Collection<Person> actors = importActors(connection);
 97  0
             Collection<Person> realizers = importRealizers(connection);
 98  0
             Collection<Kind> kinds = importKinds(connection);
 99  0
             Collection<Type> types = importTypes(connection);
 100  0
             Collection<Language> languages = importLanguages(connection);
 101  0
             Collection<Country> countries = importCountries(connection);
 102  
 
 103  0
             ImporterUtils.persistDataOfImport(films, actors, realizers, kinds, types, languages, countries, null, null);
 104  0
         } catch (DatabaseException e) {
 105  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to import data");
 106  0
         }
 107  
 
 108  0
     }
 109  
 
 110  
     @Override
 111  
     public void persistTheData() {
 112  0
     }
 113  
 
 114  
     /**
 115  
      * Import the films.
 116  
      *
 117  
      * @param connection The connection.
 118  
      * @return The imported films.
 119  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 120  
      */
 121  
     private Collection<Film> importFilms(Connection connection) throws DatabaseException {
 122  0
         Collection<Film> films = new ArrayList<Film>(25);
 123  
 
 124  0
         Statement statement = null;
 125  0
         ResultSet result = null;
 126  
         try {
 127  0
             statement = connection.createStatement();
 128  0
             result = statement.executeQuery("SELECT * FROM film");
 129  
 
 130  0
             while (result.next()) {
 131  0
                 Film film = filmsService.getEmptyFilm();
 132  
 
 133  0
                 importInformationsOfFilm(result, film);
 134  0
                 importActorsOfFilm(connection, film);
 135  
 
 136  0
                 films.add(film);
 137  0
             }
 138  0
         } catch (SQLException e) {
 139  0
             throw new DatabaseException(e);
 140  
         } finally {
 141  0
             DatabaseUtils.close(result);
 142  0
             DatabaseUtils.close(statement);
 143  0
         }
 144  
 
 145  0
         return films;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Import the informations of the films from the result set.
 150  
      *
 151  
      * @param result The result set.
 152  
      * @param film   The film to fill.
 153  
      * @throws SQLException If an error occurs during the result set read.
 154  
      */
 155  
     private void importInformationsOfFilm(ResultSet result, Film film) throws SQLException {
 156  0
         film.getTemporaryContext().setId(result.getInt("ID"));
 157  0
         film.setTitle(result.getString("nom"));
 158  0
         film.getTemporaryContext().setKinds(Arrays.asList(result.getInt("genre")));
 159  0
         film.getTemporaryContext().setType(result.getInt("type"));
 160  0
         film.setYear(result.getInt("annee"));
 161  0
         film.setDuration(result.getInt("duree"));
 162  0
         film.getTemporaryContext().setRealizer(result.getInt("realisateur"));
 163  0
         film.getTemporaryContext().setLanguage(result.getInt("langue"));
 164  0
         film.setNote(notesService.getDefaultNote());
 165  0
     }
 166  
 
 167  
     /**
 168  
      * Import the actors of the film.
 169  
      *
 170  
      * @param connection The connection to get information from.
 171  
      * @param film       The film to get the informations for.
 172  
      * @throws DatabaseException If a database exception occurs during JDBC operations.
 173  
      */
 174  
     private static void importActorsOfFilm(Connection connection, Film film) throws DatabaseException {
 175  0
         PreparedStatement statement2 = null;
 176  0
         ResultSet resultActors = null;
 177  
         try {
 178  0
             statement2 = connection.prepareStatement("SELECT * FROM film_acteur WHERE ID_film = ?");
 179  
 
 180  0
             statement2.setInt(1, film.getTemporaryContext().getId());
 181  
 
 182  0
             resultActors = statement2.executeQuery();
 183  
 
 184  0
             film.getTemporaryContext().setActors(DatabaseUtils.getAllIntResults(resultActors, "ID_acteur"));
 185  0
         } catch (SQLException e) {
 186  0
             throw new DatabaseException(e);
 187  
         } finally {
 188  0
             DatabaseUtils.close(resultActors);
 189  0
             DatabaseUtils.close(statement2);
 190  0
         }
 191  0
     }
 192  
 
 193  
     /**
 194  
      * Import the actors.
 195  
      *
 196  
      * @param connection The connection.
 197  
      * @return The imported actors.
 198  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 199  
      */
 200  
     private Collection<Person> importActors(Connection connection) throws DatabaseException {
 201  0
         Collection<Person> actors = new ArrayList<Person>(50);
 202  
 
 203  0
         Statement statement = null;
 204  0
         ResultSet result = null;
 205  
         try {
 206  0
             statement = connection.createStatement();
 207  0
             result = statement.executeQuery("SELECT * FROM acteur");
 208  
 
 209  0
             while (result.next()) {
 210  0
                 Person actor = actorService.getEmptyActor();
 211  
 
 212  0
                 actor.getTemporaryContext().setId(result.getInt("ID"));
 213  0
                 actor.setName(result.getString("nom"));
 214  0
                 actor.setFirstName(result.getString("prenom"));
 215  0
                 actor.getTemporaryContext().setCountry(result.getInt("pays"));
 216  0
                 actor.setNote(notesService.getDefaultNote());
 217  
 
 218  0
                 actors.add(actor);
 219  0
             }
 220  0
         } catch (SQLException e) {
 221  0
             throw new DatabaseException(e);
 222  
         } finally {
 223  0
             DatabaseUtils.close(result);
 224  0
             DatabaseUtils.close(statement);
 225  0
         }
 226  
 
 227  0
         return actors;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Import the realizers.
 232  
      *
 233  
      * @param connection The connection.
 234  
      * @return The imported realizers.
 235  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 236  
      */
 237  
     private Collection<Person> importRealizers(Connection connection) throws DatabaseException {
 238  0
         Collection<Person> realizers = new ArrayList<Person>(25);
 239  
 
 240  0
         Statement statement = null;
 241  0
         ResultSet result = null;
 242  
         try {
 243  0
             statement = connection.createStatement();
 244  0
             result = statement.executeQuery("SELECT * FROM realisateur");
 245  
 
 246  0
             while (result.next()) {
 247  0
                 Person realizer = realizersService.getEmptyRealizer();
 248  
 
 249  0
                 realizer.getTemporaryContext().setId(result.getInt("ID"));
 250  0
                 realizer.setName(result.getString("nom"));
 251  0
                 realizer.setFirstName(result.getString("prenom"));
 252  0
                 realizer.getTemporaryContext().setCountry(result.getInt("pays"));
 253  0
                 realizer.setNote(notesService.getDefaultNote());
 254  
 
 255  0
                 realizers.add(realizer);
 256  0
             }
 257  0
         } catch (SQLException e) {
 258  0
             throw new DatabaseException(e);
 259  
         } finally {
 260  0
             DatabaseUtils.close(result);
 261  0
             DatabaseUtils.close(statement);
 262  0
         }
 263  
 
 264  0
         return realizers;
 265  
     }
 266  
 
 267  
     /**
 268  
      * Import the kinds.
 269  
      *
 270  
      * @param connection The connection.
 271  
      * @return The imported kinds.
 272  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 273  
      */
 274  
     private Collection<Kind> importKinds(Connection connection) throws DatabaseException {
 275  0
         Collection<Kind> kinds = new ArrayList<Kind>(10);
 276  
 
 277  0
         Statement statement = null;
 278  0
         ResultSet result = null;
 279  
         try {
 280  0
             statement = connection.createStatement();
 281  0
             result = statement.executeQuery("SELECT * FROM genre");
 282  
 
 283  0
             while (result.next()) {
 284  0
                 Kind kind = kindsService.getEmptyKind();
 285  
 
 286  0
                 kind.getTemporaryContext().setId(result.getInt("ID"));
 287  0
                 kind.setName(result.getString("nom"));
 288  
 
 289  0
                 kinds.add(kind);
 290  0
             }
 291  0
         } catch (SQLException e) {
 292  0
             throw new DatabaseException(e);
 293  
         } finally {
 294  0
             DatabaseUtils.close(result);
 295  0
             DatabaseUtils.close(statement);
 296  0
         }
 297  
 
 298  0
         return kinds;
 299  
     }
 300  
 
 301  
     /**
 302  
      * Import the types.
 303  
      *
 304  
      * @param connection The connection.
 305  
      * @return The imported types.
 306  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 307  
      */
 308  
     private Collection<Type> importTypes(Connection connection) throws DatabaseException {
 309  0
         Collection<Type> types = new ArrayList<Type>(10);
 310  
 
 311  0
         Statement statement = null;
 312  0
         ResultSet result = null;
 313  
         try {
 314  0
             statement = connection.createStatement();
 315  0
             result = statement.executeQuery("SELECT * FROM type");
 316  
 
 317  0
             while (result.next()) {
 318  0
                 Type type = typesService.getEmptyType();
 319  
 
 320  0
                 type.getTemporaryContext().setId(result.getInt("ID"));
 321  0
                 type.setName(result.getString("nom"));
 322  
 
 323  0
                 types.add(type);
 324  0
             }
 325  0
         } catch (SQLException e) {
 326  0
             throw new DatabaseException(e);
 327  
         } finally {
 328  0
             DatabaseUtils.close(result);
 329  0
             DatabaseUtils.close(statement);
 330  0
         }
 331  
 
 332  0
         return types;
 333  
     }
 334  
 
 335  
     /**
 336  
      * Import the languages.
 337  
      *
 338  
      * @param connection The connection.
 339  
      * @return The imported languages.
 340  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 341  
      */
 342  
     private Collection<Language> importLanguages(Connection connection) throws DatabaseException {
 343  0
         Collection<Language> languages = new ArrayList<Language>(10);
 344  
 
 345  0
         Statement statement = null;
 346  0
         ResultSet result = null;
 347  
         try {
 348  0
             statement = connection.createStatement();
 349  0
             result = statement.executeQuery("SELECT * FROM langue");
 350  
 
 351  0
             while (result.next()) {
 352  0
                 Language language = languagesService.getEmptyLanguage();
 353  
 
 354  0
                 language.getTemporaryContext().setId(result.getInt("ID"));
 355  0
                 language.setName(result.getString("nom"));
 356  
 
 357  0
                 languages.add(language);
 358  0
             }
 359  0
         } catch (SQLException e) {
 360  0
             throw new DatabaseException(e);
 361  
         } finally {
 362  0
             DatabaseUtils.close(result);
 363  0
             DatabaseUtils.close(statement);
 364  0
         }
 365  
 
 366  0
         return languages;
 367  
     }
 368  
 
 369  
     /**
 370  
      * Import the countries.
 371  
      *
 372  
      * @param connection The connection.
 373  
      * @return The imported countries.
 374  
      * @throws DatabaseException Thrown when an error occurs during the database data getting process.
 375  
      */
 376  
     private Collection<Country> importCountries(Connection connection) throws DatabaseException {
 377  0
         Collection<Country> countries = new ArrayList<Country>(20);
 378  
 
 379  0
         Statement statement = null;
 380  0
         ResultSet result = null;
 381  
         try {
 382  0
             statement = connection.createStatement();
 383  0
             result = statement.executeQuery("SELECT * FROM pays");
 384  
 
 385  0
             while (result.next()) {
 386  0
                 Country country = countriesService.getEmptyCountry();
 387  
 
 388  0
                 country.getTemporaryContext().setId(result.getInt("ID"));
 389  0
                 country.setName(result.getString("nom"));
 390  
 
 391  0
                 countries.add(country);
 392  0
             }
 393  0
         } catch (SQLException e) {
 394  0
             throw new DatabaseException(e);
 395  
         } finally {
 396  0
             DatabaseUtils.close(result);
 397  0
             DatabaseUtils.close(statement);
 398  0
         }
 399  
 
 400  0
         return countries;
 401  
     }
 402  
 }