Package org.objectweb.speedo.tutorial.appli.additional.inheritance

Source Code of org.objectweb.speedo.tutorial.appli.additional.inheritance.TutorialStep5

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

package org.objectweb.speedo.tutorial.appli.additional.inheritance;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Employee;
import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Manager;
import org.objectweb.speedo.tutorial.pobjects.additional.inheritance.Worker;
import org.objectweb.speedo.tutorial.TutorialHelper;

/**
* @author Y.Bersihand
*/
public class TutorialStep5 {
 
  private static PersistenceManagerFactory pmf = null;
 
  /**
   * This steps describes how to:
   * make persistent inherited objects
   */
  public static void inheritance() {
    System.out.println( "***************Inheritance*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    //create the employees, workers and managers
    createInheritedObjects(pm);
    //get all the employees
    iterateExtent(pm, Employee.class);
    //get all the workers
    iterateExtent(pm, Worker.class);
    //get all the managers
    iterateExtent(pm, Manager.class);
        pm.close();
  }
 
  /**
   * Create persistent objects
   */
  public static void createInheritedObjects(PersistenceManager pm){
    //create employees
    Employee employee1 = new Employee("Herve Landry", 42, "Tetra Pack");
    Employee employee2 = new Employee("Vincent Racado", 30, "Tetra Pack");
    //create workers
    Worker worker1 = new Worker("Caroline Bret", 33, "Tetra Pack", true);
    Worker worker2 = new Worker("Evelyne Jain", 54, "Tetra Pack", false);
    Worker worker3 = new Worker("Tim Jorge", 28, "Tetra Pack", false);
    //create managers
    Manager manager1 = new Manager("Jean Duverge", 52, "Tetra Pack", "Sales");
    Manager manager2 = new Manager("Eric Mento", 49, "Tetra Pack", "Marketing");
   
    worker1.addManager(manager1);
    worker2.addManager(manager1);
    worker2.addManager(manager2);
    worker3.addManager(manager2);
   
    Collection employees = new ArrayList();
    employees.add(employee1);
    employees.add(employee2);
    employees.add(worker1);
    employees.add(worker2);
    employees.add(worker3);
    employees.add(manager1);
    employees.add(manager2);
   
    //make persistent all the persons using a collection
    pm.currentTransaction().begin();
        pm.makePersistentAll(employees);
        pm.currentTransaction().commit();
  }
 
  //iterate over all the instances of a class cl
  public static void iterateExtent(PersistenceManager pm, Class cl){
    Extent extent = pm.getExtent(cl, true);
        Iterator it = extent.iterator();
        System.out.println( "All " + cl.getName() + " instances:");
        while(it.hasNext()){
          Employee e = (Employee) it.next();
          System.out.println( e.toString());
        }
        extent.close(it);
  }
 
  public static void main(String[] args){
    TutorialHelper th = null;
    try {
      th = new TutorialHelper(args[0]);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
    TutorialStep5.pmf = th.getPMF();
    TutorialStep5.inheritance();
  }

}
TOP

Related Classes of org.objectweb.speedo.tutorial.appli.additional.inheritance.TutorialStep5

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.