| 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 |
| 14 |
|
|
|
|
|
| 0% |
Uncovered Elements: 36 (36) |
Complexity: 13 |
Complexity Density: 0.62 |
|
| 15 |
|
public class GenericEntityDao<T> implements EntityDao<T> { |
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
protected Class<T> entityClass; |
| 21 |
|
|
| 22 |
|
@PersistenceContext |
| 23 |
|
protected EntityManager em; |
| 24 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 25 |
0
|
public GenericEntityDao() {... |
| 26 |
0
|
entityClass = getEntityClass(); |
| 27 |
|
} |
| 28 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 29 |
0
|
@Override... |
| 30 |
|
public T find(Serializable primaryKey) { |
| 31 |
0
|
return em.find(entityClass, primaryKey); |
| 32 |
|
} |
| 33 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 34 |
0
|
@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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
0
|
@Override... |
| 44 |
|
@SuppressWarnings("unchecked") |
| 45 |
|
public List<T> findAll() { |
| 46 |
0
|
return (List<T>) em.createQuery("from " + entityClass.getSimpleName()).getResultList(); |
| 47 |
|
} |
| 48 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 49 |
0
|
@Override... |
| 50 |
|
public void persist(T entity) { |
| 51 |
0
|
em.persist(entity); |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 54 |
0
|
@Override... |
| 55 |
|
public void update(T entity) { |
| 56 |
0
|
em.refresh(entity); |
| 57 |
|
} |
| 58 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 59 |
0
|
@Override... |
| 60 |
|
public void remove(T entity) { |
| 61 |
0
|
em.remove(entity); |
| 62 |
|
} |
| 63 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 64 |
0
|
@Override... |
| 65 |
|
public <T> T merge(T entity) { |
| 66 |
0
|
return em.merge(entity); |
| 67 |
|
} |
| 68 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 69 |
0
|
@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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 77 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 88 |
0
|
public void setEm(EntityManager em) {... |
| 89 |
0
|
this.em = em; |
| 90 |
|
} |
| 91 |
|
} |