bind target initial commit

sample polishing
This commit is contained in:
Keith Donald
2008-03-15 07:04:45 +00:00
parent c5340f7f2b
commit ee37861149
15 changed files with 142 additions and 114 deletions

View File

@@ -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(Booking booking);
/**
* Lookup a user based on their username
* @param username the user's username
* @return the user
*/
public User findUser(String username);
}

View File

@@ -40,8 +40,7 @@ public class JpaBookingService implements BookingService {
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(SearchCriteria criteria) {
String pattern = !StringUtils.hasText(criteria.getSearchString()) ? "'%'" : "'%"
+ criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
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).setMaxResults(
@@ -54,9 +53,10 @@ public class JpaBookingService implements BookingService {
}
@Transactional(readOnly = true)
public User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username",
username).getSingleResult();
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
@@ -71,11 +71,16 @@ public class JpaBookingService implements BookingService {
// helpers
private String getSearchPattern(SearchCriteria criteria) {
if (criteria.getSearchString().length() > 0) {
return "'%'" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
if (StringUtils.hasText(criteria.getSearchString())) {
return "'%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
} else {
return "'%";
return "'%'";
}
}
private User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username",
username).getSingleResult();
}
}

View File

@@ -7,12 +7,10 @@
<persistence-context/>
<input name="id" value="flowScope.id"/>
<input name="hotelId" value="flowScope.hotelId"/>
<on-start>
<evaluate expression="bookingService.findHotelById(id)" result="flowScope.hotel" />
<evaluate expression="hotel.createBooking(bookingService.findUser(currentUser.name))" result="flowScope.booking" />
<evaluate expression="entityManager.persist(booking)" />
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)" result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails">
@@ -23,7 +21,9 @@
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="confirm" to="bookingConfirmed">
<evaluate expression="entityManager.persist(booking)" />
</transition>
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>

View File

@@ -15,28 +15,28 @@
<fieldset>
<div class="field">
<div class="label">Name:</div>
<div class="output">#{hotel.name}</div>
<div class="output">#{booking.hotel.name}</div>
</div>
<div class="field">
<div class="label">Address:</div>
<div class="output">#{hotel.address}</div>
<div class="output">#{booking.hotel.address}</div>
</div>
<div class="field">
<div class="label">City, State:</div>
<div class="output">#{hotel.city}, #{hotel.state}</div>
<div class="output">#{booking.hotel.city}, #{booking.hotel.state}</div>
</div>
<div class="field">
<div class="label">Zip:</div>
<div class="output">#{hotel.zip}</div>
<div class="output">#{booking.hotel.zip}</div>
</div>
<div class="field">
<div class="label">Country:</div>
<div class="output">#{hotel.country}</div>
<div class="output">#{booking.hotel.country}</div>
</div>
<div class="field">
<div class="label">Nightly rate:</div>
<div class="output">
<h:outputText value="#{hotel.price}">
<h:outputText value="#{booking.hotel.price}">
<f:convertNumber type="currency" currencySymbol="$"/>
</h:outputText>
</div>

View File

@@ -41,7 +41,7 @@
</view-state>
<subflow-state id="bookHotel" subflow="booking">
<input name="id" value="hotels.selectedRow.id" />
<input name="hotelId" value="hotels.selectedRow.id" />
<transition on="bookingConfirmed" to="finish" />
<transition on="bookingCancelled" to="enterSearchCriteria" />
</subflow-state>

View File

@@ -1,16 +0,0 @@
package org.springframework.webflow.samples.booking;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
public class PropertyEditors implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}

View File

@@ -8,27 +8,20 @@
<persistence-context/>
<input name="id" value="flowScope.id" />
<input name="hotelId" value="flowScope.hotelId" />
<on-start>
<evaluate expression="bookingService.findHotelById(flowScope.id)" result="flowScope.hotel" />
<evaluate expression="hotel.createBooking(bookingService.findUser(currentUser.name))" result="flowScope.booking" />
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)" result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails">
<on-render>
<evaluate expression="bookingActions.setupForm"/>
</on-render>
<transition on="proceed" to="reviewBooking">
<evaluate expression="bookingActions.bindAndValidate" />
<bind target="booking" />
</transition>
<transition on="cancel" to="cancel" />
</view-state>
<view-state id="reviewBooking">
<on-render>
<evaluate expression="bookingActions.setupForm"/>
</on-render>
<transition on="confirm" to="bookingConfirmed">
<evaluate expression="entityManager.persist(booking)" />
</transition>

View File

@@ -16,28 +16,28 @@
<fieldset>
<div class="field">
<div class="label">Name:</div>
<div class="output">${hotel.name}</div>
<div class="output">${booking.hotel.name}</div>
</div>
<div class="field">
<div class="label">Address:</div>
<div class="output">${hotel.address}</div>
<div class="output">${booking.hotel.address}</div>
</div>
<div class="field">
<div class="label">City, State:</div>
<div class="output">${hotel.city}, ${hotel.state}</div>
<div class="output">${booking.hotel.city}, ${booking.hotel.state}</div>
</div>
<div class="field">
<div class="label">Zip:</div>
<div class="output">${hotel.zip}</div>
<div class="output">${booking.hotel.zip}</div>
</div>
<div class="field">
<div class="label">Country:</div>
<div class="output">${hotel.country}</div>
<div class="output">${booking.hotel.country}</div>
</div>
<div class="field">
<div class="label">Nightly rate:</div>
<div class="output">
<spring:bind path="hotel.price">${status.value}</spring:bind>
<spring:bind path="booking.hotel.price">${status.value}</spring:bind>
</div>
</div>
<div class="field">

View File

@@ -10,37 +10,29 @@
<on-render>
<evaluate expression="bookingService.findBookings(currentUser.name)" result="requestScope.bookings" />
</on-render>
<transition on="findHotels" to="findHotels">
<evaluate expression="mainActions.bindAndValidate" />
<transition on="findHotels">
<bind target="searchCriteria" />
<evaluate expression="bookingService.findHotels(searchCriteria)" result="flowScope.hotels" />
</transition>
<transition on="selectHotel" to="reviewHotel">
<set name="requestScope.id" value="requestParameters.hotelId" type="long" />
<evaluate expression="bookingService.findHotelById(id)" result="flowScope.hotel" />
<evaluate expression="bookingService.findHotelById(requestParameters.hotelId)" result="flowScope.hotel" />
</transition>
<transition on="cancelBooking">
<evaluate expression="bookingService.cancelBooking(requestParameters.bookingId)" />
</transition>
<transition on="cancelBooking" to="cancelBooking" />
</view-state>
<action-state id="findHotels">
<evaluate expression="bookingService.findHotels(searchCriteria)" result="flowScope.hotels" />
<transition on="success" to="main" />
</action-state>
<view-state id="reviewHotel">
<transition on="book" to="bookHotel" />
<transition on="cancel" to="main" />
</view-state>
<subflow-state id="bookHotel" subflow="booking">
<input name="id" value="flowScope.hotel.id" />
<input name="hotelId" value="flowScope.hotel.id" />
<transition on="bookingConfirmed" to="finish" />
<transition on="cancel" to="main" />
</subflow-state>
<action-state id="cancelBooking">
<evaluate expression="bookingService.cancelBooking(requestParameters.bookingId)" />
<transition on="success" to="main" />
</action-state>
<end-state id="finish"/>
</flow>

View File

@@ -62,22 +62,6 @@
<property name="suffix" value=".jsp" />
</bean>
<!-- Handels form binding for the hotel search -->
<bean id="mainActions" class="org.springframework.webflow.action.FormAction">
<property name="formObjectClass" value="org.springframework.webflow.samples.booking.SearchCriteria" />
</bean>
<!-- Handels form binding and validation for the hotel booking process -->
<bean id="bookingActions" class="org.springframework.webflow.action.FormAction">
<property name="formObjectClass" value="org.springframework.webflow.samples.booking.Booking" />
<property name="propertyEditorRegistrar">
<bean class="org.springframework.webflow.samples.booking.PropertyEditors" />
</property>
<property name="validator">
<bean class="org.springframework.webflow.samples.booking.BookingValidator" />
</property>
</bean>
<!-- The central service of this application that can query hotels and bookings, as well as cancel bookings -->
<bean id="bookingService" class="org.springframework.webflow.samples.booking.JpaBookingService" />