Account adding and deleting fixed
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 096a17b..6089a74 100644 (file)
@@ -135,7 +135,6 @@ public class DB4oManager {
                        Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
                                        alfredo);
 
-
                        Account admin = new Account("admin", "admin", true);
                        db.store(Administrator.getInstance());
                        db.store(jonAcc);
@@ -220,9 +219,7 @@ public class DB4oManager {
                }
        }
 
-
-       
-       public Administrator getAdminData(){
+       public Administrator getAdminData() {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -230,8 +227,7 @@ public class DB4oManager {
 
                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) {
@@ -239,11 +235,8 @@ public class DB4oManager {
                                }
                        });
 
-                       
-                       
                        return admL.get(0);
-               }
-               finally {
+               } finally {
                        db.close();
 
                }
@@ -301,7 +294,7 @@ public class DB4oManager {
                else
                        openDB();
 
-               Vector<Booking> book = new Vector<Booking>() ;
+               Vector<Booking> book = new Vector<Booking>();
 
                try {
 
@@ -320,11 +313,11 @@ public class DB4oManager {
                                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) {
@@ -529,27 +522,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
@@ -560,29 +576,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();
                }
 
-               
        }
 }