Coverage Report - org.jtheque.core.managers.persistence.context.DaoPersistenceContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoPersistenceContext
0 %
0/21
0 %
0/12
2
 
 1  
 package org.jtheque.core.managers.persistence.context;
 2  
 
 3  
 import org.jtheque.core.managers.persistence.Query;
 4  
 import org.jtheque.core.managers.persistence.QueryMapper;
 5  
 import org.jtheque.core.managers.persistence.able.Entity;
 6  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 7  
 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
 8  
 
 9  
 import javax.annotation.Resource;
 10  
 import java.util.Collection;
 11  
 import java.util.Collections;
 12  
 import java.util.List;
 13  
 
 14  
 /*
 15  
  * This file is part of JTheque.
 16  
  *
 17  
  * JTheque is free software: you can redistribute it and/or modify
 18  
  * it under the terms of the GNU General Public License as published by
 19  
  * the Free Software Foundation, either version 3 of the License.
 20  
  *
 21  
  * JTheque is distributed in the hope that it will be useful,
 22  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 23  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 24  
  * GNU General Public License for more details.
 25  
  *
 26  
  * You should have received a copy of the GNU General Public License
 27  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 28  
  */
 29  
 
 30  
 /**
 31  
  * The persistence context for DAOs.
 32  
  *
 33  
  * @author Baptiste Wicht
 34  
  */
 35  0
 public final class DaoPersistenceContext implements IDaoPersistenceContext {
 36  
     @Resource
 37  
     private SimpleJdbcTemplate jdbcTemplate;
 38  
 
 39  
     @Override
 40  
     public <T extends Entity> Collection<T> getSortedList(String table, ParameterizedRowMapper<T> mapper) {
 41  0
         List<T> data = jdbcTemplate.query("SELECT * FROM " + table, mapper);
 42  
 
 43  0
         Collections.sort(data);
 44  
 
 45  0
         return data;
 46  
     }
 47  
 
 48  
     @Override
 49  
     public <T extends Entity> T getDataByID(String table, int id, ParameterizedRowMapper<T> mapper) {
 50  0
         List<T> results = jdbcTemplate.query("SELECT * FROM " + table + " WHERE ID = ?", mapper, id);
 51  
 
 52  0
         if (results.isEmpty()) {
 53  0
             return null;
 54  
         }
 55  
 
 56  0
         return results.get(0);
 57  
     }
 58  
 
 59  
     @Override
 60  
     public boolean delete(String table, int id) {
 61  0
         return jdbcTemplate.update("DELETE FROM " + table + " WHERE ID = ?", id) > 0;
 62  
     }
 63  
 
 64  
     @Override
 65  
     public boolean delete(String table, Entity d) {
 66  0
         return delete(table, d.getId());
 67  
     }
 68  
 
 69  
     @Override
 70  
     public boolean saveOrUpdate(Entity entity, QueryMapper mapper) {
 71  0
         if (entity == null) {
 72  0
             throw new IllegalArgumentException("AbstractEntity cannot be null");
 73  
         }
 74  
 
 75  0
         if (entity.isSaved()) {
 76  0
             Query query = mapper.constructUpdateQuery(entity);
 77  
 
 78  0
             return jdbcTemplate.update(query.getSqlQuery(), query.getParameters()) > 0;
 79  
         } else {
 80  0
             Query query = mapper.constructInsertQuery(entity);
 81  
 
 82  0
             int inserts = jdbcTemplate.update(query.getSqlQuery(), query.getParameters());
 83  
 
 84  0
             entity.setId(jdbcTemplate.queryForInt("SELECT IDENTITY()"));
 85  
 
 86  0
             return inserts > 0;
 87  
         }
 88  
     }
 89  
 
 90  
     @Override
 91  
     public void deleteAll(String table) {
 92  0
         jdbcTemplate.update("DELETE FROM " + table);
 93  0
     }
 94  
 }