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>