Problems when accepting bookings fixed
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 0e6e881..4a1a964 100644 (file)
@@ -5,7 +5,6 @@ import java.io.File;
 //import java.util.Vector;
 import java.rmi.RemoteException;
 import java.util.Date;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Vector;
@@ -22,6 +21,7 @@ import configuration.ConfigXML;
 import domain.Account;
 import domain.Administrator;
 import domain.Booking;
+import domain.Client;
 import domain.HouseFeatures;
 import domain.Offer;
 //import dataModel.Offer;
@@ -69,7 +69,8 @@ public class DB4oManager {
                } else // c.getDataBaseOpenMode().equals("open")
 
                {
-                       ObjectSet<DB4oManagerAux> res = db.queryByExample(DB4oManagerAux.class);
+                       ObjectSet<DB4oManagerAux> res = db
+                                       .queryByExample(DB4oManagerAux.class);
                        ListIterator<DB4oManagerAux> listIter = res.listIterator();
                        if (listIter.hasNext())
                                theDB4oManagerAux = (DB4oManagerAux) res.next();
@@ -81,6 +82,12 @@ public class DB4oManager {
                configuration.common().activationDepth(c.getActivationDepth());
                configuration.common().updateDepth(c.getUpdateDepth());
                configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
+               configuration.common().objectClass(Booking.class).cascadeOnDelete(true);
+               configuration.common().objectClass(RuralHouse.class)
+                               .cascadeOnDelete(true);
+               configuration.common().objectClass(Offer.class)
+               .cascadeOnDelete(true);
+               configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
                db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
        }
 
@@ -114,32 +121,30 @@ public class DB4oManager {
        public void initializeDB() {
 
                try {
-                       Owner jon = new Owner("Jon");
-                       Owner alfredo = new Owner("Alfredo");
+                       Owner jon = new Owner("Jon", "1349 5677 21 2133567777",
+                                       "Jon@gmail.com");
+                       Owner alfredo = new Owner("Alfredo", "4144 0087 23 9700002133",
+                                       "alfredo@gmail.com");
                        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("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 jonAcc = new Account("1", "1", 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);
+                       Account admin = new Account("admin", "admin", true);
+                       db.store(Administrator.getInstance());
                        db.store(jonAcc);
                        db.store(alfredoAcc);
                        db.store(admin);
                        db.commit();
                } finally {
-                       db.close();     
+                       db.close();
                }
        }
 
@@ -151,7 +156,7 @@ public class DB4oManager {
                        openDB();
 
                try {
-                       Owner proto = new Owner(null, null);
+                       Owner proto = new Owner(null, null, null);
                        ObjectSet<Owner> result = db.queryByExample(proto);
                        while (result.hasNext()) {
                                Owner o = (Owner) result.next();
@@ -195,9 +200,7 @@ public class DB4oManager {
                return o;
        }
 
-       
-       public void deleteOffer(Offer offer) throws RemoteException,
-                       Exception {
+       public void deleteOffer(Offer offer) throws RemoteException, Exception {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -205,8 +208,8 @@ public class DB4oManager {
 
                try {
                        ObjectSet<Offer> of = db.queryByExample(offer);
-                       RuralHouse rh =of.get(0).getRuralHouse();
-                       System.out.println(rh.offers.remove(of.get(0)));
+                       RuralHouse rh = of.get(0).getRuralHouse();
+                       System.out.println(rh.getAllOffers().remove(of.get(0)));
                        db.store(rh);
                        db.commit();
 
@@ -218,75 +221,66 @@ public class DB4oManager {
                }
        }
 
-       
-       @SuppressWarnings("static-access")
-       public LinkedList<RuralHouse>[] getAdminData(){
+       public Administrator getAdminData() {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        openDB();
-               @SuppressWarnings("unchecked")
-               LinkedList<RuralHouse>[] ret =  new LinkedList[2];
-               
+
                try {
-                       
-                       List<Administrator> admL = db.query(new Predicate<Administrator>(){
-                               /**
-                                * 
-                                */
+
+                       List<Administrator> admL = db.query(new Predicate<Administrator>() {
                                private static final long serialVersionUID = 1L;
 
-                               public boolean match(Administrator admin){
+                               public boolean match(Administrator admin) {
                                        return true;
                                }
                        });
-                       
-                       ret[0] = admL.get(0).getAddRequest();
-                       ret[1] = admL.get(0).getRemoveRequest();
-                       return ret;
-               }
-               finally {
+
+                       return admL.get(0);
+               } finally {
                        db.close();
-                       
+
                }
-               
+
        }
-       
-       @SuppressWarnings("static-access")
+
        public void storeAdmin() {
+
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
-                       openDB();               
+                       openDB();
                try {
-                       
-                       List<Administrator> admL = db.query(new Predicate<Administrator>(){
+
+                       List<Administrator> admL = db.query(new Predicate<Administrator>() {
                                /**
                                 * 
                                 */
                                private static final long serialVersionUID = 1L;
 
-                               public boolean match(Administrator admin){
+                               public boolean match(Administrator admin) {
                                        return true;
                                }
                        });
-                       
-                       admL.get(0).setAddRequest(Administrator.getAddRequest());
-                       admL.get(0).setRemoveRequest(Administrator.getRemoveRequest());
+
+                       admL.get(0).setAddRequest(
+                                       Administrator.getInstance().getAddRequest());
+                       admL.get(0).setRemoveRequest(
+                                       Administrator.getInstance().getRemoveRequest());
+                       admL.get(0).setNewOwnerRequest(
+                                       Administrator.getInstance().getNewOwnerRequest());
+
                        db.commit();
-                       
-               }
-               catch(Exception e){
-                       
-               }
-               finally {
+
+               } catch (Exception e) {
+
+               } finally {
                        db.close();
                }
-               
-               
+
        }
-       
-       
+
        /**
         * This method creates a book with a corresponding parameters
         * 
@@ -294,17 +288,15 @@ public class DB4oManager {
         *            day, last day, house number and telephone
         * @return a book
         */
-       public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
-                       Date lastDate, String bookTelephoneNumber)
-                       throws OfferCanNotBeBooked {
+       public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate,
+                       Date lastDate, Client cl) throws OfferCanNotBeBooked {
 
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        openDB();
 
-               Booking bok = null;
-               
+               Vector<Booking> book = new Vector<Booking>();
 
                try {
 
@@ -320,13 +312,14 @@ public class DB4oManager {
                        offer = rh.findOffer(firstDate, lastDate);
 
                        if (offer != null) {
-                               offer.createBooking(theDB4oManagerAux.bookingNumber++,
-                                               bookTelephoneNumber);
+                               offer.createBooking(theDB4oManagerAux.bookingNumber++, cl);
                                db.store(theDB4oManagerAux); // To store the new value for
                                                                                                // bookingNumber
+
                                db.store(offer);
                                db.commit();
-                               bok = offer.getBooking();
+                               book = offer.getBookings();
+
                        }
 
                } catch (com.db4o.ext.ObjectNotStorableException e) {
@@ -337,7 +330,7 @@ public class DB4oManager {
                } finally {
                        db.close();
                }
-               return bok;
+               return book;
        }
 
        /**
@@ -352,7 +345,7 @@ public class DB4oManager {
                        openDB();
 
                try {
-                       Owner proto = new Owner(null, null);
+                       Owner proto = new Owner(null, null, null);
                        ObjectSet<Owner> result = db.queryByExample(proto);
                        Vector<Owner> owners = new Vector<Owner>();
                        while (result.hasNext())
@@ -420,13 +413,13 @@ public class DB4oManager {
 
        /**
         * @param usr
-        * @param pwd
+        * @param ps
         * @return
         * @throws RemoteException
         * @throws Exception
         */
-       public Vector<Account> getAccount(String usr, String pwd)
-                       throws RemoteException, Exception {
+       public Vector<Account> getAccount(Account proto) throws RemoteException,
+                       Exception {
 
                if (c.isDatabaseLocal() == false)
                        openSDB();
@@ -434,7 +427,6 @@ public class DB4oManager {
                        openDB();
 
                try {
-                       Account proto = new Account(usr, pwd, null);
                        ObjectSet<Account> result = db.queryByExample(proto);
                        Vector<Account> accounts = new Vector<Account>();
                        while (result.hasNext())
@@ -459,8 +451,11 @@ public class DB4oManager {
                RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
                                null);
                try {
-                       ObjectSet<Owner> result = db.queryByExample(house);
+                       ObjectSet<RuralHouse> result = db.queryByExample(house);
                        if (result.isEmpty()) {
+                               Owner ow = (Owner) db.queryByExample(rh.getOwner()).get(0);
+                               rh.setOwner(ow);
+                               ow.addRuralHouse(rh);
                                db.store(rh);
                                db.commit();
                                stored = true;
@@ -486,8 +481,9 @@ public class DB4oManager {
                        ObjectSet<Owner> result = db.queryByExample(owner);
                        ObjectSet<RuralHouse> rhs = db.queryByExample(rh);
                        if (!rhs.isEmpty()) {
-                               Owner found =  result.get(0);
+                               Owner found = result.get(0);
                                found.getRuralHouses().remove(rhs.get(0));
+                               db.delete(rhs.get(0));
                                db.store(found);
                                db.commit();
                        }
@@ -499,7 +495,6 @@ public class DB4oManager {
 
        }
 
-
        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);
@@ -522,5 +517,94 @@ public class DB4oManager {
 
        }
 
-       
+       public boolean addAccount(Account acc) {
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       ObjectSet<Account> result = db.queryByExample(new Account(acc
+                                       .getUsername()));
+                       if (result.isEmpty()) {
+                               db.store(acc);
+                               db.commit();
+                               return true;
+                       }
+               } catch (Exception exc) {
+                       exc.printStackTrace();
+               } finally {
+                       db.close();
+               }
+               return false;
+       }
+
+       // TODO remove account
+
+       public boolean removeAccount(Account acc) {
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       ObjectSet<Account> result = db.queryByExample(new Account(acc
+                                       .getUsername()));
+                       if (!result.isEmpty()) {
+                               db.delete(result.get(0));
+                               ;
+                               db.commit();
+                               return true;
+                       }
+               } catch (Exception exc) {
+                       exc.printStackTrace();
+               } finally {
+                       db.close();
+               }
+               return false;
+       }
+
+       public void acceptBooking(Offer of) {
+               Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of
+                               .getRuralHouse().getHouseName(), null, null, null, null), null,
+                               null, 0);
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       ObjectSet<Offer> result = db.queryByExample(off);
+                       db.delete(result.get(0));
+                       RuralHouse rh = result.get(0).getRuralHouse();
+                       of.setRuralHouse(rh);
+                       rh.getAllOffers().remove(result.get(0));
+                       rh.getAllOffers().add(of);
+                       db.store(rh);
+                       db.close();
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       ;
+               }
+       }
+
+       public void removeBooking(Booking b) {
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+               try {
+                       ObjectSet<Booking> result = db.queryByExample(b);
+                       result.get(0).getOffer().getBookings().remove(b);
+                       db.store(result.get(0).getOffer());
+                       db.commit();
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       ;
+               } finally {
+                       db.close();
+               }
+
+       }
 }