KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > fkmapping > ejb > ManagerSessionBean


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.test.cmp2.fkmapping.ejb;
23
24 import org.jboss.logging.Logger;
25
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.RemoveException JavaDoc;
31 import javax.ejb.EJBLocalObject JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34
35
36 /**
37  * @ejb.bean
38  * type="Stateless"
39  * name="Manager"
40  * view-type="remote"
41  * @ejb.util generate="physical"
42  * @ejb.transaction type="Required"
43  *
44  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
45  */

46 public class ManagerSessionBean
47    implements SessionBean JavaDoc
48 {
49    // Attributes ------------------------------------------------
50
static Logger log = Logger.getLogger(ManagerSessionBean.class);
51    private InstituteLocalHome instituteHome;
52    private DepartmentLocalHome departmentHome;
53    private GroupLocalHome groupHome;
54    private StudentLocalHome studentHome;
55    private ExamenationLocalHome examHome;
56
57    // Scenarious
58
/**
59     * @ejb.interface-method
60     */

61    public void testStandaloneFKMapping()
62    {
63       InstituteLocal institute = null;
64       DepartmentLocal department = null;
65       try
66       {
67          institute = getInstituteHome().create("NTUUKPI", "Natinal Technical University Of The Ukraine KPI");
68          department = getDepartmentHome().create("KV", "Specialized Computer Systems");
69          institute.getDepartments().add(department);
70          assertTrue("department.getInstitute().isIdentical(institute)", department.getInstitute().isIdentical(institute));
71          department.setInstitute(null);
72          assertTrue("institute.getDepartments().isEmpty()", institute.getDepartments().isEmpty());
73       }
74       catch(EJBException JavaDoc ejbe)
75       {
76          throw ejbe;
77       }
78       catch(Exception JavaDoc e)
79       {
80          throw new EJBException JavaDoc(e);
81       }
82       finally
83       {
84          if(institute != null)
85             removeEntity(institute);
86          if(department != null)
87             removeEntity(department);
88       }
89    }
90
91    /**
92     * @ejb.interface-method
93     */

94    public void testCompleteFKToPKMapping()
95    {
96       DepartmentLocal department = null;
97       GroupLocal kv62Group = null;
98       try
99       {
100          // one-side instance created before many-side instance
101
department = getDepartmentHome().create("KV", "Specialized Computer Systems");
102          assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
103
104          kv62Group = getGroupHome().create("KV", 62, "KV-62");
105          assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
106          assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
107
108          kv62Group.remove();
109          assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
110
111          // many-side instance created before one-side instance
112
department.remove();
113          kv62Group = getGroupHome().create("KV", 62, "KV-62");
114          assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
115
116          department = getDepartmentHome().create("KV", "Specialized Computer Systems");
117          assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
118          assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
119
120          department.remove();
121          department = null;
122          assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
123       }
124       catch(EJBException JavaDoc ejbe)
125       {
126          throw ejbe;
127       }
128       catch(Exception JavaDoc e)
129       {
130          throw new EJBException JavaDoc(e);
131       }
132       finally
133       {
134          if(department != null)
135             removeEntity(department);
136          if(kv62Group != null)
137             removeEntity(kv62Group);
138       }
139    }
140
141    /**
142     * @ejb.interface-method
143     */

144    public void testPartialFKToPKMapping()
145    {
146       StudentLocal petrovStudent = null;
147       StudentLocal sidorovStudent = null;
148       GroupLocal group = null;
149       try
150       {
151          petrovStudent = getStudentHome().create("KV", "Petrov", "Petrov works on KV department.");
152          group = getGroupHome().create("KV", 62, "KV-62");
153          assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
154
155          petrovStudent.setGroup(group);
156          assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
157          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
158
159          sidorovStudent = getStudentHome().create("KV", "Sidorov", "Sidorov works on KV department.");
160          group.getStudents().add(sidorovStudent);
161          assertTrue("sidorovStudent.getGroup().isIdentical(group)", sidorovStudent.getGroup().isIdentical(group));
162          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
163          assertTrue("group.getStudents().contains(sidorovStudent)", group.getStudents().contains(sidorovStudent));
164
165          group.remove();
166          group = null;
167          assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
168          assertTrue("sidorovStudent.getGroup() == null", sidorovStudent.getGroup() == null);
169
170          /*
171          group = getGroupHome().create("KV", 62, "KV-62");
172          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
173          assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
174          */

175       }
176       catch(EJBException JavaDoc ejbe)
177       {
178          throw ejbe;
179       }
180       catch(Exception JavaDoc e)
181       {
182          throw new EJBException JavaDoc(e);
183       }
184       finally
185       {
186          if(petrovStudent != null)
187             removeEntity(petrovStudent);
188          if(sidorovStudent != null)
189             removeEntity(sidorovStudent);
190          if(group != null)
191             removeEntity(group);
192       }
193    }
194
195    /**
196     * @ejb.interface-method
197     */

198    public void testFKToCMPMapping()
199    {
200       GroupLocal kv61Group = null;
201       GroupLocal kv62Group = null;
202       ExamenationLocal exam = null;
203       try
204       {
205          kv62Group = getGroupHome().create("KV", 62, "KV-62");
206          exam = getExamHome().create("kv61-1", "Math", "KV", 62);
207          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
208          assertTrue("kv62Group.getExamenations().contains(exam)", kv62Group.getExamenations().contains(exam));
209
210          kv61Group = getGroupHome().create("KV", 61, "KV-61");
211          exam.setGroup(kv61Group);
212          assertTrue("expected: exam.getGroupNumber() == 61;"
213             + " got: exam.getGroupNumber() == " + exam.getGroupNumber(),
214             exam.getGroupNumber() == 61);
215
216          exam.setGroupNumber(62);
217          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
218          assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
219          assertTrue("kv61Group.getExamenations().isEmpty();", kv61Group.getExamenations().isEmpty());
220
221          exam.setDepartmentCode("KM");
222          assertTrue("exam.getGroup() == null", exam.getGroup() == null);
223          assertTrue("kv62Group.getExamenations().isEmpty();", kv62Group.getExamenations().isEmpty());
224
225          exam.setDepartmentCode("KV");
226          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
227          assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
228       }
229       catch(EJBException JavaDoc ejbe)
230       {
231          throw ejbe;
232       }
233       catch(Exception JavaDoc e)
234       {
235          throw new EJBException JavaDoc(e);
236       }
237       finally
238       {
239          if(exam != null)
240             removeEntity(exam);
241          if(kv61Group != null)
242             removeEntity(kv61Group);
243          if(kv62Group != null)
244             removeEntity(kv62Group);
245       }
246    }
247
248    /**
249     * @ejb.interface-method
250     * @ejb.transaction type="RequiresNew"
251     */

252    public void createParent(Long JavaDoc id, String JavaDoc firstName)
253       throws Exception JavaDoc
254    {
255       ParentUtil.getLocalHome().create(id, firstName);
256    }
257
258    /**
259     * @ejb.interface-method
260     * @ejb.transaction type="RequiresNew"
261     */

262    public void createChild(Long JavaDoc id, String JavaDoc firstName)
263       throws Exception JavaDoc
264    {
265       ChildUtil.getLocalHome().create(id, firstName);
266    }
267
268    /**
269     * @ejb.interface-method
270     * @ejb.transaction type="RequiresNew"
271     */

272    public void createChild(Long JavaDoc id, String JavaDoc firstName, Long JavaDoc parentId, String JavaDoc parentName)
273       throws Exception JavaDoc
274    {
275       ChildUtil.getLocalHome().create(id, firstName, parentId, parentName);
276    }
277
278    /**
279     * @ejb.interface-method
280     * @ejb.transaction type="RequiresNew"
281     */

282    public void assertChildHasMother(Long JavaDoc childId, Long JavaDoc parentId, String JavaDoc parentName)
283       throws Exception JavaDoc
284    {
285       ChildLocal child = ChildUtil.getLocalHome().findByPrimaryKey(childId);
286       ParentLocal parent = child.getMother();
287       if(parent == null)
288          throw new EJBException JavaDoc("No parent assigned to child: expected parentId=" + parentId);
289       ParentPK parentPK = new ParentPK(parentId, parentName);
290       if(!parent.getPrimaryKey().equals(parentPK))
291          throw new EJBException JavaDoc("Wrong parent: expected parentPK=" + parentPK
292             + ", got " + parent.getPrimaryKey());
293    }
294
295    /**
296     * @ejb.interface-method
297     * @ejb.transaction type="RequiresNew"
298     */

299    public Object JavaDoc createChildUPKWithMother() throws Exception JavaDoc
300    {
301       ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka");
302       ParentLocal mother = ParentUtil.getLocalHome().create(new Long JavaDoc(11), "Irene");
303       child.setMother(mother);
304       return child.getPrimaryKey();
305    }
306    /**
307     * @ejb.interface-method
308     * @ejb.transaction type="RequiresNew"
309     */

310    public void loadChildUPKWithMother(Object JavaDoc pk) throws Exception JavaDoc
311    {
312       // find
313
ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk);
314       // load child and check its mother
315
assertTrue("child.getMother().getFirstName() is Irene",
316          "Irene".equals(child.getMother().getFirstName()));
317    }
318
319    /**
320     * @ejb.interface-method
321     * @ejb.transaction type="RequiresNew"
322     */

323    public Object JavaDoc createChildUPKWithFather() throws Exception JavaDoc
324    {
325       ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka");
326       ParentLocal father = ParentUtil.getLocalHome().create(new Long JavaDoc(12), "Gregory");
327       child.setFather(father);
328       return child.getPrimaryKey();
329    }
330    /**
331     * @ejb.interface-method
332     * @ejb.transaction type="RequiresNew"
333     */

334    public void loadChildUPKWithFather(Object JavaDoc pk) throws Exception JavaDoc
335    {
336       log.debug("loadChildUPK");
337       // find
338
ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk);
339       // load child and check its mother
340
assertTrue("child.getFather().getFirstName() is Gregory",
341          "Gregory".equals(child.getFather().getFirstName()));
342    }
343
344    // Private ---------------------------------------------------
345
private void assertTrue(String JavaDoc message, boolean expression)
346    {
347       if(!expression)
348          throw new AssertionException(message);
349    }
350
351    private void removeEntity(EJBLocalObject JavaDoc localEntity)
352    {
353       try
354       {
355          localEntity.remove();
356       }
357       catch(RemoveException JavaDoc re)
358       {
359          throw new EJBException JavaDoc("Couldn't remove local entity " + localEntity.getPrimaryKey());
360       }
361    }
362
363    private StudentLocalHome getStudentHome()
364       throws NamingException JavaDoc
365    {
366       if(studentHome == null)
367          studentHome = StudentUtil.getLocalHome();
368       return studentHome;
369    }
370
371    private ExamenationLocalHome getExamHome()
372       throws NamingException JavaDoc
373    {
374       if(examHome == null)
375          examHome = ExamenationUtil.getLocalHome();
376       return examHome;
377    }
378
379    private InstituteLocalHome getInstituteHome()
380       throws NamingException JavaDoc
381    {
382       if(instituteHome == null)
383          instituteHome = InstituteUtil.getLocalHome();
384       return instituteHome;
385    }
386
387    private DepartmentLocalHome getDepartmentHome()
388       throws NamingException JavaDoc
389    {
390       if(departmentHome == null)
391          departmentHome = DepartmentUtil.getLocalHome();
392       return departmentHome;
393    }
394
395    private GroupLocalHome getGroupHome()
396       throws NamingException JavaDoc
397    {
398       if(groupHome == null)
399          groupHome = GroupUtil.getLocalHome();
400       return groupHome;
401    }
402
403    // SessionBean implementation --------------------------------
404
/**
405     * @ejb.create-method
406     */

407    public void ejbCreate() throws CreateException JavaDoc {}
408    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc {}
409    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc {}
410    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc {}
411    public void setSessionContext(SessionContext JavaDoc sessionContext) throws EJBException JavaDoc, RemoteException JavaDoc {}
412 }
413
Popular Tags