import java.io.File;
//import java.util.Enumeration;
//import java.util.Vector;
-
import java.rmi.RemoteException;
-import java.sql.Date;
-import java.util.HashSet;
+import java.util.Date;
+import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
-import com.db4o.*;
+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 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;
private static DB4oManager theDB4oManager = null;
private static DB4oManagerAux theDB4oManagerAux;
+
static ConfigXML c;
private DB4oManager() throws Exception {
} else // c.getDataBaseOpenMode().equals("open")
{
- ObjectSet res = db.queryByExample(DB4oManagerAux.class);
- ListIterator listIter = res.listIterator();
+ ObjectSet<DB4oManagerAux> res = db
+ .queryByExample(DB4oManagerAux.class);
+ ListIterator<DB4oManagerAux> listIter = res.listIterator();
if (listIter.hasNext())
theDB4oManagerAux = (DB4oManagerAux) res.next();
}
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(RuralHouse.class)
+ .cascadeOnUpdate(true);
+ configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
}
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("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);
+ 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);
- db.store(jon);
- db.store(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();
}
}
- @SuppressWarnings("finally")
public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
Date lastDay, float price) throws RemoteException, Exception {
Offer o = null;
try {
- RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
- null, null, null, null);
- ObjectSet result = db.queryByExample(proto);
+ 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);
return o;
}
- public void deleteDB() {
+ 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.offers.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 Administrator getAdminData(){
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);
- }
+
+
+ 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();
}
+
}
/**
* 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 {
if (c.isDatabaseLocal() == false)
openSDB();
- RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
- null, ruralHouse.getDescription(), ruralHouse.getTown(),
- null);
- ObjectSet result = db.queryByExample(proto);
+ RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+ ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
+ ObjectSet<RuralHouse> result = db.queryByExample(proto);
RuralHouse rh = (RuralHouse) result.next();
Offer offer;
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) {
} finally {
db.close();
}
- return bok;
+ return book;
}
/**
openDB();
try {
- Owner proto = new Owner(null, null);
- ObjectSet result = db.queryByExample(proto);
+ Owner proto = new Owner(null, null, null);
+ ObjectSet<Owner> result = db.queryByExample(proto);
Vector<Owner> owners = new Vector<Owner>();
while (result.hasNext())
owners.add((Owner) result.next());
try {
RuralHouse proto = new RuralHouse(null, null, null, null, null);
- ObjectSet result = db.queryByExample(proto);
+ ObjectSet<RuralHouse> result = db.queryByExample(proto);
Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
while (result.hasNext())
ruralHouses.add((RuralHouse) result.next());
try {
- RuralHouse rhn = (RuralHouse) db
- .queryByExample(
- new RuralHouse(rh.getHouseName(), null, null,
- null, null)).next();
+ RuralHouse rhn = (RuralHouse) db.queryByExample(
+ new RuralHouse(rh.getHouseName(), null, null, null, null))
+ .next();
if (rhn.overlapsWith(firstDay, lastDay) != null)
throw new OverlappingOfferExists();
else
/**
* @param usr
- * @param pwd
+ * @param ps
* @return
* @throws RemoteException
* @throws Exception
*/
- public Vector<Account> getAccount(String usr, String pwd)
- throws RemoteException, Exception {
+ public Vector<Account> getAccount(Account proto) throws RemoteException,
+ Exception {
if (c.isDatabaseLocal() == false)
openSDB();
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())
openDB();
boolean stored = false;
- RuralHouse house = new RuralHouse(rh.getHouseName(), null, null,
- null, null);
+ RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
+ null);
try {
- ObjectSet<Owner> result = db.queryByExample(house);
+ 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;
return stored;
}
- public void removeHouse(String houseName) {
+ public void removeHouse(RuralHouse rh, Owner owner) {
if (c.isDatabaseLocal() == false)
openSDB();
else
- openDB();
- RuralHouse house = new RuralHouse(houseName, null, null, null, null);
+ openDB();
try {
- ObjectSet<RuralHouse> result = db.queryByExample(house);
- if (!result.isEmpty()) {
- RuralHouse found = (RuralHouse) result.get(0);
- db.delete(found);
+ 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) {
}
}
-
- public Vector<RuralHouse> getRuralHousesByTown(String town){
- RuralHouse rh = new RuralHouse(null,null,null,town,null);
-
+
+ public Vector<RuralHouse> getRuralHouses(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, null, null, town, fea);
if (c.isDatabaseLocal() == false)
openSDB();
else
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 {
+ // 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 {
db.close();
}
+ return true;
+ }
+
+ // TODO remove account
+ public boolean removeAccount(Account acc) {
+ return false;
}
- public RuralHouse getRuralHouseByName(String name){
- RuralHouse rh = new RuralHouse(name,null,null,null,null);
-
+
+ //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<RuralHouse> result = db.queryByExample(rh);
- Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
- while(result.hasNext())
- ruralHouses.add(result.next());
+
+ 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();
- return ruralHouses.get(0);
- } catch (NullPointerException e){
- return null;
}
-
-
+
}
}