package simplejpa;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import simplejpa.exceptions.NonexistentEntityException;

/**
 * @author Daniel "3ICE" Berezvai
 */
public class PlanetJpaController implements Serializable {

public PlanetJpaController(EntityManagerFactory emf) {
  this.emf = emf;
}
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
  return emf.createEntityManager();
}

public void create(Planet planet) {
  EntityManager em = null;
  try {
    em = getEntityManager();
    em.getTransaction().begin();
    em.persist(planet);
    em.getTransaction().commit();
  } finally {
    if (em != null) {
      em.close();
    }
  }
}

public void edit(Planet planet) throws NonexistentEntityException, Exception {
  EntityManager em = null;
  try {
    em = getEntityManager();
    em.getTransaction().begin();
    planet = em.merge(planet);
    em.getTransaction().commit();
  } catch (Exception ex) {
    String msg = ex.getLocalizedMessage();
    if (msg == null || msg.length() == 0) {
      Long id = planet.getId();
      if (findPlanet(id) == null) {
        throw new NonexistentEntityException("The planet with id " + id
                                             + " no longer exists.");
      }
    }
    throw ex;
  } finally {
    if (em != null) {
      em.close();
    }
  }
}

public void destroy(Long id) throws NonexistentEntityException {
  EntityManager em = null;
  try {
    em = getEntityManager();
    em.getTransaction().begin();
    Planet planet;
    try {
      planet = em.getReference(Planet.class, id);
      planet.getId();
    } catch (EntityNotFoundException enfe) {
      throw new NonexistentEntityException("The planet with id " + id
                                           + " no longer exists.", enfe);
    }
    em.remove(planet);
    em.getTransaction().commit();
  } finally {
    if (em != null) {
      em.close();
    }
  }
}

public List<Planet> findPlanetEntities() {
  return findPlanetEntities(true, -1, -1);
}

public List<Planet> findPlanetEntities(int maxResults, int firstResult) {
  return findPlanetEntities(false, maxResults, firstResult);
}

private List<Planet> findPlanetEntities(boolean all, int maxResults,
                                        int firstResult) {
  EntityManager em = getEntityManager();
  try {
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(Planet.class));
    Query q = em.createQuery(cq);
    if (!all) {
      q.setMaxResults(maxResults);
      q.setFirstResult(firstResult);
    }
    return q.getResultList();
  } finally {
    em.close();
  }
}

public Planet findPlanet(Long id) {
  EntityManager em = getEntityManager();
  try {
    return em.find(Planet.class, id);
  } finally {
    em.close();
  }
}

public int getPlanetCount() {
  EntityManager em = getEntityManager();
  try {
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    Root<Planet> rt = cq.from(Planet.class);
    cq.select(em.getCriteriaBuilder().count(rt));
    Query q = em.createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
  } finally {
    em.close();
  }
}
}
