Coverage Report - org.jtheque.primary.dao.impl.DaoSagas
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoSagas
0 %
0/29
0 %
0/8
1.385
DaoSagas$1
N/A
N/A
1.385
DaoSagas$SagaQueryMapper
0 %
0/9
N/A
1.385
DaoSagas$SagaRowMapper
0 %
0/6
N/A
1.385
 
 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.IDaoSagas;
 26  
 import org.jtheque.primary.od.able.Saga;
 27  
 import org.jtheque.primary.od.impl.SagaImpl;
 28  
 import org.jtheque.utils.StringUtils;
 29  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 30  
 
 31  
 import javax.annotation.Resource;
 32  
 import java.sql.ResultSet;
 33  
 import java.sql.SQLException;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Collection;
 36  
 
 37  
 /**
 38  
  * A Data Access Object implementation for sagas.
 39  
  *
 40  
  * @author Baptiste Wicht
 41  
  */
 42  0
 public final class DaoSagas extends GenericDao<Saga> implements IDaoSagas {
 43  0
     private final ParameterizedRowMapper<Saga> rowMapper = new SagaRowMapper();
 44  0
     private final QueryMapper queryMapper = new SagaQueryMapper();
 45  
 
 46  
     @Resource
 47  
     private IDaoPersistenceContext persistenceContext;
 48  
 
 49  
     /**
 50  
      * Construct a new DaoSagas.
 51  
      */
 52  
     public DaoSagas() {
 53  0
         super(TABLE);
 54  0
     }
 55  
 
 56  
     @Override
 57  
     public void create(Saga entity) {
 58  0
         entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl());
 59  
 
 60  0
         super.create(entity);
 61  0
     }
 62  
 
 63  
     @Override
 64  
     public Saga createSaga() {
 65  0
         return new SagaImpl();
 66  
     }
 67  
 
 68  
     @Override
 69  
     public Collection<Saga> getSagas() {
 70  0
         return getSagas(PrimaryUtils.getPrimaryImpl());
 71  
     }
 72  
 
 73  
     /**
 74  
      * Return all the sagas of the specific primary implementation.
 75  
      *
 76  
      * @param impl The primary implementation.
 77  
      * @return A Collection containing all the sagas of the specific primary implementation.
 78  
      */
 79  
     private Collection<Saga> getSagas(CharSequence impl) {
 80  0
         if (StringUtils.isEmpty(impl)) {
 81  0
             return getAll();
 82  
         }
 83  
 
 84  0
         load();
 85  
 
 86  0
         Collection<Saga> sagas = new ArrayList<Saga>(getCache().size() / 2);
 87  
 
 88  0
         for (Saga saga : getCache().values()) {
 89  0
             if (impl.equals(saga.getPrimaryImpl())) {
 90  0
                 sagas.add(saga);
 91  
             }
 92  
         }
 93  
 
 94  0
         return sagas;
 95  
     }
 96  
 
 97  
     @Override
 98  
     public Saga getSaga(int id) {
 99  0
         return get(id);
 100  
     }
 101  
 
 102  
     @Override
 103  
     protected ParameterizedRowMapper<Saga> getRowMapper() {
 104  0
         return rowMapper;
 105  
     }
 106  
 
 107  
     @Override
 108  
     protected QueryMapper getQueryMapper() {
 109  0
         return queryMapper;
 110  
     }
 111  
 
 112  
     @Override
 113  
     protected void loadCache() {
 114  0
         Collection<Saga> sagas = persistenceContext.getSortedList(TABLE, rowMapper);
 115  
 
 116  0
         for (Saga saga : sagas) {
 117  0
             getCache().put(saga.getId(), saga);
 118  
         }
 119  
 
 120  0
         setCacheEntirelyLoaded();
 121  0
     }
 122  
 
 123  
     @Override
 124  
     protected void load(int i) {
 125  0
         Saga editor = persistenceContext.getDataByID(TABLE, i, rowMapper);
 126  
 
 127  0
         getCache().put(i, editor);
 128  0
     }
 129  
 
 130  
     /**
 131  
      * A mapper to map a resultset to a saga.
 132  
      *
 133  
      * @author Baptiste Wicht
 134  
      */
 135  0
     private final class SagaRowMapper implements ParameterizedRowMapper<Saga> {
 136  
         @Override
 137  
         public Saga mapRow(ResultSet rs, int i) throws SQLException {
 138  0
             Saga saga = createSaga();
 139  
 
 140  0
             saga.setId(rs.getInt("ID"));
 141  0
             saga.setName(rs.getString("NAME"));
 142  0
             saga.setPrimaryImpl(rs.getString("IMPL"));
 143  
 
 144  0
             return saga;
 145  
         }
 146  
     }
 147  
 
 148  
     /**
 149  
      * A mapper to map a saga to an SQL query.
 150  
      *
 151  
      * @author Baptiste Wicht
 152  
      */
 153  0
     private static final class SagaQueryMapper implements QueryMapper {
 154  
         @Override
 155  
         public Query constructInsertQuery(Entity entity) {
 156  0
             Saga saga = (Saga) entity;
 157  
 
 158  0
             String query = "INSERT INTO " + TABLE + " (NAME, IMPL) VALUES(?, ?)";
 159  
 
 160  0
             Object[] parameters = {
 161  
                     saga.getName(),
 162  
                     saga.getPrimaryImpl()
 163  
             };
 164  
 
 165  0
             return new Query(query, parameters);
 166  
         }
 167  
 
 168  
         @Override
 169  
         public Query constructUpdateQuery(Entity entity) {
 170  0
             Saga saga = (Saga) entity;
 171  
 
 172  0
             String query = "UPDATE " + TABLE + " SET NAME = ?, IMPL = ? WHERE ID = ?";
 173  
 
 174  0
             Object[] parameters = {
 175  
                     saga.getName(),
 176  
                     saga.getPrimaryImpl(),
 177  
                     saga.getId()};
 178  
 
 179  0
             return new Query(query, parameters);
 180  
         }
 181  
     }
 182  
 }