Coverage Report - org.jtheque.primary.dao.impl.DaoSimpleDatas
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoSimpleDatas
0 %
0/48
0 %
0/28
2.118
DaoSimpleDatas$1
N/A
N/A
2.118
DaoSimpleDatas$PrimarySimpleDataQueryMapper
0 %
0/9
N/A
2.118
DaoSimpleDatas$SimpleDataQueryMapper
0 %
0/9
N/A
2.118
DaoSimpleDatas$SimpleDataRowMapper
0 %
0/7
0 %
0/2
2.118
 
 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.PrimaryUtils;
 25  
 import org.jtheque.primary.dao.able.IDaoSimpleDatas;
 26  
 import org.jtheque.primary.od.able.PrimaryData;
 27  
 import org.jtheque.primary.od.able.PrimarySimpleData;
 28  
 import org.jtheque.primary.od.able.SimpleData;
 29  
 import org.jtheque.primary.od.able.SimpleData.DataType;
 30  
 import org.jtheque.primary.od.impl.PrimarySimpleDataImpl;
 31  
 import org.jtheque.primary.od.impl.SimpleDataImpl;
 32  
 import org.jtheque.utils.StringUtils;
 33  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 34  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 35  
 
 36  
 import javax.annotation.Resource;
 37  
 import java.sql.ResultSet;
 38  
 import java.sql.SQLException;
 39  
 import java.util.ArrayList;
 40  
 import java.util.Collection;
 41  
 import java.util.List;
 42  
 
 43  
 /**
 44  
  * A Data Access Object implementation for countries.
 45  
  *
 46  
  * @author Baptiste Wicht
 47  
  */
 48  0
 public final class DaoSimpleDatas extends GenericDao<SimpleData> implements IDaoSimpleDatas {
 49  0
         private final ParameterizedRowMapper<SimpleData> rowMapper = new SimpleDataRowMapper();
 50  
 
 51  
         private final QueryMapper queryMapper;
 52  
 
 53  
         @Resource
 54  
         private IDaoPersistenceContext daoPersistenceContext;
 55  
 
 56  
         @Resource
 57  
         private SimpleJdbcTemplate jdbcTemplate;
 58  
 
 59  
         private final DataType dataType;
 60  
 
 61  
         /**
 62  
          * Create a new DaoSimpleDatas.
 63  
          *
 64  
          * @param dataTypeStr The data type to manage.
 65  
          */
 66  
         public DaoSimpleDatas(String dataTypeStr){
 67  0
                 super(DataType.valueOf(dataTypeStr).getTable());
 68  
 
 69  0
                 dataType = DataType.valueOf(dataTypeStr);
 70  
 
 71  0
         queryMapper = dataType.isPrimary() ? new PrimarySimpleDataQueryMapper() : new SimpleDataQueryMapper();
 72  0
         }
 73  
 
 74  
         @Override
 75  
         public Collection<SimpleData> getSimpleDatas(){
 76  0
                 if (dataType.isPrimary()){
 77  0
                         return getAll(PrimaryUtils.getPrimaryImpl());
 78  
                 }
 79  
 
 80  0
                 return getAll();
 81  
         }
 82  
 
 83  
         /**
 84  
          * Return all the simple datas of the primary impl.
 85  
          *
 86  
          * @param impl The primary implementation.
 87  
          *
 88  
          * @return A Collection containing all the simple datas of the primary implementation.
 89  
          */
 90  
         private Collection<SimpleData> getAll(CharSequence impl){
 91  0
                 if (StringUtils.isEmpty(impl) || dataType.isPrimary()){
 92  0
                         return getAll();
 93  
                 }
 94  
 
 95  0
                 load();
 96  
 
 97  0
                 Collection<SimpleData> simpleDatas = new ArrayList<SimpleData>(getCache().size() / 2);
 98  
 
 99  0
                 for (SimpleData simpleData : getCache().values()){
 100  0
                         PrimarySimpleData primarySimpleData = (PrimarySimpleData) simpleData;
 101  
 
 102  0
                         if (impl.equals(primarySimpleData.getPrimaryImpl())){
 103  0
                                 simpleDatas.add(primarySimpleData);
 104  
                         }
 105  0
                 }
 106  
 
 107  0
                 return simpleDatas;
 108  
         }
 109  
 
 110  
         @Override
 111  
         public SimpleData getSimpleData(int id){
 112  0
                 return get(id);
 113  
         }
 114  
 
 115  
         @Override
 116  
         public SimpleData getSimpleData(String name){
 117  0
                 if (dataType.isPrimary()){
 118  0
                         return getSimplePrimaryData(name);
 119  
                 }
 120  
 
 121  0
                 List<SimpleData> simpleDatas = jdbcTemplate.query("SELECT * FROM " + dataType.getTable() + " WHERE NAME = ?",
 122  
                                 rowMapper, name);
 123  
 
 124  0
                 if (simpleDatas.isEmpty()){
 125  0
                         return null;
 126  
                 }
 127  
 
 128  0
                 SimpleData simpleData = simpleDatas.get(0);
 129  
 
 130  0
                 if (isNotInCache(simpleData.getId())){
 131  0
                         getCache().put(simpleData.getId(), simpleData);
 132  
                 }
 133  
 
 134  0
                 return getCache().get(simpleData.getId());
 135  
         }
 136  
 
 137  
         /**
 138  
          * Return the simple primary data of the specified name.
 139  
          *
 140  
          * @param name The name of the primary data to search.
 141  
          *
 142  
          * @return The simple primary data.
 143  
          */
 144  
         private SimpleData getSimplePrimaryData(String name){
 145  0
                 List<SimpleData> types = jdbcTemplate.query("SELECT * FROM " + dataType.getTable() + " WHERE NAME = ? AND IMPL = ?",
 146  
                                 rowMapper, name, PrimaryUtils.getPrimaryImpl());
 147  
 
 148  0
                 if (types.isEmpty()){
 149  0
                         return null;
 150  
                 }
 151  
 
 152  0
                 SimpleData type = types.get(0);
 153  
 
 154  0
                 if (isNotInCache(type.getId())){
 155  0
                         getCache().put(type.getId(), type);
 156  
                 }
 157  
 
 158  0
                 return getCache().get(type.getId());
 159  
         }
 160  
 
 161  
         @Override
 162  
         public boolean exist(SimpleData simpleData){
 163  0
                 return getSimpleData(simpleData.getName()) != null;
 164  
         }
 165  
 
 166  
         @Override
 167  
         public SimpleData createSimpleData(){
 168  0
                 return dataType.isPrimary() ?
 169  
                 new PrimarySimpleDataImpl(dataType, PrimaryUtils.getPrimaryImpl()) :
 170  
                                 new SimpleDataImpl(dataType);
 171  
         }
 172  
 
 173  
         @Override
 174  
         protected void loadCache(){
 175  0
                 Collection<SimpleData> simpleDatas = daoPersistenceContext.getSortedList(dataType.getTable(), rowMapper);
 176  
 
 177  0
                 for (SimpleData simpleData : simpleDatas){
 178  0
                         getCache().put(simpleData.getId(), simpleData);
 179  
                 }
 180  
 
 181  0
                 setCacheEntirelyLoaded();
 182  0
         }
 183  
 
 184  
         @Override
 185  
         protected void load(int i){
 186  0
                 SimpleData country = daoPersistenceContext.getDataByID(dataType.getTable(), i, rowMapper);
 187  
 
 188  0
                 getCache().put(i, country);
 189  0
         }
 190  
 
 191  
         @Override
 192  
         protected ParameterizedRowMapper<SimpleData> getRowMapper(){
 193  0
                 return rowMapper;
 194  
         }
 195  
 
 196  
         @Override
 197  
         protected QueryMapper getQueryMapper(){
 198  0
                 return queryMapper;
 199  
         }
 200  
 
 201  
         /**
 202  
          * A row mapper to map resultset to SimpleData.
 203  
          *
 204  
          * @author Baptiste Wicht
 205  
          */
 206  0
         private final class SimpleDataRowMapper implements ParameterizedRowMapper<SimpleData> {
 207  
                 @Override
 208  
                 public SimpleData mapRow(ResultSet rs, int i) throws SQLException{
 209  
 
 210  0
                         SimpleData simpleData = createSimpleData();
 211  
 
 212  0
                         simpleData.setId(rs.getInt("ID"));
 213  0
                         simpleData.setName(rs.getString("NAME"));
 214  
 
 215  0
                         if (dataType.isPrimary()){
 216  0
                                 ((PrimaryData) simpleData).setPrimaryImpl(rs.getString("IMPL"));
 217  
                         }
 218  
 
 219  0
                         return simpleData;
 220  
                 }
 221  
         }
 222  
 
 223  
         /**
 224  
          * A query mapper to map SimpleData to query.
 225  
          *
 226  
          * @author Baptiste Wicht
 227  
          */
 228  0
         private final class SimpleDataQueryMapper implements QueryMapper {
 229  
                 @Override
 230  
                 public Query constructInsertQuery(Entity entity){
 231  0
                         SimpleData simpleData = (SimpleData) entity;
 232  
 
 233  0
                         String query = "INSERT INTO " + dataType.getTable() + " (NAME) VALUES(?)";
 234  
 
 235  0
                         Object[] parameters = {simpleData.getName()};
 236  
 
 237  0
                         return new Query(query, parameters);
 238  
                 }
 239  
 
 240  
                 @Override
 241  
                 public Query constructUpdateQuery(Entity entity){
 242  0
                         SimpleData simpleData = (SimpleData) entity;
 243  
 
 244  0
                         String query = "UPDATE " + dataType.getTable() + " SET NAME = ? WHERE ID = ?";
 245  
 
 246  0
                         Object[] parameters = {
 247  
                                         simpleData.getName(),
 248  
                                         simpleData.getId()};
 249  
 
 250  0
                         return new Query(query, parameters);
 251  
                 }
 252  
         }
 253  
 
 254  
         /**
 255  
          * A query mapper to map SimpleData to query.
 256  
          *
 257  
          * @author Baptiste Wicht
 258  
          */
 259  0
         private final class PrimarySimpleDataQueryMapper implements QueryMapper {
 260  
                 @Override
 261  
                 public Query constructInsertQuery(Entity entity){
 262  0
                         PrimarySimpleData simpleData = (PrimarySimpleData) entity;
 263  
 
 264  0
                         String query = "INSERT INTO " + dataType.getTable() + " (NAME, IMPL) VALUES(?, ?)";
 265  
 
 266  0
                         Object[] parameters = {simpleData.getName(), simpleData.getPrimaryImpl()};
 267  
 
 268  0
                         return new Query(query, parameters);
 269  
                 }
 270  
 
 271  
                 @Override
 272  
                 public Query constructUpdateQuery(Entity entity){
 273  0
                         PrimarySimpleData simpleData = (PrimarySimpleData) entity;
 274  
 
 275  0
                         String query = "UPDATE " + dataType.getTable() + " SET NAME = ?, IMPL = ? WHERE ID = ?";
 276  
 
 277  0
                         Object[] parameters = {
 278  
                                         simpleData.getName(),
 279  
                     simpleData.getPrimaryImpl(),
 280  
                                         simpleData.getId()};
 281  
 
 282  0
                         return new Query(query, parameters);
 283  
                 }
 284  
         }
 285  
 }