Clover Coverage Report - Kuali Student 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 05:03:32 EDT
../../../../../img/srcFileCovDistChart0.png 2% of files have more coverage
21   91   13   1.91
4   69   0.62   11
11     1.18  
1    
 
  GenericEntityDao       Line # 15 21 0% 13 36 0% 0.0
 
No Tests
 
1    package org.kuali.student.enrollment.dao;
2   
3    import java.io.Serializable;
4    import java.lang.reflect.ParameterizedType;
5    import java.lang.reflect.Type;
6    import java.util.ArrayList;
7    import java.util.List;
8   
9    import javax.persistence.EntityManager;
10    import javax.persistence.PersistenceContext;
11   
12    /**
13    * @author Igor
14    */
 
15    public class GenericEntityDao<T> implements EntityDao<T> {
16   
17    /**
18    * Entity class.
19    */
20    protected Class<T> entityClass;
21   
22    @PersistenceContext
23    protected EntityManager em;
24   
 
25  0 toggle public GenericEntityDao() {
26  0 entityClass = getEntityClass();
27    }
28   
 
29  0 toggle @Override
30    public T find(Serializable primaryKey) {
31  0 return em.find(entityClass, primaryKey);
32    }
33   
 
34  0 toggle @Override
35    public List<T> findByIds(List<? extends Serializable> primaryKeys) {
36  0 List<T> resultList = new ArrayList<T>();
37  0 for (Serializable primaryKey : primaryKeys) {
38  0 resultList.add(find(primaryKey));
39    }
40  0 return resultList;
41    }
42   
 
43  0 toggle @Override
44    @SuppressWarnings("unchecked")
45    public List<T> findAll() {
46  0 return (List<T>) em.createQuery("from " + entityClass.getSimpleName()).getResultList();
47    }
48   
 
49  0 toggle @Override
50    public void persist(T entity) {
51  0 em.persist(entity);
52    }
53   
 
54  0 toggle @Override
55    public void update(T entity) {
56  0 em.refresh(entity);
57    }
58   
 
59  0 toggle @Override
60    public void remove(T entity) {
61  0 em.remove(entity);
62    }
63   
 
64  0 toggle @Override
65    public <T> T merge(T entity) {
66  0 return em.merge(entity);
67    }
68   
 
69  0 toggle @SuppressWarnings("unchecked")
70    protected <K extends T> Class<K> getEntityClass() {
71  0 if (entityClass == null) {
72  0 entityClass = (Class<T>) getEntityType(this);
73    }
74  0 return (Class<K>) entityClass;
75    }
76   
 
77  0 toggle private Type getEntityType(Object object) {
78  0 Type type = object.getClass().getGenericSuperclass();
79  0 if (type instanceof ParameterizedType) {
80  0 ParameterizedType paramType = (ParameterizedType) type;
81  0 Type result = paramType.getActualTypeArguments()[0];
82  0 return result;
83    } else {
84  0 throw new IllegalArgumentException("Could not guess entity type by reflection.");
85    }
86    }
87   
 
88  0 toggle public void setEm(EntityManager em) {
89  0 this.em = em;
90    }
91    }