KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tutorial > appli > additional > inheritance > TutorialStep5


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.tutorial.appli.additional.inheritance;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.jdo.Extent;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.PersistenceManagerFactory;
36
37 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Employee;
38 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Manager;
39 import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Worker;
40 import org.objectweb.speedo.tutorial.TutorialHelper;
41
42 /**
43  * @author Y.Bersihand
44  */

45 public class TutorialStep5 {
46     
47     private static PersistenceManagerFactory pmf = null;
48     
49     /**
50      * This steps describes how to:
51      * make persistent inherited objects
52      */

53     public static void inheritance() {
54         System.out.println( "***************Inheritance*****************");
55         PersistenceManager pm = pmf.getPersistenceManager();
56         //create the employees, workers and managers
57
createInheritedObjects(pm);
58         //get all the employees
59
iterateExtent(pm, Employee.class);
60         //get all the workers
61
iterateExtent(pm, Worker.class);
62         //get all the managers
63
iterateExtent(pm, Manager.class);
64         pm.close();
65     }
66     
67     /**
68      * Create persistent objects
69      */

70     public static void createInheritedObjects(PersistenceManager pm){
71         //create employees
72
Employee employee1 = new Employee("Herve Landry", 42, "Tetra Pack");
73         Employee employee2 = new Employee("Vincent Racado", 30, "Tetra Pack");
74         //create workers
75
Worker worker1 = new Worker("Caroline Bret", 33, "Tetra Pack", true);
76         Worker worker2 = new Worker("Evelyne Jain", 54, "Tetra Pack", false);
77         Worker worker3 = new Worker("Tim Jorge", 28, "Tetra Pack", false);
78         //create managers
79
Manager manager1 = new Manager("Jean Duverge", 52, "Tetra Pack", "Sales");
80         Manager manager2 = new Manager("Eric Mento", 49, "Tetra Pack", "Marketing");
81         
82         worker1.addManager(manager1);
83         worker2.addManager(manager1);
84         worker2.addManager(manager2);
85         worker3.addManager(manager2);
86         
87         Collection JavaDoc employees = new ArrayList JavaDoc();
88         employees.add(employee1);
89         employees.add(employee2);
90         employees.add(worker1);
91         employees.add(worker2);
92         employees.add(worker3);
93         employees.add(manager1);
94         employees.add(manager2);
95         
96         //make persistent all the persons using a collection
97
pm.currentTransaction().begin();
98         pm.makePersistentAll(employees);
99         pm.currentTransaction().commit();
100     }
101     
102     //iterate over all the instances of a class cl
103
public static void iterateExtent(PersistenceManager pm, Class JavaDoc cl){
104         Extent extent = pm.getExtent(cl, true);
105         Iterator JavaDoc it = extent.iterator();
106         System.out.println( "All " + cl.getName() + " instances:");
107         while(it.hasNext()){
108             Employee e = (Employee) it.next();
109             System.out.println( e.toString());
110         }
111         extent.close(it);
112     }
113     
114     public static void main(String JavaDoc[] args){
115         TutorialHelper th = null;
116         try {
117             th = new TutorialHelper(args[0]);
118         } catch (IOException JavaDoc e) {
119             e.printStackTrace();
120             System.exit(-1);
121         }
122         TutorialStep5.pmf = th.getPMF();
123         TutorialStep5.inheritance();
124     }
125
126 }
127
Popular Tags