summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java
diff options
context:
space:
mode:
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java')
-rw-r--r--Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java
new file mode 100644
index 0000000..babb67e
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/impl/session/UserManagerBean.java
@@ -0,0 +1,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());
+ }
+ }
+}