From 15a3ac6cb260b76fc02582ec4b191afa7ae69ad7 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 20 Dec 2013 14:57:41 -0500 Subject: [PATCH] Upgrade to Spring Framework 4.0 and Mojara 2.2.4 --- booking-faces/pom.xml | 15 +- .../samples/booking/HotelLazyDataModel.java | 79 +++++---- .../samples/booking/JpaBookingService.java | 154 +++++++++--------- .../samples/booking/SearchCriteria.java | 82 +++++----- 4 files changed, 177 insertions(+), 153 deletions(-) diff --git a/booking-faces/pom.xml b/booking-faces/pom.xml index 8786846..90f3428 100644 --- a/booking-faces/pom.xml +++ b/booking-faces/pom.xml @@ -9,12 +9,12 @@ 1.0.0.BUILD-SNAPSHOT - 3.2.1.RELEASE + 4.0.0.RELEASE 3.1.3.RELEASE - 2.4.0.M1 - 1.5.10 - 2.1.7 - 3.1.1 + 2.4.0.BUILD-SNAPSHOT + 1.6.1 + 2.2.4 + 4.0 @@ -29,6 +29,11 @@ http://repository.primefaces.org default + + spring-snapshots + SpringSource Repository + http://repo.springsource.org/snapshot + diff --git a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/HotelLazyDataModel.java b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/HotelLazyDataModel.java index cdcb2cb..5d48baa 100644 --- a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/HotelLazyDataModel.java +++ b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/HotelLazyDataModel.java @@ -1,5 +1,5 @@ /** - * + * */ package org.springframework.webflow.samples.booking; @@ -11,44 +11,63 @@ import org.primefaces.model.SortOrder; public class HotelLazyDataModel extends LazyDataModel { - private static final long serialVersionUID = -8832831134966938627L; + private static final long serialVersionUID = -8832831134966938627L; - SearchCriteria searchCriteria; + SearchCriteria searchCriteria; - BookingService bookingService; + BookingService bookingService; - private Hotel selected; + private List hotels; - public HotelLazyDataModel(SearchCriteria searchCriteria, BookingService bookingService) { - this.searchCriteria = searchCriteria; - this.bookingService = bookingService; - } + private Hotel selected; - @Override - public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { - searchCriteria.setCurrentPage(first / pageSize + 1); - return bookingService.findHotels(searchCriteria, first, sortField, sortOrder.equals(SortOrder.ASCENDING)); - } - @Override - public int getRowCount() { - return bookingService.getNumberOfHotels(searchCriteria); - } + public HotelLazyDataModel(SearchCriteria searchCriteria, BookingService bookingService) { + this.searchCriteria = searchCriteria; + this.bookingService = bookingService; + } - public Hotel getSelected() { - return selected; - } + @Override + public List load(int first, int pageSize, String sortField, SortOrder order, Map filters) { + this.searchCriteria.setCurrentPage(first / pageSize + 1); + this.hotels = bookingService.findHotels(searchCriteria, first, sortField, order.equals(SortOrder.ASCENDING)); + return hotels; + } - public void setSelected(Hotel selected) { - this.selected = selected; - } + @Override + public Hotel getRowData(String rowKey) { + for (Hotel hotel : this.hotels){ + if (hotel.getId().equals(rowKey)) { + return hotel; + } + } + return null; + } - public int getCurrentPage() { - return this.searchCriteria.getCurrentPage(); - } + @Override + public Object getRowKey(Hotel hotel) { + return hotel.getId(); + } - public int getPageSize() { - return this.searchCriteria.getPageSize(); - } + @Override + public int getRowCount() { + return bookingService.getNumberOfHotels(searchCriteria); + } + + public Hotel getSelected() { + return selected; + } + + public void setSelected(Hotel selected) { + this.selected = selected; + } + + public int getCurrentPage() { + return this.searchCriteria.getCurrentPage(); + } + + public int getPageSize() { + return this.searchCriteria.getPageSize(); + } } \ No newline at end of file diff --git a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java index 3f23df1..460b63e 100755 --- a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java +++ b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/JpaBookingService.java @@ -20,90 +20,90 @@ import org.springframework.util.StringUtils; @Repository public class JpaBookingService implements BookingService, Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private EntityManager em; + private EntityManager em; - @PersistenceContext - public void setEntityManager(EntityManager em) { - this.em = em; - } - - @Transactional(readOnly = true) - @SuppressWarnings("unchecked") - public List findBookings(String username) { - if (username != null) { - return em.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate") - .setParameter("username", username).getResultList(); - } else { - return null; + @PersistenceContext + public void setEntityManager(EntityManager em) { + this.em = em; } - } - @Transactional(readOnly = true) - @SuppressWarnings("unchecked") - public List findHotels(SearchCriteria criteria, int firstResult, String orderBy, boolean ascending) { - String pattern = getSearchPattern(criteria); - orderBy = (orderBy != null) ? orderBy : "name"; - String orderDirection = (ascending) ? " ASC" : " DESC"; - 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 order by h." - + orderBy + orderDirection).setParameter("pattern", pattern) - .setMaxResults(criteria.getPageSize()).setFirstResult(firstResult).getResultList(); - } - - @Transactional(readOnly = true) - public int getNumberOfHotels(SearchCriteria criteria) { - String pattern = getSearchPattern(criteria); - Long count = (Long) em - .createQuery( - "select count(h.id) 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).getSingleResult(); - return count.intValue(); - } - - @Transactional(readOnly = true) - public Hotel findHotelById(Long id) { - return em.find(Hotel.class, id); - } - - @Transactional(readOnly = true) - public Booking createBooking(Long hotelId, String username) { - Hotel hotel = em.find(Hotel.class, hotelId); - User user = findUser(username); - Booking booking = new Booking(hotel, user); - return booking; - } - - @Transactional - public void persistBooking(Booking booking) { - em.persist(booking); - } - - @Transactional - public void cancelBooking(Booking booking) { - booking = em.find(Booking.class, booking.getId()); - if (booking != null) { - em.remove(booking); + @Transactional(readOnly = true) + @SuppressWarnings("unchecked") + public List findBookings(String username) { + if (username != null) { + return em.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate") + .setParameter("username", username).getResultList(); + } else { + return null; + } } - } - // helpers - - private String getSearchPattern(SearchCriteria criteria) { - if (StringUtils.hasText(criteria.getSearchString())) { - return "%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%"; - } else { - return "%"; + @Transactional(readOnly = true) + @SuppressWarnings("unchecked") + public List findHotels(SearchCriteria criteria, int firstResult, String orderBy, boolean ascending) { + String pattern = getSearchPattern(criteria); + orderBy = (orderBy != null) ? orderBy : "name"; + String orderDirection = (ascending) ? " ASC" : " DESC"; + 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 order by h." + + orderBy + orderDirection).setParameter("pattern", pattern) + .setMaxResults(criteria.getPageSize()).setFirstResult(firstResult).getResultList(); } - } - private User findUser(String username) { - return (User) em.createQuery("select u from User u where u.username = :username") - .setParameter("username", username).getSingleResult(); - } + @Transactional(readOnly = true) + public int getNumberOfHotels(SearchCriteria criteria) { + String pattern = getSearchPattern(criteria); + Long count = (Long) em + .createQuery( + "select count(h.id) 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).getSingleResult(); + return count.intValue(); + } + + @Transactional(readOnly = true) + public Hotel findHotelById(Long id) { + return em.find(Hotel.class, id); + } + + @Transactional(readOnly = true) + public Booking createBooking(Long hotelId, String username) { + Hotel hotel = em.find(Hotel.class, hotelId); + User user = findUser(username); + Booking booking = new Booking(hotel, user); + return booking; + } + + @Transactional + public void persistBooking(Booking booking) { + em.persist(booking); + } + + @Transactional + public void cancelBooking(Booking booking) { + booking = em.find(Booking.class, booking.getId()); + if (booking != null) { + em.remove(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) em.createQuery("select u from User u where u.username = :username") + .setParameter("username", username).getSingleResult(); + } } \ No newline at end of file diff --git a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/SearchCriteria.java b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/SearchCriteria.java index ddae095..9888777 100755 --- a/booking-faces/src/main/java/org/springframework/webflow/samples/booking/SearchCriteria.java +++ b/booking-faces/src/main/java/org/springframework/webflow/samples/booking/SearchCriteria.java @@ -9,57 +9,57 @@ import javax.faces.model.DataModel; */ public class SearchCriteria implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * The user-provided search criteria for finding Hotels. - */ - private String searchString = ""; + /** + * The user-provided search criteria for finding Hotels. + */ + private String searchString = ""; - /** - * The maximum page size of the Hotel result list - */ - private int pageSize = 5; + /** + * The maximum page size of the Hotel result list + */ + private int pageSize = 5; - /** - * The page the user is currently on. - */ - private int currentPage = 1; + /** + * The page the user is currently on. + */ + private int currentPage = 1; - /** - * Returns a {@link DataModel} based on the search criteria. - * @param bookingService the service to use to retrieve hotels. - */ - public DataModel getDataModel(BookingService bookingService) { - return new HotelLazyDataModel(this, bookingService); - } + /** + * Returns a {@link DataModel} based on the search criteria. + * @param bookingService the service to use to retrieve hotels. + */ + public DataModel getDataModel(BookingService bookingService) { + return new HotelLazyDataModel(this, bookingService); + } - public String getSearchString() { - return searchString; - } + public String getSearchString() { + return searchString; + } - public void setSearchString(String searchString) { - this.searchString = searchString; - } + public void setSearchString(String searchString) { + this.searchString = searchString; + } - public int getPageSize() { - return pageSize; - } + public int getPageSize() { + return pageSize; + } - public void setPageSize(int pageSize) { - this.pageSize = pageSize; - } + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } - public int getCurrentPage() { - return currentPage; - } + public int getCurrentPage() { + return currentPage; + } - public void setCurrentPage(int currentPage) { - this.currentPage = currentPage; - } + public void setCurrentPage(int currentPage) { + this.currentPage = currentPage; + } - public String toString() { - return "[Search Criteria searchString = '" + searchString + "'"; - } + public String toString() { + return "[Search Criteria searchString = '" + searchString + "'"; + } } \ No newline at end of file