4 //import java.util.Enumeration;
5 //import java.util.Vector;
6 import java.rmi.RemoteException;
8 import java.util.ListIterator;
9 import java.util.Vector;
11 import com.db4o.Db4oEmbedded;
12 import com.db4o.ObjectContainer;
13 import com.db4o.ObjectSet;
14 import com.db4o.config.EmbeddedConfiguration;
15 import com.db4o.cs.Db4oClientServer;
16 import com.db4o.cs.config.ClientConfiguration;
18 import configuration.ConfigXML;
19 import domain.Account;
20 import domain.Booking;
21 import domain.HouseFeatures;
23 //import dataModel.Offer;
25 import domain.RuralHouse;
26 import exceptions.OfferCanNotBeBooked;
27 import exceptions.OverlappingOfferExists;
29 public class DB4oManager {
31 private static ObjectContainer db;
32 private static EmbeddedConfiguration configuration;
33 private static ClientConfiguration configurationCS;
34 private int bookingNumber = 0; // if it is "static" then it is not
36 private int offerNumber = 0; // if it is "static" then it is not serialized
37 private static DB4oManager theDB4oManager = null;
39 private static DB4oManagerAux theDB4oManagerAux;
43 private DB4oManager() throws Exception {
44 theDB4oManagerAux = new DB4oManagerAux(0, 0);
45 c = ConfigXML.getInstance();
46 System.out.println("Creating DB4oManager instance => isDatabaseLocal: "
47 + c.isDatabaseLocal() + " getDatabBaseOpenMode: "
48 + c.getDataBaseOpenMode());
50 if ((c.getDataBaseOpenMode().equals("initialize"))
51 && (c.isDatabaseLocal()))
52 new File(c.getDb4oFilename()).delete();
54 if (c.isDatabaseLocal()) {
56 System.out.println("DataBase opened");
57 } else // c.isDatabaseLocal==false
60 System.out.println("Remote DataBase opened");
62 if (c.getDataBaseOpenMode().equals("initialize")) {
64 System.out.println("DataBase initialized");
65 } else // c.getDataBaseOpenMode().equals("open")
68 ObjectSet res = db.queryByExample(DB4oManagerAux.class);
69 ListIterator listIter = res.listIterator();
70 if (listIter.hasNext())
71 theDB4oManagerAux = (DB4oManagerAux) res.next();
75 private static void openDB() {
76 configuration = Db4oEmbedded.newConfiguration();
77 configuration.common().activationDepth(c.getActivationDepth());
78 configuration.common().updateDepth(c.getUpdateDepth());
79 configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
80 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
83 private void openSDB() {
85 configurationCS = Db4oClientServer.newClientConfiguration();
86 configurationCS.common().activationDepth(c.getActivationDepth());
87 configurationCS.common().updateDepth(c.getUpdateDepth());
88 configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
89 db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
90 c.getDatabasePort(), c.getUser(), c.getPassword());
94 class DB4oManagerAux {
98 DB4oManagerAux(int bookingNumber, int offerNumber) {
99 this.bookingNumber = bookingNumber;
100 this.offerNumber = offerNumber;
104 public static DB4oManager getInstance() throws Exception {
105 if (theDB4oManager == null)
106 theDB4oManager = new DB4oManager();
107 return theDB4oManager;
110 public void initializeDB() {
113 Owner jon = new Owner("Jon");
114 Owner alfredo = new Owner("Alfredo");
115 jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3, 3,
117 jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4, 4,
119 jon.setBankAccount("1349 5677 21 2133567777");
120 alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5,
122 alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6);
123 alfredo.setBankAccount("4144 0087 23 9700002133");
124 Account jonAcc = new Account("userJon", "passJon", jon);
125 Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
130 db.store(alfredoAcc);
137 public void deleteDB() {
139 if (c.isDatabaseLocal() == false)
145 Owner proto = new Owner(null, null);
146 ObjectSet result = db.queryByExample(proto);
147 Vector<Owner> owners = new Vector<Owner>();
148 while (result.hasNext()) {
149 Owner o = (Owner) result.next();
150 System.out.println("Deleted owner: " + o.toString());
161 public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
162 Date lastDay, float price) throws RemoteException, Exception {
165 if (c.isDatabaseLocal() == false)
172 RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
174 ObjectSet result = db.queryByExample(proto);
175 RuralHouse rh = (RuralHouse) result.next();
176 o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
178 db.store(theDB4oManagerAux); // To store the new value for
183 } catch (com.db4o.ext.ObjectNotStorableException e) {
185 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
192 public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
193 if (c.isDatabaseLocal() == false)
203 } catch (com.db4o.ext.ObjectNotStorableException e) {
205 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
212 public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException,
214 if (c.isDatabaseLocal() == false)
225 } catch (com.db4o.ext.ObjectNotStorableException e) {
227 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
234 * This method creates a book with a corresponding parameters
237 * day, last day, house number and telephone
240 public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
241 Date lastDate, String bookTelephoneNumber)
242 throws OfferCanNotBeBooked {
244 if (c.isDatabaseLocal() == false)
253 if (c.isDatabaseLocal() == false)
256 RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
257 ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
258 ObjectSet result = db.queryByExample(proto);
259 RuralHouse rh = (RuralHouse) result.next();
262 offer = rh.findOffer(firstDate, lastDate);
265 offer.createBooking(theDB4oManagerAux.bookingNumber++,
266 bookTelephoneNumber);
267 db.store(theDB4oManagerAux); // To store the new value for
271 bok = offer.getBooking();
274 } catch (com.db4o.ext.ObjectNotStorableException e) {
276 .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
277 } catch (Exception exc) {
278 exc.printStackTrace();
286 * This method existing owners
289 public Vector<Owner> getOwners() throws RemoteException, Exception {
291 if (c.isDatabaseLocal() == false)
297 Owner proto = new Owner(null, null);
298 ObjectSet result = db.queryByExample(proto);
299 Vector<Owner> owners = new Vector<Owner>();
300 while (result.hasNext())
301 owners.add((Owner) result.next());
308 public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
311 if (c.isDatabaseLocal() == false)
317 RuralHouse proto = new RuralHouse(null, null, null, null, null);
318 ObjectSet result = db.queryByExample(proto);
319 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
320 while (result.hasNext())
321 ruralHouses.add((RuralHouse) result.next());
328 public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
329 Date lastDay) throws RemoteException, OverlappingOfferExists {
331 if (c.isDatabaseLocal() == false)
338 RuralHouse rhn = (RuralHouse) db.queryByExample(
339 new RuralHouse(rh.getHouseName(), null, null, null, null))
341 if (rhn.overlapsWith(firstDay, lastDay) != null)
342 throw new OverlappingOfferExists();
350 public static ObjectContainer getContainer() {
354 public void close() {
356 System.out.println("DataBase closed");
359 public String toString() {
360 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
367 * @throws RemoteException
370 public Vector<Account> getAccount(String usr, String pwd)
371 throws RemoteException, Exception {
373 if (c.isDatabaseLocal() == false)
379 Account proto = new Account(usr, pwd, new Owner(null, null));
380 ObjectSet<Account> result = db.queryByExample(proto);
381 Vector<Account> accounts = new Vector<Account>();
382 while (result.hasNext())
383 accounts.add((Account) result.next());
393 public boolean storeRuralHouses(RuralHouse rh) {
395 if (c.isDatabaseLocal() == false)
400 boolean stored = false;
401 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
404 ObjectSet<Owner> result = db.queryByExample(house);
405 if (result.isEmpty()) {
416 public void removeHouse(RuralHouse rh, Owner owner) {
418 if (c.isDatabaseLocal() == false)
423 ObjectSet<RuralHouse> result = db.queryByExample(rh);
424 if (!result.isEmpty()) {
425 RuralHouse found = (RuralHouse) result.get(0);
426 // db.delete(found.getOwner());
431 } catch (Exception exc) {
432 exc.printStackTrace();
439 public Vector<RuralHouse> getRuralHousesByTown(String town) {
440 RuralHouse rh = new RuralHouse(null, null, null, town, null);
442 if (c.isDatabaseLocal() == false)
448 ObjectSet<RuralHouse> result = db.queryByExample(rh);
449 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
450 while (result.hasNext())
451 ruralHouses.add(result.next());
459 public RuralHouse getRuralHouseByName(String name) {
460 RuralHouse rh = new RuralHouse(name, null, null, null, null);
462 if (c.isDatabaseLocal() == false)
468 ObjectSet<RuralHouse> result = db.queryByExample(rh);
469 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
470 while (result.hasNext())
471 ruralHouses.add(result.next());
473 if (!ruralHouses.isEmpty())
474 return ruralHouses.get(0);
477 } catch (NullPointerException e) {
483 public Vector<RuralHouse> getRuralHouses(String town, int nBed, int nKit,
484 int nBath, int nPark, int nLiv) {
485 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
486 RuralHouse rh = new RuralHouse(null, null, null, town, fea);
487 if (c.isDatabaseLocal() == false)
493 ObjectSet<RuralHouse> result = db.queryByExample(rh);
494 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
495 while (result.hasNext())
496 ruralHouses.add(result.next());
499 } catch (NullPointerException e) {