| 1 | |
package org.kuali.student.enrollment.dao; |
| 2 | |
|
| 3 | |
import org.kuali.student.r2.common.exceptions.DoesNotExistException; |
| 4 | |
|
| 5 | |
import java.io.Serializable; |
| 6 | |
import java.lang.reflect.ParameterizedType; |
| 7 | |
import java.lang.reflect.Type; |
| 8 | |
import java.util.ArrayList; |
| 9 | |
import java.util.List; |
| 10 | |
|
| 11 | |
import javax.persistence.EntityManager; |
| 12 | |
import javax.persistence.PersistenceContext; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
public class GenericEntityDao<T> implements EntityDao<T> { |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
protected Class<T> entityClass; |
| 23 | |
|
| 24 | |
@PersistenceContext |
| 25 | |
protected EntityManager em; |
| 26 | |
|
| 27 | 0 | public GenericEntityDao() { |
| 28 | 0 | entityClass = getEntityClass(); |
| 29 | 0 | } |
| 30 | |
|
| 31 | |
@Override |
| 32 | |
public T find(Serializable primaryKey) { |
| 33 | 0 | return em.find(entityClass, primaryKey); |
| 34 | |
} |
| 35 | |
|
| 36 | |
@Override |
| 37 | |
public List<T> findByIds(List<? extends Serializable> primaryKeys) throws DoesNotExistException { |
| 38 | 0 | List<T> resultList = new ArrayList<T>(); |
| 39 | 0 | for (Serializable primaryKey : primaryKeys) { |
| 40 | |
|
| 41 | 0 | T entity = find(primaryKey); |
| 42 | |
|
| 43 | 0 | if (entity == null) { |
| 44 | |
|
| 45 | 0 | throw new DoesNotExistException("No data was found for :" + primaryKey); |
| 46 | |
|
| 47 | |
} |
| 48 | 0 | resultList.add(entity); |
| 49 | 0 | } |
| 50 | 0 | return resultList; |
| 51 | |
} |
| 52 | |
|
| 53 | |
@Override |
| 54 | |
@SuppressWarnings("unchecked") |
| 55 | |
public List<T> findAll() { |
| 56 | 0 | return (List<T>) em.createQuery("from " + entityClass.getSimpleName()).getResultList(); |
| 57 | |
} |
| 58 | |
|
| 59 | |
@Override |
| 60 | |
public void persist(T entity) { |
| 61 | 0 | em.persist(entity); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public void update(T entity) { |
| 66 | 0 | em.merge(entity); |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
@Override |
| 70 | |
public void remove(T entity) { |
| 71 | 0 | em.remove(entity); |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
@Override |
| 75 | |
public T merge(T entity) { |
| 76 | 0 | return em.merge(entity); |
| 77 | |
} |
| 78 | |
|
| 79 | |
@SuppressWarnings("unchecked") |
| 80 | |
protected <K extends T> Class<K> getEntityClass() { |
| 81 | 0 | if (entityClass == null) { |
| 82 | 0 | entityClass = (Class<T>) getEntityType(this); |
| 83 | |
} |
| 84 | 0 | return (Class<K>) entityClass; |
| 85 | |
} |
| 86 | |
|
| 87 | |
private Type getEntityType(Object object) { |
| 88 | 0 | Type type = object.getClass().getGenericSuperclass(); |
| 89 | 0 | if (type instanceof ParameterizedType) { |
| 90 | 0 | ParameterizedType paramType = (ParameterizedType) type; |
| 91 | 0 | Type result = paramType.getActualTypeArguments()[0]; |
| 92 | 0 | return result; |
| 93 | |
} else { |
| 94 | 0 | throw new IllegalArgumentException("Could not guess entity type by reflection."); |
| 95 | |
} |
| 96 | |
} |
| 97 | |
|
| 98 | |
public void setEm(EntityManager em) { |
| 99 | 0 | this.em = em; |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
public EntityManager getEm() { |
| 103 | 0 | return em; |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
} |