public List<Establishment> getEstablishments() {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     final TypedQuery<Establishment> query =
         em.createNamedQuery(Establishment.QUERY_ALL, Establishment.class);
     return query.getResultList();
   } finally {
     em.close();
   }
 }
 public List<Review> getReviewsByEstablishment(Establishment establishment) {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     final TypedQuery<Review> query =
         em.createNamedQuery(Review.QUERY_BY_ESTABLISHMENT, Review.class);
     query.setParameter("establishment", establishment);
     return query.getResultList();
   } finally {
     em.close();
   }
 }
 public Establishment getEstablishmentByUsername(String username) {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     final TypedQuery<Establishment> query =
         em.createNamedQuery(Establishment.QUERY_BY_USERNAME, Establishment.class);
     query.setParameter("username", username);
     return query.getSingleResult();
   } finally {
     em.close();
   }
 }
 public Establishment getEstablishment(long establishmentId) {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     final Establishment result = em.find(Establishment.class, establishmentId);
     if (result == null) {
       throw new IllegalArgumentException("No establishment with id: " + establishmentId);
     }
     return result;
   } finally {
     em.close();
   }
 }
 /**
  * Returns the delegated Session. If the persistence context is transaction-scoped, the Session
  * associated with the current transaction will be used.
  *
  * @return the Session
  */
 private Session getSession() {
   // a transaction-scoped persistence context
   try {
     Transaction trx = tm.getTransaction();
     if (trx == null) {
       throw new IllegalStateException(
           "A transaction is not active - ensure the component is executing in a managed transaction");
     }
     EntityManager em = emService.getEntityManager(unitName, this, trx);
     return (Session) em.getDelegate();
   } catch (SystemException | Fabric3Exception e) {
     throw new ServiceRuntimeException(e);
   }
 }
 public Establishment updateEstablishment(Establishment establishment) {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     em.getTransaction().begin();
     final Establishment result = em.merge(establishment);
     em.getTransaction().commit();
     return result;
   } finally {
     if (em.getTransaction().isActive()) {
       em.getTransaction().rollback();
     }
     em.close();
   }
 }
 public Establishment createEstablishment(Establishment establishment) {
   establishment.setPassword(authenticationService.encryptPassword(establishment.getPassword()));
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     em.getTransaction().begin();
     em.persist(establishment);
     em.getTransaction().commit();
     return establishment;
   } finally {
     if (em.getTransaction().isActive()) {
       em.getTransaction().rollback();
     }
     em.close();
   }
 }
 public Review createReview(Review review, long establishment) {
   final EntityManager em = entityManagerService.createEntityManager();
   try {
     em.getTransaction().begin();
     review.setCreatedOn(new Date());
     review.setEstablishment(em.find(Establishment.class, establishment));
     em.persist(review);
     em.getTransaction().commit();
     return review;
   } finally {
     if (em.getTransaction().isActive()) {
       em.getTransaction().rollback();
     }
     em.close();
   }
 }