KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tutorial > appli > additional > detach > TutorialStep7


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.detach;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.jdo.PersistenceManager;
31 import javax.jdo.PersistenceManagerFactory;
32 import javax.jdo.FetchPlan;
33 import javax.jdo.JDODetachedFieldAccessException;
34
35 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Address;
36 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Author;
37 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Book;
38 import org.objectweb.speedo.tutorial.pobjects.additional.detach.Editor;
39 import org.objectweb.speedo.tutorial.TutorialHelper;
40
41 /**
42  * @author Y.Bersihand
43  */

44 public class TutorialStep7 {
45     
46     private static PersistenceManagerFactory pmf = null;
47     
48     /**This steps describes how to:
49      * use fetch groups
50      */

51     public static void fetchPlan() {
52         System.out.println( "***************fetchPlan*****************");
53         PersistenceManager pm = pmf.getPersistenceManager();
54         usePredefinedFetchGroups(pm);
55         useDefinedFetchGroups(pm);
56         pm.close();
57     }
58     
59     /**
60      * Detach objects with the predefined fetchgroups:
61      * default
62      * all
63      */

64     public static void usePredefinedFetchGroups(PersistenceManager pm){
65         //create a fetch plan
66
FetchPlan fp = pm.getFetchPlan();
67         
68         //create a book
69
Author author = new Author("William S. Burroughs");
70         Address address = new Address("Fenton Street", "931ZR2", "Leeds");
71         Editor editor = new Editor("Mille et Une Nuits", address);
72         Book book = new Book("The Yage Letters", author, editor, 1955);
73         
74         //make the book persistent
75
pm.currentTransaction().begin();
76         System.out.println( "make persistent the book " + book.toString());
77         pm.makePersistent(book);
78         pm.currentTransaction().commit();
79         
80         //detach the book --> "default" fetch group
81
//fields title and year are loaded
82
// fields editor and author are not loaded
83
Book defaultBook = (Book) pm.detachCopy(book);
84         System.out.println( "With the default fetchgroup:");
85         try{
86             System.out.println( "Title can be accessed: " + defaultBook.getTitle());
87             System.out.println( "Year can be accessed: " + defaultBook.getYear());
88             System.out.println( "Author should not be accessed: " + defaultBook.getAuthor().toString());
89             System.out.println( "Editor should not be accessed: " + defaultBook.getEditor().toString());
90         }
91         catch(Exception JavaDoc e){
92             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("author") != -1)
93                 System.out.println( "Correct exception caught: " + e.getMessage());
94             else
95                 System.out.println( "Error: " + e);
96         }
97         
98         //detach the book --> "all" fetch group
99
//all fields are loaded
100
fp.addGroup("all").removeGroup("default");
101         Book allBook = (Book) pm.detachCopy(book);
102         System.out.println( "With the all fetchgroup:");
103         try{
104             System.out.println( "Title can be accessed: " + allBook.getTitle());
105             System.out.println( "Year can be accessed: " + allBook.getYear());
106             System.out.println( "Author can be accessed: " + allBook.getAuthor().toString());
107             System.out.println( "Editor can be accessed: " + allBook.getEditor().toString());
108         }
109         catch(Exception JavaDoc e){
110                 System.out.println( "Error: " + e);
111         }
112     }
113     
114     /**
115      * Detach objects with user-defined fetchgroups (see the detach.jdo file)
116      */

117     public static void useDefinedFetchGroups(PersistenceManager pm){
118         //create a fetch plan
119
FetchPlan fp = pm.getFetchPlan();
120         
121         //create a book
122
Author author = new Author("John Fante");
123         Address address = new Address("South Street", "211ZL2", "York");
124         Editor editor = new Editor("Plon", address);
125         Book book = new Book("Bandini", author, editor, 1938);
126         
127         //make the book persistent
128
pm.currentTransaction().begin();
129         System.out.println( "make persistent the book " + book.toString());
130         pm.makePersistent(book);
131         pm.currentTransaction().commit();
132         //detach the book --> "onlyAuthor" fetch group
133
//fields title year and author are loaded
134
// fields editor is not loaded
135
fp.addGroup("onlyAuthor").removeGroup("default");
136         Book onlyAuthorBook = (Book) pm.detachCopy(book);
137         System.out.println( "With the onlyAuthor fetchgroup:");
138         try{
139             System.out.println( "Title can be accessed: " + onlyAuthorBook.getTitle());
140             System.out.println( "Year can be accessed: " + onlyAuthorBook.getYear());
141             System.out.println( "Author can be accessed: " + onlyAuthorBook.getAuthor().toString());
142             System.out.println( "Editor should not be accessed: " + onlyAuthorBook.getEditor().toString());
143         }
144         catch(Exception JavaDoc e){
145             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("editor") != -1)
146                 System.out.println( "Correct exception caught: " + e.getMessage());
147             else
148                 System.out.println( "Error: " + e);
149         }
150         
151         //detach the book --> "editorName" fetch group
152
//fields title, year, editor.name are loaded
153
//fields author, editor.address are not loaded
154
fp.addGroup("editorName").removeGroup("onlyAuthor");
155         Book editorNameBook = (Book) pm.detachCopy(book);
156         System.out.println( "With the editorName fetchgroup:");
157         try{
158             System.out.println( "Title can be accessed: " + editorNameBook.getTitle());
159             System.out.println( "Year can be accessed: " + editorNameBook.getYear());
160             System.out.println( "Editor name can be accessed: " + editorNameBook.getEditor().getName());
161             System.out.println( "Editor address should not be accessed: " + editorNameBook.getEditor().getAddress().toString());
162             System.out.println( "Author should not be accessed: " + editorNameBook.getAuthor().toString());
163         }
164         catch(Exception JavaDoc e){
165             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("address") != -1)
166                 System.out.println( "Correct exception caught: " + e.getMessage());
167             else
168                 System.out.println( "Error: " + e);
169         }
170     }
171     
172     public static void main(String JavaDoc[] args){
173         TutorialHelper th = null;
174         try {
175             th = new TutorialHelper(args[0]);
176         } catch (IOException JavaDoc e) {
177             e.printStackTrace();
178             System.exit(-1);
179         }
180         TutorialStep7.pmf = th.getPMF();
181         TutorialStep7.fetchPlan();
182     }
183
184 }
185
Popular Tags