From bf4144d9dbe8e9c2688d2d99b89e7bd42078480c Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sat, 15 Mar 2008 07:07:43 +0000 Subject: [PATCH] bind target initial commit sample polishing --- .../samples/booking/BookingService.java | 22 +++++----- .../samples/booking/JpaBookingService.java | 41 +++++++++++++------ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/BookingService.java b/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/BookingService.java index 5d109fcf..854859b9 100755 --- a/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/BookingService.java +++ b/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/BookingService.java @@ -17,10 +17,10 @@ public interface BookingService { /** * Find hotels available for booking by some criteria. - * @param searchCriteria the search criteria - * @return a list of hotels not exceeding the page size + * @param criteria the search criteria + * @return a list of hotels meeting the criteria */ - public List findHotels(SearchCriteria searchCriteria); + public List findHotels(SearchCriteria criteria); /** * Find hotels by their identifier. @@ -29,16 +29,18 @@ public interface BookingService { */ public Hotel findHotelById(Long id); + /** + * Create a new, transient hotel booking instance for the given user. + * @param hotelId the hotelId + * @param userName the user name + * @return the new transient booking instance + */ + public Booking createBooking(Long hotelId, String userName); + /** * Cancel an existing booking. * @param id the booking id */ - public void cancelBooking(Long id); + public void cancelBooking(Booking booking); - /** - * Lookup a user based on their username - * @param username the user's username - * @return the user - */ - public User findUser(String username); } diff --git a/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java b/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java index a29348d6..13d7618d 100755 --- a/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java +++ b/spring-webflow-samples/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java @@ -6,6 +6,7 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; @@ -14,6 +15,7 @@ import org.springframework.util.StringUtils; * against the backing repository. The EntityManager reference is provided by the managing container (Spring) * automatically. */ +@Service("bookingService") @Repository public class JpaBookingService implements BookingService { @@ -37,14 +39,12 @@ public class JpaBookingService implements BookingService { @Transactional(readOnly = true) @SuppressWarnings("unchecked") - public List findHotels(SearchCriteria search) { - String pattern = !StringUtils.hasText(search.getSearchString()) ? "%" : "%" - + search.getSearchString().toLowerCase().replace('*', '%') + "%"; + public List findHotels(SearchCriteria criteria) { + String pattern = getSearchPattern(criteria); return em.createQuery( - "select h from Hotel h where lower(h.name) like :pattern or lower(h.city) like :pattern " - + "or lower(h.zip) like :pattern or lower(h.address) like :pattern").setParameter("pattern", - pattern).setMaxResults(search.getPageSize()).setFirstResult(search.getPage() * search.getPageSize()) - .getResultList(); + "select h from Hotel h where lower(h.name) like " + pattern + " or lower(h.city) like " + pattern + + " or lower(h.zip) like " + pattern + " or lower(h.address) like " + pattern).setMaxResults( + criteria.getPageSize()).setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList(); } @Transactional(readOnly = true) @@ -52,18 +52,35 @@ public class JpaBookingService implements BookingService { return em.find(Hotel.class, id); } - // this one is a read/write transaction + @Transactional(readOnly = true) + public Booking createBooking(Long hotelId, String username) { + Hotel hotel = em.find(Hotel.class, hotelId); + User user = findUser(username); + return new Booking(hotel, user); + } + + // read-write transactional methods @Transactional - public void cancelBooking(Long id) { - Booking booking = em.find(Booking.class, id); + public void cancelBooking(Booking booking) { + booking = em.find(Booking.class, booking.getId()); if (booking != null) { em.remove(booking); } } - @Transactional(readOnly = true) - public User findUser(String username) { + // helpers + + private String getSearchPattern(SearchCriteria criteria) { + if (StringUtils.hasText(criteria.getSearchString())) { + return "'%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'"; + } else { + return "'%'"; + } + } + + private User findUser(String username) { return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username", username).getSingleResult(); } + } \ No newline at end of file