1 9 10 package enterprise.customer_cmp_ejb.ejb.session; 11 12 import javax.ejb.Stateless ; 13 import javax.ejb.Stateful ; 14 import javax.ejb.SessionContext ; 15 import javax.persistence.*; 16 import javax.ejb.*; 17 import java.util.List ; 18 19 import enterprise.customer_cmp_ejb.persistence.*; 20 import enterprise.customer_cmp_ejb.common.*; 21 31 @Stateless 32 @TransactionManagement(value=TransactionManagementType.CONTAINER) 33 34 public class CustomerSession implements CustomerSessionLocal, CustomerSessionRemote{ 35 36 @javax.persistence.PersistenceContext(unitName="persistence_sample") 37 private EntityManager em ; 38 39 40 public CustomerSession(){ 41 42 } 43 44 public Customer searchForCustomer(String id){ 45 46 Customer cust = (Customer)em.find(Customer.class, id); 47 return cust; 48 } 49 50 51 public Subscription searchForSubscription(String id){ 52 53 Subscription subscription = (Subscription)em.find(Subscription.class, id); 54 return subscription; 55 } 56 57 public Address searchForAddress(String id){ 58 59 Address address = (Address)em.find(Address.class, id); 60 return address; 61 62 } 63 64 @TransactionAttribute(TransactionAttributeType.REQUIRED) 66 public void remove(Object obj){ 67 Object mergedObj = em.merge(obj); 68 em.remove(mergedObj); 69 } 70 71 public void persist(Object obj){ 72 em.persist(obj); 73 } 74 75 public List findAllSubscriptions(){ 76 77 List subscriptions = em.createNamedQuery("findAllSubscriptions").getResultList(); 78 return subscriptions; 79 80 } 81 82 public List findCustomerByFirstName(String firstName){ 83 List customers = em.createNamedQuery("findCustomerByFirstName").setParameter("firstName", firstName).getResultList(); 84 return customers; 85 } 86 87 public List findCustomerByLastName(String lastName){ 88 List customers = em.createNamedQuery("findCustomerByLastName").setParameter("lastName", lastName).getResultList(); 89 return customers; 90 } 91 92 93 public Customer addCustomerAddress(Customer cust, Address address){ 94 95 Customer mergedCust = em.merge(cust); 96 mergedCust.getAddresses().add(address); 97 return mergedCust; 98 99 } 100 101 public Customer removeCustomerSubscription(String cust, String subs) throws SubscriptionNotFoundException{ 102 103 105 Customer customer = (Customer)em.find(Customer.class, cust); 106 Subscription subscription = (Subscription)em.find(Subscription.class, subs); 107 108 if(!customer.getSubscriptions().contains(subscription)){ 109 System.out.println("remove: did not find a subscription obj for :"+subscription.getTitle()); 110 throw new SubscriptionNotFoundException(); 111 } 112 113 customer.getSubscriptions().remove(subscription); 114 subscription.getCustomers().remove(customer); 115 116 return customer; 117 } 118 119 public Customer addCustomerSubscription(String cust, String subs) throws DuplicateSubscriptionException{ 120 121 Customer customer = (Customer)em.find(Customer.class, cust); 123 Subscription subscription = (Subscription)em.find(Subscription.class, subs); 124 125 if(customer.getSubscriptions().contains(subscription)){ 126 System.out.println("add: found an existing subscription obj for :"+subscription.getTitle()); 127 throw new DuplicateSubscriptionException(); 128 } 129 130 customer.getSubscriptions().add(subscription); 131 subscription.getCustomers().add(customer); 132 133 return customer; 134 135 } 136 137 } 138 | Popular Tags |