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

Source Code of org.objectweb.speedo.tutorial.appli.additional.detach.TutorialStep7

/**
* 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.detach;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.FetchPlan;
import javax.jdo.JDODetachedFieldAccessException;

import org.objectweb.speedo.tutorial.pobjects.additional.detach.Address;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Author;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Book;
import org.objectweb.speedo.tutorial.pobjects.additional.detach.Editor;
import org.objectweb.speedo.tutorial.TutorialHelper;

/**
* @author Y.Bersihand
*/
public class TutorialStep7 {
 
  private static PersistenceManagerFactory pmf = null;
 
  /**This steps describes how to:
   * use fetch groups
   */
  public static void fetchPlan() {
    System.out.println( "***************fetchPlan*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    usePredefinedFetchGroups(pm);
    useDefinedFetchGroups(pm);
        pm.close();
  }
 
  /**
   * Detach objects with the predefined fetchgroups:
   * default
   * all
   */
  public static void usePredefinedFetchGroups(PersistenceManager pm){
    //create a fetch plan
    FetchPlan fp = pm.getFetchPlan();
   
    //create a book
    Author author = new Author("William S. Burroughs");
    Address address = new Address("Fenton Street", "931ZR2", "Leeds");
    Editor editor = new Editor("Mille et Une Nuits", address);
    Book book = new Book("The Yage Letters", author, editor, 1955);
   
    //make the book persistent
    pm.currentTransaction().begin();
    System.out.println( "make persistent the book " + book.toString());
    pm.makePersistent(book);
    pm.currentTransaction().commit();
   
    //detach the book --> "default" fetch group
    //fields title and year are loaded
    // fields editor and author are not loaded
    Book defaultBook = (Book) pm.detachCopy(book);
    System.out.println( "With the default fetchgroup:");
    try{
      System.out.println( "Title can be accessed: " + defaultBook.getTitle());
      System.out.println( "Year can be accessed: " + defaultBook.getYear());
      System.out.println( "Author should not be accessed: " + defaultBook.getAuthor().toString());
      System.out.println( "Editor should not be accessed: " + defaultBook.getEditor().toString());
    }
    catch(Exception e){
      if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("author") != -1)
        System.out.println( "Correct exception caught: " + e.getMessage());
      else
        System.out.println( "Error: " + e);
    }
   
    //detach the book --> "all" fetch group
    //all fields are loaded
    fp.addGroup("all").removeGroup("default");
    Book allBook = (Book) pm.detachCopy(book);
    System.out.println( "With the all fetchgroup:");
    try{
      System.out.println( "Title can be accessed: " + allBook.getTitle());
      System.out.println( "Year can be accessed: " + allBook.getYear());
      System.out.println( "Author can be accessed: " + allBook.getAuthor().toString());
      System.out.println( "Editor can be accessed: " + allBook.getEditor().toString());
    }
    catch(Exception e){
        System.out.println( "Error: " + e);
    }
  }
 
  /**
   * Detach objects with user-defined fetchgroups (see the detach.jdo file)
   */
  public static void useDefinedFetchGroups(PersistenceManager pm){
    //create a fetch plan
    FetchPlan fp = pm.getFetchPlan();
   
    //create a book
    Author author = new Author("John Fante");
    Address address = new Address("South Street", "211ZL2", "York");
    Editor editor = new Editor("Plon", address);
    Book book = new Book("Bandini", author, editor, 1938);
   
    //make the book persistent
    pm.currentTransaction().begin();
    System.out.println( "make persistent the book " + book.toString());
    pm.makePersistent(book);
    pm.currentTransaction().commit();
    //detach the book --> "onlyAuthor" fetch group
    //fields title year and author are loaded
    // fields editor is not loaded
    fp.addGroup("onlyAuthor").removeGroup("default");
    Book onlyAuthorBook = (Book) pm.detachCopy(book);
    System.out.println( "With the onlyAuthor fetchgroup:");
    try{
      System.out.println( "Title can be accessed: " + onlyAuthorBook.getTitle());
      System.out.println( "Year can be accessed: " + onlyAuthorBook.getYear());
      System.out.println( "Author can be accessed: " + onlyAuthorBook.getAuthor().toString());
      System.out.println( "Editor should not be accessed: " + onlyAuthorBook.getEditor().toString());
    }
    catch(Exception e){
      if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("editor") != -1)
        System.out.println( "Correct exception caught: " + e.getMessage());
      else
        System.out.println( "Error: " + e);
    }
   
    //detach the book --> "editorName" fetch group
    //fields title, year, editor.name are loaded
    //fields author, editor.address are not loaded
    fp.addGroup("editorName").removeGroup("onlyAuthor");
    Book editorNameBook = (Book) pm.detachCopy(book);
    System.out.println( "With the editorName fetchgroup:");
    try{
      System.out.println( "Title can be accessed: " + editorNameBook.getTitle());
      System.out.println( "Year can be accessed: " + editorNameBook.getYear());
      System.out.println( "Editor name can be accessed: " + editorNameBook.getEditor().getName());
      System.out.println( "Editor address should not be accessed: " + editorNameBook.getEditor().getAddress().toString());
      System.out.println( "Author should not be accessed: " + editorNameBook.getAuthor().toString());   
    }
    catch(Exception e){
      if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("address") != -1)
        System.out.println( "Correct exception caught: " + e.getMessage());
      else
        System.out.println( "Error: " + e);
    }
  }
 
  public static void main(String[] args){
    TutorialHelper th = null;
    try {
      th = new TutorialHelper(args[0]);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
    TutorialStep7.pmf = th.getPMF();
    TutorialStep7.fetchPlan();
  }

}
TOP

Related Classes of org.objectweb.speedo.tutorial.appli.additional.detach.TutorialStep7

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.