blob: babb67eab940814dc94b5dcfa107820f985e2d7b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package examples.shop.impl.session;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import examples.shop.impl.entity.Customer;
import examples.shop.logic.InvalidPasswordException;
import examples.shop.logic.UserManager;
/**
* UserManager is Stateless session bean resposible for creating and
* retrieving a customer record. It also authenticates the user.
*/
@Stateless
@Remote(UserManager.class)
public class UserManagerBean implements UserManager {
@PersistenceContext
EntityManager manager;
public UserManagerBean() {
}
/**
* Returns an customer object for the given customer id.
*/
public Customer getUser(String customerId) {
return manager.find(Customer.class, customerId);
}
/**
* It uses the customer entity bean to create a record in the databse
* @param customerId
* @param name
* @param password
* @param address
*/
public Customer createUser(String customerId, String name, String password, String address) {
Customer customer = new Customer();
customer.init(customerId, name, password, address);
manager.persist(customer);
return customer;
}
/**
* This method authenticates the user
*
* @return true, if the password is correct
* @throws an InvalidPasswordException if password is incorrect.
*/
public boolean validateUser(String customerID, String password)
throws InvalidPasswordException {
if(customerID== null || password == null)
throw new IllegalArgumentException("id " + customerID + " pw " + password);
Customer user = getUser(customerID);
if (user != null && password.equals(user.getPassword())) {
return true;
} else {
System.out.println("Failure to validate user ID " + customerID
+ " with password " + password + " against password "
+ user.getPassword());
throw new InvalidPasswordException("Invalid Password:"
+ password);
}
}
public List<Customer> findAllCustomers() {
return manager.createQuery("SELECT c FROM Customer c").getResultList();
}
public void removeAllCustomers() {
List l = manager.createQuery("SELECT c FROM Customer c ").getResultList();
for(Iterator iter = l.iterator(); iter.hasNext();) {
manager.remove(iter.next());
}
}
}
|