Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 80bc080..eb80b86 100644 (file)
@@ -3,21 +3,29 @@ package dataAccess;
 import java.io.File;
 //import java.util.Enumeration;
 //import java.util.Vector;
-
 import java.rmi.RemoteException;
-import java.sql.Date;
-import java.util.HashSet;
+
+
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.ListIterator;
 import java.util.Vector;
 
-import com.db4o.*;
+
+import com.db4o.Db4oEmbedded;
+import com.db4o.ObjectContainer;
+import com.db4o.ObjectSet;
 import com.db4o.config.EmbeddedConfiguration;
 import com.db4o.cs.Db4oClientServer;
 import com.db4o.cs.config.ClientConfiguration;
+import com.db4o.query.Predicate;
 
 import configuration.ConfigXML;
 import domain.Account;
+import domain.Administrator;
 import domain.Booking;
+import domain.HouseFeatures;
 import domain.Offer;
 //import dataModel.Offer;
 import domain.Owner;
@@ -36,6 +44,7 @@ public class DB4oManager {
        private static DB4oManager theDB4oManager = null;
 
        private static DB4oManagerAux theDB4oManagerAux;
+
        static ConfigXML c;
 
        private DB4oManager() throws Exception {
@@ -50,14 +59,12 @@ public class DB4oManager {
                        new File(c.getDb4oFilename()).delete();
 
                if (c.isDatabaseLocal()) {
-                       configuration = Db4oEmbedded.newConfiguration();
-                       configuration.common().activationDepth(c.getActivationDepth());
-                       configuration.common().updateDepth(c.getUpdateDepth());
-                       db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
+                       openDB();
                        System.out.println("DataBase opened");
                } else // c.isDatabaseLocal==false
                {
-                       openObjectContainer();
+                       openSDB();
+                       System.out.println("Remote DataBase opened");
                }
                if (c.getDataBaseOpenMode().equals("initialize")) {
                        initializeDB();
@@ -76,10 +83,11 @@ public class DB4oManager {
                configuration = Db4oEmbedded.newConfiguration();
                configuration.common().activationDepth(c.getActivationDepth());
                configuration.common().updateDepth(c.getUpdateDepth());
+               configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
                db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
        }
 
-       private void openObjectContainer() {
+       private void openSDB() {
 
                configurationCS = Db4oClientServer.newClientConfiguration();
                configurationCS.common().activationDepth(c.getActivationDepth());
@@ -111,38 +119,69 @@ public class DB4oManager {
                try {
                        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.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3,
+                                       3, 3);
+                       jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 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.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, 5,
+                                       5, 5, 5);
+                       
+                       alfredo.addRuralHouse("Murgoitz", "", "Cedro", 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);
+
+                       Account admin = new Account("admin","admin", true);
+                       db.store(Administrator.giveAdmin());
                        db.store(jon);
                        db.store(alfredo);
                        db.store(jonAcc);
                        db.store(alfredoAcc);
+                       db.store(admin);
                        db.commit();
                } finally {
-                       // db.close();
+                       db.close();     
+               }
+       }
+
+       public void deleteDB() {
+
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       Owner proto = new Owner(null, null);
+                       ObjectSet result = db.queryByExample(proto);
+                       Vector<Owner> owners = new Vector<Owner>();
+                       while (result.hasNext()) {
+                               Owner o = (Owner) result.next();
+                               System.out.println("Deleted owner: " + o.toString());
+                               db.delete(o);
+                       }
+                       db.commit();
+               } finally {
+                       db.close();
                }
        }
 
-       @SuppressWarnings("finally")
        public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
                        Date lastDay, float price) throws RemoteException, Exception {
                Offer o = null;
-               try {
 
-                       if (c.isDatabaseLocal() == false)
-                               openObjectContainer();
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
 
-                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
-                                       null, null, null, null);
-                       ObjectSet result = db.queryByExample(proto);
+               try {
+
+                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+                                       null, null, null);
+                       ObjectSet<RuralHouse> result = db.queryByExample(proto);
                        RuralHouse rh = (RuralHouse) result.next();
                        o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
                                        lastDay, price);
@@ -155,27 +194,90 @@ public class DB4oManager {
                        System.out
                                        .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
                } finally {
-                       return o;
+                       db.close();
                }
+               return o;
        }
 
-       public void deleteDB() {
+       
+       public void deleteOffer(Offer offer) throws RemoteException,
+                       Exception {
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
 
                try {
-                       Owner proto = new Owner(null, null);
-                       ObjectSet result = db.queryByExample(proto);
-                       Vector<Owner> owners = new Vector<Owner>();
-                       while (result.hasNext()) {
-                               Owner o = (Owner) result.next();
-                               System.out.println("Deleted owner: " + o.toString());
-                               db.delete(o);
-                       }
+                       ObjectSet<Offer> of = db.queryByExample(offer);
+                       RuralHouse rh =of.get(0).getRuralHouse();
+                       System.out.println(rh.offers.remove(of.get(0)));
+                       db.store(rh);
                        db.commit();
+
+               } catch (com.db4o.ext.ObjectNotStorableException e) {
+                       System.out
+                                       .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
                } finally {
-                       // db.close();
+                       db.close();
                }
        }
 
+       
+       public LinkedList<RuralHouse>[] getAdminData(){
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+               LinkedList<RuralHouse>[] ret =  new LinkedList[2];
+               
+               try {
+                       
+                       List<Administrator> admL = db.query(new Predicate<Administrator>(){
+                               public boolean match(Administrator admin){
+                                       return true;
+                               }
+                       });
+                       
+                       ret[0] = admL.get(0).getAddRequest();
+                       ret[1] = admL.get(0).getRemoveRequest();
+                       return ret;
+               }
+               finally {
+                       db.close();
+                       
+               }
+               
+       }
+       
+       public void storeAdmin() {
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();               
+               try {
+                       
+                       List<Administrator> admL = db.query(new Predicate<Administrator>(){
+                               public boolean match(Administrator admin){
+                                       return true;
+                               }
+                       });
+                       
+                       admL.get(0).setAddRequest(Administrator.getAddRequest());
+                       admL.get(0).setRemoveRequest(Administrator.getRemoveRequest());
+                       db.commit();
+                       
+               }
+               catch(Exception e){
+                       
+               }
+               finally {
+                       db.close();
+               }
+               
+               
+       }
+       
+       
        /**
         * This method creates a book with a corresponding parameters
         * 
@@ -187,14 +289,21 @@ public class DB4oManager {
                        Date lastDate, String bookTelephoneNumber)
                        throws OfferCanNotBeBooked {
 
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               Booking bok = null;
+               
+
                try {
 
                        if (c.isDatabaseLocal() == false)
-                               openObjectContainer();
+                               openSDB();
 
-                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
-                                       null, ruralHouse.getDescription(), ruralHouse.getTown(),
-                                       null);
+                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+                                       ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
                        ObjectSet result = db.queryByExample(proto);
                        RuralHouse rh = (RuralHouse) result.next();
 
@@ -208,18 +317,18 @@ public class DB4oManager {
                                                                                                // bookingNumber
                                db.store(offer);
                                db.commit();
-                               return offer.getBooking();
+                               bok = offer.getBooking();
                        }
-                       return null;
 
                } catch (com.db4o.ext.ObjectNotStorableException e) {
                        System.out
                                        .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
-                       return null;
                } catch (Exception exc) {
                        exc.printStackTrace();
-                       return null;
+               } finally {
+                       db.close();
                }
+               return bok;
        }
 
        /**
@@ -229,53 +338,61 @@ public class DB4oManager {
        public Vector<Owner> getOwners() throws RemoteException, Exception {
 
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
+                       openSDB();
+               else
+                       openDB();
 
                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<RuralHouse> getAllRuralHouses() throws RemoteException,
                        Exception {
+
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
+                       openSDB();
+               else
+                       openDB();
 
                try {
-                       RuralHouse proto = new RuralHouse(0, null, null, null, null);
+                       RuralHouse proto = new RuralHouse(null, null, null, null, null);
                        ObjectSet 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();
                }
        }
 
        public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
                        Date lastDay) throws RemoteException, OverlappingOfferExists {
+
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
                try {
-                       if (c.isDatabaseLocal() == false)
-                               openObjectContainer();
 
-                       RuralHouse rhn = (RuralHouse) db
-                                       .queryByExample(
-                                                       new RuralHouse(rh.getHouseNumber(), null, null,
-                                                                       null, null)).next();
+                       RuralHouse rhn = (RuralHouse) db.queryByExample(
+                                       new RuralHouse(rh.getHouseName(), null, null, null, null))
+                                       .next();
                        if (rhn.overlapsWith(firstDay, lastDay) != null)
                                throw new OverlappingOfferExists();
                        else
                                return false;
                } finally {
-                       // db.close();
+                       db.close();
                }
        }
 
@@ -301,17 +418,21 @@ public class DB4oManager {
         */
        public Vector<Account> getAccount(String usr, String pwd)
                        throws RemoteException, Exception {
+
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
+                       openSDB();
+               else
+                       openDB();
+
                try {
-                       Account proto = new Account(usr, pwd, new Owner(null, null));
+                       Account proto = new Account(usr, pwd, null);
                        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();
                }
        }
 
@@ -319,58 +440,78 @@ public class DB4oManager {
         * @param rh
         */
        public boolean storeRuralHouses(RuralHouse rh) {
-//             DB4oManager.openDB();
+
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
+                       openSDB();
+               else
+                       openDB();
+
                boolean stored = false;
-               RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null,
-                               null, null);
+               RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
+                               null);
                try {
                        ObjectSet<Owner> result = db.queryByExample(house);
                        if (result.isEmpty()) {
                                db.store(rh);
                                db.commit();
                                stored = true;
+                       } else {
+                               db.delete(result.get(0));
+                               db.store(rh);
+                               db.commit();
+                               stored = true;
                        }
                } finally {
-//                     db.close();
+                       db.close();
                }
                return stored;
        }
 
-       public void removeHouse(int houseNumber) {
-//             DB4oManager.openDB();
+       public void removeHouse(RuralHouse rh, Owner owner) {
+
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
-               RuralHouse house = new RuralHouse(houseNumber, null, null, null, null);
+                       openSDB();
+               else
+                       openDB();
                try {
-                       ObjectSet<RuralHouse> result = db.queryByExample(house);
-                       if (!result.isEmpty()) {
-                               RuralHouse found = (RuralHouse) result.get(0);
-                               db.delete(found);
+                       ObjectSet<Owner> result = db.queryByExample(owner);
+                       ObjectSet<RuralHouse> rhs = db.queryByExample(rh);
+                       if (!rhs.isEmpty()) {
+                               Owner found =  result.get(0);
+                               found.getRuralHouses().remove(rhs.get(0));
+                               db.store(found);
                                db.commit();
                        }
                } catch (Exception exc) {
                        exc.printStackTrace();
                } finally {
-//                     db.close();
+                       db.close();
                }
 
        }
-       
-       public Vector<RuralHouse> getRuralHousesByTown(String town){
-               RuralHouse rh = new RuralHouse(0,null,null,town,null);
+
+
+       public Vector<RuralHouse> getRuralHouses(String name, String town,
+                       int nBed, int nKit, int nBath, int nPark, int nLiv) {
+               HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
+               RuralHouse rh = new RuralHouse(name, null, null, town, fea);
                if (c.isDatabaseLocal() == false)
-                       openObjectContainer();
-               try{
+                       openSDB();
+               else
+                       openDB();
+
+               try {
                        ObjectSet<RuralHouse> result = db.queryByExample(rh);
                        Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
-                       while(result.hasNext())
+                       while (result.hasNext())
                                ruralHouses.add(result.next());
+                       db.close();
                        return ruralHouses;
-               }finally{
-                       //db.close();
+               } catch (NullPointerException e) {
+                       return null;
                }
-               
+
        }
+
+       
 }