package dbj.odmg; import COM.POET.odmg.*; import COM.POET.odmg.collection.*; import java.io.IOException; import java.io.FileInputStream; import java.util.Properties; public class Populate { Database dbase; public Populate (Database db) { dbase = db; } public void populate () throws ODMGException { BagOfObject authors, categories, books, orders, customers; Transaction txn = new Transaction (); txn.begin (); try { // Autoren erzeugen authors = new BagOfObject (); dbase.bind (authors, "authors"); Author a1 = new Author ("Gunter", "Saake"); authors.add (a1); Author a2 = new Author ("Andreas", "Heuer"); authors.add (a2); Author a3 = new Author ("Michel", "Goossens"); authors.add (a3); Author a4 = new Author ("Frank", "Mittelbach"); authors.add (a3); Author a5 = new Author ("Alexander", "Samarin"); authors.add (a5); // Sachgebiete erzeugen categories = new BagOfObject (); dbase.bind (categories, "categories"); Category c = new Category ("Informatik"); categories.add (c); // Bücher erzeugen books = new BagOfObject (); dbase.bind (books, "books"); Book b1 = new Book ("3-8266-0513-6"); b1.setTitle ("Datenbanken: Implementierungstechniken"); b1.setPrice (69.0); b1.addAuthor (a1); b1.addAuthor (a2); b1.setCategory (c); books.add (b1); System.out.println ("Book: " + b1); Book b2 = new Book ("3-89319-646-3"); b2.setTitle ("Der LaTeX-Begleiter"); b2.setPrice (79.9); b2.addAuthor (a3); b2.addAuthor (a4); b2.addAuthor (a5); b2.setCategory (c); books.add (b2); System.out.println ("Book: " + b2); // Kunden erzeugen CustomerId custId = new CustomerId (); dbase.bind (custId, "customer_id"); customers = new BagOfObject (); dbase.bind (customers, "customers"); Customer cust = new Customer (custId.next (), "Otto", "Guericke"); customers.add (cust); // Bestellungen erzeugen orders = new BagOfObject (); dbase.bind (orders, "orders"); } catch (ODMGException exc) { txn.abort (); throw exc; } txn.commit (); } public void cleanup () throws ODMGException { BagOfObject authors, categories, books, orders, customers; Transaction txn = new Transaction (); txn.begin (); try { orders = (BagOfObject) dbase.lookup ("orders"); ObjectServices.current ().deleteAll (orders.elements ()); dbase.unbind ("orders"); ObjectServices.current ().delete (orders); } catch (ObjectNameNotFoundException e) { } try { books = (BagOfObject) dbase.lookup ("books"); ObjectServices.current ().deleteAll (books.elements ()); dbase.unbind ("books"); ObjectServices.current ().delete (books); } catch (ObjectNameNotFoundException e) { } try { authors = (BagOfObject) dbase.lookup ("authors"); ObjectServices.current ().deleteAll (authors.elements ()); dbase.unbind ("authors"); ObjectServices.current ().delete (authors); } catch (ObjectNameNotFoundException e) { } try { categories = (BagOfObject) dbase.lookup ("categories"); ObjectServices.current ().deleteAll (categories.elements ()); dbase.unbind ("categories"); ObjectServices.current ().delete (categories); } catch (ObjectNameNotFoundException e) { } try { customers = (BagOfObject) dbase.lookup ("customers"); ObjectServices.current ().deleteAll (customers.elements ()); dbase.unbind ("customers"); ObjectServices.current ().delete (customers); } catch (ObjectNameNotFoundException e) { } try { CustomerId custId = (CustomerId) dbase.lookup ("customer_id"); dbase.unbind ("customer_id"); ObjectServices.current ().delete (custId); } catch (ObjectNameNotFoundException e) { } txn.commit (); } public static void main (String args[]) throws IOException { Properties props = new Properties (); props.load (new FileInputStream ("dbj.properties")); try { Database db = Database.open (props.getProperty ("odmg.database"), Database.openReadWrite); Populate app = new Populate (db); app.cleanup (); app.populate (); } catch (ODMGException exc) { exc.printStackTrace (); } } }