Coverage Report - org.jtheque.primary.dao.impl.DaoCollections
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoCollections
0 %
0/39
0 %
0/12
1.588
DaoCollections$1
N/A
N/A
1.588
DaoCollections$CollectionQueryMapper
0 %
0/13
0 %
0/4
1.588
DaoCollections$CollectionRowMapper
0 %
0/8
N/A
1.588
 
 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.IDaoCollections;
 26  
 import org.jtheque.primary.od.able.Collection;
 27  
 import org.jtheque.primary.od.impl.CollectionImpl;
 28  
 import org.jtheque.utils.StringUtils;
 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.ArrayList;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * A Data Access Object implementation for collections.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  0
 public final class DaoCollections extends GenericDao<Collection> implements IDaoCollections {
 44  0
         private final ParameterizedRowMapper<Collection> rowMapper = new CollectionRowMapper();
 45  0
         private final QueryMapper queryMapper = new CollectionQueryMapper();
 46  
 
 47  
         @Resource
 48  
         private IDaoPersistenceContext daoPersistenceContext;
 49  
 
 50  
         @Resource
 51  
         private SimpleJdbcTemplate jdbcTemplate;
 52  
 
 53  
         /**
 54  
          * The current collection.
 55  
          */
 56  
         private Collection currentCollection;
 57  
 
 58  
         /**
 59  
          * Construct a new DaoCollections.
 60  
          */
 61  
         public DaoCollections(){
 62  0
                 super(TABLE);
 63  0
         }
 64  
 
 65  
         @Override
 66  
         public void create(Collection entity){
 67  0
                 entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl());
 68  
 
 69  0
                 super.create(entity);
 70  0
         }
 71  
 
 72  
         @Override
 73  
         public java.util.Collection<Collection> getCollections(){
 74  0
                 return getCollections(PrimaryUtils.getPrimaryImpl());
 75  
         }
 76  
 
 77  
         /**
 78  
          * Return all the collections of the specific implementations.
 79  
          *
 80  
          * @param impl The primary implementation.
 81  
          *
 82  
          * @return A Collection of Collection.
 83  
          */
 84  
         private java.util.Collection<Collection> getCollections(CharSequence impl){
 85  0
                 if (StringUtils.isEmpty(impl)){
 86  0
                         return getAll();
 87  
                 }
 88  
 
 89  0
                 load();
 90  
 
 91  0
                 java.util.Collection<Collection> collections = new ArrayList<Collection>(getCache().size() / 2);
 92  
 
 93  0
                 for (Collection collection : getCache().values()){
 94  0
                         if (impl.equals(collection.getPrimaryImpl())){
 95  0
                                 collections.add(collection);
 96  
                         }
 97  
                 }
 98  
 
 99  0
                 return collections;
 100  
         }
 101  
 
 102  
         @Override
 103  
         public Collection getCollection(int id){
 104  0
                 return get(id);
 105  
         }
 106  
 
 107  
         @Override
 108  
         public Collection getCollection(String name){
 109  0
                 List<Collection> collections = jdbcTemplate.query("SELECT * FROM " + TABLE + " WHERE TITLE = ? AND IMPL = ?",
 110  
                                 rowMapper, name, PrimaryUtils.getPrimaryImpl());
 111  
 
 112  0
                 if (collections.isEmpty()){
 113  0
                         return null;
 114  
                 }
 115  
 
 116  0
                 Collection collection = collections.get(0);
 117  
 
 118  0
                 if (isNotInCache(collection.getId())){
 119  0
                         getCache().put(collection.getId(), collection);
 120  
                 }
 121  
 
 122  0
                 return getCache().get(collection.getId());
 123  
         }
 124  
 
 125  
         @Override
 126  
         protected ParameterizedRowMapper<Collection> getRowMapper(){
 127  0
                 return rowMapper;
 128  
         }
 129  
 
 130  
         @Override
 131  
         protected QueryMapper getQueryMapper(){
 132  0
                 return queryMapper;
 133  
         }
 134  
 
 135  
         @Override
 136  
         protected void loadCache(){
 137  0
                 java.util.Collection<Collection> collections = daoPersistenceContext.getSortedList(TABLE, rowMapper);
 138  
 
 139  0
                 for (Collection collection : collections){
 140  0
                         getCache().put(collection.getId(), collection);
 141  
                 }
 142  
 
 143  0
                 setCacheEntirelyLoaded();
 144  0
         }
 145  
 
 146  
         @Override
 147  
         protected void load(int i){
 148  0
                 Collection collection = daoPersistenceContext.getDataByID(TABLE, i, rowMapper);
 149  
 
 150  0
                 getCache().put(i, collection);
 151  0
         }
 152  
 
 153  
         @Override
 154  
         public Collection getCurrentCollection(){
 155  0
                 return currentCollection;
 156  
         }
 157  
 
 158  
         @Override
 159  
         public void setCurrentCollection(Collection collection){
 160  0
                 currentCollection = collection;
 161  0
         }
 162  
 
 163  
         @Override
 164  
         public Collection createCollection(){
 165  0
                 return new CollectionImpl();
 166  
         }
 167  
 
 168  
         /**
 169  
          * A row mapper to map resultset to collection.
 170  
          *
 171  
          * @author Baptiste Wicht
 172  
          */
 173  0
         private final class CollectionRowMapper implements ParameterizedRowMapper<Collection> {
 174  
                 @Override
 175  
                 public Collection mapRow(ResultSet rs, int i) throws SQLException{
 176  0
                         Collection collection = createCollection();
 177  
 
 178  0
                         collection.setId(rs.getInt("ID"));
 179  0
                         collection.setTitle(rs.getString("TITLE"));
 180  0
                         collection.setPassword(rs.getString("PASSWORD"));
 181  0
                         collection.setProtection(rs.getBoolean("PROTECTED"));
 182  0
                         collection.setPrimaryImpl(rs.getString("IMPL"));
 183  
 
 184  0
                         return collection;
 185  
                 }
 186  
         }
 187  
 
 188  
         /**
 189  
          * A query mapper to map collection to sql query.
 190  
          *
 191  
          * @author Baptiste Wicht
 192  
          */
 193  0
         private static final class CollectionQueryMapper implements QueryMapper {
 194  
                 @Override
 195  
                 public Query constructInsertQuery(Entity entity){
 196  0
                         String query = "INSERT INTO " + TABLE + " (TITLE, PASSWORD, PROTECTED, IMPL) VALUES(?,?,?,?)";
 197  
 
 198  0
                         return new Query(query, fillArray((Collection) entity, false));
 199  
                 }
 200  
 
 201  
                 @Override
 202  
                 public Query constructUpdateQuery(Entity entity){
 203  0
                         String query = "UPDATE " + TABLE + " SET TITLE = ?, PASSWORD = ?, PROTECTED = ?, IMPL = ? WHERE ID = ?";
 204  
 
 205  0
                         return new Query(query, fillArray((Collection) entity, true));
 206  
                 }
 207  
 
 208  
                 /**
 209  
                  * Fill the array with the informations of the collection.
 210  
                  *
 211  
                  * @param collection The collection to use to fill the array.
 212  
                  * @param id Indicate if we must add the id to the array.
 213  
                  *
 214  
                  * @return The filled array.
 215  
                  */
 216  
                 private static Object[] fillArray(Collection collection, boolean id){
 217  0
                         Object[] values = new Object[4 + (id ? 1 : 0)];
 218  
 
 219  0
                         values[0] = collection.getTitle();
 220  0
                         values[1] = collection.getPassword();
 221  0
                         values[2] = collection.isProtection();
 222  0
                         values[3] = collection.getPrimaryImpl();
 223  
 
 224  0
                         if (id){
 225  0
                                 values[4] = collection.getId();
 226  
                         }
 227  
 
 228  0
                         return values;
 229  
                 }
 230  
         }
 231  
 }