package dataAccess;
import java.io.File;
-import java.rmi.RemoteException;
-import java.util.Date;
-import java.util.Vector;
//import java.util.Enumeration;
//import java.util.Vector;
-import businessLogic.BookingManager;
+import java.rmi.RemoteException;
+import java.sql.Date;
+import java.util.HashSet;
+import java.util.ListIterator;
+import java.util.Vector;
import com.db4o.*;
+import com.db4o.config.EmbeddedConfiguration;
+import com.db4o.cs.Db4oClientServer;
+import com.db4o.cs.config.ClientConfiguration;
-import configuration.Config;
-
+import configuration.ConfigXML;
+import domain.Account;
import domain.Booking;
import domain.Offer;
+//import dataModel.Offer;
import domain.Owner;
import domain.RuralHouse;
import exceptions.OfferCanNotBeBooked;
+import exceptions.OverlappingOfferExists;
-public class DB4oManager {
+public class DB4oManager {
private static ObjectContainer db;
- private static DB4oManager theDB4oManager=new DB4oManager();
-
- private DB4oManager() {
- Config c=Config.getInstance();
- String dataBaseOpenMode=c.getDataBaseOpenMode();
- DB4oManager.openDatabase(dataBaseOpenMode);
- }
-
- 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);
- System.out.println("DataBase Opened");
-
- } else if (mode.compareTo("initialize")==0){
- new File(db4oFileName).delete();
- db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
- db.ext().configure().updateDepth(5);
- Owner jon = new Owner("Jon", "userJon", "passJon");
- Owner alfredo = new Owner("Alfredo","userAlfredo", "passAlfredo");
- jon.addRuralHouse(1, "Ezkioko etxea","Ezkio");
- jon.addRuralHouse(2, "Eskiatzeko etxea","Jaca");
+ 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 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 res = db.queryByExample(DB4oManagerAux.class);
+ ListIterator listIter = res.listIterator();
+ if (listIter.hasNext())
+ theDB4oManagerAux = (DB4oManagerAux) res.next();
+ }
+ }
+
+ private static void openDB() {
+ configuration = Db4oEmbedded.newConfiguration();
+ configuration.common().activationDepth(c.getActivationDepth());
+ configuration.common().updateDepth(c.getUpdateDepth());
+ configuration.common().objectClass(Owner.class).cascadeOnDelete(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);
+ 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");
+ Owner alfredo = new Owner("Alfredo");
+ jon.addRuralHouse("Ezkio", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
+ jon.addRuralHouse("Eskia", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
jon.setBankAccount("1349 5677 21 2133567777");
- alfredo.addRuralHouse(3, "Casa del abuelo","Pitillas");
- alfredo.addRuralHouse(4, "Refugio","Murgia");
+ alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
+ 5);
+ alfredo.addRuralHouse("Murgoitz", "", "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();
}
}
- public static ObjectContainer getContainer() {
- return db;
+ @SuppressWarnings("finally")
+ 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 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;
}
- public static void close(){
- db.close();
- System.out.println("DataBase closed");
+
+ public void deleteDB() {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try {
+ Owner proto = new Owner(null, null);
+ ObjectSet result = db.queryByExample(proto);
+ Vector<Owner> owners = new Vector<Owner>();
+ while (result.hasNext()) {
+ Owner o = (Owner) result.next();
+ System.out.println("Deleted owner: " + o.toString());
+ db.delete(o);
+ }
+ db.commit();
+ } finally {
+ db.close();
+ }
}
-
+
/**
- * This method finds all existing owners
+ * This method creates a book with a corresponding parameters
*
+ * @param First
+ * day, last day, house number and telephone
+ * @return a book
*/
- public Vector<Owner> getOwners() throws RemoteException,
- Exception {
- ObjectContainer db=DB4oManager.getContainer();
+ public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
+ Date lastDate, String bookTelephoneNumber)
+ throws OfferCanNotBeBooked {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ Booking bok = null;
try {
- Owner proto = new Owner(null,null,null,null);
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+
+ RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
+ null, ruralHouse.getDescription(), ruralHouse.getTown(),
+ null);
ObjectSet result = db.queryByExample(proto);
- Vector<Owner> owners=new Vector<Owner>();
- while(result.hasNext())
- owners.add((Owner)result.next());
- return owners;
+ RuralHouse rh = (RuralHouse) result.next();
+
+ Offer offer;
+ offer = rh.findOffer(firstDate, lastDate);
+
+ if (offer != null) {
+ offer.createBooking(theDB4oManagerAux.bookingNumber++,
+ bookTelephoneNumber);
+ db.store(theDB4oManagerAux); // To store the new value for
+ // bookingNumber
+ db.store(offer);
+ db.commit();
+ bok = offer.getBooking();
+ }
+
+ } catch (com.db4o.ext.ObjectNotStorableException e) {
+ System.out
+ .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
+ } catch (Exception exc) {
+ exc.printStackTrace();
} finally {
- //db.close();
+ db.close();
}
- }
-
- public Vector<Owner> getSingleOwner(String usr, String pwd) throws RemoteException,
- Exception {
- ObjectContainer db=DB4oManager.getContainer();
+ return bok;
+ }
+
+ /**
+ * This method existing owners
+ *
+ */
+ public Vector<Owner> getOwners() throws RemoteException, Exception {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
try {
- Owner proto = new Owner(null,usr,pwd,null);
+ Owner proto = new Owner(null, null);
ObjectSet result = db.queryByExample(proto);
- Vector<Owner> owners=new Vector<Owner>();
- while(result.hasNext())
- owners.add((Owner)result.next());
+ Vector<Owner> owners = new Vector<Owner>();
+ while (result.hasNext())
+ owners.add((Owner) result.next());
return owners;
} finally {
- //db.close();
+ db.close();
}
}
-
+
public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
- Exception {
- ObjectContainer db=DB4oManager.getContainer();
+ Exception {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
try {
- RuralHouse proto = new RuralHouse(0,null,null,null);
+ RuralHouse proto = new RuralHouse(null, null, null, null, null);
ObjectSet result = db.queryByExample(proto);
- Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
- while(result.hasNext())
- ruralHouses.add((RuralHouse)result.next());
+ Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
+ while (result.hasNext())
+ ruralHouses.add((RuralHouse) result.next());
return ruralHouses;
} finally {
- //db.close();
+ db.close();
+ }
+ }
+
+ 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 static ObjectContainer getContainer() {
+ return db;
+ }
+
+ public void close() {
+ db.close();
+ System.out.println("DataBase closed");
+ }
+
+ public String toString() {
+ return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
+ }
+
/**
- * 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
+ * @param usr
+ * @param pwd
+ * @return
+ * @throws RemoteException
+ * @throws Exception
*/
- public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
- float price) throws RemoteException, Exception {
- ObjectContainer db=DB4oManager.getContainer();
- RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null,
- ruralHouse.getDescription(), ruralHouse.getTown());
- ObjectSet result = db.queryByExample(proto);
- RuralHouse rh=(RuralHouse)result.next();
- Offer o=rh.createOffer(firstDay, lastDay, price);
- db.store(o);
- db.commit();
- return o;
+ public Vector<Account> getAccount(String usr, String pwd)
+ 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())
+ accounts.add((Account) result.next());
+ return accounts;
+ } finally {
+ db.close();
+ }
}
- public RuralHouse getRuralHouse(RuralHouse rh){
+ /**
+ * @param rh
+ */
+ 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 {
- ObjectContainer db=DB4oManager.getContainer();
- RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
- rh.getDescription(), rh.getTown());
- ObjectSet result = db.queryByExample(proto);
- return rh=(RuralHouse)result.next();
- } catch (Exception exc) {
- exc.printStackTrace();
- return null;
+ ObjectSet<Owner> result = db.queryByExample(house);
+ if (result.isEmpty()) {
+ db.store(rh);
+ db.commit();
+ stored = true;
+ }
+ } finally {
+ db.close();
}
+ return stored;
}
- public Booking createBooking(Offer offer, String clientTelephoneNumber)
- throws OfferCanNotBeBooked {
+ public void removeHouse(String houseName, Owner owner) {
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+ RuralHouse house = new RuralHouse(houseName, null, null, null, null);
try {
- Booking b=null;
- if (offer!=null) {
- b=offer.createBook(clientTelephoneNumber);
- db.store(b);
- db.store(offer);
+ ObjectSet<RuralHouse> result = db.queryByExample(house);
+ if (!result.isEmpty()) {
+ RuralHouse found = (RuralHouse) result.get(0);
+// db.delete(found.getOwner());
+ db.store(owner);
+ db.delete(found);
db.commit();
}
- return b;
} catch (Exception exc) {
exc.printStackTrace();
- return null;
+ } finally {
+ db.close();
}
- }
- /**
- * This method returns the instance of the DB4oManager class
- *
- * @return the db4o manager
- */
- public static DB4oManager getInstance() {
- return theDB4oManager;
}
+
+ public Vector<RuralHouse> getRuralHousesByTown(String town){
+ RuralHouse rh = new RuralHouse(null,null,null,town,null);
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
-}
+ try {
+ ObjectSet<RuralHouse> result = db.queryByExample(rh);
+ Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
+ while (result.hasNext())
+ ruralHouses.add(result.next());
+ return ruralHouses;
+ } finally {
+ db.close();
+ }
+ }
+ public RuralHouse getRuralHouseByName(String name){
+ RuralHouse rh = new RuralHouse(name,null,null,null,null);
+
+ if (c.isDatabaseLocal() == false)
+ openSDB();
+ else
+ openDB();
+
+ try{
+ ObjectSet<RuralHouse> result = db.queryByExample(rh);
+ Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
+ while(result.hasNext())
+ ruralHouses.add(result.next());
+ db.close();
+ return ruralHouses.get(0);
+ } catch (NullPointerException e){
+ return null;
+ }
+
+
+
+ }
+}