package rmrs.persist.test; import rmrs.persist.PersistentObject; import rmrs.persist.ObjectSet; import rmrs.persist.PersistException; import rmrs.persist.PersistenceBroker; import rmrs.persist.RelationalBroker; import rmrs.persist.ObjectRelationalBroker; import rmrs.persist.OID; import java.util.Date; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.FileInputStream; import java.util.Enumeration; public class Patient extends Person { String mrn; Person doctor = null; ObjectSet observations = new ObjectSet(Observation.class); public static Patient newInstance() { return new Patient(); } private Patient() { } public Patient(String name, String address, Date birth_dttm, String mrn) { super(name, address, birth_dttm); this.mrn = mrn; } public void setDoctor(Person doctor) { this.doctor = doctor; } public void addObservation(Observation obx) throws PersistException { observations.add(obx); } public String toString() { StringBuffer buf = new StringBuffer(super.toString()); buf.append(":Patient[mrn="); buf.append(mrn); buf.append(",doctor="); buf.append(doctor); buf.append(",observations={"); Enumeration e = observations.elements(); while(e.hasMoreElements()) buf.append(e.nextElement().toString()); buf.append("}]"); return buf.toString(); } static void main(String args[]) throws PersistException, ClassNotFoundException, java.io.IOException { int argi = 0; PersistenceBroker pb = null; // java.sql.DriverManager.setLogStream(System.out); if(args[argi].equals("pgsql")) { argi++; Class.forName("postgresql.Driver"); pb = new ObjectRelationalBroker( "jdbc:postgresql://aurora.rg.iupui.edu/test", "scott","tiger"); } else if(args[argi].equals("mysql")) { argi++; Class.forName("org.gjt.mm.mysql.Driver"); pb = new RelationalBroker( "jdbc:mysql://aurora.rg.iupui.edu/test", "scott","tiger"); } else { if(args[argi].equals("hsql")) argi++; Class.forName("hSql.hDriver"); pb = new RelationalBroker("jdbc:HypersonicSQL:test","sa",""); } if(args[argi].equals("save")) { argi++; Person doc = new Person("Dr. Smith", "Indianapolis", new Date(57,10,15)); Patient a = new Patient("Hans Meier", "Berlin", new Date(66,2,19), "123-45-6789"); Patient b = new Patient("Peter Mueller", "Sondheim", new Date(69,4,10), "987-65-4321"); Observation o1 = new Observation("Na", new Interval(135,138), "mmol/L", new Date()); Observation o2 = new Observation("K", new Interval(3.8,4.0), "mmol/L", new Date()); Observation o3 = new Observation("Cl", new Interval(95.4,97.0), "mmol/L", new Date()); a.setDoctor(doc); a.addObservation(o1); a.addObservation(o2); a.addObservation(o3); System.out.println("DOC----"); doc.save(pb); System.out.println("A----"); a.save(pb); System.out.println("B----"); b.save(pb); System.out.println("END----"); o1.save(pb); o2.save(pb); o3.save(pb); a.save(); System.out.println(doc); System.out.println(a); System.out.println(b); ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.oids")); os.writeObject(doc.getOID()); os.writeObject(a.getOID()); os.writeObject(b.getOID()); } else if(args[argi].equals("load")) { argi++; ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.oids")); OID oid_doc = (OID)is.readObject(); OID oid_a = (OID)is.readObject(); OID oid_b = (OID)is.readObject(); Person doc = (Person)pb.load(Person.class, oid_doc); Patient a = (Patient)pb.load(Person.class, oid_a); Patient b = (Patient)pb.load(Person.class, oid_b); System.out.println(doc); System.out.println(a); System.out.println(b); } else if(args[argi].equals("query")) { argi++; Patient a = new Patient("Hans Meier", null, null, "123-45-6789"); a = (Patient)a.cloneQueryExample(); System.out.println(a); ObjectSet os = a.query(pb); Enumeration e = os.elements(); while(e.hasMoreElements()) { Patient pat = (Patient)e.nextElement(); System.out.println(pat); } } } }