Coverage Report - org.jtheque.films.persistence.dao.impl.DaoActors
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoActors
0 %
0/23
0 %
0/8
1.154
DaoActors$1
N/A
N/A
1.154
DaoActors$ActorQueryMapper
0 %
0/9
N/A
1.154
DaoActors$ActorRowMapper
0 %
0/8
N/A
1.154
 
 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.persistence.Entity;
 20  
 import org.jtheque.core.managers.persistence.GenericDao;
 21  
 import org.jtheque.core.managers.persistence.Query;
 22  
 import org.jtheque.core.managers.persistence.QueryMapper;
 23  
 import org.jtheque.core.managers.persistence.context.IDaoPersistenceContext;
 24  
 import org.jtheque.core.utils.db.DaoNotes;
 25  
 import org.jtheque.core.utils.db.DaoNotes.NoteType;
 26  
 import org.jtheque.films.persistence.dao.able.IDaoActors;
 27  
 import org.jtheque.films.persistence.od.ActorImpl;
 28  
 import org.jtheque.primary.dao.able.IDaoCountries;
 29  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 30  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 31  
 
 32  
 import javax.annotation.Resource;
 33  
 import java.sql.ResultSet;
 34  
 import java.sql.SQLException;
 35  
 import java.util.List;
 36  
 
 37  
 /**
 38  
  * Cette classe permet de g�rer la persistance des acteurs.
 39  
  *
 40  
  * @author Baptiste Wicht
 41  
  */
 42  0
 public final class DaoActors extends GenericDao<ActorImpl> implements IDaoActors {
 43  0
     private final ParameterizedRowMapper<ActorImpl> rowMapper = new ActorRowMapper();
 44  0
     private final QueryMapper queryMapper = new ActorQueryMapper();
 45  
 
 46  
     @Resource
 47  
     private IDaoPersistenceContext persistenceContext;
 48  
 
 49  
     @Resource
 50  
     private IDaoCountries daoCountries;
 51  
 
 52  
     @Resource
 53  
     private SimpleJdbcTemplate jdbcTemplate;
 54  
 
 55  
     /**
 56  
      * Construct a new DaoActors. 
 57  
      *
 58  
      */
 59  
     public DaoActors() {
 60  0
         super(TABLE);
 61  0
     }
 62  
 
 63  
     @Override
 64  
     public List<ActorImpl> getActors() {
 65  0
         return getAll();
 66  
     }
 67  
 
 68  
     @Override
 69  
     public ActorImpl getActor(String firstName, String name) {
 70  0
         ActorImpl actor = jdbcTemplate.queryForObject("SELECT * FROM " + TABLE + " WHERE NAME = ? AND FIRSTNAME = ?", rowMapper, name, firstName);
 71  
 
 72  0
         if (isNotInCache(actor.getId())) {
 73  0
             getCache().put(actor.getId(), actor);
 74  
         }
 75  
 
 76  0
         return getCache().get(actor.getId());
 77  
     }
 78  
 
 79  
     @Override
 80  
     public boolean exists(String firstName, String name) {
 81  0
         return getActor(firstName, name) != null;
 82  
     }
 83  
 
 84  
     @Override
 85  
     public boolean exist(ActorImpl actor) {
 86  0
         return getActor(actor.getFirstName(), actor.getName()) != null;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public ActorImpl getActor(int id) {
 91  0
         return get(id);
 92  
     }
 93  
 
 94  
     @Override
 95  
     protected ParameterizedRowMapper<ActorImpl> getRowMapper() {
 96  0
         return rowMapper;
 97  
     }
 98  
 
 99  
     @Override
 100  
     protected QueryMapper getQueryMapper() {
 101  0
         return queryMapper;
 102  
     }
 103  
 
 104  
     @Override
 105  
     protected void loadCache() {
 106  0
         List<ActorImpl> actors = persistenceContext.getSortedList(TABLE, rowMapper);
 107  
 
 108  0
         for (ActorImpl actor : actors) {
 109  0
             getCache().put(actor.getId(), actor);
 110  
         }
 111  
 
 112  0
         setCacheEntirelyLoaded(true);
 113  0
     }
 114  
 
 115  
     @Override
 116  
     protected void load(int i) {
 117  0
         ActorImpl actor = persistenceContext.getDataByID(TABLE, i, rowMapper);
 118  
 
 119  0
         getCache().put(i, actor);
 120  0
     }
 121  
 
 122  
     /**
 123  
      * A row mapper to map resultset to actor.
 124  
      *
 125  
      * @author Baptiste Wicht
 126  
      */
 127  0
     private final class ActorRowMapper implements ParameterizedRowMapper<ActorImpl> {
 128  
         @Override
 129  
         public ActorImpl mapRow(ResultSet rs, int i) throws SQLException {
 130  0
             ActorImpl actor = new ActorImpl();
 131  
 
 132  0
             actor.setId(rs.getInt("ID"));
 133  0
             actor.setName(rs.getString("NAME"));
 134  0
             actor.setFirstName(rs.getString("FIRSTNAME"));
 135  0
             actor.setTheCountry(daoCountries.getCountry(rs.getInt("THE_COUNTRY_FK")));
 136  0
             actor.setNote(DaoNotes.getINSTANCE().getNote(NoteType.getEnum(rs.getInt("NOTE"))));
 137  
 
 138  0
             return actor;
 139  
         }
 140  
     }
 141  
 
 142  
     /**
 143  
      * A query mapper to map actor to sql query. 
 144  
      *
 145  
      * @author Baptiste Wicht
 146  
      */
 147  0
     private static final class ActorQueryMapper implements QueryMapper {
 148  
         @Override
 149  
         public Query constructInsertQuery(Entity entity) {
 150  0
             ActorImpl actor = (ActorImpl) entity;
 151  
 
 152  0
             String query = "INSERT INTO " + TABLE + " (NAME, FIRSTNAME, THE_COUNTRY_FK, NOTE) VALUES(?,?,?,?)";
 153  
 
 154  0
             Object[] parameters = {
 155  
                     actor.getName(),
 156  
                     actor.getFirstName(),
 157  
                     actor.getTheCountry().getId(),
 158  
                     actor.getNote().getValue().intValue()
 159  
             };
 160  
 
 161  0
             return new Query(query, parameters);
 162  
         }
 163  
 
 164  
         @Override
 165  
         public Query constructUpdateQuery(Entity entity) {
 166  0
             ActorImpl actor = (ActorImpl) entity;
 167  
 
 168  0
             String query = "UPDATE " + TABLE + " SET NAME = ?, FIRSTNAME = ?, THE_COUNTRY_FK = ?, NOTE = ? WHERE ID = ?";
 169  
 
 170  0
             Object[] parameters = {
 171  
                     actor.getName(),
 172  
                     actor.getFirstName(),
 173  
                     actor.getTheCountry().getId(),
 174  
                     actor.getNote().getValue().intValue(),
 175  
                     actor.getId()};
 176  
 
 177  0
             return new Query(query, parameters);
 178  
         }
 179  
     }
 180  
 }