This commit is contained in:
Rossen Stoyanchev
2018-02-07 15:28:52 -05:00
parent 9d27aa25a4
commit 5de2a59fb4
14 changed files with 668 additions and 677 deletions

View File

@@ -30,197 +30,197 @@ import org.springframework.binding.validation.ValidationContext;
@Entity
public class Booking implements Serializable {
private static final long serialVersionUID = 1171567558348174963L;
private static final long serialVersionUID = 1171567558348174963L;
private Long id;
private Long id;
private User user;
private User user;
private Hotel hotel;
private Hotel hotel;
private Date checkinDate;
private Date checkinDate;
private Date checkoutDate;
private Date checkoutDate;
private String creditCard;
private String creditCard;
private String creditCardName;
private String creditCardName;
private int creditCardExpiryMonth;
private int creditCardExpiryMonth;
private int creditCardExpiryYear;
private int creditCardExpiryYear;
private boolean smoking;
private boolean smoking;
private int beds;
private int beds;
private Amenity[] amenities;
private Amenity[] amenities;
public Booking() {
}
public Booking(Hotel hotel, User user) {
this.hotel = hotel;
this.user = user;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckinDate(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckoutDate(calendar.getTime());
}
@Transient
public BigDecimal getTotal() {
return hotel.getPrice().multiply(new BigDecimal(getNights()));
}
@Transient
public int getNights() {
if (checkinDate == null || checkoutDate == null) {
return 0;
} else {
return (int) (checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24;
public Booking() {
}
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Basic
@Temporal(TemporalType.DATE)
@Future
@NotNull
public Date getCheckinDate() {
return checkinDate;
}
public void setCheckinDate(Date datetime) {
this.checkinDate = datetime;
}
@ManyToOne
public Hotel getHotel() {
return hotel;
}
public void setHotel(Hotel hotel) {
this.hotel = hotel;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Basic
@Temporal(TemporalType.DATE)
@Future
@NotNull
public Date getCheckoutDate() {
return checkoutDate;
}
public void setCheckoutDate(Date checkoutDate) {
this.checkoutDate = checkoutDate;
}
@Pattern(regexp = "[0-9]{16}", message = "{invalidCreditCardPattern}")
public String getCreditCard() {
return creditCard;
}
public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}
@Transient
public String getDescription() {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
return hotel == null ? null : hotel.getName() + ", " + df.format(getCheckinDate()) + " to "
+ df.format(getCheckoutDate());
}
public boolean isSmoking() {
return smoking;
}
public void setSmoking(boolean smoking) {
this.smoking = smoking;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
@NotEmpty
public String getCreditCardName() {
return creditCardName;
}
public void setCreditCardName(String creditCardName) {
this.creditCardName = creditCardName;
}
public int getCreditCardExpiryMonth() {
return creditCardExpiryMonth;
}
public void setCreditCardExpiryMonth(int creditCardExpiryMonth) {
this.creditCardExpiryMonth = creditCardExpiryMonth;
}
public int getCreditCardExpiryYear() {
return creditCardExpiryYear;
}
public void setCreditCardExpiryYear(int creditCardExpiryYear) {
this.creditCardExpiryYear = creditCardExpiryYear;
}
@Transient
public Amenity[] getAmenities() {
return amenities;
}
public void setAmenities(Amenity[] amenities) {
this.amenities = amenities;
}
public void validateEnterBookingDetails(ValidationContext context) {
MessageContext messages = context.getMessageContext();
if (checkinDate.before(today())) {
messages.addMessage(new MessageBuilder().error().source("checkinDate")
.code("booking.checkinDate.beforeToday").build());
} else if (checkoutDate.before(checkinDate)) {
messages.addMessage(new MessageBuilder().error().source("checkoutDate")
.code("booking.checkoutDate.beforeCheckinDate").build());
public Booking(Hotel hotel, User user) {
this.hotel = hotel;
this.user = user;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckinDate(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckoutDate(calendar.getTime());
}
}
private Date today() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
@Transient
public BigDecimal getTotal() {
return hotel.getPrice().multiply(new BigDecimal(getNights()));
}
@Override
public String toString() {
return "Booking(" + user + "," + hotel + ")";
}
@Transient
public int getNights() {
if (checkinDate == null || checkoutDate == null) {
return 0;
} else {
return (int) (checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24;
}
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Basic
@Temporal(TemporalType.DATE)
@Future
@NotNull
public Date getCheckinDate() {
return checkinDate;
}
public void setCheckinDate(Date datetime) {
this.checkinDate = datetime;
}
@ManyToOne
public Hotel getHotel() {
return hotel;
}
public void setHotel(Hotel hotel) {
this.hotel = hotel;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Basic
@Temporal(TemporalType.DATE)
@Future
@NotNull
public Date getCheckoutDate() {
return checkoutDate;
}
public void setCheckoutDate(Date checkoutDate) {
this.checkoutDate = checkoutDate;
}
@Pattern(regexp = "[0-9]{16}", message = "{invalidCreditCardPattern}")
public String getCreditCard() {
return creditCard;
}
public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}
@Transient
public String getDescription() {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
return hotel == null ? null : hotel.getName() + ", " + df.format(getCheckinDate()) + " to "
+ df.format(getCheckoutDate());
}
public boolean isSmoking() {
return smoking;
}
public void setSmoking(boolean smoking) {
this.smoking = smoking;
}
public int getBeds() {
return beds;
}
public void setBeds(int beds) {
this.beds = beds;
}
@NotEmpty
public String getCreditCardName() {
return creditCardName;
}
public void setCreditCardName(String creditCardName) {
this.creditCardName = creditCardName;
}
public int getCreditCardExpiryMonth() {
return creditCardExpiryMonth;
}
public void setCreditCardExpiryMonth(int creditCardExpiryMonth) {
this.creditCardExpiryMonth = creditCardExpiryMonth;
}
public int getCreditCardExpiryYear() {
return creditCardExpiryYear;
}
public void setCreditCardExpiryYear(int creditCardExpiryYear) {
this.creditCardExpiryYear = creditCardExpiryYear;
}
@Transient
public Amenity[] getAmenities() {
return amenities;
}
public void setAmenities(Amenity[] amenities) {
this.amenities = amenities;
}
public void validateEnterBookingDetails(ValidationContext context) {
MessageContext messages = context.getMessageContext();
if (checkinDate.before(today())) {
messages.addMessage(new MessageBuilder().error().source("checkinDate")
.code("booking.checkinDate.beforeToday").build());
} else if (checkoutDate.before(checkinDate)) {
messages.addMessage(new MessageBuilder().error().source("checkoutDate")
.code("booking.checkoutDate.beforeCheckinDate").build());
}
}
private Date today() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
@Override
public String toString() {
return "Booking(" + user + "," + hotel + ")";
}
}

View File

@@ -13,7 +13,7 @@ public interface BookingService {
* @param username the user's name
* @return their bookings
*/
public List<Booking> findBookings(String username);
List<Booking> findBookings(String username);
/**
* Find hotels available for booking by some criteria.
@@ -23,14 +23,14 @@ public interface BookingService {
* @param ascending true if the sorting should be in ascending order, false for descending
* @return a list of hotels meeting the criteria
*/
public List<Hotel> findHotels(SearchCriteria criteria, int firstResult, String sortBy, boolean ascending);
List<Hotel> findHotels(SearchCriteria criteria, int firstResult, String sortBy, boolean ascending);
/**
* Find hotels by their identifier.
* @param id the hotel id
* @return the hotel
*/
public Hotel findHotelById(Long id);
Hotel findHotelById(Long id);
/**
* Create a new, transient hotel booking instance for the given user.
@@ -38,19 +38,19 @@ public interface BookingService {
* @param userName the user name
* @return the new transient booking instance
*/
public Booking createBooking(Long hotelId, String userName);
Booking createBooking(Long hotelId, String userName);
/**
* Persist the booking to the database
* @param booking the booking
*/
public void persistBooking(Booking booking);
void persistBooking(Booking booking);
/**
* Cancel an existing booking.
* @param id the booking id
*/
public void cancelBooking(Booking booking);
void cancelBooking(Booking booking);
/**
* Return the total number of hotels for the given criteria.

View File

@@ -14,97 +14,97 @@ import javax.persistence.Id;
@Entity
public class Hotel implements Serializable {
private static final long serialVersionUID = 4011346719502656269L;
private static final long serialVersionUID = 4011346719502656269L;
private Long id;
private Long id;
private String name;
private String name;
private String address;
private String address;
private String city;
private String city;
private String state;
private String state;
private String zip;
private String zip;
private String country;
private String country;
private BigDecimal price;
private BigDecimal price;
@Id
@GeneratedValue
public Long getId() {
return id;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getState() {
return state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(precision = 6, scale = 2)
public BigDecimal getPrice() {
return price;
}
@Column(precision = 6, scale = 2)
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Booking createBooking(User user) {
return new Booking(this, user);
}
public Booking createBooking(User user) {
return new Booking(this, user);
}
@Override
public String toString() {
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
@Override
public String toString() {
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
}

View File

@@ -1,6 +1,3 @@
/**
*
*/
package org.springframework.webflow.samples.booking;
import java.util.List;

View File

@@ -13,50 +13,50 @@ import javax.persistence.Table;
@Table(name = "Customer")
public class User implements Serializable {
private static final long serialVersionUID = -3652559447682574722L;
private static final long serialVersionUID = -3652559447682574722L;
private String username;
private String username;
private String password;
private String password;
private String name;
private String name;
public User() {
}
public User() {
}
public User(String username, String password, String name) {
this.username = username;
this.password = password;
this.name = name;
}
public User(String username, String password, String name) {
this.username = username;
this.password = password;
this.name = name;
}
@Id
public String getUsername() {
return username;
}
@Id
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User(" + username + ")";
}
@Override
public String toString() {
return "User(" + username + ")";
}
}

View File

@@ -29,187 +29,187 @@ import org.springframework.format.annotation.DateTimeFormat;
@BookingDateRange
public class Booking implements Serializable {
private Long id;
private Long id;
private User user;
private User user;
private Hotel hotel;
private Hotel hotel;
@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date checkinDate;
@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date checkinDate;
@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date checkoutDate;
@DateTimeFormat(pattern = "MM-dd-yyyy")
private Date checkoutDate;
private String creditCard;
private String creditCard;
private String creditCardName;
private String creditCardName;
private int creditCardExpiryMonth;
private int creditCardExpiryMonth;
private int creditCardExpiryYear;
private int creditCardExpiryYear;
private boolean smoking;
private boolean smoking;
private int beds;
private int beds;
private Set<Amenity> amenities;
private Set<Amenity> amenities;
public Booking() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckinDate(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckoutDate(calendar.getTime());
}
public Booking(Hotel hotel, User user) {
this();
this.hotel = hotel;
this.user = user;
}
@Transient
public BigDecimal getTotal() {
return hotel.getPrice().multiply(new BigDecimal(getNights()));
}
@Transient
public int getNights() {
if (checkinDate == null || checkoutDate == null) {
return 0;
} else {
return (int) ((checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24);
public Booking() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckinDate(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
setCheckoutDate(calendar.getTime());
}
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId() {
return id;
}
public Booking(Hotel hotel, User user) {
this();
this.hotel = hotel;
this.user = user;
}
public void setId(Long id) {
this.id = id;
}
@Transient
public BigDecimal getTotal() {
return hotel.getPrice().multiply(new BigDecimal(getNights()));
}
@Basic
@Temporal(TemporalType.DATE)
@NotNull
@Future
public Date getCheckinDate() {
return checkinDate;
}
@Transient
public int getNights() {
if (checkinDate == null || checkoutDate == null) {
return 0;
} else {
return (int) ((checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24);
}
}
public void setCheckinDate(Date datetime) {
this.checkinDate = datetime;
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId() {
return id;
}
@ManyToOne
public Hotel getHotel() {
return hotel;
}
public void setId(Long id) {
this.id = id;
}
public void setHotel(Hotel hotel) {
this.hotel = hotel;
}
@Basic
@Temporal(TemporalType.DATE)
@NotNull
@Future
public Date getCheckinDate() {
return checkinDate;
}
@ManyToOne
public User getUser() {
return user;
}
public void setCheckinDate(Date datetime) {
this.checkinDate = datetime;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne
public Hotel getHotel() {
return hotel;
}
@Basic
@Temporal(TemporalType.DATE)
@NotNull
@Future
public Date getCheckoutDate() {
return checkoutDate;
}
public void setHotel(Hotel hotel) {
this.hotel = hotel;
}
public void setCheckoutDate(Date checkoutDate) {
this.checkoutDate = checkoutDate;
}
@ManyToOne
public User getUser() {
return user;
}
@NotEmpty
public String getCreditCard() {
return creditCard;
}
public void setUser(User user) {
this.user = user;
}
public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}
@Basic
@Temporal(TemporalType.DATE)
@NotNull
@Future
public Date getCheckoutDate() {
return checkoutDate;
}
@Transient
public String getDescription() {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
return hotel == null ? null : hotel.getName() + ", " + df.format(getCheckinDate()) + " to "
+ df.format(getCheckoutDate());
}
public void setCheckoutDate(Date checkoutDate) {
this.checkoutDate = checkoutDate;
}
public boolean isSmoking() {
return smoking;
}
@NotEmpty
public String getCreditCard() {
return creditCard;
}
public void setSmoking(boolean smoking) {
this.smoking = smoking;
}
public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}
public int getBeds() {
return beds;
}
@Transient
public String getDescription() {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
return hotel == null ? null : hotel.getName() + ", " + df.format(getCheckinDate()) + " to "
+ df.format(getCheckoutDate());
}
public void setBeds(int beds) {
this.beds = beds;
}
public boolean isSmoking() {
return smoking;
}
@NotEmpty
public String getCreditCardName() {
return creditCardName;
}
public void setSmoking(boolean smoking) {
this.smoking = smoking;
}
public void setCreditCardName(String creditCardName) {
this.creditCardName = creditCardName;
}
public int getBeds() {
return beds;
}
public int getCreditCardExpiryMonth() {
return creditCardExpiryMonth;
}
public void setBeds(int beds) {
this.beds = beds;
}
public void setCreditCardExpiryMonth(int creditCardExpiryMonth) {
this.creditCardExpiryMonth = creditCardExpiryMonth;
}
@NotEmpty
public String getCreditCardName() {
return creditCardName;
}
public int getCreditCardExpiryYear() {
return creditCardExpiryYear;
}
public void setCreditCardName(String creditCardName) {
this.creditCardName = creditCardName;
}
public void setCreditCardExpiryYear(int creditCardExpiryYear) {
this.creditCardExpiryYear = creditCardExpiryYear;
}
public int getCreditCardExpiryMonth() {
return creditCardExpiryMonth;
}
@Transient
public Set<Amenity> getAmenities() {
return amenities;
}
public void setCreditCardExpiryMonth(int creditCardExpiryMonth) {
this.creditCardExpiryMonth = creditCardExpiryMonth;
}
public void setAmenities(Set<Amenity> amenities) {
this.amenities = amenities;
}
public int getCreditCardExpiryYear() {
return creditCardExpiryYear;
}
private Date today() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
public void setCreditCardExpiryYear(int creditCardExpiryYear) {
this.creditCardExpiryYear = creditCardExpiryYear;
}
@Override
public String toString() {
return "Booking(" + user + "," + hotel + ")";
}
@Transient
public Set<Amenity> getAmenities() {
return amenities;
}
public void setAmenities(Set<Amenity> amenities) {
this.amenities = amenities;
}
private Date today() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
@Override
public String toString() {
return "Booking(" + user + "," + hotel + ")";
}
}

View File

@@ -12,19 +12,19 @@ public class BookingFlowHandler extends AbstractFlowHandler {
private static final String DEFAULT_URL = "/hotels/search";
@Override
public String handleExecutionOutcome(FlowExecutionOutcome outcome, HttpServletRequest request,
HttpServletResponse response) {
return DEFAULT_URL;
}
@Override
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
if (e instanceof NoSuchFlowExecutionException) {
return DEFAULT_URL;
} else {
throw e;
@Override
public String handleExecutionOutcome(FlowExecutionOutcome outcome, HttpServletRequest req, HttpServletResponse res) {
return DEFAULT_URL;
}
@Override
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
if (e instanceof NoSuchFlowExecutionException) {
return DEFAULT_URL;
}
else {
throw e;
}
}
}
}

View File

@@ -13,21 +13,21 @@ public interface BookingService {
* @param username the user's name
* @return their bookings
*/
public List<Booking> findBookings(String username);
List<Booking> findBookings(String username);
/**
* Find hotels available for booking by some criteria.
* @param criteria the search criteria
* @return a list of hotels meeting the criteria
*/
public List<Hotel> findHotels(SearchCriteria criteria);
List<Hotel> findHotels(SearchCriteria criteria);
/**
* Find hotels by their identifier.
* @param id the hotel id
* @return the hotel
*/
public Hotel findHotelById(Long id);
Hotel findHotelById(Long id);
/**
* Create a new, transient hotel booking instance for the given user.
@@ -35,18 +35,18 @@ public interface BookingService {
* @param userName the user name
* @return the new transient booking instance
*/
public Booking createBooking(Long hotelId, String userName);
Booking createBooking(Long hotelId, String userName);
/**
* Persist the booking to the database
* @param booking the booking
*/
public void persistBooking(Booking booking);
void persistBooking(Booking booking);
/**
* Cancel an existing booking.
* @param id the booking id
*/
public void cancelBooking(Long id);
void cancelBooking(Long id);
}

View File

@@ -13,95 +13,95 @@ import javax.persistence.Id;
*/
@Entity
public class Hotel implements Serializable {
private Long id;
private Long id;
private String name;
private String name;
private String address;
private String address;
private String city;
private String city;
private String state;
private String state;
private String zip;
private String zip;
private String country;
private String country;
private BigDecimal price;
private BigDecimal price;
@Id
@GeneratedValue
public Long getId() {
return id;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getState() {
return state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(precision = 6, scale = 2)
public BigDecimal getPrice() {
return price;
}
@Column(precision = 6, scale = 2)
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Booking createBooking(User user) {
return new Booking(this, user);
}
public Booking createBooking(User user) {
return new Booking(this, user);
}
@Override
public String toString() {
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
@Override
public String toString() {
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
}

View File

@@ -6,45 +6,45 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HotelsController {
private BookingService bookingService;
private BookingService bookingService;
@Autowired
public HotelsController(BookingService bookingService) {
this.bookingService = bookingService;
}
@RequestMapping(value = "/hotels/search", method = RequestMethod.GET)
public void search(SearchCriteria searchCriteria, Principal currentUser, Model model) {
if (currentUser != null) {
List<Booking> booking = bookingService.findBookings(currentUser.getName());
model.addAttribute(booking);
@Autowired
public HotelsController(BookingService bookingService) {
this.bookingService = bookingService;
}
}
@RequestMapping(value = "/hotels", method = RequestMethod.GET)
public String list(SearchCriteria criteria, Model model) {
List<Hotel> hotels = bookingService.findHotels(criteria);
model.addAttribute(hotels);
return "hotels/list";
}
@GetMapping("/hotels/search")
public void search(SearchCriteria searchCriteria, Principal currentUser, Model model) {
if (currentUser != null) {
List<Booking> booking = bookingService.findBookings(currentUser.getName());
model.addAttribute(booking);
}
}
@RequestMapping(value = "/hotels/{id}", method = RequestMethod.GET)
public String show(@PathVariable Long id, Model model) {
model.addAttribute(bookingService.findHotelById(id));
return "hotels/show";
}
@GetMapping("/hotels")
public String list(SearchCriteria criteria, Model model) {
List<Hotel> hotels = bookingService.findHotels(criteria);
model.addAttribute(hotels);
return "hotels/list";
}
@RequestMapping(value = "/bookings/{id}", method = RequestMethod.DELETE)
public String deleteBooking(@PathVariable Long id) {
bookingService.cancelBooking(id);
return "redirect:../hotels/search";
}
@GetMapping("/hotels/{id}")
public String show(@PathVariable Long id, Model model) {
model.addAttribute(bookingService.findHotelById(id));
return "hotels/show";
}
@DeleteMapping("/bookings/{id}")
public String deleteBooking(@PathVariable Long id) {
bookingService.cancelBooking(id);
return "redirect:../hotels/search";
}
}

View File

@@ -19,76 +19,76 @@ import org.springframework.util.StringUtils;
@Repository
public class JpaBookingService implements BookingService {
private EntityManager em;
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Booking> 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<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
int startIndex = criteria.getPage() * criteria.getPageSize();
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).setFirstResult(startIndex).setMaxResults(criteria.getPageSize())
.getResultList();
}
@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(Long id) {
Booking booking = em.find(Booking.class, id);
if (booking != null) {
em.remove(booking);
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Booking> 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<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
int startIndex = criteria.getPage() * criteria.getPageSize();
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).setFirstResult(startIndex).setMaxResults(criteria.getPageSize())
.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 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(Long id) {
Booking booking = em.find(Booking.class, id);
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();
}
}

View File

@@ -5,52 +5,47 @@ import java.io.Serializable;
/**
* A backing bean for the main hotel search form. Encapsulates the criteria needed to perform a hotel search.
*
* It is expected a future milestone of Spring Web Flow 2.0 will allow flow-scoped beans like this one to hold
* references to transient services that are restored automatically when the flow is resumed on subsequent requests.
* This would allow this SearchCriteria object to delegate to the {@link BookingService} directly, for example,
* eliminating the need for the actions in {@link MainActions}.
*/
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;
/**
* The maximum page size of the Hotel result list
*/
private int pageSize;
/**
* The current page of the Hotel result list.
*/
private int page;
/**
* The current page of the Hotel result list.
*/
private int page;
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 getPage() {
return page;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(int page) {
this.page = page;
}
}

View File

@@ -12,48 +12,48 @@ import javax.persistence.Table;
@Entity
@Table(name = "Customer")
public class User implements Serializable {
private String username;
private String username;
private String password;
private String password;
private String name;
private String name;
public User() {
}
public User() {
}
public User(String username, String password, String name) {
this.username = username;
this.password = password;
this.name = name;
}
public User(String username, String password, String name) {
this.username = username;
this.password = password;
this.name = name;
}
@Id
public String getUsername() {
return username;
}
@Id
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User(" + username + ")";
}
@Override
public String toString() {
return "User(" + username + ")";
}
}

View File

@@ -1,12 +1,11 @@
package org.springframework.webflow.samples.booking.config;
import java.util.Arrays;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.webflow.config.AbstractFlowConfiguration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
@@ -46,7 +45,7 @@ public class WebFlowConfig extends AbstractFlowConfiguration {
@Bean
public MvcViewFactoryCreator mvcViewFactoryCreator() {
MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
factoryCreator.setViewResolvers(Arrays.<ViewResolver>asList(this.webMvcConfig.tilesViewResolver()));
factoryCreator.setViewResolvers(Collections.singletonList(this.webMvcConfig.tilesViewResolver()));
factoryCreator.setUseSpringBeanBinding(true);
return factoryCreator;
}