KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > entitycallback > EntityCallbackTestBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.entitycallback;
23
24 import java.util.Iterator JavaDoc;
25 import javax.ejb.Remote JavaDoc;
26 import javax.ejb.Stateless JavaDoc;
27 import javax.ejb.TransactionAttribute JavaDoc;
28 import javax.ejb.TransactionAttributeType JavaDoc;
29 import javax.persistence.EntityManager;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.PersistenceContext;
32
33 /**
34  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
35  * @version $Revision: 37459 $
36  */

37 @Stateless JavaDoc
38 @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
39 @Remote JavaDoc(EntityCallbackTest.class)
40 public class EntityCallbackTestBean implements EntityCallbackTest
41 {
42    private @PersistenceContext EntityManager entityManager;
43
44    public void createCustomer()
45    {
46       System.out.println("----createCustomer()");
47       CallbackCounterBean.clear();
48       Customer cust = new Customer("Kabir");
49       entityManager.persist(cust);
50       System.out.println("----createCustomer() - END");
51    }
52
53    public void addJourneysToCustomer()
54    {
55       System.out.println("----addJourneysToCustomer()");
56       CallbackCounterBean.clear();
57       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
58
59       cust.addJourney(new TrainJourney("Oslo", "London", "T1"));
60       cust.addJourney(new TrainJourney("Oslo", "London", "T2"));
61       cust.addJourney(new BusJourney("Oslo", "London", "B1"));
62       cust.addJourney(new BusJourney("Oslo", "London", "B2"));
63       cust.addJourney(new BusJourney("Oslo", "London", "B3"));
64       System.out.println("----addJourneysToCustomer() - END");
65    }
66
67    public void updateCustomer()
68    {
69       System.out.println("----updateCustomer()");
70       CallbackCounterBean.clear();
71       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
72
73       cust.setName("Kabir Khan");
74       System.out.println("----updateCustomer() -END");
75    }
76
77    public void updateOneTrainJourney()
78    {
79       System.out.println("----updateOneTrainJourney()");
80       CallbackCounterBean.clear();
81       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
82
83       for (Iterator JavaDoc it = cust.getJourneys().iterator(); it.hasNext();)
84       {
85          Object JavaDoc obj = it.next();
86          if (obj instanceof TrainJourney)
87          {
88             TrainJourney trainJourney = (TrainJourney) obj;
89             String JavaDoc train = trainJourney.getTrain();
90             trainJourney.setTrain(train + "-1");
91             entityManager.flush();
92             break;
93          }
94       }
95       System.out.println("----updateOneTrainJourney() - END");
96    }
97
98    public void updateAllBusJourneys()
99    {
100       System.out.println("----updateAllBusJourneys()");
101       CallbackCounterBean.clear();
102       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
103
104       for (Iterator JavaDoc it = cust.getJourneys().iterator(); it.hasNext();)
105       {
106          Object JavaDoc obj = it.next();
107          if (obj instanceof BusJourney)
108          {
109             BusJourney busJourney = (BusJourney) obj;
110             String JavaDoc bus = busJourney.getBus();
111             busJourney.setBus(bus + "-1");
112          }
113       }
114       System.out.println("----updateAllBusJourneys() - EN");
115    }
116
117    public void updateEverything()
118    {
119       System.out.println("----updateEverything()");
120       CallbackCounterBean.clear();
121       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
122       cust.setName("Kabir");
123
124       for (Iterator JavaDoc<Journey> it = cust.getJourneys().iterator(); it.hasNext();)
125       {
126          Journey journey = it.next();
127          journey.setDest("Somewhere fun and sunny");
128       }
129       System.out.println("----updateEverything() - END");
130    }
131
132    public void createAndDeleteCustomer()
133    {
134       System.out.println("----createAndDeleteCustomer()");
135
136       CallbackCounterBean.clear();
137       Customer cust = new Customer("Djengis");
138       entityManager.persist(cust);
139
140       entityManager.remove(cust);
141       System.out.println("----createAndDeleteCustomer()");
142    }
143
144
145    public void deleteSomeJourneys()
146    {
147       System.out.println("----deleteSomeJourneys()");
148       CallbackCounterBean.clear();
149       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
150
151       boolean deletedBus = false;
152       boolean deletedTrain = false;
153
154       for (Iterator JavaDoc<Journey> it = cust.getJourneys().iterator(); it.hasNext();)
155       {
156          Journey journey = (Journey) it.next();
157
158          if (journey instanceof BusJourney && !deletedBus)
159          {
160             it.remove();
161             entityManager.remove(journey);
162             deletedBus = true;
163          }
164          else if (journey instanceof TrainJourney && !deletedTrain)
165          {
166             it.remove();
167             entityManager.remove(journey);
168             deletedTrain = true;
169          }
170       }
171       System.out.println("----deleteSomeJourneys() - END");
172    }
173
174    public void deleteCustomerAndJourneys()
175    {
176       System.out.println("----deleteCustomerAndJourneys()");
177       CallbackCounterBean.clear();
178       Customer cust = entityManager.find(Customer.class, new Long JavaDoc(1));
179       entityManager.remove(cust);
180       System.out.println("----deleteCustomerAndJourneys() - END");
181    }
182
183 }
184
Popular Tags