KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > samples > basic > JalistoDemo


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.samples.basic;
25
26 import org.objectweb.jalisto.samples.basic.data.Book;
27 import org.objectweb.jalisto.se.impl.meta.type.MetaTypeFactory;
28 import org.objectweb.jalisto.se.api.Session;
29 import org.objectweb.jalisto.se.api.Transaction;
30 import org.objectweb.jalisto.se.api.ClassDescription;
31 import org.objectweb.jalisto.se.JalistoFactory;
32
33 public class JalistoDemo {
34
35     private Session session;
36     private Transaction tx;
37
38     private Object JavaDoc oid;
39
40     public static void main(String JavaDoc[] args) {
41         JalistoDemo demo = new JalistoDemo();
42
43         demo.initJalisto("samples.properties");
44         demo.defineData();
45         demo.createBook();
46         demo.updateBook();
47         demo.deleteBook();
48         demo.closeJalisto();
49     }
50
51     public void initJalisto(String JavaDoc propertiesFile) {
52         System.out.println("JalistoDemo.initJalisto : "+propertiesFile);
53
54         // get jalisto working session
55
session = JalistoFactory.getSession(propertiesFile);
56         session.openSession();
57         // get the transaction associated to this session
58
tx = session.currentTransaction();
59     }
60
61     public void closeJalisto() {
62         System.out.println("JalistoDemo.closeJalisto");
63
64         session.closeSession();
65     }
66
67     public void defineData() {
68         System.out.println("JalistoDemo.defineData");
69
70         // define structure of persistent data
71
ClassDescription meta = JalistoFactory.createClassDescription(Book.class.getName());
72         meta.addField(JalistoFactory.createFieldDescription("title", MetaTypeFactory.StringType));
73         meta.addField(JalistoFactory.createFieldDescription("author", MetaTypeFactory.StringType));
74         meta.addField(JalistoFactory.createFieldDescription("price", MetaTypeFactory.IntegerType));
75
76         // define this structure in jalisto
77
session.defineClass(meta);
78     }
79
80     public void createBook() {
81         System.out.println("JalistoDemo.createBook");
82
83         tx.begin();
84         Book book = new Book("The Bible", "God", 777);
85         Object JavaDoc[] array = book.toArray();
86         oid = session.createObject(array, Book.class);
87         tx.commit();
88     }
89
90     public void updateBook() {
91         System.out.println("JalistoDemo.updateBook");
92
93         tx.begin();
94         Object JavaDoc[] o = session.readObjectByOid(oid);
95         Book book = Book.toBook(o);
96         book.setPrice(8888);
97         session.updateObjectByOid(oid, book.toArray());
98         tx.commit();
99     }
100
101     public void deleteBook() {
102         System.out.println("JalistoDemo.deleteBook");
103
104         tx.begin();
105         session.deleteObjectByOid(oid);
106         tx.commit();
107     }
108 }
109
Popular Tags