[jboss-cvs] jboss-seam/examples/spring/src/org/jboss/seam/example/spring ...

Michael Youngstrom youngm at gmail.com
Mon Jul 9 19:10:53 EDT 2007


  User: myoungstrom
  Date: 07/07/09 19:10:53

  Modified:    examples/spring/src/org/jboss/seam/example/spring    
                        BookingService.java RegisterAction.java
                        UserService.java HotelBookingAction.java
  Log:
  JBSEAM-992
  
  Revision  Changes    Path
  1.10      +91 -86    jboss-seam/examples/spring/src/org/jboss/seam/example/spring/BookingService.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: BookingService.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/BookingService.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- BookingService.java	3 Jul 2007 20:37:38 -0000	1.9
  +++ BookingService.java	9 Jul 2007 23:10:53 -0000	1.10
  @@ -1,6 +1,5 @@
   package org.jboss.seam.example.spring;
   
  -
   import java.util.Calendar;
   import java.util.Collections;
   import java.util.List;
  @@ -8,7 +7,10 @@
   import javax.persistence.EntityManager;
   import javax.persistence.PersistenceException;
   
  +import org.jboss.seam.Component;
   import org.jboss.seam.annotations.Logger;
  +import org.jboss.seam.annotations.async.Asynchronous;
  +import org.jboss.seam.core.Expressions;
   import org.jboss.seam.log.Log;
   import org.springframework.orm.jpa.JpaCallback;
   import org.springframework.orm.jpa.support.JpaDaoSupport;
  @@ -19,33 +21,34 @@
    *
    * @author Mike Youngstrom
    */
  -public class BookingService 
  -    extends JpaDaoSupport 
  -{
  +public class BookingService extends JpaDaoSupport {
  +
  +	public static ThreadLocal<Boolean> currentThread = new ThreadLocal<Boolean>();
  +
   	@Logger
   	private static Log logger;
  +
       @SuppressWarnings("unchecked")
       @Transactional
       public List<Hotel> findHotels(final String searchPattern, final int firstResult, final int maxResults) {
       	logger.debug("Looking for a Hotel.");
           return getJpaTemplate().executeFind(new JpaCallback() {
           	public Object doInJpa(EntityManager em) throws PersistenceException {
  -                return em.createQuery("select h from Hotel h where lower(h.name) like :search or lower(h.city) like :search or lower(h.zip) like :search or lower(h.address) like :search")
  -                .setParameter("search", searchPattern)
  -                .setMaxResults(maxResults)
  -                .setFirstResult(firstResult)
  +				return em
  +						.createQuery(
  +								"select h from Hotel h where lower(h.name) like :search or lower(h.city) like :search or lower(h.zip) like :search or lower(h.address) like :search")
  +						.setParameter("search", searchPattern).setMaxResults(maxResults).setFirstResult(firstResult)
                   .getResultList();
           	}
           });
       }
   
  -
  -
       @SuppressWarnings("unchecked")
       @Transactional
       public List<Booking> findBookingsByUsername(String username) {
       	logger.debug("Finding Bookings for user {0}", username);
  -        return getJpaTemplate().findByNamedParams("select b from Booking b where b.user.username = :username order by b.checkinDate",
  +		return getJpaTemplate().findByNamedParams(
  +				"select b from Booking b where b.user.username = :username order by b.checkinDate",
                                                     Collections.singletonMap("username", username));
   
       }
  @@ -63,9 +66,7 @@
           }
       }
   
  -    public void validateBooking(Booking booking) 
  -        throws ValidationException 
  -    {
  +	public void validateBooking(Booking booking) throws ValidationException {
           Calendar calendar = Calendar.getInstance();
           calendar.add(Calendar.DAY_OF_MONTH, -1);
   
  @@ -76,26 +77,30 @@
           }
       }
   
  -
  -
       @Transactional
  -    public void bookHotel(Booking booking) 
  -        throws ValidationException 
  -    {
  +	public void bookHotel(Booking booking) throws ValidationException {
           validateBooking(booking);
   
           getJpaTemplate().persist(booking);
           getJpaTemplate().flush();
       }
       
  +	@Asynchronous
       @Transactional
  -    public void testNonWebRequest() {
  -    	List<Hotel> hotels = findHotels("%", 0, 1);
  -    	if(!hotels.isEmpty()) {
  -        	logger.info("Asynchronously found hotel: {0}", hotels.get(0).getName());
  +	public void sendRegisterEmail(String username) {
  +		if (currentThread.get() != null) {
  +			throw new RuntimeException("Not really happening asyncronously");
  +		}
  +		logger.info("pretending to send email asyncronously");
  +		//Could be injected using spring injection just fine but wanted to test
  +		//the use of Expressions Asynchronously
  +		UserService userService = (UserService)Expressions.instance().createValueExpression("#{userService}").getValue();
  +		User user = userService.findUser(username);
  +		if (user != null) {
  +			logger.info("Asynchronously found User: {0}", user.getName());
       		return;
       	}
  -    	logger.info("No Hotels Found.");
  +		throw new RuntimeException("Didn't find the user that made the asynchronous call");
       }
   
       @Transactional
  
  
  
  1.7       +1 -4      jboss-seam/examples/spring/src/org/jboss/seam/example/spring/RegisterAction.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: RegisterAction.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/RegisterAction.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- RegisterAction.java	20 Jun 2007 01:30:36 -0000	1.6
  +++ RegisterAction.java	9 Jul 2007 23:10:53 -0000	1.7
  @@ -1,4 +1,4 @@
  -//$Id: RegisterAction.java,v 1.6 2007/06/20 01:30:36 gavin Exp $
  +//$Id: RegisterAction.java,v 1.7 2007/07/09 23:10:53 myoungstrom Exp $
   package org.jboss.seam.example.spring;
   
   import static org.jboss.seam.ScopeType.EVENT;
  @@ -32,9 +32,6 @@
   
               try {
                   userService.createUser(user);
  -                userService.currentThread.set(true);
  -                userService.sendRegisterEmail();
  -                userService.currentThread.set(null);
                   registered = true;
               } catch(ValidationException e) {
                   facesMessages.add(e.getMessage());
  
  
  
  1.9       +0 -18     jboss-seam/examples/spring/src/org/jboss/seam/example/spring/UserService.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UserService.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/UserService.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- UserService.java	3 Jul 2007 20:37:38 -0000	1.8
  +++ UserService.java	9 Jul 2007 23:10:53 -0000	1.9
  @@ -4,11 +4,6 @@
   import javax.persistence.PersistenceContext;
   import javax.persistence.PersistenceException;
   
  -import org.jboss.seam.annotations.Logger;
  -import org.jboss.seam.annotations.async.Asynchronous;
  -import org.jboss.seam.log.Log;
  -import org.springframework.aop.framework.AopContext;
  -import org.springframework.aop.support.AopUtils;
   import org.springframework.transaction.annotation.Transactional;
   
   /**
  @@ -16,10 +11,6 @@
    *
    */
   public class UserService {
  -	@Logger
  -	private static Log logger;
  -
  -	public static ThreadLocal<Boolean> currentThread = new ThreadLocal<Boolean>();
   
   	@PersistenceContext
       private EntityManager entityManager;
  @@ -73,14 +64,5 @@
               throw new ValidationException("Username "+user.getUsername()+" already exists");
           }
           entityManager.persist(user);
  -        logger.info("Created a new User: {0}", user.getName());
  -    }
  -	
  -	@Asynchronous
  -	public void sendRegisterEmail() {
  -		if(currentThread.get() != null) {
  -			throw new RuntimeException("Not really happening asyncrohously");
  -		}
  -		logger.info("pretending to send email asyncronously");
   	}
   }
  
  
  
  1.5       +97 -90    jboss-seam/examples/spring/src/org/jboss/seam/example/spring/HotelBookingAction.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: HotelBookingAction.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/HotelBookingAction.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- HotelBookingAction.java	20 Jun 2007 01:30:36 -0000	1.4
  +++ HotelBookingAction.java	9 Jul 2007 23:10:53 -0000	1.5
  @@ -1,8 +1,10 @@
  -//$Id: HotelBookingAction.java,v 1.4 2007/06/20 01:30:36 gavin Exp $
  +//$Id: HotelBookingAction.java,v 1.5 2007/07/09 23:10:53 myoungstrom Exp $
   package org.jboss.seam.example.spring;
   
   import java.util.Calendar;
   import javax.faces.application.FacesMessage;
  +
  +import org.jboss.seam.Component;
   import org.jboss.seam.annotations.Begin;
   import org.jboss.seam.annotations.End;
   import org.jboss.seam.annotations.In;
  @@ -76,6 +78,11 @@
               return null;
           }
   
  +        BookingService.currentThread.set(true);
  +        //Cannot call this.sendRegisterEmail because it won't hit the @Asynchronous intercepter
  +        bookingService.sendRegisterEmail(booking.getUser().getUsername());
  +        BookingService.currentThread.set(null);
  +
           facesMessages.add("Thank you, #{user.name}, your confimation number for #{hotel.name} is #{booking.id}");
           log.info("New booking: #{booking.id} for #{user.username}");
   
  
  
  



More information about the jboss-cvs-commits mailing list