[jboss-cvs] jboss-seam/examples/spring/src/org/jboss/seam/example/spring ...
Norman Richards
norman.richards at jboss.com
Wed Feb 21 09:37:49 EST 2007
User: nrichards
Date: 07/02/21 09:37:49
Modified: examples/spring/src/org/jboss/seam/example/spring
BookingService.java ChangePasswordAction.java
HotelSearchingAction.java LoginAction.java
RegisterAction.java User.java UserService.java
Log:
update example
Revision Changes Path
1.5 +62 -58 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/BookingService.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BookingService.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/BookingService.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- BookingService.java 20 Feb 2007 22:14:18 -0000 1.4
+++ BookingService.java 21 Feb 2007 14:37:49 -0000 1.5
@@ -1,41 +1,47 @@
package org.jboss.seam.example.spring;
+
import java.util.Calendar;
+import java.util.Collections;
import java.util.List;
-import javax.persistence.EntityManager;
-
-import org.hibernate.Session;
+import org.springframework.orm.jpa.support.JpaDaoSupport;
/**
- * @author youngm
+ * Example of using the JpaDaoSupport.
*
+ * @author youngm
*/
-public class BookingService {
- private EntityManager entityManager;
-
+public class BookingService
+ extends JpaDaoSupport
+{
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(String searchPattern, int firstResult, int maxResults) {
- return entityManager.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")
+ return getJpaTemplate().getEntityManager()
+ .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)
.setMaxResults(maxResults)
- .setFirstResult( firstResult )
+ .setFirstResult(firstResult)
.getResultList();
}
+
+
@SuppressWarnings("unchecked")
public List<Booking> findBookingsByUsername(String username) {
- return entityManager.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate")
- .setParameter("username", username).getResultList();
+ return getJpaTemplate().findByNamedParams("select b from Booking b where b.user.username = :username order by b.checkinDate",
+ Collections.singletonMap("username", username));
+
}
public void cancelBooking(Long bookingId) {
if (bookingId == null) {
throw new IllegalArgumentException("BookingId cannot be null");
}
- Booking cancelled = (Booking) entityManager.find(Booking.class, bookingId);
+
+ Booking cancelled = (Booking) getJpaTemplate().find(Booking.class, bookingId);
if (cancelled != null) {
- entityManager.remove(cancelled);
+ getJpaTemplate().remove(cancelled);
}
}
@@ -44,6 +50,7 @@
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
+
if (booking.getCheckinDate().before(calendar.getTime())) {
throw new ValidationException("Check in date must be a future date");
} else if (!booking.getCheckinDate().before(booking.getCheckoutDate())) {
@@ -51,25 +58,22 @@
}
}
+
+
public void bookHotel(Booking booking)
throws ValidationException
{
validateBooking(booking);
- entityManager.persist(booking);
- entityManager.flush();
+
+ getJpaTemplate().persist(booking);
+ getJpaTemplate().flush();
}
public Hotel findHotelById(Long hotelId) {
if (hotelId == null) {
throw new IllegalArgumentException("hotelId cannot be null");
}
- return (Hotel) entityManager.find(Hotel.class, hotelId);
- }
- /**
- * @param entityManager the entity manager
- */
- public void setEntityManager(EntityManager entityManager) {
- this.entityManager = entityManager;
+ return (Hotel) getJpaTemplate().find(Hotel.class, hotelId);
}
}
1.2 +33 -38 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/ChangePasswordAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ChangePasswordAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/ChangePasswordAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- ChangePasswordAction.java 19 Feb 2007 21:53:50 -0000 1.1
+++ ChangePasswordAction.java 21 Feb 2007 14:37:49 -0000 1.2
@@ -1,17 +1,13 @@
-//$Id: ChangePasswordAction.java,v 1.1 2007/02/19 21:53:50 nrichards Exp $
package org.jboss.seam.example.spring;
import static org.jboss.seam.ScopeType.EVENT;
-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.core.FacesMessages;
-import org.hibernate.Session;
-
@Scope(EVENT)
@Name("changePassword")
public class ChangePasswordAction
@@ -20,7 +16,7 @@
@In @Out
private User user;
- @In("#{userService}")
+ @In(create=true)
private UserService userService;
private String verify;
@@ -29,7 +25,7 @@
public void changePassword()
{
- if(userService.changePassword(user.getUsername(), verify, user.getPassword())) {
+ if (userService.changePassword(user.getUsername(), verify, user.getPassword())) {
FacesMessages.instance().add("Password updated");
changed = true;
} else {
@@ -53,5 +49,4 @@
{
this.verify = verify;
}
-
}
1.4 +50 -51 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/HotelSearchingAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: HotelSearchingAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/HotelSearchingAction.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- HotelSearchingAction.java 20 Feb 2007 00:21:10 -0000 1.3
+++ HotelSearchingAction.java 21 Feb 2007 14:37:49 -0000 1.4
@@ -1,4 +1,4 @@
-//$Id: HotelSearchingAction.java,v 1.3 2007/02/20 00:21:10 nrichards Exp $
+//$Id: HotelSearchingAction.java,v 1.4 2007/02/21 14:37:49 nrichards Exp $
package org.jboss.seam.example.spring;
import java.util.List;
@@ -13,7 +13,6 @@
@Scope(ScopeType.SESSION)
public class HotelSearchingAction
{
-
@In("#{bookingService}")
private BookingService bookingService;
1.2 +2 -2 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/LoginAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LoginAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/LoginAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- LoginAction.java 19 Feb 2007 21:53:50 -0000 1.1
+++ LoginAction.java 21 Feb 2007 14:37:49 -0000 1.2
@@ -1,4 +1,4 @@
-//$Id: LoginAction.java,v 1.1 2007/02/19 21:53:50 nrichards Exp $
+//$Id: LoginAction.java,v 1.2 2007/02/21 14:37:49 nrichards Exp $
package org.jboss.seam.example.spring;
import org.jboss.seam.ScopeType;
@@ -19,7 +19,7 @@
@Out(required = false)
private User user;
- @In("#{userService}")
+ @In(create=true)
private UserService userService;
@In
1.2 +49 -54 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/RegisterAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RegisterAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/RegisterAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- RegisterAction.java 19 Feb 2007 21:53:50 -0000 1.1
+++ RegisterAction.java 21 Feb 2007 14:37:49 -0000 1.2
@@ -1,18 +1,13 @@
-//$Id: RegisterAction.java,v 1.1 2007/02/19 21:53:50 nrichards Exp $
+//$Id: RegisterAction.java,v 1.2 2007/02/21 14:37:49 nrichards Exp $
package org.jboss.seam.example.spring;
import static org.jboss.seam.ScopeType.EVENT;
-import java.util.List;
-
-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.core.FacesMessages;
-import org.hibernate.Session;
-
@Scope(EVENT)
@Name("register")
public class RegisterAction
1.3 +3 -3 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/User.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: User.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/User.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- User.java 19 Feb 2007 21:53:50 -0000 1.2
+++ User.java 21 Feb 2007 14:37:49 -0000 1.3
@@ -1,4 +1,4 @@
-//$Id: User.java,v 1.2 2007/02/19 21:53:50 nrichards Exp $
+//$Id: User.java,v 1.3 2007/02/21 14:37:49 nrichards Exp $
package org.jboss.seam.example.spring;
import static org.jboss.seam.ScopeType.SESSION;
@@ -46,7 +46,7 @@
}
@NotNull
- @Length(min=5, max=15)
+ @Length(min=4, max=15)
public String getPassword()
{
return password;
@@ -57,7 +57,7 @@
}
@Id
- @Length(min=5, max=15)
+ @Length(min=4, max=15)
@Pattern(regex="^\\w*$", message="not a valid username")
public String getUsername()
{
1.3 +33 -31 jboss-seam/examples/spring/src/org/jboss/seam/example/spring/UserService.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: UserService.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/spring/src/org/jboss/seam/example/spring/UserService.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- UserService.java 20 Feb 2007 22:14:18 -0000 1.2
+++ UserService.java 21 Feb 2007 14:37:49 -0000 1.3
@@ -14,11 +14,13 @@
private EntityManager entityManager;
public boolean changePassword(String username, String oldPassword, String newPassword) {
- if (newPassword == null || "".equals(newPassword)) {
+ System.out.println("change password " + oldPassword + " to " + newPassword);
+ if (newPassword == null || newPassword.length()==0) {
throw new IllegalArgumentException("newPassword cannot be null.");
}
User user = findUser(username);
+ System.out.println("USER" + user);
if (user.getPassword().equals(oldPassword)) {
user.setPassword(newPassword);
return true;
More information about the jboss-cvs-commits
mailing list