X-Git-Url: https://xp-dev.com/git/RRRRHHHH_Code/blobdiff_plain/7b7027e13369849d213c9b4e118395e4bfd23dc2..a927255f6eb55fb1f52738b9b88ce51f9b0ed9bc:/ruralHouses/src/dataAccess/DB4oManager.java diff --git a/ruralHouses/src/dataAccess/DB4oManager.java b/ruralHouses/src/dataAccess/DB4oManager.java index 3e9fd76..b0e4005 100644 --- a/ruralHouses/src/dataAccess/DB4oManager.java +++ b/ruralHouses/src/dataAccess/DB4oManager.java @@ -7,7 +7,6 @@ import java.util.Vector; //import java.util.Enumeration; //import java.util.Vector; - import businessLogic.BookingManager; import com.db4o.*; @@ -16,105 +15,153 @@ import configuration.Config; import domain.*; import exceptions.OfferCanNotBeBooked; -public class DB4oManager { +public class DB4oManager { private static ObjectContainer db; - private static DB4oManager theDB4oManager=new DB4oManager(); + private static DB4oManager theDB4oManager = new DB4oManager(); + /** + * + */ private DB4oManager() { - Config c=Config.getInstance(); - String dataBaseOpenMode=c.getDataBaseOpenMode(); + Config c = Config.getInstance(); + String dataBaseOpenMode = c.getDataBaseOpenMode(); DB4oManager.openDatabase(dataBaseOpenMode); } + /** + * @param 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); - } else if (mode.compareTo("initialize")==0){ + + } 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"); - 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"); + 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(); + db.close(); } - + } } - public static ObjectContainer getContainer() { + /** + * @return + */ + public static ObjectContainer getContainer() { return db; } - - + + + /** + * @param rh + */ + public boolean storeRuralHouses(RuralHouse rh) { + DB4oManager.openDatabase("open"); + boolean stored = false; + ObjectContainer db = DB4oManager.getContainer(); + RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null, + null, null); + try { + ObjectSet result = db.queryByExample(house); + if (result.isEmpty()) { + db.store(rh); + db.commit(); + stored = true; + } + } finally { + db.close(); + } + return stored; + } + /** - * This method finds all existing owners + * This method finds all existing owners * */ - public Vector getOwners() throws RemoteException, - Exception { + + public Vector getOwners() throws RemoteException, Exception { DB4oManager.openDatabase("open"); - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); try { - Owner proto = new Owner(null,null); + Owner proto = new Owner(null, null); ObjectSet result = db.queryByExample(proto); - Vector owners=new Vector(); - while(result.hasNext()) - owners.add((Owner)result.next()); + Vector owners = new Vector(); + while (result.hasNext()) + owners.add((Owner) result.next()); return owners; } finally { db.close(); } - } - - public Vector getAccount(String usr, String pwd) throws RemoteException, - Exception { + + } + + /** + * @param usr + * @param pwd + * @return + * @throws RemoteException + * @throws Exception + */ + public Vector getAccount(String usr, String pwd) + throws RemoteException, Exception { DB4oManager.openDatabase("open"); - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); try { - Account proto = new Account(usr,pwd, new Owner(null,null)); + Account proto = new Account(usr, pwd, new Owner(null, null)); ObjectSet result = db.queryByExample(proto); - Vector accounts=new Vector(); - while(result.hasNext()) - accounts.add((Account)result.next()); + Vector accounts = new Vector(); + while (result.hasNext()) + accounts.add((Account) result.next()); return accounts; } finally { db.close(); } } - + + /** + * @return + * @throws RemoteException + * @throws Exception + */ public Vector getAllRuralHouses() throws RemoteException, - Exception { + + Exception { DB4oManager.openDatabase("open"); - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); try { - RuralHouse proto = new RuralHouse(0,null,null,null); + RuralHouse proto = new RuralHouse(0, null, null, null, null); ObjectSet result = db.queryByExample(proto); - Vector ruralHouses=new Vector(); - while(result.hasNext()) - ruralHouses.add((RuralHouse)result.next()); + Vector ruralHouses = new Vector(); + while (result.hasNext()) + ruralHouses.add((RuralHouse) result.next()); return ruralHouses; } finally { db.close(); @@ -122,57 +169,71 @@ public class DB4oManager { } /** - * 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 */ - public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, - float price) throws RemoteException, Exception { + + public Offer createOffer(RuralHouse ruralHouse, Date firstDay, + Date lastDay, float price) throws RemoteException, Exception { DB4oManager.openDatabase("open"); - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); Offer o; try { - RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null, - ruralHouse.getDescription(), ruralHouse.getTown()); - ObjectSet result = db.queryByExample(proto); - RuralHouse rh=(RuralHouse)result.next(); - o=rh.createOffer(firstDay, lastDay, price); - db.store(o); - db.commit(); - } - finally{ - db.close(); + RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), + null, ruralHouse.getDescription(), ruralHouse.getTown(), + ruralHouse.getFeatures()); + ObjectSet 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; } - public RuralHouse getRuralHouse(RuralHouse rh){ - + + /** + * @param rh + * @return + */ + public RuralHouse getRuralHouse(RuralHouse rh) { + DB4oManager.openDatabase("open"); try { - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null, - rh.getDescription(), rh.getTown()); + + rh.getDescription(), rh.getTown(), rh.getFeatures()); ObjectSet result = db.queryByExample(proto); - return rh=(RuralHouse)result.next(); + return rh = (RuralHouse) result.next(); } catch (Exception exc) { exc.printStackTrace(); return null; - } - finally { + } finally { db.close(); } + } + /** + * @param offer + * @param clientTelephoneNumber + * @return + * @throws OfferCanNotBeBooked + */ public Booking createBooking(Offer offer, String clientTelephoneNumber) throws OfferCanNotBeBooked { DB4oManager.openDatabase("open"); 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(); @@ -181,20 +242,37 @@ public class DB4oManager { } catch (Exception exc) { exc.printStackTrace(); return null; + } finally { + db.close(); } - finally { + + } + + public void removeHouse(int houseNumber) { + ObjectContainer db = DB4oManager.getContainer(); + RuralHouse house = new RuralHouse(houseNumber, null, null, null, null); + try { + ObjectSet result = db.queryByExample(house); + if (!result.isEmpty()) { + RuralHouse found = (RuralHouse) result.get(0); + db.delete(found); + db.commit(); + } + } catch (Exception exc) { + exc.printStackTrace(); + } 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 */ - public static DB4oManager getInstance() { + public static DB4oManager getInstance() { return theDB4oManager; } } -