Coverage Report - org.jtheque.films.persistence.dao.impl.DaoFilms
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoFilms
0 %
0/55
0 %
0/16
2.409
DaoFilms$1
N/A
N/A
2.409
DaoFilms$FilmQueryMapper
0 %
0/9
0 %
0/20
2.409
DaoFilms$FilmRowMapper
0 %
0/48
0 %
0/24
2.409
DaoFilms$RelationActorRowMapper
0 %
0/5
N/A
2.409
DaoFilms$RelationKindRowMapper
0 %
0/5
N/A
2.409
 
 1  
 package org.jtheque.films.persistence.dao.impl;
 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.log.ILoggingManager;
 21  
 import org.jtheque.core.managers.persistence.GenericDao;
 22  
 import org.jtheque.core.managers.persistence.Query;
 23  
 import org.jtheque.core.managers.persistence.QueryMapper;
 24  
 import org.jtheque.core.managers.persistence.able.Entity;
 25  
 import org.jtheque.core.managers.persistence.context.IDaoPersistenceContext;
 26  
 import org.jtheque.core.utils.db.DaoNotes;
 27  
 import org.jtheque.core.utils.db.DaoNotes.NoteType;
 28  
 import org.jtheque.films.persistence.dao.able.IDaoFilms;
 29  
 import org.jtheque.films.persistence.od.able.Film;
 30  
 import org.jtheque.films.persistence.od.impl.FilmActorRelation;
 31  
 import org.jtheque.films.persistence.od.impl.FilmImpl;
 32  
 import org.jtheque.films.persistence.od.impl.FilmKindRelation;
 33  
 import org.jtheque.primary.dao.able.IDaoCollections;
 34  
 import org.jtheque.primary.dao.able.IDaoKinds;
 35  
 import org.jtheque.primary.dao.able.IDaoLanguages;
 36  
 import org.jtheque.primary.dao.able.IDaoLendings;
 37  
 import org.jtheque.primary.dao.able.IDaoPersons;
 38  
 import org.jtheque.primary.dao.able.IDaoSagas;
 39  
 import org.jtheque.primary.dao.able.IDaoTypes;
 40  
 import org.jtheque.primary.od.able.Collection;
 41  
 import org.jtheque.primary.od.able.Kind;
 42  
 import org.jtheque.primary.od.able.Person;
 43  
 import org.jtheque.utils.collections.CollectionUtils;
 44  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 45  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 46  
 
 47  
 import javax.annotation.Resource;
 48  
 import java.sql.ResultSet;
 49  
 import java.sql.SQLException;
 50  
 import java.util.ArrayList;
 51  
 import java.util.Collections;
 52  
 import java.util.List;
 53  
 
 54  
 /**
 55  
  * A DAO implementation for films.
 56  
  *
 57  
  * @author Baptiste Wicht
 58  
  */
 59  0
 public final class DaoFilms extends GenericDao<Film> implements IDaoFilms {
 60  0
     private final ParameterizedRowMapper<Film> rowMapper = new FilmRowMapper();
 61  0
     private final ParameterizedRowMapper<FilmKindRelation> relationKindRowMapper = new RelationKindRowMapper();
 62  0
     private final ParameterizedRowMapper<FilmActorRelation> relationActorRowMapper = new RelationActorRowMapper();
 63  0
     private final QueryMapper queryMapper = new FilmQueryMapper();
 64  
 
 65  
     private java.util.Collection<FilmActorRelation> relationsToActors;
 66  
     private java.util.Collection<FilmKindRelation> relationsToKinds;
 67  
 
 68  
     @Resource
 69  
     private IDaoPersistenceContext persistenceContext;
 70  
 
 71  
     @Resource
 72  
     private SimpleJdbcTemplate jdbcTemplate;
 73  
 
 74  
     @Resource
 75  
     private IDaoCollections daoCollections;
 76  
 
 77  
     @Resource
 78  
     private IDaoKinds daoKinds;
 79  
 
 80  
     @Resource
 81  
     private IDaoTypes daoTypes;
 82  
 
 83  
     @Resource
 84  
     private IDaoSagas daoSagas;
 85  
 
 86  
     @Resource
 87  
     private IDaoLendings daoLendings;
 88  
 
 89  
     @Resource
 90  
     private IDaoPersons daoPersons;
 91  
 
 92  
     @Resource
 93  
     private IDaoLanguages daoLanguages;
 94  
 
 95  
     /**
 96  
      * Construct a new DaoFilms.
 97  
      */
 98  
     public DaoFilms() {
 99  0
         super(TABLE);
 100  0
     }
 101  
 
 102  
     @Override
 103  
     @SuppressWarnings("unchecked")
 104  
     public java.util.Collection<Film> getFilms() {
 105  0
         List<Film> films = CollectionUtils.copyOf(getFilms(daoCollections.getCurrentCollection()));
 106  
 
 107  0
         Collections.sort(films);
 108  
 
 109  0
         return films;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Return all the films of the collection.
 114  
      *
 115  
      * @param collection The collection.
 116  
      * @return A List containing all the films of the collections.
 117  
      */
 118  
     private java.util.Collection<Film> getFilms(Collection collection) {
 119  0
         if (collection == null || !collection.isSaved()) {
 120  0
             return getAll();
 121  
         }
 122  
 
 123  0
         load();
 124  
 
 125  0
         java.util.Collection<Film> films = new ArrayList<Film>(getCache().size() / 2);
 126  
 
 127  0
         for (Film film : getCache().values()) {
 128  0
             if (film.getTheCollection().getId() == collection.getId()) {
 129  0
                 films.add(film);
 130  
             }
 131  
         }
 132  
 
 133  0
         return films;
 134  
     }
 135  
 
 136  
     @Override
 137  
     public Film getFilm(int id) {
 138  0
         return get(id);
 139  
     }
 140  
 
 141  
     @Override
 142  
     public void createAll(Iterable<Film> films) {
 143  0
         for (Film film : films) {
 144  0
             create(film);
 145  
         }
 146  0
     }
 147  
 
 148  
     @Override
 149  
     public Film createFilm() {
 150  0
         return new FilmImpl();
 151  
     }
 152  
 
 153  
     @Override
 154  
     public boolean delete(Film film) {
 155  0
         boolean deleted = super.delete(film);
 156  
 
 157  0
         jdbcTemplate.update("DELETE FROM " + ACTORS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", film.getId());
 158  0
         jdbcTemplate.update("DELETE FROM " + KINDS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", film.getId());
 159  
 
 160  0
         return deleted;
 161  
     }
 162  
 
 163  
     @Override
 164  
     public void save(Film film) {
 165  0
         super.save(film);
 166  
 
 167  0
         jdbcTemplate.update("DELETE FROM " + ACTORS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", film.getId());
 168  0
         jdbcTemplate.update("DELETE FROM " + KINDS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", film.getId());
 169  
 
 170  0
         updateLinksBetweenTable(film);
 171  0
     }
 172  
 
 173  
     @Override
 174  
     public void create(Film film) {
 175  0
         film.setTheCollection(daoCollections.getCurrentCollection());
 176  
 
 177  0
         super.create(film);
 178  
 
 179  0
         updateLinksBetweenTable(film);
 180  0
     }
 181  
 
 182  
     /**
 183  
      * Update the links between table.
 184  
      *
 185  
      * @param film The film.
 186  
      */
 187  
     private void updateLinksBetweenTable(Film film) {
 188  0
         for (Person actor : film.getActors()) {
 189  0
             jdbcTemplate.update("INSERT INTO " + ACTORS_FILMS_TABLE + " (THE_FILM_FK, THE_ACTOR_FK) VALUES(?,?)", film.getId(), actor.getId());
 190  
         }
 191  
 
 192  0
         for (Kind kind : film.getKinds()) {
 193  0
             jdbcTemplate.update("INSERT INTO " + KINDS_FILMS_TABLE + " (THE_FILM_FK, THE_KIND_FK) VALUES(?,?)", film.getId(), kind.getId());
 194  
         }
 195  0
     }
 196  
 
 197  
     @Override
 198  
     protected ParameterizedRowMapper<Film> getRowMapper() {
 199  0
         return rowMapper;
 200  
     }
 201  
 
 202  
     @Override
 203  
     protected QueryMapper getQueryMapper() {
 204  0
         return queryMapper;
 205  
     }
 206  
 
 207  
     @Override
 208  
     protected void loadCache() {
 209  0
         relationsToActors = jdbcTemplate.query("SELECT * FROM " + ACTORS_FILMS_TABLE, relationActorRowMapper);
 210  0
         relationsToKinds = jdbcTemplate.query("SELECT * FROM " + KINDS_FILMS_TABLE, relationKindRowMapper);
 211  
 
 212  0
         java.util.Collection<Film> movies = persistenceContext.getSortedList(TABLE, rowMapper);
 213  
 
 214  0
         for (Film film : movies) {
 215  0
             getCache().put(film.getId(), film);
 216  
         }
 217  
 
 218  0
         setCacheEntirelyLoaded();
 219  
 
 220  0
         relationsToActors.clear();
 221  0
         relationsToKinds.clear();
 222  0
     }
 223  
 
 224  
     @Override
 225  
     protected void load(int i) {
 226  0
         Film film = persistenceContext.getDataByID(TABLE, i, rowMapper);
 227  
 
 228  0
         getCache().put(i, film);
 229  0
     }
 230  
 
 231  
     /**
 232  
      * A row mapper to map resultset to relation between film and kind.
 233  
      *
 234  
      * @author Baptiste Wicht
 235  
      */
 236  0
     private static final class RelationKindRowMapper implements ParameterizedRowMapper<FilmKindRelation> {
 237  
         @Override
 238  
         public FilmKindRelation mapRow(ResultSet rs, int i) throws SQLException {
 239  0
             FilmKindRelation relation = new FilmKindRelation();
 240  
 
 241  0
             relation.setTheFilm(rs.getInt("THE_FILM_FK"));
 242  0
             relation.setTheKind(rs.getInt("THE_KIND_FK"));
 243  
 
 244  0
             return relation;
 245  
         }
 246  
     }
 247  
 
 248  
     /**
 249  
      * A row mapper to map resultset to relation between film and actor.
 250  
      *
 251  
      * @author Baptiste Wicht
 252  
      */
 253  0
     private static final class RelationActorRowMapper implements ParameterizedRowMapper<FilmActorRelation> {
 254  
         @Override
 255  
         public FilmActorRelation mapRow(ResultSet rs, int i) throws SQLException {
 256  0
             FilmActorRelation relation = new FilmActorRelation();
 257  
 
 258  0
             relation.setTheFilm(rs.getInt("THE_FILM_FK"));
 259  0
             relation.setTheActor(rs.getInt("THE_ACTOR_FK"));
 260  
 
 261  0
             return relation;
 262  
         }
 263  
     }
 264  
 
 265  
     /**
 266  
      * A row mapper to map resultset to film.
 267  
      *
 268  
      * @author Baptiste Wicht
 269  
      */
 270  0
     private final class FilmRowMapper implements ParameterizedRowMapper<Film> {
 271  
         @Override
 272  
         public Film mapRow(ResultSet rs, int i) throws SQLException {
 273  0
             Film film = createFilm();
 274  
 
 275  0
             film.setId(rs.getInt("ID"));
 276  0
             film.setTitle(rs.getString("TITLE"));
 277  0
             film.setYear(rs.getInt("YEAR"));
 278  0
             film.setNote(DaoNotes.getInstance().getNote(NoteType.getEnum(rs.getInt("NOTE"))));
 279  0
             film.setComment(rs.getString("COMMENT"));
 280  0
             film.setDuration(rs.getInt("DURATION"));
 281  0
             film.setFilePath(rs.getString("FILEPATH"));
 282  0
             film.setImage(rs.getString("IMAGE"));
 283  0
             film.setResume(rs.getString("RESUME"));
 284  0
             film.setTheCollection(daoCollections.getCollection(rs.getInt("THE_COLLECTION_FK")));
 285  0
             film.setTheLanguage(daoLanguages.getLanguage(rs.getInt("THE_LANGUAGE_FK")));
 286  0
             film.setTheLending(daoLendings.getLending(rs.getInt("THE_LENDING_FK")));
 287  0
             film.setTheRealizer(daoPersons.getPerson(rs.getInt("THE_REALIZER_FK")));
 288  0
             film.setTheSaga(daoSagas.getSaga(rs.getInt("THE_SAGA_FK")));
 289  0
             film.setTheType(daoTypes.getType(rs.getInt("THE_TYPE_FK")));
 290  
 
 291  0
             mapRelations(film);
 292  
 
 293  0
             return film;
 294  
         }
 295  
 
 296  
         /**
 297  
          * Map the relations.
 298  
          *
 299  
          * @param film The film to map the relations for.
 300  
          */
 301  
         private void mapRelations(Film film) {
 302  0
             mapKindsRelations(film);
 303  0
             mapActorsRelations(film);
 304  0
         }
 305  
 
 306  
         /**
 307  
          * Map the kind relations.
 308  
          *
 309  
          * @param film The film to map the relations for.
 310  
          */
 311  
         private void mapKindsRelations(Film film) {
 312  0
             if (relationsToKinds != null && !relationsToKinds.isEmpty()) {
 313  0
                 for (FilmKindRelation relation : relationsToKinds) {
 314  0
                     if (relation.getTheFilm() == film.getId()) {
 315  0
                         film.addKind(daoKinds.getKind(relation.getTheKind()));
 316  
                     }
 317  
                 }
 318  
             } else {
 319  0
                 relationsToKinds = jdbcTemplate.query("SELECT * FROM " + KINDS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", relationKindRowMapper, film.getId());
 320  
 
 321  0
                 for (FilmKindRelation relation : relationsToKinds) {
 322  0
                     film.addKind(daoKinds.getKind(relation.getTheKind()));
 323  
                 }
 324  
 
 325  0
                 relationsToKinds.clear();
 326  
             }
 327  0
         }
 328  
 
 329  
         /**
 330  
          * Map the actor relations.
 331  
          *
 332  
          * @param film The film to map the relations for.
 333  
          */
 334  
         private void mapActorsRelations(Film film) {
 335  0
             if (relationsToActors != null && !relationsToActors.isEmpty()) {
 336  0
                 for (FilmActorRelation relation : relationsToActors) {
 337  0
                     if (relation.getTheFilm() == film.getId()) {
 338  0
                         Person actor = daoPersons.getPerson(relation.getTheActor());
 339  
 
 340  0
                         if (actor == null) {
 341  0
                             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error("Film ({}) references a null actor id = {}",
 342  
                                     film.getId(), relation.getTheActor());
 343  
                         } else {
 344  0
                             film.addActor(actor);
 345  
                         }
 346  0
                     }
 347  
                 }
 348  
             } else {
 349  0
                 relationsToActors = jdbcTemplate.query("SELECT * FROM " + ACTORS_FILMS_TABLE + " WHERE THE_FILM_FK = ?", relationActorRowMapper, film.getId());
 350  
 
 351  0
                 for (FilmActorRelation relation : relationsToActors) {
 352  0
                     Person actor = daoPersons.getPerson(relation.getTheActor());
 353  
 
 354  0
                     if (actor == null) {
 355  0
                         Managers.getManager(ILoggingManager.class).getLogger(getClass()).error("Film ({}) references a null actor id = {}",
 356  
                                 film.getId(), relation.getTheActor());
 357  
                     } else {
 358  0
                         film.addActor(actor);
 359  
                     }
 360  0
                 }
 361  
 
 362  0
                 relationsToActors.clear();
 363  
             }
 364  0
         }
 365  
     }
 366  
 
 367  
     /**
 368  
      * A query mapper to map film to sql query.
 369  
      *
 370  
      * @author Baptiste Wicht
 371  
      */
 372  0
     private static final class FilmQueryMapper implements QueryMapper {
 373  
         @Override
 374  
         public Query constructInsertQuery(Entity entity) {
 375  0
             Film film = (Film) entity;
 376  
 
 377  0
             String query = "INSERT INTO " + TABLE + " (TITLE, NOTE, COMMENT, DURATION, FILEPATH, IMAGE, RESUME, THE_COLLECTION_FK, THE_LANGUAGE_FK, THE_LENDING_FK, " +
 378  
                     "THE_REALIZER_FK, THE_SAGA_FK, THE_TYPE_FK) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
 379  
 
 380  0
             Object[] parameters = {
 381  
                     film.getTitle(),
 382  
                     film.getNote().getValue().intValue(),
 383  
                     film.getComment(),
 384  
                     film.getDuration(),
 385  
                     film.getFilePath(),
 386  
                     film.getImage(),
 387  
                     film.getResume(),
 388  
                     film.getTheCollection().getId(),
 389  
                     film.getTheLanguage() == null ? null : film.getTheLanguage().getId(),
 390  
                     film.getTheLending() == null ? null : film.getTheLending().getId(),
 391  
                     film.getTheRealizer() == null ? null : film.getTheRealizer().getId(),
 392  
                     film.getTheSaga() == null ? null : film.getTheSaga().getId(),
 393  
                     film.getTheType() == null ? null : film.getTheType().getId(),
 394  
             };
 395  
 
 396  0
             return new Query(query, parameters);
 397  
         }
 398  
 
 399  
         @Override
 400  
         public Query constructUpdateQuery(Entity entity) {
 401  0
             Film film = (Film) entity;
 402  
 
 403  0
             String query = "UPDATE " + TABLE + " SET TITLE = ?, NOTE = ?, COMMENT = ?, DURATION = ?, FILEPATH = ?, IMAGE = ?, RESUME = ?, THE_COLLECTION_FK = ?, " +
 404  
                     "THE_LANGUAGE_FK = ?, THE_LENDING_FK = ?, THE_REALIZER_FK = ?, THE_SAGA_FK = ?, THE_TYPE_FK = ? WHERE ID = ?";
 405  
 
 406  0
             Object[] parameters = {
 407  
                     film.getTitle(),
 408  
                     film.getNote().getValue().intValue(),
 409  
                     film.getComment(),
 410  
                     film.getDuration(),
 411  
                     film.getFilePath(),
 412  
                     film.getImage(),
 413  
                     film.getResume(),
 414  
                     film.getTheCollection().getId(),
 415  
                     film.getTheLanguage() == null ? null : film.getTheLanguage().getId(),
 416  
                     film.getTheLending() == null ? null : film.getTheLending().getId(),
 417  
                     film.getTheRealizer() == null ? null : film.getTheRealizer().getId(),
 418  
                     film.getTheSaga() == null ? null : film.getTheSaga().getId(),
 419  
                     film.getTheType() == null ? null : film.getTheType().getId(),
 420  
                     film.getId()
 421  
             };
 422  
 
 423  0
             return new Query(query, parameters);
 424  
         }
 425  
     }
 426  
 }