# HG changeset patch # User Raimund Renkert # Date 1369389061 -7200 # Node ID 13178bbe77ff33cc96282e76aa4ba7944fd0ee5b # Parent 28b9167e5a5bea534c810326f2ed8edf2eb1ebf5 New generic repository for data requests. diff -r 28b9167e5a5b -r 13178bbe77ff src/main/java/de/intevation/lada/data/Repository.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/data/Repository.java Fri May 24 11:51:01 2013 +0200 @@ -0,0 +1,53 @@ +package de.intevation.lada.data; + +import java.util.List; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import javax.inject.Named; + +/** + * This generic Container is an interface to request and select Data + * obejcts from the connected database. + * + * @author Raimund Renkert + */ +@Named +@ApplicationScoped +public class Repository +{ + /** + * The entitymanager managing the data. + */ + @Inject + private EntityManager em; + + /** + * Get all objects of type clazzfrom database. + * + * @param clazz The class type. + * @return List of objects. + */ + public List findAll(Class clazz) { + CriteriaBuilder builder = em.getCriteriaBuilder(); + CriteriaQuery criteria = builder.createQuery(clazz); + Root member = criteria.from(clazz); + criteria.select(member); + return em.createQuery(criteria).getResultList(); + } + + /** + * Find a single object identified by its id. + * + * @param clazz The class type. + * @param id The object id. + * @return The requested object of type clazz + */ + public T findById(Class clazz, String id) { + return em.find(clazz, id); + } +}