Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jdo/Test.java


1   package jdo;
2   
3   
4   import myapp.*;
5   import java.util.Hashtable;
6   import java.util.Vector;
7   import java.util.Enumeration;
8   import java.io.PrintWriter;
9   import java.io.InputStreamReader;
10  import java.io.Serializable;
11  import java.sql.Connection;
12  import java.sql.PreparedStatement;
13  import java.sql.ResultSet;
14  import java.sql.SQLException;
15  import java.sql.DriverManager;
16  import org.xml.sax.ContentHandler;
17  import org.exolab.castor.jdo.JDO;
18  import org.exolab.castor.jdo.Database;
19  import org.exolab.castor.jdo.OQLQuery;
20  import org.exolab.castor.jdo.QueryResults;
21  import org.exolab.castor.util.Logger;
22  import org.exolab.castor.xml.Marshaller;
23  import org.exolab.castor.mapping.Mapping;
24  import org.apache.xml.serialize.*;
25  
26  
27  /**
28   * This example is only intended to show how castor should be set up 
29   * in a standalone environment. For detail examples on the mapping file, 
30   * database schemas, supported features and their expected behaviors, 
31   * please consult the JDO test cases instead. JDO Test cases can be 
32   * found in the full CVS snapshot and located under the directory of 
33   * src\tests\jdo.
34   */
35  public class Test
36  {
37  
38    public static final String DatabaseFile = "../../src/de/nava/informa/impl/jdo/database.xml";
39      
40    public static final String MappingFile = "../../src/de/nava/informa/impl/jdo/mapping.xml";
41  
42    public static final String Usage = "Usage: example jdo";
43  
44    
45    private Mapping  _mapping;
46  
47  
48    private JDO      _jdo;
49  
50  
51    public static void main( String[] args )
52    {
53      PrintWriter   writer;
54      Test          test;
55          
56      writer = new Logger( System.out ).setPrefix( "test" );
57      try {
58        test = new Test( writer );
59        test.run( writer );
60      } catch ( Exception except ) {
61        writer.println( except );
62        except.printStackTrace( writer );
63      }
64    }
65  
66  
67    public Test( PrintWriter writer )
68      throws Exception
69    {
70      // Load the mapping file
71      _mapping = new Mapping( getClass().getClassLoader() );
72      _mapping.setLogWriter( writer );
73      _mapping.loadMapping( getClass().getResource( MappingFile ) );
74  
75      _jdo = new JDO();
76      _jdo.setLogWriter( writer );
77      _jdo.setConfiguration( getClass().getResource( DatabaseFile ).toString() );
78      _jdo.setDatabaseName( "jdotest" );
79    }
80  
81  
82    public void run( PrintWriter writer )
83      throws Exception
84    {
85      Database      db;
86      Product       product;
87      ProductGroup  group;
88      ProductDetail detail;
89      Computer      computer;
90      OQLQuery      productOql;
91      OQLQuery      groupOql;
92      OQLQuery      computerOql;
93      QueryResults  results;
94  
95      db = _jdo.getDatabase();
96  
97      db.begin();
98      writer.println( "Begin transaction" );
99  
100     // Look up the product and if found in the database,
101     // delete this object from the database
102     productOql = db.getOQLQuery( "SELECT p FROM myapp.Product p WHERE id = $1" );
103     productOql.bind( 4 );
104     results = productOql.execute();
105     while ( results.hasMore() ) {
106       product = (Product) results.next();
107       writer.println( "Deleting existing product: " + product );
108       db.remove(  product );
109     }
110         
111     // Look up the computer and if found in the database,
112     // delete ethis object from the database
113     computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c WHERE id = $1" );
114     computerOql.bind( 6 );
115     results = computerOql.execute();
116     while ( results.hasMore() ) {
117       computer = (Computer) results.next();
118       writer.println( "Deleting existing computer: " + computer );
119       db.remove( computer );
120     }
121 
122     // Look up the group and if found in the database,
123     // delete this object from the database
124     groupOql = db.getOQLQuery( "SELECT g FROM myapp.ProductGroup g WHERE id = $1" );
125     groupOql.bind( 3 );
126     results = groupOql.execute();
127     while ( results.hasMore() ) {
128       group = (ProductGroup) results.next();
129       writer.println( "Deleting existing group: " + group );
130       db.remove( group );
131     }
132         
133     // Checkpoint commits all the updates to the database
134     // but leaves the transaction (and locks) open
135     writer.println( "Transaction checkpoint" );
136     db.commit();
137 
138     db.begin();
139     // If no such group exists in the database, create a new
140     // object and persist it
141     groupOql.bind( 3 );
142     results = groupOql.execute();
143     if ( ! results.hasMore() ) {
144       group = new ProductGroup();
145       group.setId( 3 );
146       group.setName( "a group" );
147       db.create( group );
148       writer.println( "Creating new group: " + group );
149     } else {
150       group = (ProductGroup) results.next();
151       writer.println( "Query result: " + group );
152     }
153 
154     // If no such product exists in the database, create a new
155     // object and persist it
156     // Note: product uses group, so group object has to be
157     //       created first, but can be persisted later
158     productOql.bind( 4 );
159     results = productOql.execute();
160     if ( ! results.hasMore() ) {
161       product = new Product();
162       product.setId( 4 );
163       product.setName( "some product" );
164       product.setPrice( 55 );
165       product.setGroup( group );
166       detail = new ProductDetail();
167       detail.setId( 1 );
168       detail.setName( "one" );
169       product.addDetail( detail );
170       detail = new ProductDetail();
171       detail.setId( 2 );
172       detail.setName( "two" );
173       product.addDetail( detail );
174       writer.println( "Creating new product: " + product );
175       db.create( product );
176     } else {
177       writer.println( "Query result: " + results.next() );
178     }
179 
180     // If no such computer exists in the database, create a new
181     // object and persist it
182     // Note: computer uses group, so group object has to be
183     //       created first, but can be persisted later
184     computerOql.bind( 6 );
185     results = computerOql.execute();
186     if ( ! results.hasMore() ) {
187       computer = new Computer();
188       computer.setId( 6 );
189       computer.setCpu( "Pentium" );
190       computer.setName( "MyPC" );
191       computer.setPrice( 300 );
192       computer.setGroup( group );
193       detail = new ProductDetail();
194       detail.setId( 4 );
195       detail.setName( "mouse" );
196       computer.addDetail( detail );
197       detail = new ProductDetail();
198       detail.setId( 5 );
199       detail.setName( "screen" );
200       computer.addDetail( detail );
201       writer.println( "Creating new computer: " + computer );
202       db.create( computer );
203     } else {
204       writer.println( "Query result: " + results.next() );
205     }
206     writer.println( "Commit transaction" );
207     db.commit();
208 
209     Marshaller     marshaller;
210 
211     marshaller = new Marshaller( writer );
212     marshaller.setMapping( _mapping );
213 
214     db.begin();
215     marshaller.marshal( db.load( Product.class, new Integer( 4 ) ) );
216     computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c" );
217     results = computerOql.execute();
218     while( results.hasMore() )
219       marshaller.marshal( results.next() );
220     db.commit();
221 
222     db.close();
223   }
224 
225 }