Merge conflicts solutioned
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
index 61dd24b..b0513ae 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;
@@ -135,8 +135,10 @@ public class DB4oManager {
                        Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
                                        alfredo);
 
+
                        Account admin = new Account("admin", "admin", true);
                        db.store(Administrator.getInstance());
+
                        db.store(jonAcc);
                        db.store(alfredoAcc);
                        db.store(admin);
@@ -219,7 +221,9 @@ public class DB4oManager {
                }
        }
 
-       public Administrator getAdminData() {
+
+       
+       public Administrator getAdminData(){
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -227,10 +231,8 @@ 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) {
@@ -238,16 +240,19 @@ public class DB4oManager {
                                }
                        });
 
+                       
+                       
                        return admL.get(0);
-               } finally {
+               }
+               finally {
                        db.close();
 
                }
 
        }
 
-       @SuppressWarnings("static-access")
        public void storeAdmin() {
+
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
@@ -271,6 +276,7 @@ public class DB4oManager {
                                        Administrator.getInstance().getRemoveRequest());
                        admL.get(0).setNewOwnerRequest(
                                        Administrator.getInstance().getNewOwnerRequest());
+
                        db.commit();
 
                } catch (Exception e) {
@@ -288,16 +294,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 {
 
@@ -313,13 +318,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) {
@@ -330,7 +336,7 @@ public class DB4oManager {
                } finally {
                        db.close();
                }
-               return bok;
+               return book;
        }
 
        /**
@@ -524,9 +530,10 @@ 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();
+                       // TODO realize if there is another account with same username.
+                       // Quite difficult with the f***ing salt
+                       db.store(acc);
+                       db.commit();
                } catch (Exception exc) {
                        exc.printStackTrace();
                } finally {
@@ -534,11 +541,49 @@ public class DB4oManager {
                }
                return true;
        }
-       
-       //TODO remove account
+
+       // TODO remove account
 
        public boolean removeAccount(Account acc) {
                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();
+               }
 
+               
+       }
 }