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