diff --git a/booking-mvc/pom.xml b/booking-mvc/pom.xml index 0469ff0..1bcca0e 100644 --- a/booking-mvc/pom.xml +++ b/booking-mvc/pom.xml @@ -11,7 +11,7 @@ 4.0.0.RELEASE 3.2.0.RELEASE - 2.3.3.RELEASE + 2.4.0.RELEASE 1.7.5 2.0.15 2.0.0 @@ -167,6 +167,9 @@ org.hibernate hibernate-entitymanager 3.6.9.Final + diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/HibernateBookingService.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/HibernateBookingService.java new file mode 100644 index 0000000..18629ba --- /dev/null +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/HibernateBookingService.java @@ -0,0 +1,95 @@ +package org.springframework.webflow.samples.booking; + +import java.util.List; + +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +@Service("bookingService") +@Repository +public class HibernateBookingService implements BookingService { + + private SessionFactory sessionFactory; + + @Autowired + public void setSessionFactory(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + @Transactional(readOnly = true) + @SuppressWarnings("unchecked") + public List findBookings(String username) { + if (username != null) { + return sessionFactory + .getCurrentSession() + .createQuery( + "select b from Booking b where b.user.username = :username order by b.checkinDate") + .setParameter("username", username).list(); + } else { + return null; + } + } + + @Transactional(readOnly = true) + @SuppressWarnings("unchecked") + public List findHotels(SearchCriteria criteria) { + String pattern = getSearchPattern(criteria); + int startIndex = criteria.getPage() * criteria.getPageSize(); + return sessionFactory.getCurrentSession() + .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).setFirstResult(startIndex) + .setMaxResults(criteria.getPageSize()).list(); + } + + @Transactional(readOnly = true) + public Hotel findHotelById(Long id) { + return (Hotel) sessionFactory.getCurrentSession().get(Hotel.class, id); + } + + @Transactional(readOnly = true) + public Booking createBooking(Long hotelId, String username) { + Hotel hotel = (Hotel) sessionFactory.getCurrentSession().get(Hotel.class, hotelId); + User user = findUser(username); + Booking booking = new Booking(hotel, user); + return booking; + } + + @Transactional + public void persistBooking(Booking booking) { + sessionFactory.getCurrentSession().persist(booking); + } + + @Transactional + public void cancelBooking(Long id) { + Booking booking = (Booking) sessionFactory.getCurrentSession().get(Booking.class, id); + if (booking != null) { + sessionFactory.getCurrentSession().delete(booking); + } + } + + // 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) sessionFactory.getCurrentSession() + .createQuery( + "select u from User u where u.username = :username") + .setParameter("username", username).uniqueResult(); + } + +} \ No newline at end of file diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java index e88436e..c59c8fa 100755 --- a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java @@ -15,8 +15,8 @@ import org.springframework.util.StringUtils; * against the backing repository. The EntityManager reference is provided by the managing container (Spring) * automatically. */ -@Service("bookingService") -@Repository +//@Service("bookingService") +//@Repository public class JpaBookingService implements BookingService { private EntityManager em; diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml index 924ede3..1edafa7 100644 --- a/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml +++ b/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml @@ -9,20 +9,32 @@ - - - + + + - - - + + + - - + + + org.springframework.webflow.samples.booking.User + org.springframework.webflow.samples.booking.Booking + org.springframework.webflow.samples.booking.Hotel + - + - hibernate.session_factory_name=mySessionFactory + hibernate.dialect=org.hibernate.dialect.HSQLDialect + hibernate.hbm2ddl.auto=create-drop + hibernate.show_sql=true diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml index 06cb11a..74c1ff0 100644 --- a/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml +++ b/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml @@ -10,8 +10,14 @@ + + + + + + diff --git a/booking-mvc/src/main/webapp/WEB-INF/hotels/booking/booking-flow.xml b/booking-mvc/src/main/webapp/WEB-INF/hotels/booking/booking-flow.xml index f20ddb9..b27dbfa 100644 --- a/booking-mvc/src/main/webapp/WEB-INF/hotels/booking/booking-flow.xml +++ b/booking-mvc/src/main/webapp/WEB-INF/hotels/booking/booking-flow.xml @@ -7,6 +7,8 @@ + + @@ -43,10 +45,10 @@ - + - +