On 9/4/06, Adrian Mitev <[EMAIL PROTECTED]> wrote:

BTW could i completely disable the exception handler?


Without caching exceptions, you're likely going to break the contract that
destroy() will *always* be called if init() is called.  However, if that's
what you want to do, then simply replace the ExceptionHandler (as described
above) with something that does a different behavior in the
handleException() method -- either throw the exception immediately, or log
and throw away, or whatever you want.  If there are no cached exceptions
(because you bypassed the behavior of the standard exception handler) then
the forwarding machinery won't kick in.

For finer grained control, note that handleException() is called
*immediately* when the exception is thrown by your event handler method, so
you can examine its call stack and determine what to do based on where it
came from.

Craig

What would you/*
* Logic.java
*
* Created on July 31, 2006, 11:47 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package shortfin.logic;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import shortfin.entities.Person;
import shortfin.entities.Trip;
import shortfin.entities.TripType;

/**
* <p>Class containing the business logic for this application.  This is
* separated so that it can be used separately, or in conjunction with
* an EJB or web service that wraps it.</p>
*/
public class Logic {


   // ------------------------------------------------------------
Constructors


   /**
    * <p>Construct a default instance.</p>
    */
   public Logic() {
       ;
   }


   /**
    * <p>Construct a fully configured instance.</p>
    *
    * @param emf The entity manager factory to be used
    */
   public Logic(EntityManagerFactory emf) {
       this.entityManagerFactory = emf;
   }


   // ------------------------------------------------------- Public
Properties


   /**
    * <p>The entity manager factory we will use for accessing the database.
    * <strong>WARNING</strong>:  resource injection will only occur
    * if this is a container managed object (such as a JSF managed
    * bean), so provide getter/setter methods as well to allow
    * manual injection.</p>
    */
   @PersistenceUnit(unitName="travel")
   EntityManagerFactory entityManagerFactory;


   /**
    * <p>Return the entity manager factory used for accessing the
database.</p>
    */
   public EntityManagerFactory getEntityManagerFactory() {
       return this.entityManagerFactory;
   }


   /**
    * <p>Set the entity manager factory used for accessing the
database.</p>
    *
    * @param entityManagerFactory The new entity manager factory
    */
   public void setEntityManagerFactory
     (EntityManagerFactory entityManagerFactory) {
       this.entityManagerFactory = entityManagerFactory;
   }


   /**
    * <p>Read-only property to return all [EMAIL PROTECTED] Person} instances.
    * You would not usually see such a method in a real application,
    * but we know that our demo database has a very constrained
    * set of rows.</p>
    */
   public List<Person> getPersons() {

       EntityManager em = entityManager();
       try {
           Query query = em.createQuery("SELECT p FROM Person p");
           return query.getResultList();
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   /**
    * <p>Read-only property to return all [EMAIL PROTECTED] Triptype} instances.
    */
   public List<TripType> getTripTypes() {

       EntityManager em = entityManager();
       try {
           Query query = em.createQuery("SELECT t FROM Triptype t");
           return query.getResultList();
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   // ---------------------------------------------------------- Public
Methods


   /**
    * <p>Add a new [EMAIL PROTECTED] Person} instance.</p>
    *
    * @param person The [EMAIL PROTECTED] Person} instance to be added
    *
    * @return The updated [EMAIL PROTECTED] Person} instance
    */
   public Person addPerson(Person person) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           em.persist(person);
           et.commit();
           return person;
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   /**
    * <p>Add a new [EMAIL PROTECTED] Trip} for the specified [EMAIL PROTECTED] 
Person}.</p>
    *
    * @param person The [EMAIL PROTECTED] Person} for whom to add a new [EMAIL 
PROTECTED] Trip}
    * @param trip The [EMAIL PROTECTED] Trip} to be added
    *
    * @return The updated [EMAIL PROTECTED] Trip} instance
    */
   public Trip addTrip(Person person, Trip trip) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           person = em.find(Person.class, person.getId());
           trip.setPerson(person);
           em.persist(trip);
           person.getTrips().add(trip);
           et.commit();
           return trip;
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   /**
    * <p>Find an existing [EMAIL PROTECTED] Person} instance by id.</p>
    *
    * @return The found [EMAIL PROTECTED] Person} instance, if any;
    *  otherwise <code>null</code>
    */
   public Person findPersonById(int id) {

       EntityManager em = entityManager();
       try {
           return em.find(Person.class, id);
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   /**
    * <p>Find an existing [EMAIL PROTECTED] Trip} instance by id.</p>
    *
    * @return The found [EMAIL PROTECTED] Trip} instance, if any;
    *  otherwise <code>null</code>
    */
   public Trip findTripById(int id) {

       EntityManager em = entityManager();
       try {
           return em.find(Trip.class, id);
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   /**
    * <p>Return a list of [EMAIL PROTECTED] Trip}s for the specified
    * [EMAIL PROTECTED] Person}.</p>
    *
    * @param person [EMAIL PROTECTED] Person} for which to find trips
    */
   public List<Trip> findTripsByPerson(Person person) {

       EntityManager em = entityManager();
       try {
           Query query = em.createQuery("SELECT t FROM Trip t WHERE
t.personid = :personid");
           query.setParameter("personid", person.getId());
           return query.getResultList();
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   /**
    * <p>Find an existing [EMAIL PROTECTED] Triptype} instance by id.</p>
    *
    * @return The found [EMAIL PROTECTED] Triptype} instance, if any;
    *  otherwise <code>null</code>
    */
   public TripType findTripTypeById(int id) {

       EntityManager em = entityManager();
       try {
           return em.find(TripType.class, id);
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           em.close();
       }

   }


   /**
    * <p>Remove an existing [EMAIL PROTECTED] Person} instance.</p>
    *
    * @param person The [EMAIL PROTECTED] Person} instance to be removed
    */
   public void removePerson(Person person) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           person = em.merge(person);
           em.remove(person);
           et.commit();
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   /**
    * <p>Remove an existing [EMAIL PROTECTED] Trip} instance.</p>
    *
    * @param trip The [EMAIL PROTECTED] Trip} instance to be removed
    */
   public void removeTrip(Trip trip) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           trip = em.merge(trip);
           Person person = em.find(Person.class, trip.getPerson().getId());
           person.getTrips().remove(trip);
           em.remove(trip);
           et.commit();
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   /**
    * <p>Update an existing [EMAIL PROTECTED] Person} instance.</p>
    *
    * @param person The [EMAIL PROTECTED] Person} instance to be updated
    *
    * @return The updated [EMAIL PROTECTED] Person} instance
    */
   public Person updatePerson(Person person) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           person = em.merge(person);
           et.commit();
           return person;
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   /**
    * <p>Update an existing [EMAIL PROTECTED] Trip} instance.</p>
    *
    * @param trip The [EMAIL PROTECTED] Trip} instance to be updated
    *
    * @return The updated [EMAIL PROTECTED] Trip} instance
    */
   public Trip updateTrip(Trip trip) {

       EntityManager em = entityManager();
       EntityTransaction et = null;
       try {
           et = em.getTransaction();
           et.begin();
           trip = em.merge(trip);
           et.commit();
           return trip;
       } catch (PersistenceException e) {
           throw e;
       } catch (Exception e) {
           throw new PersistenceException(e);
       } finally {
           if ((et != null) && et.isActive()) {
               et.rollback();
           }
           em.close();
       }

   }


   // --------------------------------------------------------- Support
Methods


   /**
    * <p>Return an entity manager instance that may be used for a specific
    * transaction, and then closed.</p>
    */
   private EntityManager entityManager() {

       return entityManagerFactory.createEntityManager();

   }


}


--
Seeing is believing

Reply via email to