[seam-commits] Seam SVN: r13281 - examples/trunk/booking-simplified/src/main/java/org/jboss/seam/examples/booking/bootstrap.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sat Jun 19 21:57:33 EDT 2010


Author: pete.muir at jboss.org
Date: 2010-06-19 21:57:33 -0400 (Sat, 19 Jun 2010)
New Revision: 13281

Modified:
   examples/trunk/booking-simplified/src/main/java/org/jboss/seam/examples/booking/bootstrap/ApplicationInitializer.java
Log:
Use EJB to startup, not hacked onto JSF event

Modified: examples/trunk/booking-simplified/src/main/java/org/jboss/seam/examples/booking/bootstrap/ApplicationInitializer.java
===================================================================
--- examples/trunk/booking-simplified/src/main/java/org/jboss/seam/examples/booking/bootstrap/ApplicationInitializer.java	2010-06-20 01:43:49 UTC (rev 13280)
+++ examples/trunk/booking-simplified/src/main/java/org/jboss/seam/examples/booking/bootstrap/ApplicationInitializer.java	2010-06-20 01:57:33 UTC (rev 13281)
@@ -24,15 +24,14 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.ejb.Stateless;
-import javax.enterprise.event.Observes;
+import javax.annotation.PostConstruct;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
 import javax.enterprise.inject.Alternative;
-import javax.faces.event.PostConstructApplicationEvent;
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
+import javax.transaction.UserTransaction;
 
 import org.jboss.seam.examples.booking.model.Hotel;
 import org.jboss.seam.examples.booking.model.User;
@@ -41,12 +40,15 @@
 /**
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
  */
- at Stateless
+ at Singleton @Startup
 @Alternative
 public class ApplicationInitializer
 {
    @PersistenceContext
-   private EntityManager em;
+   private EntityManager entityManager;
+   
+   @Inject
+   private UserTransaction utx;
 
    @Inject
    Logger log;
@@ -88,7 +90,8 @@
       hotels.add(new Hotel(100, 2, "Hotel Cammerpoorte", "Nationalestraat 38-40", "Antwerp", null, "2000", "BE"));
    }
 
-   public void init(@Observes final PostConstructApplicationEvent event)
+   @PostConstruct
+   public void init()
    {
       try
       {
@@ -101,7 +104,7 @@
       }
    }
 
-   private void persist(final List entities)
+   private void persist(List<?> entities)
    {
       for (Object e : entities)
       {
@@ -109,18 +112,25 @@
       }
    }
 
-   private void persist(final Object entity)
+   private void persist(Object entity)
    {
       try
       {
-         em.persist(entity);
+         utx.begin();
+         entityManager.persist(entity);
+         utx.commit();
       }
-      catch (ConstraintViolationException e)
+      catch (Exception e) 
       {
-         for (ConstraintViolation v : e.getConstraintViolations())
+         log.error("Error importing data: " + entity, e);
+         try
          {
-            log.error("Cannot persist entity because it has validation errors " + v.getRootBean() + ": " + v.getPropertyPath() + " " + v.getMessage());
+            utx.rollback();
          }
+         catch (Exception e1)
+         {
+            log.error("Error rolling back transaction", e1);
+         }
       }
    }
 }



More information about the seam-commits mailing list