cleaning
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index a541e95..baab98a 100644 (file)
@@ -219,7 +219,24 @@ public class DB4oManager {
                }
        }
 
-       public Administrator getAdminData() {
+       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(){
+
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -228,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,6 +261,7 @@ public class DB4oManager {
        }
 
        public void storeAdmin() {
+
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -270,6 +285,7 @@ public class DB4oManager {
                                        Administrator.getInstance().getRemoveRequest());
                        admL.get(0).setNewOwnerRequest(
                                        Administrator.getInstance().getNewOwnerRequest());
+
                        db.commit();
 
                } catch (Exception e) {
@@ -295,7 +311,7 @@ public class DB4oManager {
                else
                        openDB();
 
-               Vector<Booking> book = new Vector<Booking>() ;
+               Vector<Booking> book = new Vector<Booking>();
 
                try {
 
@@ -303,22 +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++, cl);
                                db.store(theDB4oManagerAux); // To store the new value for
                                                                                                // bookingNumber
-                               
+
                                db.store(offer);
                                db.commit();
                                book = offer.getBookings();
-                               
+
                        }
 
                } catch (com.db4o.ext.ObjectNotStorableException e) {
@@ -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");
@@ -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
@@ -523,27 +515,50 @@ public class DB4oManager {
                        openDB();
 
                try {
-                       // TODO realize if there is another account with same username.
-                       // Quite difficult with the f***ing salt
-                       db.store(acc);
-                       db.commit();
+                       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 true;
+               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());
+
+       // 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
@@ -554,29 +569,30 @@ public class DB4oManager {
                        this.deleteOffer(result.get(0));
                        db.store(of);
                        db.close();
-                       
+
                } catch (Exception e) {
-                       e.printStackTrace();;
-               }               
+                       e.printStackTrace();
+                       ;
+               }
        }
-       
-       public void removeBooking(Booking b){
+
+       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());
+                       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{
+                       e.printStackTrace();
+                       ;
+               } finally {
                        db.close();
                }
 
-               
        }
 }