Coverage Report - org.jtheque.primary.dao.impl.DaoKinds
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoKinds
0 %
0/37
0 %
0/14
1.533
DaoKinds$1
N/A
N/A
1.533
DaoKinds$KindQueryMapper
0 %
0/9
N/A
1.533
DaoKinds$KindRowMapper
0 %
0/6
N/A
1.533
 
 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.IDaoKinds;
 26  
 import org.jtheque.primary.od.able.Kind;
 27  
 import org.jtheque.primary.od.impl.KindImpl;
 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.Collection;
 37  
 import java.util.List;
 38  
 
 39  
 /**
 40  
  * A Data Access Object implementation for kinds.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  0
 public final class DaoKinds extends GenericDao<Kind> implements IDaoKinds {
 45  0
     private final ParameterizedRowMapper<Kind> rowMapper = new KindRowMapper();
 46  0
     private final QueryMapper queryMapper = new KindQueryMapper();
 47  
 
 48  
     @Resource
 49  
     private IDaoPersistenceContext persistenceContext;
 50  
 
 51  
     @Resource
 52  
     private SimpleJdbcTemplate jdbcTemplate;
 53  
 
 54  
     /**
 55  
      * Construct a new DaoKinds.
 56  
      */
 57  
     public DaoKinds() {
 58  0
         super(TABLE);
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public void create(Kind entity) {
 63  0
         entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl());
 64  
 
 65  0
         super.create(entity);
 66  0
     }
 67  
 
 68  
     @Override
 69  
     public Kind createKind() {
 70  0
         return new KindImpl();
 71  
     }
 72  
 
 73  
     @Override
 74  
     public boolean exists(String name) {
 75  0
         return getKind(name) != null;
 76  
     }
 77  
 
 78  
     @Override
 79  
     public Collection<Kind> getKinds() {
 80  0
         return getKinds(PrimaryUtils.getPrimaryImpl());
 81  
     }
 82  
 
 83  
     /**
 84  
      * Return all the kinds of the specific primary implementation.
 85  
      *
 86  
      * @param impl The implementation.
 87  
      * @return A Collection containing all the kinds of the specific primary implementation.
 88  
      */
 89  
     private Collection<Kind> getKinds(CharSequence impl) {
 90  0
         if (StringUtils.isEmpty(impl)) {
 91  0
             return getAll();
 92  
         }
 93  
 
 94  0
         load();
 95  
 
 96  0
         Collection<Kind> kinds = new ArrayList<Kind>(getCache().size() / 2);
 97  
 
 98  0
         for (Kind kind : getCache().values()) {
 99  0
             if (impl.equals(kind.getPrimaryImpl())) {
 100  0
                 kinds.add(kind);
 101  
             }
 102  
         }
 103  
 
 104  0
         return kinds;
 105  
     }
 106  
 
 107  
     @Override
 108  
     public Kind getKind(int id) {
 109  0
         return get(id);
 110  
     }
 111  
 
 112  
     @Override
 113  
     public Kind getKind(String name) {
 114  0
         List<Kind> kinds = jdbcTemplate.query("SELECT * FROM " + TABLE + " WHERE NAME = ? AND IMPL = ?",
 115  
                 rowMapper, name, PrimaryUtils.getPrimaryImpl());
 116  
 
 117  0
         if (kinds.isEmpty()) {
 118  0
             return null;
 119  
         }
 120  
 
 121  0
         Kind kind = kinds.get(0);
 122  
 
 123  0
         if (isNotInCache(kind.getId())) {
 124  0
             getCache().put(kind.getId(), kind);
 125  
         }
 126  
 
 127  0
         return getCache().get(kind.getId());
 128  
     }
 129  
 
 130  
     @Override
 131  
     protected ParameterizedRowMapper<Kind> getRowMapper() {
 132  0
         return rowMapper;
 133  
     }
 134  
 
 135  
     @Override
 136  
     protected QueryMapper getQueryMapper() {
 137  0
         return queryMapper;
 138  
     }
 139  
 
 140  
     @Override
 141  
     protected void loadCache() {
 142  0
         Collection<Kind> kinds = persistenceContext.getSortedList(TABLE, rowMapper);
 143  
 
 144  0
         for (Kind kind : kinds) {
 145  0
             getCache().put(kind.getId(), kind);
 146  
         }
 147  
 
 148  0
         setCacheEntirelyLoaded();
 149  0
     }
 150  
 
 151  
     @Override
 152  
     protected void load(int i) {
 153  0
         Kind kind = persistenceContext.getDataByID(TABLE, i, rowMapper);
 154  
 
 155  0
         getCache().put(i, kind);
 156  0
     }
 157  
 
 158  
     /**
 159  
      * A mapper to map resultset to kind.
 160  
      *
 161  
      * @author Baptiste Wicht
 162  
      */
 163  0
     private final class KindRowMapper implements ParameterizedRowMapper<Kind> {
 164  
         @Override
 165  
         public Kind mapRow(ResultSet rs, int i) throws SQLException {
 166  0
             Kind kind = createKind();
 167  
 
 168  0
             kind.setId(rs.getInt("ID"));
 169  0
             kind.setName(rs.getString("NAME"));
 170  0
             kind.setPrimaryImpl(rs.getString("IMPL"));
 171  
 
 172  0
             return kind;
 173  
         }
 174  
     }
 175  
 
 176  
     /**
 177  
      * A mapper to map kind to query.
 178  
      *
 179  
      * @author Baptiste Wicht
 180  
      */
 181  0
     private static final class KindQueryMapper implements QueryMapper {
 182  
         @Override
 183  
         public Query constructInsertQuery(Entity entity) {
 184  0
             Kind kind = (Kind) entity;
 185  
 
 186  0
             String query = "INSERT INTO " + TABLE + " (NAME, IMPL) VALUES(?, ?)";
 187  
 
 188  0
             Object[] parameters = {
 189  
                     kind.getName(),
 190  
                     kind.getPrimaryImpl()
 191  
             };
 192  
 
 193  0
             return new Query(query, parameters);
 194  
         }
 195  
 
 196  
         @Override
 197  
         public Query constructUpdateQuery(Entity entity) {
 198  0
             Kind kind = (Kind) entity;
 199  
 
 200  0
             String query = "UPDATE " + TABLE + " SET NAME = ?, IMPL = ? WHERE ID = ?";
 201  
 
 202  0
             Object[] parameters = {
 203  
                     kind.getName(),
 204  
                     kind.getPrimaryImpl(),
 205  
                     kind.getId()};
 206  
 
 207  0
             return new Query(query, parameters);
 208  
         }
 209  
     }
 210  
 }