Coverage Report - org.jtheque.core.managers.persistence.GenericDao
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericDao
0 %
0/46
0 %
0/12
1.25
 
 1  
 package org.jtheque.core.managers.persistence;
 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.jdesktop.swingx.event.WeakEventListenerList;
 20  
 import org.jtheque.core.managers.persistence.able.DataListener;
 21  
 import org.jtheque.core.managers.persistence.able.Entity;
 22  
 import org.jtheque.core.managers.persistence.able.JThequeDao;
 23  
 import org.jtheque.core.managers.persistence.context.IDaoPersistenceContext;
 24  
 import org.jtheque.utils.collections.CollectionUtils;
 25  
 import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 26  
 
 27  
 import javax.annotation.Resource;
 28  
 import java.util.Collection;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * A generic data access object.
 33  
  *
 34  
  * @author Baptiste Wicht
 35  
  * @param <T> The class managed by the dao.
 36  
  */
 37  
 public abstract class GenericDao<T extends Entity> implements JThequeDao {
 38  
     @Resource
 39  
     private IDaoPersistenceContext persistenceContext;
 40  
 
 41  
     private final String table;
 42  
 
 43  
     private final Map<Integer, T> cache;
 44  
 
 45  
     private boolean cacheEntirelyLoaded;
 46  
 
 47  0
     private final WeakEventListenerList listenerList = new WeakEventListenerList();
 48  
 
 49  
     /**
 50  
      * Construct a new GenericDao.
 51  
      *
 52  
      * @param table The database Table.
 53  
      */
 54  
     protected GenericDao(String table) {
 55  0
         super();
 56  
 
 57  0
         this.table = table;
 58  
 
 59  0
         cache = new CacheHashMap<Integer, T>(50);
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Return all the entity managed by this dao.
 64  
      *
 65  
      * @return A List containing all the entities managed by the dao.
 66  
      */
 67  
     protected final Collection<T> getAll() {
 68  0
         load();
 69  
 
 70  0
         return CollectionUtils.copyOf(cache.values());
 71  
     }
 72  
 
 73  
     /**
 74  
      * Return the cache of the DAO.
 75  
      *
 76  
      * @return The cache of the DAO.
 77  
      */
 78  
     protected final Map<Integer, T> getCache() {
 79  0
         return cache;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Load the DAO.
 84  
      */
 85  
     protected final void load() {
 86  0
         if (!cacheEntirelyLoaded) {
 87  0
             loadCache();
 88  
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Load the cache.
 93  
      */
 94  
     protected abstract void loadCache();
 95  
 
 96  
     /**
 97  
      * Load the entity.
 98  
      *
 99  
      * @param i The ID of the entity.
 100  
      */
 101  
     protected abstract void load(int i);
 102  
 
 103  
     /**
 104  
      * Return the row mapper for the DAO.
 105  
      *
 106  
      * @return The row mapper for the DAO.
 107  
      */
 108  
     protected abstract ParameterizedRowMapper<T> getRowMapper();
 109  
 
 110  
     /**
 111  
      * Return the query mapper for the DAO.
 112  
      *
 113  
      * @return The query mapper for the DAO.
 114  
      */
 115  
     protected abstract QueryMapper getQueryMapper();
 116  
 
 117  
     /**
 118  
      * Return the entity of a specific id.
 119  
      *
 120  
      * @param id The id for which we search the entity.
 121  
      * @return The Entity.
 122  
      */
 123  
     protected final T get(int id) {
 124  0
         if (isNotInCache(id)) {
 125  0
             load(id);
 126  
         }
 127  
 
 128  0
         return cache.get(id);
 129  
     }
 130  
 
 131  
     /**
 132  
      * Create the entity.
 133  
      *
 134  
      * @param entity The entity to create.
 135  
      */
 136  
     public void create(T entity) {
 137  0
         persistenceContext.saveOrUpdate(entity, getQueryMapper());
 138  
 
 139  0
         cache.put(entity.getId(), entity);
 140  
 
 141  0
         fireDataChanged();
 142  0
     }
 143  
 
 144  
     /**
 145  
      * Save the entity.
 146  
      *
 147  
      * @param entity The entity to save.
 148  
      */
 149  
     public void save(T entity) {
 150  0
         persistenceContext.saveOrUpdate(entity, getQueryMapper());
 151  
 
 152  0
         fireDataChanged();
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Delete the entity.
 157  
      *
 158  
      * @param entity The entity to delete.
 159  
      * @return true if the object is deleted else false.
 160  
      */
 161  
     public boolean delete(T entity) {
 162  0
         boolean deleted = persistenceContext.delete(table, entity);
 163  
 
 164  0
         if (deleted) {
 165  0
             cache.remove(entity.getId());
 166  
 
 167  0
             fireDataChanged();
 168  
         }
 169  
 
 170  0
         return deleted;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Delete the entity with the specified ID.
 175  
      *
 176  
      * @param id The id of the entity to delete.
 177  
      * @return true if the entity has been deleted else false.
 178  
      */
 179  
     public boolean delete(int id) {
 180  0
         boolean deleted = persistenceContext.delete(table, id);
 181  
 
 182  0
         if (deleted) {
 183  0
             fireDataChanged();
 184  
 
 185  0
             cache.remove(id);
 186  
         }
 187  
 
 188  0
         return deleted;
 189  
     }
 190  
 
 191  
     /**
 192  
      * Delete all the entities.
 193  
      */
 194  
     @Override
 195  
     public final void clearAll() {
 196  0
         persistenceContext.deleteAll(table);
 197  
 
 198  0
         cache.clear();
 199  0
     }
 200  
 
 201  
     /**
 202  
      * Indicate if the entity with the ID is in cache.
 203  
      *
 204  
      * @param i The ID of the entity.
 205  
      * @return true if an entity with the ID is in cache.
 206  
      */
 207  
     protected final boolean isNotInCache(int i) {
 208  0
         return !cache.containsKey(i);
 209  
     }
 210  
 
 211  
     /**
 212  
      * Indicate if the cache is entirely loaded.
 213  
      *
 214  
      * @return true if the cache is entirely loaded else false.
 215  
      */
 216  
     final boolean isCacheEntirelyLoaded() {
 217  0
         return cacheEntirelyLoaded;
 218  
     }
 219  
 
 220  
     /**
 221  
      * Set if the cache has been entirely loaded or not.
 222  
      */
 223  
     protected final void setCacheEntirelyLoaded() {
 224  0
         cacheEntirelyLoaded = true;
 225  0
     }
 226  
 
 227  
     @Override
 228  
     public final void addDataListener(DataListener listener) {
 229  0
         listenerList.add(DataListener.class, listener);
 230  0
     }
 231  
 
 232  
     @Override
 233  
     public final void removeDataListener(DataListener listener) {
 234  0
         listenerList.remove(DataListener.class, listener);
 235  0
     }
 236  
 
 237  
     /**
 238  
      * Avert the listeners that the data have changed.
 239  
      */
 240  
     final void fireDataChanged() {
 241  0
         DataListener[] listeners = listenerList.getListeners(DataListener.class);
 242  
 
 243  0
         for (DataListener listener : listeners) {
 244  0
             listener.dataChanged();
 245  
         }
 246  0
     }
 247  
 }