Database management was changed, now it opens the database and it closes it every...
authorpinene <epinzolas001@ikasle.ehu.eus>
Mon, 2 Mar 2015 09:12:44 +0000 (10:12 +0100)
committerpinene <epinzolas001@ikasle.ehu.eus>
Mon, 2 Mar 2015 09:12:44 +0000 (10:12 +0100)
ruralHouses/db/DBjcampos004.yap
ruralHouses/src/businessLogic/ApplicationFacadeInterface.java
ruralHouses/src/businessLogic/FacadeImplementation.java
ruralHouses/src/businessLogic/LoginManager.java
ruralHouses/src/dataAccess/DB4oManager.java
ruralHouses/src/gui/BookRuralHouseGUI.java
ruralHouses/src/gui/StartWindow.java

index 27d41f6..23ef7bc 100644 (file)
Binary files a/ruralHouses/db/DBjcampos004.yap and b/ruralHouses/db/DBjcampos004.yap differ
index 215e60b..8863af3 100644 (file)
@@ -68,6 +68,4 @@ public interface ApplicationFacadeInterface extends Remote {
        public Vector<RuralHouse> getAllRuralHouses()throws RemoteException,
        Exception;
        
-       public void close() throws RemoteException;
-       
 }
\ No newline at end of file
index c4670bb..162a38b 100644 (file)
@@ -75,9 +75,7 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica
                return dbMngr.getAllRuralHouses();              
        }
 
-       public void close() throws RemoteException{
-               DB4oManager.close();
-       }
+       
 
        public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber)
                        throws OfferCanNotBeBooked {
@@ -90,7 +88,10 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica
                        exc.printStackTrace();
                        return null;
                }
+               
        }
 
+       
+
 }
 
index da6078b..58ec39b 100644 (file)
@@ -1,8 +1,8 @@
 package businessLogic;
 
-import gui.StartWindow;
 
-import java.rmi.RemoteException;
+
+
 import java.util.Vector;
 
 import dataAccess.DB4oManager;
index 6d55e8d..3e9fd76 100644 (file)
@@ -33,9 +33,9 @@ public class DB4oManager {
                if (mode.compareTo("open")==0) {
                        db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
                        db.ext().configure().updateDepth(5);
-                       System.out.println("DataBase Opened");
 
                } else if (mode.compareTo("initialize")==0){
+                       try {
                        new File(db4oFileName).delete();
                        db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
                        db.ext().configure().updateDepth(5);
@@ -55,16 +55,17 @@ public class DB4oManager {
                        db.store(alfredoAcc);
                        db.commit();
                        System.out.println("DataBase Initialized");
+                       } finally {
+                       db.close();
+                       }
+                       
                }
        }
 
        public  static ObjectContainer getContainer() {
                return db;
        }
-       public static void close(){
-               db.close();
-               System.out.println("DataBase closed");
-       }
+       
        
        /**
         * This method finds all existing  owners 
@@ -72,48 +73,51 @@ public class DB4oManager {
         */
        public Vector<Owner> getOwners() throws RemoteException,
        Exception {
+               DB4oManager.openDatabase("open");
                ObjectContainer db=DB4oManager.getContainer();
 
                try {
                        Owner proto = new Owner(null,null);
-                       ObjectSet result = db.queryByExample(proto);
+                       ObjectSet<Owner> result = db.queryByExample(proto);
                        Vector<Owner> owners=new Vector<Owner>();
                        while(result.hasNext())                          
                                owners.add((Owner)result.next());
                        return owners;
                } finally {
-                       //db.close();
+                       db.close();
                }
        } 
        
        public Vector<Account> getAccount(String usr, String pwd) throws RemoteException,
        Exception {
+               DB4oManager.openDatabase("open");
                ObjectContainer db=DB4oManager.getContainer();
 
                try {
                        Account proto = new Account(usr,pwd, new Owner(null,null));
-                       ObjectSet result = db.queryByExample(proto);
+                       ObjectSet<Account> result = db.queryByExample(proto);
                        Vector<Account> accounts=new Vector<Account>();
                        while(result.hasNext())                          
                                accounts.add((Account)result.next());
                        return accounts;
                } finally {
-                       //db.close();
+                       db.close();
                }
        }
        
        public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
        Exception {
+               DB4oManager.openDatabase("open");
                ObjectContainer db=DB4oManager.getContainer();
                try {
                        RuralHouse proto = new RuralHouse(0,null,null,null);
-                       ObjectSet result = db.queryByExample(proto);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
                        Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
                        while(result.hasNext()) 
                                ruralHouses.add((RuralHouse)result.next());
                        return ruralHouses;
                } finally {
-                       //db.close();
+                       db.close();
                }
        }
 
@@ -126,32 +130,45 @@ public class DB4oManager {
         */
        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());
-               ObjectSet result = db.queryByExample(proto);
+               ObjectSet<RuralHouse> result = db.queryByExample(proto);
                RuralHouse rh=(RuralHouse)result.next();
-               Offer o=rh.createOffer(firstDay, lastDay, price);
+               o=rh.createOffer(firstDay, lastDay, price);
                db.store(o);
                db.commit(); 
+               }
+               finally{
+               db.close();
+               }
                return o;
        }
 
        public RuralHouse getRuralHouse(RuralHouse rh){
+               
+               DB4oManager.openDatabase("open");
                try {
                        ObjectContainer db=DB4oManager.getContainer();
                        RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
                                        rh.getDescription(), rh.getTown());
-                       ObjectSet result = db.queryByExample(proto);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
                        return  rh=(RuralHouse)result.next();
                } catch (Exception exc) {
                        exc.printStackTrace();
                        return null;
                }
-       }
+               finally {
+                       db.close();
+               }
+       } 
 
        public Booking createBooking(Offer offer, String clientTelephoneNumber)
                        throws OfferCanNotBeBooked {
+               DB4oManager.openDatabase("open");
                try {
                        Booking b=null;
                        if (offer!=null) {
@@ -165,6 +182,9 @@ public class DB4oManager {
                        exc.printStackTrace();
                        return null;
                }
+               finally {
+                       db.close();
+               }
        }
 
        /**
index fead030..eadd8b0 100644 (file)
@@ -136,8 +136,8 @@ public class BookRuralHouseGUI extends JFrame {
                                        ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
 
                                        Booking book=facade.createBooking(house, firstDay, lastDay, telephone);
-                                       System.out.println(book.getPrice());
                                        if (book!=null) {
+                                               System.out.println(book.getPrice());
                                                BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
                                                confirmWindow.setVisible(true);
                                        }
index 98b912e..f5e5b10 100644 (file)
@@ -122,11 +122,11 @@ public class StartWindow extends JFrame {
                                        //JFrame a = new QueryAvailabilityWindow();
                                        ApplicationFacadeInterface facade=StartWindow.facadeInterface;
                                        try {
-                                               facade.close();
+                                               
                                                setVisible(false);
                                                System.exit(1);
                                        }
-                                       catch (RemoteException e1) {
+                                       catch (Exception e1) {
                                                // TODO Auto-generated catch block
                                                e1.printStackTrace();
                                        }                                       
@@ -160,7 +160,6 @@ public class StartWindow extends JFrame {
                        //System.out.println(e.toString());
                        e.printStackTrace();
                }
-               int x=0;
                JFrame a = new StartWindow();
                a.setVisible(true);
        }