cleaning
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 5ccfda4..baab98a 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;
@@ -119,18 +119,18 @@ 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("1", "1", jon);
                        Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
                                        alfredo);
@@ -154,7 +154,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();
@@ -219,8 +219,24 @@ public class DB4oManager {
                }
        }
 
+       public Vector<Offer> getRHsOffer(String name){
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       RuralHouse rh = (RuralHouse)db.queryByExample(new RuralHouse(name, null, null, null, null)).get(0);
+                       Offer proto = new Offer(0, rh, null, null, 0);
+                       ObjectSet<Offer> result = db.queryByExample(proto);
+                       return new Vector<Offer>(result);
+               } finally {
+                       db.close();
+               }
+       }
+       
+       public Administrator getAdminData(){
 
-       public Administrator getAdminData() {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -229,9 +245,6 @@ public class DB4oManager {
                try {
 
                        List<Administrator> admL = db.query(new Predicate<Administrator>() {
-                               /**
-                                * 
-                                */
                                private static final long serialVersionUID = 1L;
 
                                public boolean match(Administrator admin) {
@@ -247,8 +260,8 @@ public class DB4oManager {
 
        }
 
-       @SuppressWarnings("static-access")
        public void storeAdmin() {
+
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -266,10 +279,13 @@ public class DB4oManager {
                                }
                        });
 
-                       
-                       admL.get(0).setAddRequest(Administrator.getInstance().getAddRequest());
-                       admL.get(0).setRemoveRequest(Administrator.getInstance().getRemoveRequest());
-                       admL.get(0).setNewOwnerRequest(Administrator.getInstance().getNewOwnerRequest());
+                       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) {
@@ -287,16 +303,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 {
 
@@ -304,21 +319,22 @@ public class DB4oManager {
                                openSDB();
 
                        RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
-                                       ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
+                                       null, null, null);
                        ObjectSet<RuralHouse> result = db.queryByExample(proto);
                        RuralHouse rh = (RuralHouse) result.next();
 
                        Offer offer;
-                       offer = rh.findOffer(firstDate, lastDate);
+                       offer = (Offer)db.queryByExample(new Offer(0, rh, firstDate, lastDate, 0)).get(0);
 
                        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) {
@@ -329,7 +345,7 @@ public class DB4oManager {
                } finally {
                        db.close();
                }
-               return bok;
+               return book;
        }
 
        /**
@@ -344,7 +360,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())
@@ -355,26 +371,6 @@ public class DB4oManager {
                }
        }
 
-       public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
-                       Exception {
-
-               if (c.isDatabaseLocal() == false)
-                       openSDB();
-               else
-                       openDB();
-
-               try {
-                       RuralHouse proto = new RuralHouse(null, null, null, null, null);
-                       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();
-               }
-       }
-
        public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
                        Date lastDay) throws RemoteException, OverlappingOfferExists {
 
@@ -397,10 +393,6 @@ public class DB4oManager {
                }
        }
 
-       public static ObjectContainer getContainer() {
-               return db;
-       }
-
        public void close() {
                db.close();
                System.out.println("DataBase closed");
@@ -417,8 +409,8 @@ public class DB4oManager {
         * @throws RemoteException
         * @throws Exception
         */
-       public Vector<Account> getAccount(Account proto)
-                       throws RemoteException, Exception {
+       public Vector<Account> getAccount(Account proto) throws RemoteException,
+                       Exception {
 
                if (c.isDatabaseLocal() == false)
                        openSDB();
@@ -494,10 +486,10 @@ public class DB4oManager {
 
        }
 
-       public Vector<RuralHouse> getRuralHouses(String name, String town,
+       public Vector<RuralHouse> getRuralHouses(Owner ow, 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);
+               RuralHouse rh = new RuralHouse(name, ow, null, town, fea);
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -516,4 +508,91 @@ 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;
+       }
+
+       // TODO this method should be improved.
+       public void acceptBooking(Offer of) {
+               Offer off = new Offer(of.getOfferNumber(), of.getRuralHouse(),
+                               of.getFirstDay(), of.getLastDay(), of.getPrice());
+               if (c.isDatabaseLocal() == false)
+                       openSDB();
+               else
+                       openDB();
+
+               try {
+                       ObjectSet<Offer> result = db.queryByExample(off);
+                       this.deleteOffer(result.get(0));
+                       db.store(of);
+                       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);
+                       ObjectSet<Client> result2 = db.queryByExample(b.getClient());
+                       db.delete(result.get(0));
+                       db.delete(result2.get(0));
+                       db.commit();
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       ;
+               } finally {
+                       db.close();
+               }
+
+       }
 }