The code for different owner operations mostly implemented
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 6d55e8d..295af73 100644 (file)
@@ -7,7 +7,6 @@ import java.util.Vector;
 //import java.util.Enumeration;
 //import java.util.Vector;
 
 //import java.util.Enumeration;
 //import java.util.Vector;
 
-
 import businessLogic.BookingManager;
 
 import com.db4o.*;
 import businessLogic.BookingManager;
 
 import com.db4o.*;
@@ -16,146 +15,209 @@ import configuration.Config;
 import domain.*;
 import exceptions.OfferCanNotBeBooked;
 
 import domain.*;
 import exceptions.OfferCanNotBeBooked;
 
-public class DB4oManager { 
+public class DB4oManager {
 
        private static ObjectContainer db;
 
        private static ObjectContainer db;
-       private static DB4oManager theDB4oManager=new DB4oManager();
+       private static DB4oManager theDB4oManager = new DB4oManager();
 
 
+       /**
+        * 
+        */
        private DB4oManager() {
        private DB4oManager() {
-               Config c=Config.getInstance();
-               String dataBaseOpenMode=c.getDataBaseOpenMode();
+               Config c = Config.getInstance();
+               String dataBaseOpenMode = c.getDataBaseOpenMode();
                DB4oManager.openDatabase(dataBaseOpenMode);
        }
 
                DB4oManager.openDatabase(dataBaseOpenMode);
        }
 
+       /**
+        * @param mode
+        */
        public static void openDatabase(String mode) {
        public static void openDatabase(String mode) {
-               Config c=Config.getInstance();
-               String db4oFileName=c.getDb4oFilename();
-               if (mode.compareTo("open")==0) {
-                       db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
+               Config c = Config.getInstance();
+               String db4oFileName = c.getDb4oFilename();
+               if (mode.compareTo("open") == 0) {
+                       db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
                        db.ext().configure().updateDepth(5);
                        db.ext().configure().updateDepth(5);
-                       System.out.println("DataBase Opened");
 
 
-               } else if (mode.compareTo("initialize")==0){
-                       new File(db4oFileName).delete();
-                       db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
-                       db.ext().configure().updateDepth(5);
-                       Owner jon = new Owner("Jon");
-                       Owner alfredo = new Owner("Alfredo");
-                       jon.addRuralHouse(1, "Ezkioko etxea","Ezkio");
-                       jon.addRuralHouse(2, "Eskiatzeko etxea","Jaca");
-                       jon.setBankAccount("1349 5677 21 2133567777");
-                       alfredo.addRuralHouse(3, "Casa del abuelo","Pitillas");
-                       alfredo.addRuralHouse(4, "Refugio","Murgia");
-                       alfredo.setBankAccount("4144 0087 23 9700002133");
-                       Account jonAcc = new Account("userJon", "passJon",jon);
-                       Account alfredoAcc = new Account("userAlfredo", "passAlfredo",alfredo);
-                       db.store(jon);
-                       db.store(alfredo);
-                       db.store(jonAcc);
-                       db.store(alfredoAcc);
-                       db.commit();
-                       System.out.println("DataBase Initialized");
+               } else if (mode.compareTo("initialize") == 0) {
+                       try {
+                               new File(db4oFileName).delete();
+                               db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
+                               db.ext().configure().updateDepth(5);
+                               Owner jon = new Owner("Jon");
+                               Owner alfredo = new Owner("Alfredo");
+                               jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
+                               jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
+                               jon.setBankAccount("1349 5677 21 2133567777");
+                               alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5,
+                                               5, 5, 5);
+                               alfredo.addRuralHouse(4, "Murgia", 6, 6, 6, 6, 6);
+                               alfredo.setBankAccount("4144 0087 23 9700002133");
+                               Account jonAcc = new Account("userJon", "passJon", jon);
+                               Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
+                                               alfredo);
+                               db.store(jon);
+                               db.store(alfredo);
+                               db.store(jonAcc);
+                               db.store(alfredoAcc);
+                               db.commit();
+                               System.out.println("DataBase Initialized");
+                       } finally {
+                               db.close();
+                       }
+
                }
        }
 
                }
        }
 
-       public  static ObjectContainer getContainer() {
+       /**
+        * @return
+        */
+       public static ObjectContainer getContainer() {
                return db;
        }
                return db;
        }
-       public static void close(){
-               db.close();
-               System.out.println("DataBase closed");
+
+       /**
+        * @param rh
+        */
+       public void storeRuralHouses(RuralHouse rh) {
+               DB4oManager.openDatabase("open");
+               ObjectContainer db = DB4oManager.getContainer();
+               try {
+                       db.store(rh);
+                       db.commit();
+               } finally {
+                       db.close();
+               }
        }
        }
-       
+
        /**
        /**
-        * This method finds all existing  owners 
+        * This method finds all existing owners
         * 
         */
         * 
         */
-       public Vector<Owner> getOwners() throws RemoteException,
-       Exception {
-               ObjectContainer db=DB4oManager.getContainer();
+       public Vector<Owner> getOwners() throws RemoteException, Exception {
+               DB4oManager.openDatabase("open");
+               ObjectContainer db = DB4oManager.getContainer();
 
                try {
 
                try {
-                       Owner proto = new Owner(null,null);
-                       ObjectSet result = db.queryByExample(proto);
-                       Vector<Owner> owners=new Vector<Owner>();
-                       while(result.hasNext())                          
-                               owners.add((Owner)result.next());
+                       Owner proto = new Owner(null, null);
+                       ObjectSet<Owner> result = db.queryByExample(proto);
+                       Vector<Owner> owners = new Vector<Owner>();
+                       while (result.hasNext())
+                               owners.add((Owner) result.next());
                        return owners;
                } finally {
                        return owners;
                } finally {
-                       //db.close();
+                       db.close();
                }
                }
-       } 
-       
-       public Vector<Account> getAccount(String usr, String pwd) throws RemoteException,
-       Exception {
-               ObjectContainer db=DB4oManager.getContainer();
+       }
+
+       /**
+        * @param usr
+        * @param pwd
+        * @return
+        * @throws RemoteException
+        * @throws Exception
+        */
+       public Vector<Account> getAccount(String usr, String pwd)
+                       throws RemoteException, Exception {
+               DB4oManager.openDatabase("open");
+               ObjectContainer db = DB4oManager.getContainer();
 
                try {
 
                try {
-                       Account proto = new Account(usr,pwd, new Owner(null,null));
-                       ObjectSet result = db.queryByExample(proto);
-                       Vector<Account> accounts=new Vector<Account>();
-                       while(result.hasNext())                          
-                               accounts.add((Account)result.next());
+                       Account proto = new Account(usr, pwd, new Owner(null, null));
+                       ObjectSet<Account> result = db.queryByExample(proto);
+                       Vector<Account> accounts = new Vector<Account>();
+                       while (result.hasNext())
+                               accounts.add((Account) result.next());
                        return accounts;
                } finally {
                        return accounts;
                } finally {
-                       //db.close();
+                       db.close();
                }
        }
                }
        }
-       
+
+       /**
+        * @return
+        * @throws RemoteException
+        * @throws Exception
+        */
        public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
        public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
-       Exception {
-               ObjectContainer db=DB4oManager.getContainer();
+                       Exception {
+               DB4oManager.openDatabase("open");
+               ObjectContainer db = DB4oManager.getContainer();
                try {
                try {
-                       RuralHouse proto = new RuralHouse(0,null,null,null);
-                       ObjectSet result = db.queryByExample(proto);
-                       Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
-                       while(result.hasNext()) 
-                               ruralHouses.add((RuralHouse)result.next());
+                       RuralHouse proto = new RuralHouse(0, null, null, null, 0, 0, 0, 0,
+                                       0);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
+                       Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
+                       while (result.hasNext())
+                               ruralHouses.add((RuralHouse) result.next());
                        return ruralHouses;
                } finally {
                        return ruralHouses;
                } finally {
-                       //db.close();
+                       db.close();
                }
        }
 
        /**
                }
        }
 
        /**
-        * This method creates an offer with a house number, first day, last day and price
-        * precondition: There are no overlapping offers
+        * This method creates an offer with a house number, first day, last day and
+        * price precondition: There are no overlapping offers
+        * 
         * @param House
         *            number, start day, last day and price
         * @return None
         */
         * @param House
         *            number, start day, last day and price
         * @return None
         */
-       public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
-                       float price) throws RemoteException, Exception {
-               ObjectContainer db=DB4oManager.getContainer();
-               RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null, 
-                               ruralHouse.getDescription(), ruralHouse.getTown());
-               ObjectSet result = db.queryByExample(proto);
-               RuralHouse rh=(RuralHouse)result.next();
-               Offer o=rh.createOffer(firstDay, lastDay, price);
-               db.store(o);
-               db.commit(); 
+       public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
+                       Date lastDay, float price) throws RemoteException, Exception {
+               DB4oManager.openDatabase("open");
+               ObjectContainer db = DB4oManager.getContainer();
+               Offer o;
+               try {
+                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
+                                       null, ruralHouse.getDescription(), ruralHouse.getTown(), 0,
+                                       0, 0, 0, 0);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
+                       RuralHouse rh = (RuralHouse) result.next();
+                       o = rh.createOffer(firstDay, lastDay, price);
+                       db.store(o);
+                       db.commit();
+               } finally {
+                       db.close();
+               }
                return o;
        }
 
                return o;
        }
 
-       public RuralHouse getRuralHouse(RuralHouse rh){
+       /**
+        * @param rh
+        * @return
+        */
+       public RuralHouse getRuralHouse(RuralHouse rh) {
+
+               DB4oManager.openDatabase("open");
                try {
                try {
-                       ObjectContainer db=DB4oManager.getContainer();
+                       ObjectContainer db = DB4oManager.getContainer();
                        RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
                        RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
-                                       rh.getDescription(), rh.getTown());
-                       ObjectSet result = db.queryByExample(proto);
-                       return  rh=(RuralHouse)result.next();
+                                       rh.getDescription(), rh.getTown(), 0, 0, 0, 0, 0);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
+                       return rh = (RuralHouse) result.next();
                } catch (Exception exc) {
                        exc.printStackTrace();
                        return null;
                } catch (Exception exc) {
                        exc.printStackTrace();
                        return null;
+               } finally {
+                       db.close();
                }
        }
 
                }
        }
 
+       /**
+        * @param offer
+        * @param clientTelephoneNumber
+        * @return
+        * @throws OfferCanNotBeBooked
+        */
        public Booking createBooking(Offer offer, String clientTelephoneNumber)
                        throws OfferCanNotBeBooked {
        public Booking createBooking(Offer offer, String clientTelephoneNumber)
                        throws OfferCanNotBeBooked {
+               DB4oManager.openDatabase("open");
                try {
                try {
-                       Booking b=null;
-                       if (offer!=null) {
-                               b=offer.createBook(clientTelephoneNumber);
+                       Booking b = null;
+                       if (offer != null) {
+                               b = offer.createBook(clientTelephoneNumber);
                                db.store(b);
                                db.store(offer);
                                db.commit();
                                db.store(b);
                                db.store(offer);
                                db.commit();
@@ -164,17 +226,18 @@ public class DB4oManager {
                } catch (Exception exc) {
                        exc.printStackTrace();
                        return null;
                } catch (Exception exc) {
                        exc.printStackTrace();
                        return null;
+               } finally {
+                       db.close();
                }
        }
 
        /**
                }
        }
 
        /**
-        * This method returns the instance of the DB4oManager class 
+        * This method returns the instance of the DB4oManager class
         * 
         * @return the db4o manager
         */
         * 
         * @return the db4o manager
         */
-       public static DB4oManager getInstance()  {
+       public static DB4oManager getInstance() {
                return theDB4oManager;
        }
 
 }
                return theDB4oManager;
        }
 
 }
-