Coverage Report - org.jtheque.primary.dao.impl.DaoLendings
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoLendings
0 %
0/29
0 %
0/8
1.5
DaoLendings$1
N/A
N/A
1.5
DaoLendings$LendingQueryMapper
0 %
0/13
0 %
0/4
1.5
DaoLendings$LendingRowMapper
0 %
0/8
N/A
1.5
 
 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.IDaoLendings;
 26  
 import org.jtheque.primary.dao.able.IDaoPersons;
 27  
 import org.jtheque.primary.od.able.Lending;
 28  
 import org.jtheque.primary.od.impl.LendingImpl;
 29  
 import org.jtheque.utils.StringUtils;
 30  
 import org.jtheque.utils.bean.IntDate;
 31  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 32  
 
 33  
 import javax.annotation.Resource;
 34  
 import java.sql.ResultSet;
 35  
 import java.sql.SQLException;
 36  
 import java.util.ArrayList;
 37  
 import java.util.Collection;
 38  
 
 39  
 /**
 40  
  * A Data Access Object implementation for lendings.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  0
 public final class DaoLendings extends GenericDao<Lending> implements IDaoLendings {
 45  0
         private final ParameterizedRowMapper<Lending> rowMapper = new LendingRowMapper();
 46  0
         private final QueryMapper queryMapper = new LendingQueryMapper();
 47  
 
 48  
         @Resource
 49  
         private IDaoPersistenceContext daoPersistenceContext;
 50  
 
 51  
         @Resource
 52  
         private IDaoPersons daoPersons;
 53  
 
 54  
         /**
 55  
          * Construct a new DaoLendings.
 56  
          */
 57  
         public DaoLendings(){
 58  0
                 super(TABLE);
 59  0
         }
 60  
 
 61  
         @Override
 62  
         public void create(Lending entity){
 63  0
                 entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl());
 64  
 
 65  0
                 super.create(entity);
 66  0
         }
 67  
 
 68  
         @Override
 69  
         public Lending getLending(int id){
 70  0
                 return get(id);
 71  
         }
 72  
 
 73  
         @Override
 74  
         protected ParameterizedRowMapper<Lending> getRowMapper(){
 75  0
                 return rowMapper;
 76  
         }
 77  
 
 78  
         @Override
 79  
         protected QueryMapper getQueryMapper(){
 80  0
                 return queryMapper;
 81  
         }
 82  
 
 83  
         @Override
 84  
         protected void loadCache(){
 85  0
                 Collection<Lending> lendings = daoPersistenceContext.getSortedList(TABLE, rowMapper);
 86  
 
 87  0
                 for (Lending lending : lendings){
 88  0
                         getCache().put(lending.getId(), lending);
 89  
                 }
 90  
 
 91  0
                 setCacheEntirelyLoaded();
 92  0
         }
 93  
 
 94  
         @Override
 95  
         public Collection<Lending> getLendings(){
 96  0
                 return getLendings(PrimaryUtils.getPrimaryImpl());
 97  
         }
 98  
 
 99  
         /**
 100  
          * Return all the lendings of the specific primary implementation.
 101  
          *
 102  
          * @param impl The primary implementation.
 103  
          *
 104  
          * @return A Collection containing all the lendings of the specific primary implementation.
 105  
          */
 106  
         private Collection<Lending> getLendings(CharSequence impl){
 107  0
                 if (StringUtils.isEmpty(impl)){
 108  0
                         return getAll();
 109  
                 }
 110  
 
 111  0
                 load();
 112  
 
 113  0
                 Collection<Lending> lendings = new ArrayList<Lending>(getCache().size() / 2);
 114  
 
 115  0
                 for (Lending lending : getCache().values()){
 116  0
                         if (impl.equals(lending.getPrimaryImpl())){
 117  0
                                 lendings.add(lending);
 118  
                         }
 119  
                 }
 120  
 
 121  0
                 return lendings;
 122  
         }
 123  
 
 124  
         @Override
 125  
         protected void load(int i){
 126  0
                 Lending lending = daoPersistenceContext.getDataByID(TABLE, i, rowMapper);
 127  
 
 128  0
                 getCache().put(i, lending);
 129  0
         }
 130  
 
 131  
         @Override
 132  
         public Lending createLending(){
 133  0
                 return new LendingImpl();
 134  
         }
 135  
 
 136  
         /**
 137  
          * A mapper to map resultset to lending.
 138  
          *
 139  
          * @author Baptiste Wicht
 140  
          */
 141  0
         private final class LendingRowMapper implements ParameterizedRowMapper<Lending> {
 142  
                 @Override
 143  
                 public Lending mapRow(ResultSet rs, int i) throws SQLException{
 144  0
                         Lending lending = createLending();
 145  
 
 146  0
                         lending.setId(rs.getInt("ID"));
 147  0
                         lending.setTheOther(rs.getInt("THE_OTHER_FK"));
 148  0
                         lending.setThePerson(daoPersons.getPerson(rs.getInt("THE_PERSON_FK")));
 149  0
                         lending.setDate(new IntDate(rs.getInt("DATE")));
 150  0
                         lending.setPrimaryImpl(rs.getString("IMPL"));
 151  
 
 152  0
                         return lending;
 153  
                 }
 154  
         }
 155  
 
 156  
         /**
 157  
          * A mapper to map lending to query.
 158  
          *
 159  
          * @author Baptiste Wicht
 160  
          */
 161  0
         private static final class LendingQueryMapper implements QueryMapper {
 162  
                 @Override
 163  
                 public Query constructInsertQuery(Entity entity){
 164  0
                         String query = "INSERT INTO " + TABLE + " (THE_OTHER_FK, THE_PERSON_FK, DATE, IMPL) VALUES(?,?,?, ?)";
 165  
 
 166  0
                         return new Query(query, fillArray((Lending) entity, false));
 167  
                 }
 168  
 
 169  
                 @Override
 170  
                 public Query constructUpdateQuery(Entity entity){
 171  0
                         String query = "UPDATE " + TABLE + " SET THE_OTHER_FK = ?, THE_BORROWER_FK = ?, DATE = ?, IMPL = ? WHERE ID = ?";
 172  
 
 173  0
                         return new Query(query, fillArray((Lending) entity, true));
 174  
                 }
 175  
 
 176  
                 /**
 177  
                  * Fill the array with the informations of the lending.
 178  
                  *
 179  
                  * @param lending The lending to use to fill the array.
 180  
                  * @param id Indicate if we must add the id to the array.
 181  
                  *
 182  
                  * @return The filled array.
 183  
                  */
 184  
                 private static Object[] fillArray(Lending lending, boolean id){
 185  0
                         Object[] values = new Object[4 + (id ? 1 : 0)];
 186  
 
 187  0
                         values[0] = lending.getTheOther();
 188  0
                         values[1] = lending.getThePerson().getId();
 189  0
                         values[2] = lending.getDate().intValue();
 190  0
                         values[3] = lending.getPrimaryImpl();
 191  
 
 192  0
                         if (id){
 193  0
                                 values[4] = lending.getId();
 194  
                         }
 195  
 
 196  0
                         return values;
 197  
                 }
 198  
         }
 199  
 }