KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enterprise > customer_cmp_ejb > ejb > session > CustomerSession


1 /*
2  * CustomerSession.java
3  *
4  * Created on January 16, 2006, 6:38 PM
5  *
6  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
7  * Use is subject to license terms.
8  */

9
10 package enterprise.customer_cmp_ejb.ejb.session;
11
12 import javax.ejb.Stateless JavaDoc;
13 import javax.ejb.Stateful JavaDoc;
14 import javax.ejb.SessionContext JavaDoc;
15 import javax.persistence.*;
16 import javax.ejb.*;
17 import java.util.List JavaDoc;
18
19 import enterprise.customer_cmp_ejb.persistence.*;
20 import enterprise.customer_cmp_ejb.common.*;
21 /**
22  *
23  * @author Rahul Biswas
24  *
25  * Why a facade?
26  * 1. session beans are thread safe, and EMs are not necessarily; so injecting a EM into a SessionBean makes it safe.
27  * 2. Tx management is taken care of by container
28  * 3. of course, because it's a facade [we can combine operations].
29  *
30  */

31 @Stateless JavaDoc
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 JavaDoc id){
45         
46         Customer cust = (Customer)em.find(Customer.class, id);
47         return cust;
48     }
49     
50     
51     public Subscription searchForSubscription(String JavaDoc id){
52         
53         Subscription subscription = (Subscription)em.find(Subscription.class, id);
54         return subscription;
55     }
56     
57     public Address searchForAddress(String JavaDoc id){
58         
59         Address address = (Address)em.find(Address.class, id);
60         return address;
61         
62     }
63     
64     //This is the default; here as an example of @TransactionAttribute
65
@TransactionAttribute(TransactionAttributeType.REQUIRED)
66     public void remove(Object JavaDoc obj){
67         Object JavaDoc mergedObj = em.merge(obj);
68         em.remove(mergedObj);
69     }
70     
71     public void persist(Object JavaDoc obj){
72         em.persist(obj);
73     }
74     
75     public List JavaDoc findAllSubscriptions(){
76     
77         List JavaDoc subscriptions = em.createNamedQuery("findAllSubscriptions").getResultList();
78         return subscriptions;
79         
80     }
81     
82     public List JavaDoc findCustomerByFirstName(String JavaDoc firstName){
83         List JavaDoc customers = em.createNamedQuery("findCustomerByFirstName").setParameter("firstName", firstName).getResultList();
84         return customers;
85     }
86     
87     public List JavaDoc findCustomerByLastName(String JavaDoc lastName){
88         List JavaDoc 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 JavaDoc cust, String JavaDoc subs) throws SubscriptionNotFoundException{
102         
103         //System.out.println("called remove Customer Subscription.....");
104

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 JavaDoc cust, String JavaDoc subs) throws DuplicateSubscriptionException{
120         
121         //System.out.println("called add Customer Subscription.....");
122
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