Booking deletion fixed some minor problems remain
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index d8a59f2..a677c95 100644 (file)
@@ -81,10 +81,12 @@ public class DB4oManager {
                configuration = Db4oEmbedded.newConfiguration();
                configuration.common().activationDepth(c.getActivationDepth());
                configuration.common().updateDepth(c.getUpdateDepth());
-               configuration.common().objectClass(Owner.class).cascadeOnUpdate(true);
                configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
+               configuration.common().objectClass(Booking.class).cascadeOnDelete(true);
                configuration.common().objectClass(RuralHouse.class)
-                               .cascadeOnUpdate(true);
+                               .cascadeOnDelete(true);
+               configuration.common().objectClass(Account.class).cascadeOnDelete(true);
+               configuration.common().objectClass(Offer.class).cascadeOnDelete(true);
                configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
                db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
        }
@@ -207,7 +209,7 @@ 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)));
+                       System.out.println(rh.getAllOffers().remove(of.get(0)));
                        db.store(rh);
                        db.commit();
 
@@ -219,14 +221,15 @@ public class DB4oManager {
                }
        }
 
-       public Vector<Offer> getRHsOffer(String name){
+       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);
+                       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);
@@ -234,8 +237,8 @@ public class DB4oManager {
                        db.close();
                }
        }
-       
-       public Administrator getAdminData(){
+
+       public Administrator getAdminData() {
 
                if (c.isDatabaseLocal() == false)
                        openSDB();
@@ -371,7 +374,6 @@ public class DB4oManager {
                }
        }
 
-       
        public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
                        Exception {
 
@@ -508,8 +510,8 @@ public class DB4oManager {
 
        }
 
-       public Vector<RuralHouse> getRuralHouses(Owner ow, String name, String town,
-                       int nBed, int nKit, int nBath, int nPark, int nLiv) {
+       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, ow, null, town, fea);
                if (c.isDatabaseLocal() == false)
@@ -552,20 +554,16 @@ public class DB4oManager {
                return false;
        }
 
-       // TODO remove account
-
-       public boolean removeAccount(Account acc) {
+       public boolean removeAccount(Owner own) {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        openDB();
 
                try {
-                       ObjectSet<Account> result = db.queryByExample(new Account(acc
-                                       .getUsername()));
+                       ObjectSet<Account> result = db.queryByExample(new Account(own));
                        if (!result.isEmpty()) {
                                db.delete(result.get(0));
-                               ;
                                db.commit();
                                return true;
                        }
@@ -577,10 +575,10 @@ public class DB4oManager {
                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());
+               Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of
+                               .getRuralHouse().getHouseName(), null, null, null, null), null,
+                               null, 0);
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -588,8 +586,12 @@ public class DB4oManager {
 
                try {
                        ObjectSet<Offer> result = db.queryByExample(off);
-                       this.deleteOffer(result.get(0));
-                       db.store(of);
+                       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) {
@@ -604,14 +606,18 @@ public class DB4oManager {
                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));
+                       Booking book = new Booking(b.getBookNumber(), new Offer(b
+                                       .getOffer().getOfferNumber(), new RuralHouse(b.getOffer()
+                                       .getRuralHouse().getHouseName(), null, null, null, null),
+                                       null, null, 0), b.getClient(), b.getBookDate());
+
+                       ObjectSet<Booking> result = db.queryByExample(book);
+                       Offer of = result.get(0).getOffer();
+                       of.getBookings().remove(result.get(0));
+                       db.store(of);
                        db.commit();
                } catch (Exception e) {
                        e.printStackTrace();
-                       ;
                } finally {
                        db.close();
                }