/**
* This file is part of Faktotum.
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import de.romankreisel.faktotum.FaktotumObject;
import de.romankreisel.faktotum.datamodel.FaktotumEntity;
import de.romankreisel.faktotum.datamodel.FaktotumEntity_;
U Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Faktotum.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
package de.romankreisel.faktotum.dao;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import de.romankreisel.faktotum.FaktotumObject;
import de.romankreisel.faktotum.datamodel.FaktotumEntity;
import de.romankreisel.faktotum.datamodel.FaktotumEntity_;
@Stateless
public abstract class JpaDao<E extends FaktotumEntity> extends FaktotumObject implements Dao<E> {
@EJB
StorageService storageService;
/**
* Create a query which returns a Long value
*
* @param criteriaQuery
* The query criteria
* @return A TypedQuery for the criteria
*/
public TypedQuery<Long> createLongQuery(CriteriaQuery<Long> criteriaQuery) {
return this.getStorageService().getEntityManager().createQuery(criteriaQuery);
}
/**
* Create a typed query for the given criteria
*
* @param criteriaQuery
* the query criteria
* @return A TypedQuery for the criteria
*/
public TypedQuery<E> createQuery(CriteriaQuery<E> criteriaQuery) {
return this.getStorageService().getEntityManager().createQuery(criteriaQuery);
}
@Override
public E findById(Long id, Class<E> searchedClass) {
CriteriaBuilder criteriaBuilder = this.getCriteriaBuilder();
CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(searchedClass);
Root<E> entityRoot = criteriaQuery.from(searchedClass);
Predicate condition = criteriaBuilder.equal(entityRoot.get(FaktotumEntity_.id), id);
criteriaQuery.where(condition);
TypedQuery<E> typedQuery = this.createQuery(criteriaQuery);
return typedQuery.getSingleResult();
}
@Override
public List<E> getAll(Class<E> searchedClass) {
CriteriaBuilder criteriaBuilder = this.getCriteriaBuilder();
CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(searchedClass);
TypedQuery<E> typedQuery = this.createQuery(criteriaQuery);
return typedQuery.getResultList();
}
/*
* (non-Javadoc)
*
* @see de.romankreisel.faktotum.dao.Dao#getCount(java.lang.Class)
*/
@Override
public Long getCount(Class<E> searchedClass) {
CriteriaBuilder criteriaBuilder = this.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
criteriaQuery.select(criteriaBuilder.count(criteriaQuery.from(searchedClass)));
TypedQuery<Long> typedQuery = this.createLongQuery(criteriaQuery);
return typedQuery.getSingleResult();
}
/**
* @return the criteria builder
*/
public CriteriaBuilder getCriteriaBuilder() {
return this.getStorageService().getCriteriaBuilder();
}
/**
* @return the storage service
*/
public StorageService getStorageService() {
if (this.storageService == null) {
throw new NullPointerException("StorageService not initialized!");
}
return this.storageService;
}
@Override
public E merge(E entity) {
try {
return this.getStorageService().getEntityManager().merge(entity);
} catch (Exception e) {
this.getLogger().log(Level.SEVERE, "Error merging FaktotumEntity", e);
throw e;
}
}
@Override
public void persist(E entity) {
try {
this.getStorageService().getEntityManager().persist(entity);
} catch (Exception e) {
this.getLogger().log(Level.SEVERE, "Error persisting FaktotumEntity", e);
throw e;
}
}
@Override
public void remove(E entity) {
this.getStorageService().getEntityManager().remove(entity);
}
}