/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dbcontrollers;
import dbbeans.Trees;
import dbcontrollers.exceptions.NonexistentEntityException;
import dbcontrollers.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
/**
*
* @author Laur
*/
public class TreesJpaController implements Serializable {
public TreesJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Trees trees) throws RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
em.persist(trees);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Trees trees) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
trees = em.merge(trees);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = trees.getId();
if (findTrees(id) == null) {
throw new NonexistentEntityException("The trees with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
Trees trees;
try {
trees = em.getReference(Trees.class, id);
trees.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The trees with id " + id + " no longer exists.", enfe);
}
em.remove(trees);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public List<Trees> findTreesEntities() {
return findTreesEntities(true, -1, -1);
}
public List<Trees> findTreesEntities(int maxResults, int firstResult) {
return findTreesEntities(false, maxResults, firstResult);
}
private List<Trees> findTreesEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Trees.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Trees findTrees(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Trees.class, id);
} finally {
em.close();
}
}
public int getTreesCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Trees> rt = cq.from(Trees.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
public int findTreeByName(String name){
List<Trees> trees=findTreesEntities();
for (Trees t:trees)
if (t.getName().equals(name))
return t.getId();
return -1;
}
}