import java.io.*; //import java.util.*; import java.util.Enumeration; import COM.odi.*; import COM.odi.util.*; import COM.odi.util.query.Query; public class readData { public static void main(String argv[]){ String dbName = argv[0]; try { // initialize the objectStore database ObjectStore.initialize(null,null); Database db= Database.open(dbName,ObjectStore.OPEN_READONLY); readDatabase(db); } catch (Throwable throwable) { java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream(); java.io.PrintWriter writer = new java.io.PrintWriter(stream); throwable.printStackTrace(writer); writer.flush(); System.err.println("An unhandled throw occurred " + "while executing the Movie Read program:\n" + stream); System.exit(1); } ObjectStore.shutdown(true); } static void readDatabase(Database db) { // start the transaction Transaction tr =Transaction.begin(ObjectStore.READONLY); // get database roots OSTreeSet allMovies = (OSTreeSet) db.getRoot("allMovies"); OSTreeSet allStars = (OSTreeSet) db.getRoot("allStars"); System.out.println("------------------------------------"); // create an iterator to go through all the objects System.out.println("The title of movies are:"); Iterator movieIterator = allMovies.iterator(); int i=1; while(movieIterator.hasNext()) { Movie m = (Movie) movieIterator.next(); System.out.println(" " + i++ + "." + " " + m.getTitle()); } System.out.println("------------------------------------"); //create iterator to go through all Star objects Iterator starIterator = allStars.iterator(); System.out.println("The stars are:"); i=1; while(starIterator.hasNext()){ Star s = (Star) starIterator.next(); System.out.println(" " + i++ + "." + " " + s.getStarName()); } System.out.println("------------------------------------"); System.out.println(); // This a query. Get the movies in which "tina" has acted Query q = new Query(Star.class, "getStarName() == \"tina\""); Collection stars = (Collection) db.getRoot("allStars"); Set result = q.select(stars); Iterator qryIterator = result.iterator(); while(qryIterator.hasNext()){ Star s1 = (Star) qryIterator.next(); System.out.println("The number of movies acted by tina are:"+ s1.getNumMovies()); System.out.println("They are:"); Enumeration m = s1.getMovies().elements(); while (m.hasMoreElements()) { Movie x = (Movie) m.nextElement(); System.out.println(x.getTitle()); } } System.out.println("-----------------------------------"); System.out.println(); // Query to get names of all stars who have acted in movie bugs life Query q1 = new Query(Movie.class,"getTitle() == \"bugs life\""); Collection mvs = (Collection) db.getRoot("allMovies"); Set result1 = q1.select(mvs); Iterator q1Iterator = result1.iterator(); while(q1Iterator.hasNext()){ Movie mv = (Movie) q1Iterator.next(); System.out.println("The number of stars in bugs life are "+ mv.getNumStars()); System.out.println("They are:"); Enumeration strs = mv.getStars().elements(); while (strs.hasMoreElements()) { Star s = (Star) strs.nextElement(); System.out.println(s.getStarName() + " " + s.getStarAddress()); } } System.out.println("------------------------------------"); System.out.println(); // commit the transation tr.current().commit(); // close the database db.close(); } }