package dataAccess;
import java.io.File;
+//import java.util.Enumeration;
+//import java.util.Vector;
import java.rmi.RemoteException;
import java.util.Date;
+import java.util.List;
+import java.util.ListIterator;
import java.util.Vector;
-//import java.util.Enumeration;
-//import java.util.Vector;
-import businessLogic.BookingManager;
+import com.db4o.Db4oEmbedded;
+import com.db4o.ObjectContainer;
+import com.db4o.ObjectSet;
+import com.db4o.config.EmbeddedConfiguration;
+import com.db4o.cs.Db4oClientServer;
+import com.db4o.cs.config.ClientConfiguration;
+import com.db4o.query.Predicate;
-import com.db4o.*;
-
-import configuration.Config;
-import domain.*;
+import configuration.ConfigXML;
+import domain.Account;
+import domain.Administrator;
+import domain.Booking;
+import domain.Client;
+import domain.HouseFeatures;
+import domain.Offer;
+//import dataModel.Offer;
+import domain.Owner;
+import domain.RuralHouse;
import exceptions.OfferCanNotBeBooked;
+import exceptions.OverlappingOfferExists;
public class DB4oManager {
private static ObjectContainer db;
- private static DB4oManager theDB4oManager = new DB4oManager();
+ private static EmbeddedConfiguration configuration;
+ private static ClientConfiguration configurationCS;
+ private int bookingNumber = 0; // if it is "static" then it is not
+ // serialized
+ private int offerNumber = 0; // if it is "static" then it is not serialized
+ private static DB4oManager theDB4oManager = null;
- /**
- *
- */
- private DB4oManager() {
- Config c = Config.getInstance();
- String dataBaseOpenMode = c.getDataBaseOpenMode();
- DB4oManager.openDatabase(dataBaseOpenMode);
+ private static DB4oManagerAux theDB4oManagerAux;
+
+ static ConfigXML c;
+
+ private DB4oManager() throws Exception {
+ theDB4oManagerAux = new DB4oManagerAux(0, 0);
+ c = ConfigXML.getInstance();
+ System.out.println("Creating DB4oManager instance => isDatabaseLocal: "
+ + c.isDatabaseLocal() + " getDatabBaseOpenMode: "
+ + c.getDataBaseOpenMode());
+
+ if ((c.getDataBaseOpenMode().equals("initialize"))
+ && (c.isDatabaseLocal()))
+ new File(c.getDb4oFilename()).delete();
+
+ if (c.isDatabaseLocal()) {
+ openDB();
+ System.out.println("DataBase opened");
+ } else // c.isDatabaseLocal==false
+ {
+ openSDB();
+ System.out.println("Remote DataBase opened");
+ }
+ if (c.getDataBaseOpenMode().equals("initialize")) {
+ initializeDB();
+ System.out.println("DataBase initialized");
+ } else // c.getDataBaseOpenMode().equals("open")
+
+ {
+ ObjectSet<DB4oManagerAux> res = db
+ .queryByExample(DB4oManagerAux.class);
+ ListIterator<DB4oManagerAux> listIter = res.listIterator();
+ if (listIter.hasNext())
+ theDB4oManagerAux = (DB4oManagerAux) res.next();
+ }
}
- /**
- * @param mode
- */
- public static void openDatabase(String mode) {
- Config c = Config.getInstance();
- String db4oFileName = c.getDb4oFilename();
- if (mode.compareTo("open") == 0) {
- db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
- db.ext().configure().updateDepth(5);
-
- } else if (mode.compareTo("initialize") == 0) {
- try {
- new File(db4oFileName).delete();
- db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
- db.ext().configure().updateDepth(5);
- Owner jon = new Owner("Jon");
- Owner alfredo = new Owner("Alfredo");
- jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
- jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
- jon.setBankAccount("1349 5677 21 2133567777");
- alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5,
- 5, 5, 5);
- alfredo.addRuralHouse(4, "Murgia", 6, 6, 6, 6, 6);
- alfredo.setBankAccount("4144 0087 23 9700002133");
- Account jonAcc = new Account("userJon", "passJon", jon);
- Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
- alfredo);
- db.store(jon);
- db.store(alfredo);
- db.store(jonAcc);
- db.store(alfredoAcc);
- db.commit();
- System.out.println("DataBase Initialized");
- } finally {
- db.close();
+ private static void openDB() {
+ configuration = Db4oEmbedded.newConfiguration();
+ configuration.common().activationDepth(c.getActivationDepth());
+ configuration.common().updateDepth(c.getUpdateDepth());
+ configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
+ configuration.common().objectClass(Booking.class).cascadeOnDelete(true);
+ configuration.common().objectClass(RuralHouse.class)
+ .cascadeOnDelete(true);
+ configuration.common().objectClass(Account.class).cascadeOnDelete(true);
+ configuration.common().objectClass(Offer.class).cascadeOnDelete(true);
+ configuration.common().objectClass(Owner.class).cascadeOnUpdate(true);
+ configuration.common().objectClass(Booking.class).cascadeOnUpdate(true);
+ configuration.common().objectClass(RuralHouse.class)
+ .cascadeOnUpdate(true);
+ configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
+ configuration.common().objectClass(Offer.class).cascadeOnUpdate(true);
+ configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
+ db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
+ }
+
+ private void openSDB() {
+
+ configurationCS = Db4oClientServer.newClientConfiguration();
+ configurationCS.common().activationDepth(c.getActivationDepth());
+ configurationCS.common().updateDepth(c.getUpdateDepth());
+ configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
+ configurationCS.common().objectClass(Booking.class)
+ .cascadeOnDelete(true);
+ configurationCS.common().objectClass(RuralHouse.class)
+ .cascadeOnDelete(true);
+ configurationCS.common().objectClass(Account.class)
+ .cascadeOnDelete(true);
+ configurationCS.common().objectClass(Offer.class).cascadeOnDelete(true);
+ configurationCS.common().objectClass(Owner.class).cascadeOnUpdate(true);
+ configurationCS.common().objectClass(Booking.class)
+ .cascadeOnUpdate(true);
+ configurationCS.common().objectClass(RuralHouse.class)
+ .cascadeOnUpdate(true);
+ configurationCS.common().objectClass(Account.class)
+ .cascadeOnUpdate(true);
+ configurationCS.common().objectClass(Offer.class).cascadeOnUpdate(true);
+ configurationCS.common().objectClass(Account.class)
+ .cascadeOnUpdate(true);
+ db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
+ c.getDatabasePort(), c.getUser(), c.getPassword());
+
+ }
+
+ class DB4oManagerAux {
+ int bookingNumber;
+ int offerNumber;
+
+ DB4oManagerAux(int bookingNumber, int offerNumber) {
+ this.bookingNumber = bookingNumber;
+ this.offerNumber = offerNumber;
+ }
+ }
+
+ public static DB4oManager getInstance() throws Exception {
+ if (theDB4oManager == null)
+ theDB4oManager = new DB4oManager();
+ return theDB4oManager;
+ }
+
+ public void initializeDB() {
+
+ try {
+ 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);
+ alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, 5,
+ 5, 5, 5);
+
+ alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6);
+ Account jonAcc = new Account("1", "1", jon);
+ 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);
+ db.commit();
+ } finally {
+ db.close();
+ }
+ }
+
+ public void deleteDB() {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+ Owner proto = new Owner(null, null, null);
+ ObjectSet<Owner> result = db.queryByExample(proto);
+ while (result.hasNext()) {
+ Owner o = (Owner) result.next();
+ System.out.println("Deleted owner: " + o.toString());
+ db.delete(o);
}
+ db.commit();
+ } finally {
+ db.close();
+ }
+ }
+
+ public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
+ Date lastDay, float price) throws RemoteException, Exception {
+ Offer o = null;
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+
+ RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+ null, null, null);
+ ObjectSet<RuralHouse> result = db.queryByExample(proto);
+ RuralHouse rh = (RuralHouse) result.next();
+ o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
+ lastDay, price);
+ db.store(theDB4oManagerAux); // To store the new value for
+ // offerNumber
+ db.store(o);
+ db.commit();
+ } catch (com.db4o.ext.ObjectNotStorableException e) {
+ System.out
+ .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
+ } finally {
+ db.close();
}
+ return o;
}
- /**
- * @return
- */
- public static ObjectContainer getContainer() {
- return db;
+ public void deleteOffer(Offer offer) throws RemoteException, Exception {
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+ ObjectSet<Offer> of = db.queryByExample(offer);
+ RuralHouse rh = of.get(0).getRuralHouse();
+ System.out.println(rh.getAllOffers().remove(of.get(0)));
+ db.store(rh);
+ db.commit();
+
+ } catch (com.db4o.ext.ObjectNotStorableException e) {
+ System.out
+ .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
+ } finally {
+ db.close();
+ }
+ }
+
+ 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
+ openDB();
+
+ try {
+
+ List<Administrator> admL = db.query(new Predicate<Administrator>() {
+ private static final long serialVersionUID = 1L;
+
+ public boolean match(Administrator admin) {
+ return true;
+ }
+ });
+
+ return admL.get(0);
+ } finally {
+ db.close();
+
+ }
+
+ }
+
+ public void storeAdmin() {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+ try {
+
+ List<Administrator> admL = db.query(new Predicate<Administrator>() {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public boolean match(Administrator admin) {
+ return true;
+ }
+ });
+
+ 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) {
+
+ } finally {
+ db.close();
+ }
+
}
/**
- * @param rh
+ * This method creates a book with a corresponding parameters
+ *
+ * @param First
+ * day, last day, house number and telephone
+ * @return a book
*/
- public void storeRuralHouses(RuralHouse rh) {
- DB4oManager.openDatabase("open");
- ObjectContainer db = DB4oManager.getContainer();
+ public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate,
+ Date lastDate, Client cl) throws OfferCanNotBeBooked {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ Vector<Booking> book = new Vector<Booking>();
+
try {
- db.store(rh);
- db.commit();
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+
+ RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+ null, null, null);
+ ObjectSet<RuralHouse> result = db.queryByExample(proto);
+ RuralHouse rh = (RuralHouse) result.next();
+
+ Offer offer;
+ 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) {
+ System.out
+ .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
+ } catch (Exception exc) {
+ exc.printStackTrace();
} finally {
db.close();
}
+ return book;
}
/**
- * This method finds all existing owners
+ * This method existing owners
*
*/
public Vector<Owner> getOwners() throws RemoteException, Exception {
- DB4oManager.openDatabase("open");
- ObjectContainer db = DB4oManager.getContainer();
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ 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())
}
}
+ public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
+ Date lastDay) throws RemoteException, OverlappingOfferExists {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+
+ RuralHouse rhn = (RuralHouse) db.queryByExample(
+ new RuralHouse(rh.getHouseName(), null, null, null, null))
+ .next();
+ if (rhn.overlapsWith(firstDay, lastDay) != null)
+ throw new OverlappingOfferExists();
+ else
+ return false;
+ } finally {
+ db.close();
+ }
+ }
+
+ public void close() {
+ db.close();
+ System.out.println("DataBase closed");
+ }
+
+ public String toString() {
+ return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
+ }
+
/**
* @param usr
- * @param pwd
+ * @param ps
* @return
* @throws RemoteException
* @throws Exception
*/
- public Vector<Account> getAccount(String usr, String pwd)
- throws RemoteException, Exception {
- DB4oManager.openDatabase("open");
- ObjectContainer db = DB4oManager.getContainer();
+ public Vector<Account> getAccount(Account proto) throws RemoteException,
+ Exception {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
try {
- Account proto = new Account(usr, pwd, new Owner(null, null));
ObjectSet<Account> result = db.queryByExample(proto);
Vector<Account> accounts = new Vector<Account>();
while (result.hasNext())
}
/**
- * @return
- * @throws RemoteException
- * @throws Exception
+ * @param rh
*/
- public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
- Exception {
- DB4oManager.openDatabase("open");
- ObjectContainer db = DB4oManager.getContainer();
+ public boolean storeRuralHouses(RuralHouse rh) {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ boolean stored = false;
+ RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
+ null);
try {
- RuralHouse proto = new RuralHouse(0, null, null, null, 0, 0, 0, 0,
- 0);
- ObjectSet<RuralHouse> result = db.queryByExample(proto);
- Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
- while (result.hasNext())
- ruralHouses.add((RuralHouse) result.next());
- return ruralHouses;
+ ObjectSet<RuralHouse> result = db.queryByExample(house);
+ if (result.isEmpty()) {
+ Owner ow = (Owner) db.queryByExample(rh.getOwner()).get(0);
+ rh.setOwner(ow);
+ ow.addRuralHouse(rh);
+ db.store(rh);
+ db.commit();
+ stored = true;
+ } else {
+ db.delete(result.get(0));
+ db.store(rh);
+ db.commit();
+ stored = true;
+ }
} finally {
db.close();
}
+ return stored;
}
- /**
- * This method creates an offer with a house number, first day, last day and
- * price precondition: There are no overlapping offers
- *
- * @param House
- * number, start day, last day and price
- * @return None
- */
- public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
- Date lastDay, float price) throws RemoteException, Exception {
- DB4oManager.openDatabase("open");
- ObjectContainer db = DB4oManager.getContainer();
- Offer o;
+ public void removeHouse(RuralHouse rh, Owner owner) {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
try {
- RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
- null, ruralHouse.getDescription(), ruralHouse.getTown(), 0,
- 0, 0, 0, 0);
- ObjectSet<RuralHouse> result = db.queryByExample(proto);
- RuralHouse rh = (RuralHouse) result.next();
- o = rh.createOffer(firstDay, lastDay, price);
- db.store(o);
- db.commit();
+ ObjectSet<Owner> result = db.queryByExample(owner);
+ ObjectSet<RuralHouse> rhs = db.queryByExample(rh);
+ if (!rhs.isEmpty()) {
+ Owner found = result.get(0);
+ found.getRuralHouses().remove(rhs.get(0));
+ db.delete(rhs.get(0));
+ db.store(found);
+ db.commit();
+ }
+ } catch (Exception exc) {
+ exc.printStackTrace();
} finally {
db.close();
}
- return o;
+
}
- /**
- * @param rh
- * @return
- */
- public RuralHouse getRuralHouse(RuralHouse rh) {
+ 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)
+ openSDB();
+ else
+ openDB();
- DB4oManager.openDatabase("open");
try {
- ObjectContainer db = DB4oManager.getContainer();
- RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
- rh.getDescription(), rh.getTown(), 0, 0, 0, 0, 0);
- ObjectSet<RuralHouse> result = db.queryByExample(proto);
- return rh = (RuralHouse) result.next();
+ ObjectSet<RuralHouse> result = db.queryByExample(rh);
+ Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
+ while (result.hasNext())
+ ruralHouses.add(result.next());
+ db.close();
+ return ruralHouses;
+ } catch (NullPointerException e) {
+ return null;
+ }
+
+ }
+
+ 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();
- return null;
} finally {
db.close();
}
+ return false;
}
- /**
- * @param offer
- * @param clientTelephoneNumber
- * @return
- * @throws OfferCanNotBeBooked
- */
- public Booking createBooking(Offer offer, String clientTelephoneNumber)
- throws OfferCanNotBeBooked {
- DB4oManager.openDatabase("open");
+ public boolean removeAccount(Owner own) {
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
try {
- Booking b = null;
- if (offer != null) {
- b = offer.createBook(clientTelephoneNumber);
- db.store(b);
- db.store(offer);
+ ObjectSet<Account> result = db.queryByExample(new Account(own));
+ if (!result.isEmpty()) {
+ db.delete(result.get(0));
db.commit();
+ return true;
}
- return b;
} catch (Exception exc) {
exc.printStackTrace();
- return null;
} finally {
db.close();
}
+ return false;
}
- /**
- * This method returns the instance of the DB4oManager class
- *
- * @return the db4o manager
- */
- public static DB4oManager getInstance() {
- return theDB4oManager;
+ public void acceptBooking(Offer of) {
+ Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of
+ .getRuralHouse().getHouseName(), null, null, null, null), null,
+ null, 0);
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+ ObjectSet<Offer> result = db.queryByExample(off);
+ 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) {
+ e.printStackTrace();
+ ;
+ }
}
+ public void removeBooking(Booking b) {
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+ try {
+ 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());
+ book.setOffer(null);
+ db.delete(book);
+ db.commit();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ db.close();
+ }
+
+ }
+
+ public Vector<Booking> getOfBok(Offer o) {
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+ Offer of = (Offer) db.queryByExample(
+ new Offer(0, o.getRuralHouse(), null, null, 0)).get(0);
+ Booking proto = new Booking(0, of, null, null);
+ ObjectSet<Booking> result = db.queryByExample(proto);
+ return new Vector<Booking>(result);
+ } finally {
+ db.close();
+ }
+ }
}