package com.gtp.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import com.gtp.domain.FreeChair;
import commons.ExistedException;
import commons.NotFoundException;
public class FreeChairDaoImp extends JpaDaoSupport implements FreeChairDao {
@Override
public FreeChair addFreeChair(FreeChair a) throws ExistedException {
getJpaTemplate().persist(a);
return a;
}
@Override
public FreeChair removeFreeChair(FreeChair a) throws NotFoundException {
a = getJpaTemplate().merge(a);
getJpaTemplate().remove(a);
return a;
}
@Override
public FreeChair updateFreeChair(FreeChair a) throws NotFoundException,
ExistedException {
getJpaTemplate().merge(a);
return a;
}
@Override
public FreeChair findByName(final String name) {
return (FreeChair) getJpaTemplate().execute(
new JpaCallback<FreeChair>() {
public FreeChair doInJpa(EntityManager em) {
CriteriaBuilder criteriaBuilder = em
.getCriteriaBuilder();
CriteriaQuery<FreeChair> query = criteriaBuilder
.createQuery(FreeChair.class);
Root<FreeChair> auth = query.from(FreeChair.class);
FreeChair FreeChair = null;
query.select(auth).where(
criteriaBuilder.equal(auth.get("name"), name));
List<FreeChair> result = em.createQuery(query)
.getResultList();
if (result.isEmpty())
return null;
FreeChair = (FreeChair) result.get(0);
return FreeChair;
}
});
}
@Override
public List<FreeChair> findAllFreeChairs() {
return (List<FreeChair>) getJpaTemplate().execute(
new JpaCallback<List<FreeChair>>() {
public List<FreeChair> doInJpa(EntityManager em) {
CriteriaBuilder criteriaBuilder = em
.getCriteriaBuilder();
CriteriaQuery<FreeChair> query = criteriaBuilder
.createQuery(FreeChair.class);
Root<FreeChair> auth = query.from(FreeChair.class);
query.select(auth);
List<FreeChair> result = em.createQuery(query)
.getResultList();
return result;
}
});
}
@Override
public FreeChair find(int id) {
return getJpaTemplate().find(FreeChair.class, id);
}
}