[jboss-cvs] jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking ...
Michael Yuan
michael.yuan at jboss.com
Mon Aug 6 23:21:11 EDT 2007
User: myuan
Date: 07/08/06 23:21:11
Added: examples/jee5/booking/src/org/jboss/seam/example/booking
Authenticator.java AuthenticatorAction.java
Booking.java BookingList.java
BookingListAction.java ChangePassword.java
ChangePasswordAction.java Hotel.java
HotelBooking.java HotelBookingAction.java
HotelSearching.java HotelSearchingAction.java
Register.java RegisterAction.java User.java
Log:
re-structure the jee5 directory
Revision Changes Path
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/Authenticator.java
Index: Authenticator.java
===================================================================
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface Authenticator
{
public boolean authenticate();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/AuthenticatorAction.java
Index: AuthenticatorAction.java
===================================================================
package org.jboss.seam.example.booking;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
@Stateless
@Name("authenticator")
public class AuthenticatorAction implements Authenticator, Serializable
{
@PersistenceContext EntityManager em;
@Out(required=false, scope = SESSION)
private User user;
public boolean authenticate()
{
List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
.getResultList();
if ( results.size()==0 )
{
return false;
}
else
{
user = (User) results.get(0);
return true;
}
}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/Booking.java
Index: Booking.java
===================================================================
//$Id: Booking.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
import org.jboss.seam.annotations.Name;
@Entity
@Name("booking")
public class Booking implements Serializable
{
private Long id;
private User user;
private Hotel hotel;
private Date checkinDate;
private Date checkoutDate;
private String creditCard;
private String creditCardName;
private int creditCardExpiryMonth;
private int creditCardExpiryYear;
private boolean smoking;
private int beds;
public Booking() {}
public Booking(Hotel hotel, User user)
{
this.hotel = hotel;
this.user = user;
}
@Transient
public BigDecimal getTotal()
{
return hotel.getPrice().multiply( new BigDecimal( getNights() ) );
}
@Transient
public int getNights()
{
return (int) ( checkoutDate.getTime() - checkinDate.getTime() ) / 1000 / 60 / 60 / 24;
}
@Id @GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
@NotNull
@Basic @Temporal(TemporalType.DATE)
public Date getCheckinDate()
{
return checkinDate;
}
public void setCheckinDate(Date datetime)
{
this.checkinDate = datetime;
}
@ManyToOne @NotNull
public Hotel getHotel()
{
return hotel;
}
public void setHotel(Hotel hotel)
{
this.hotel = hotel;
}
@ManyToOne @NotNull
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
@Basic @Temporal(TemporalType.DATE)
@NotNull
public Date getCheckoutDate()
{
return checkoutDate;
}
public void setCheckoutDate(Date checkoutDate)
{
this.checkoutDate = checkoutDate;
}
@NotNull(message="Credit card number is required")
@Length(min=16, max=16, message="Credit card number must 16 digits long")
@Pattern(regex="^\\d*$", message="Credit card number must be numeric")
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;
}
@NotNull(message="Credit card name is required")
@Length(min=3, max=70, message="Credit card name is required")
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;
}
@Override
public String toString()
{
return "Booking(" + user + ","+ hotel + ")";
}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/BookingList.java
Index: BookingList.java
===================================================================
//$Id: BookingList.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface BookingList
{
public void getBookings();
public Booking getBooking();
public void cancel();
public void destroy();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/BookingListAction.java
Index: BookingListAction.java
===================================================================
//$Id: BookingListAction.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
@Stateful
@Scope(SESSION)
@Name("bookingList")
// @LoggedIn
@TransactionAttribute(REQUIRES_NEW)
public class BookingListAction implements BookingList, Serializable
{
@PersistenceContext
private EntityManager em;
@In
private User user;
@DataModel
private List<Booking> bookings;
@DataModelSelection
private Booking booking;
@Logger
private Log log;
@Factory
@Observer("bookingConfirmed")
public void getBookings()
{
bookings = em.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate")
.setParameter("username", user.getUsername())
.getResultList();
}
public void cancel()
{
log.info("Cancel booking: #{bookingList.booking.id} for #{user.username}");
Booking cancelled = em.find(Booking.class, booking.getId());
if (cancelled!=null) em.remove( cancelled );
getBookings();
FacesMessages.instance().add("Booking cancelled for confirmation number #{bookingList.booking.id}");
}
public Booking getBooking()
{
return booking;
}
@Destroy @Remove
public void destroy() {}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/ChangePassword.java
Index: ChangePassword.java
===================================================================
//$Id: ChangePassword.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface ChangePassword
{
public void changePassword();
public boolean isChanged();
public String getVerify();
public void setVerify(String verify);
public void destroy();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/ChangePasswordAction.java
Index: ChangePasswordAction.java
===================================================================
//$Id: ChangePasswordAction.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import static org.jboss.seam.ScopeType.EVENT;
import java.io.Serializable;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
@Stateful
@Scope(EVENT)
@Name("changePassword")
// @LoggedIn
public class ChangePasswordAction implements ChangePassword, Serializable
{
@In @Out
private User user;
@PersistenceContext
private EntityManager em;
private String verify;
private boolean changed;
public void changePassword()
{
if ( user.getPassword().equals(verify) )
{
user = em.merge(user);
FacesMessages.instance().add("Password updated");
changed = true;
}
else
{
FacesMessages.instance().add("verify", "Re-enter new password");
revertUser();
verify=null;
}
}
public boolean isChanged()
{
return changed;
}
private void revertUser()
{
user = em.find(User.class, user.getUsername());
}
public String getVerify()
{
return verify;
}
public void setVerify(String verify)
{
this.verify = verify;
}
@Destroy @Remove
public void destroy() {}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/Hotel.java
Index: Hotel.java
===================================================================
//$Id: Hotel.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;
@Entity
@Name("hotel")
public class Hotel implements Serializable
{
private Long id;
private String name;
private String address;
private String city;
private String state;
private String zip;
private String country;
private BigDecimal price;
@Id @GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
@Length(max=50) @NotNull
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Length(max=100) @NotNull
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Length(max=40) @NotNull
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
@Length(min=4, max=6) @NotNull
public String getZip()
{
return zip;
}
public void setZip(String zip)
{
this.zip = zip;
}
@Length(min=2, max=10) @NotNull
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
@Length(min=2, max=40) @NotNull
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
@Column(precision=6, scale=2)
public BigDecimal getPrice()
{
return price;
}
public void setPrice(BigDecimal price)
{
this.price = price;
}
@Override
public String toString()
{
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/HotelBooking.java
Index: HotelBooking.java
===================================================================
//$Id: HotelBooking.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface HotelBooking
{
public void selectHotel(Hotel selectedHotel);
public void bookHotel();
public void setBookingDetails();
public boolean isBookingValid();
public void confirm();
public void cancel();
public void destroy();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/HotelBookingAction.java
Index: HotelBookingAction.java
===================================================================
//$Id: HotelBookingAction.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import static javax.persistence.PersistenceContextType.EXTENDED;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
@Stateful
@Name("hotelBooking")
// @LoggedIn
public class HotelBookingAction implements HotelBooking, Serializable
{
@PersistenceContext(type=EXTENDED)
private EntityManager em;
@In
private User user;
@In(required=false) @Out
private Hotel hotel;
@In(required=false)
@Out(required=false)
private Booking booking;
@Out(required=false, scope=SESSION)
List<Booking> bookings;
@In
private FacesMessages facesMessages;
@In
private Events events;
@Logger
private Log log;
private boolean bookingValid;
@Begin
public void selectHotel(Hotel selectedHotel)
{
hotel = em.merge(selectedHotel);
}
public void bookHotel()
{
booking = new Booking(hotel, user);
Calendar calendar = Calendar.getInstance();
booking.setCheckinDate( calendar.getTime() );
calendar.add(Calendar.DAY_OF_MONTH, 1);
booking.setCheckoutDate( calendar.getTime() );
}
public void setBookingDetails()
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
if ( booking.getCheckinDate().before( calendar.getTime() ) )
{
facesMessages.addToControl("checkinDate", "Check in date must be a future date");
bookingValid=false;
}
else if ( !booking.getCheckinDate().before( booking.getCheckoutDate() ) )
{
facesMessages.addToControl("checkoutDate", "Check out date must be later than check in date");
bookingValid=false;
}
else
{
bookingValid=true;
}
}
public boolean isBookingValid()
{
return bookingValid;
}
// @Out (required=false, scope=SESSION)
// List <Booking> bookings;
@End
public void confirm()
{
em.persist(booking);
facesMessages.add("Thank you, #{user.name}, your confimation number for #{hotel.name} is #{booking.id}");
log.info("New booking: #{booking.id} for #{user.username}");
// events.raiseTransactionSuccessEvent("bookingConfirmed");
bookings = null;
}
@End
public void cancel() {}
@Destroy @Remove
public void destroy() {}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/HotelSearching.java
Index: HotelSearching.java
===================================================================
//$Id: HotelSearching.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface HotelSearching
{
public int getPageSize();
public void setPageSize(int pageSize);
public String getSearchString();
public void setSearchString(String searchString);
public void find();
public void nextPage();
public boolean isNextPageAvailable();
public String getSearchPattern();
public void destroy();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/HotelSearchingAction.java
Index: HotelSearchingAction.java
===================================================================
//$Id: HotelSearchingAction.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.Factory;
@Stateful
@Name("hotelSearch")
@Scope(ScopeType.SESSION)
// @LoggedIn
public class HotelSearchingAction implements HotelSearching, Serializable
{
@PersistenceContext
private EntityManager em;
private String searchString;
private int pageSize = 10;
private int page;
@DataModel
private List<Hotel> hotels;
public void find()
{
page = 0;
queryHotels();
}
public void nextPage()
{
page++;
queryHotels();
}
private void queryHotels()
{
// String searchPattern = searchString==null ? "%" : '%' + searchString.toLowerCase().replace('*', '%') + '%';
// hotels = em.createQuery("select h from Hotel h where lower(h.name) like :search or lower(h.city) like :search or lower(h.zip) like :search or lower(h.address) like :search")
// .setParameter("search", searchPattern)
hotels = 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(pageSize)
.setFirstResult( page * pageSize )
.getResultList();
}
public boolean isNextPageAvailable()
{
return hotels!=null && hotels.size()==pageSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getSearchString()
{
return searchString;
}
public void setSearchString(String searchString)
{
this.searchString = searchString;
}
@Factory(value="pattern", scope=ScopeType.EVENT)
public String getSearchPattern()
{
return searchString==null ?
"%" : '%' + searchString.toLowerCase().replace('*', '%') + '%';
}
@Destroy @Remove
public void destroy() {}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/Register.java
Index: Register.java
===================================================================
//$Id: Register.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import javax.ejb.Local;
@Local
public interface Register
{
public void register();
public void invalid();
public String getVerify();
public void setVerify(String verify);
public boolean isRegistered();
public void destroy();
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/RegisterAction.java
Index: RegisterAction.java
===================================================================
//$Id: RegisterAction.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import static org.jboss.seam.ScopeType.EVENT;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
@Stateful
@Scope(EVENT)
@Name("register")
public class RegisterAction implements Register, Serializable
{
@In
private User user;
@PersistenceContext
private EntityManager em;
@In
private FacesMessages facesMessages;
private String verify;
private boolean registered;
public void register()
{
if ( user.getPassword().equals(verify) )
{
// List existing = em.createQuery("select u.username from User u where u.username=:username")
// .setParameter("username", user.getUsername())
List existing = em.createQuery("select u.username from User u where u.username=#{user.username}")
.getResultList();
if (existing.size()==0)
{
em.persist(user);
// facesMessages.add("Successfully registered as #{user.username}");
facesMessages.addToControl("username", "Username #{user.username} already exists");
registered = true;
}
else
{
facesMessages.add("Username #{user.username} already exists");
}
}
else
{
facesMessages.add("verify", "Re-enter your password");
verify=null;
}
}
public void invalid()
{
facesMessages.add("Please try again");
}
public boolean isRegistered()
{
return registered;
}
public String getVerify()
{
return verify;
}
public void setVerify(String verify)
{
this.verify = verify;
}
@Destroy @Remove
public void destroy() {}
}
1.1 date: 2007/08/07 03:21:11; author: myuan; state: Exp;jboss-seam/examples/jee5/booking/src/org/jboss/seam/example/booking/User.java
Index: User.java
===================================================================
//$Id: User.java,v 1.1 2007/06/23 18:33:59 pmuir Exp $
package org.jboss.seam.example.booking;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Entity
@Name("user")
@Scope(SESSION)
@Table(name="Customer")
public class User implements Serializable
{
private String username;
private String password;
private String name;
public User(String name, String password, String username)
{
this.name = name;
this.password = password;
this.username = username;
}
public User() {}
@NotNull
@Length(max=100)
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@NotNull
@Length(min=5, max=15)
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Id
@Length(min=5, max=15)
@Pattern(regex="^\\w*$", message="not a valid username")
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
@Override
public String toString()
{
return "User(" + username + ")";
}
}
More information about the jboss-cvs-commits
mailing list