Coverage Report - org.jtheque.primary.dao.impl.DaoPersons
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoPersons
0 %
0/41
0 %
0/24
2.059
DaoPersons$1
N/A
N/A
2.059
DaoPersons$PersonQueryMapper
0 %
0/15
0 %
0/8
2.059
DaoPersons$PersonRowMapper
0 %
0/11
0 %
0/2
2.059
 
 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.core.utils.db.DaoNotes;
 25  
 import org.jtheque.primary.dao.able.IDaoPersons;
 26  
 import org.jtheque.primary.dao.able.IDaoSimpleDatas;
 27  
 import org.jtheque.primary.od.able.Person;
 28  
 import org.jtheque.primary.od.impl.PersonImpl;
 29  
 import org.jtheque.utils.StringUtils;
 30  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 31  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 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 borrowers.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  0
 public final class DaoPersons extends GenericDao<Person> implements IDaoPersons {
 45  0
         private final ParameterizedRowMapper<Person> rowMapper = new PersonRowMapper();
 46  0
         private final QueryMapper queryMapper = new PersonQueryMapper();
 47  
 
 48  
         @Resource
 49  
         private IDaoPersistenceContext daoPersistenceContext;
 50  
 
 51  
         @Resource
 52  
         private SimpleJdbcTemplate jdbcTemplate;
 53  
 
 54  
         @Resource
 55  
         private IDaoSimpleDatas daoCountries;
 56  
 
 57  0
         private final DaoNotes daoNotes = DaoNotes.getInstance();
 58  
 
 59  
         /**
 60  
          * Construct a new DaoBorrowers.
 61  
          */
 62  
         public DaoPersons(){
 63  0
                 super(TABLE);
 64  0
         }
 65  
 
 66  
         @Override
 67  
         public Collection<Person> getPersons(String type){
 68  0
                 return getAll(type);
 69  
         }
 70  
 
 71  
         /**
 72  
          * Return all the persons of the specified type.
 73  
          *
 74  
          * @param type The searched type.
 75  
          *
 76  
          * @return Return a Collection containing all the persons of the specified type.
 77  
          */
 78  
         private Collection<Person> getAll(CharSequence type){
 79  0
                 if (StringUtils.isEmpty(type)){
 80  0
                         return getAll();
 81  
                 }
 82  
 
 83  0
                 load();
 84  
 
 85  0
                 Collection<Person> persons = new ArrayList<Person>(getCache().size() / 3);
 86  
 
 87  0
                 for (Person person : getCache().values()){
 88  0
                         if (type.equals(person.getType())){
 89  0
                                 persons.add(person);
 90  
                         }
 91  
                 }
 92  
 
 93  0
                 return persons;
 94  
         }
 95  
 
 96  
         @Override
 97  
         public Person getPerson(int id){
 98  0
                 return get(id);
 99  
         }
 100  
 
 101  
         @Override
 102  
         public Person getPerson(String firstName, String name, String type){
 103  0
                 load();
 104  
 
 105  0
                 if (getCache().isEmpty()){
 106  0
                         return null;
 107  
                 }
 108  
 
 109  0
                 for (Person person : getCache().values()){
 110  0
                         if (type.equals(person.getType()) && firstName.equals(person.getFirstName()) && name.equals(person.getName())){
 111  0
                                 return person;
 112  
                         }
 113  
                 }
 114  
 
 115  0
                 return null;
 116  
         }
 117  
 
 118  
         @Override
 119  
         public boolean exists(String firstName, String name, String type){
 120  0
                 return getPerson(firstName, name, type) != null;
 121  
         }
 122  
 
 123  
         @Override
 124  
         public boolean exist(Person person){
 125  0
                 return getPerson(person.getFirstName(), person.getName(), person.getType()) != null;
 126  
         }
 127  
 
 128  
         @Override
 129  
         public Person createPerson(){
 130  0
                 return new PersonImpl();
 131  
         }
 132  
 
 133  
         @Override
 134  
         public void clearAll(String type){
 135  0
                 Collection<Person> persons = getPersons(type);
 136  
 
 137  0
                 jdbcTemplate.update("DELETE FROM " + TABLE + " WHERE TYPE = ?", type);
 138  
 
 139  0
                 for (Person person : persons){
 140  0
                         getCache().remove(person.getId());
 141  
                 }
 142  0
         }
 143  
 
 144  
         @Override
 145  
         protected void loadCache(){
 146  0
                 Collection<Person> persons = daoPersistenceContext.getSortedList(TABLE, rowMapper);
 147  
 
 148  0
                 for (Person borrower : persons){
 149  0
                         getCache().put(borrower.getId(), borrower);
 150  
                 }
 151  
 
 152  0
                 setCacheEntirelyLoaded();
 153  0
         }
 154  
 
 155  
         @Override
 156  
         protected void load(int i){
 157  0
                 Person person = daoPersistenceContext.getDataByID(TABLE, i, rowMapper);
 158  
 
 159  0
                 getCache().put(i, person);
 160  0
         }
 161  
 
 162  
         @Override
 163  
         protected ParameterizedRowMapper<Person> getRowMapper(){
 164  0
                 return rowMapper;
 165  
         }
 166  
 
 167  
         @Override
 168  
         protected QueryMapper getQueryMapper(){
 169  0
                 return queryMapper;
 170  
         }
 171  
 
 172  
         /**
 173  
          * A row mapper to map resultset to borrower.
 174  
          *
 175  
          * @author Baptiste Wicht
 176  
          */
 177  0
         private final class PersonRowMapper implements ParameterizedRowMapper<Person> {
 178  
                 @Override
 179  
                 public Person mapRow(ResultSet rs, int i) throws SQLException{
 180  0
                         Person person = createPerson();
 181  
 
 182  0
                         person.setId(rs.getInt("ID"));
 183  0
                         person.setName(rs.getString("NAME"));
 184  0
                         person.setFirstName(rs.getString("FIRST_NAME"));
 185  0
                         person.setEmail(rs.getString("EMAIL"));
 186  0
                         person.setType(rs.getString("TYPE"));
 187  0
                         person.setTheCountry(daoCountries.getSimpleData(rs.getInt("THE_COUNTRY_FK")));
 188  
 
 189  0
                         if (StringUtils.isNotEmpty(rs.getString("NOTE"))){
 190  0
                                 person.setNote(daoNotes.getNote(DaoNotes.NoteType.getEnum(rs.getInt("NOTE"))));
 191  
                         }
 192  
 
 193  0
                         return person;
 194  
                 }
 195  
         }
 196  
 
 197  
         /**
 198  
          * A query mapper to map borrower to query.
 199  
          *
 200  
          * @author Baptiste Wicht
 201  
          */
 202  0
         private static final class PersonQueryMapper implements QueryMapper {
 203  
                 @Override
 204  
                 public Query constructInsertQuery(Entity entity){
 205  0
                         String query = "INSERT INTO " + TABLE + " (NAME, FIRST_NAME, EMAIL, NOTE, THE_COUNTRY_FK, TYPE) VALUES(?,?,?,?,?,?)";
 206  
 
 207  0
                         return new Query(query, fillArray((Person) entity, false));
 208  
                 }
 209  
 
 210  
                 @Override
 211  
                 public Query constructUpdateQuery(Entity entity){
 212  0
                         String query = "UPDATE " + TABLE + " SET NAME = ?, FIRST_NAME = ?, EMAIL = ?, NOTE = ?, THE_COUNTRY_FK = ?, TYPE = ? WHERE ID = ?";
 213  
 
 214  0
                         return new Query(query, fillArray((Person) entity, true));
 215  
                 }
 216  
 
 217  
                 /**
 218  
                  * Fill the array with the informations of the person.
 219  
                  *
 220  
                  * @param person The person to use to fill the array.
 221  
                  * @param id Indicate if we must add the id to the array.
 222  
                  *
 223  
                  * @return The filled array.
 224  
                  */
 225  
                 private static Object[] fillArray(Person person, boolean id){
 226  0
                         Object[] values = new Object[6 + (id ? 1 : 0)];
 227  
 
 228  0
                         values[0] = person.getName();
 229  0
                         values[1] = person.getFirstName();
 230  0
                         values[2] = person.getEmail();
 231  0
                         values[3] = person.getNote() == null ? 0 : person.getNote().getValue().intValue();
 232  0
                         values[4] = person.getTheCountry() == null ? null : person.getTheCountry().getId();
 233  0
                         values[5] = person.getType();
 234  
 
 235  0
                         if (id){
 236  0
                                 values[6] = person.getId();
 237  
                         }
 238  
 
 239  0
                         return values;
 240  
                 }
 241  
         }
 242  
 }