4 //import java.util.Enumeration;
5 //import java.util.Vector;
7 import java.rmi.RemoteException;
9 import java.util.HashSet;
10 import java.util.ListIterator;
11 import java.util.Vector;
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;
22 //import dataModel.Offer;
24 import domain.RuralHouse;
25 import exceptions.OfferCanNotBeBooked;
26 import exceptions.OverlappingOfferExists;
28 public class DB4oManager {
30 private static ObjectContainer db;
31 private static EmbeddedConfiguration configuration;
32 private static ClientConfiguration configurationCS;
33 private int bookingNumber = 0; // if it is "static" then it is not
35 private int offerNumber = 0; // if it is "static" then it is not serialized
36 private static DB4oManager theDB4oManager = null;
38 private static DB4oManagerAux theDB4oManagerAux;
41 private DB4oManager() throws Exception {
42 theDB4oManagerAux = new DB4oManagerAux(0, 0);
43 c = ConfigXML.getInstance();
44 System.out.println("Creating DB4oManager instance => isDatabaseLocal: "
45 + c.isDatabaseLocal() + " getDatabBaseOpenMode: "
46 + c.getDataBaseOpenMode());
48 if ((c.getDataBaseOpenMode().equals("initialize"))
49 && (c.isDatabaseLocal()))
50 new File(c.getDb4oFilename()).delete();
52 if (c.isDatabaseLocal()) {
54 System.out.println("DataBase opened");
55 } else // c.isDatabaseLocal==false
58 System.out.println("Remote DataBase opened");
60 if (c.getDataBaseOpenMode().equals("initialize")) {
62 System.out.println("DataBase initialized");
63 } else // c.getDataBaseOpenMode().equals("open")
66 ObjectSet res = db.queryByExample(DB4oManagerAux.class);
67 ListIterator listIter = res.listIterator();
68 if (listIter.hasNext())
69 theDB4oManagerAux = (DB4oManagerAux) res.next();
73 private static void openDB() {
74 configuration = Db4oEmbedded.newConfiguration();
75 configuration.common().activationDepth(c.getActivationDepth());
76 configuration.common().updateDepth(c.getUpdateDepth());
77 configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
78 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
81 private void openSDB() {
83 configurationCS = Db4oClientServer.newClientConfiguration();
84 configurationCS.common().activationDepth(c.getActivationDepth());
85 configurationCS.common().updateDepth(c.getUpdateDepth());
86 configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
87 db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
88 c.getDatabasePort(), c.getUser(), c.getPassword());
92 class DB4oManagerAux {
96 DB4oManagerAux(int bookingNumber, int offerNumber) {
97 this.bookingNumber = bookingNumber;
98 this.offerNumber = offerNumber;
102 public static DB4oManager getInstance() throws Exception {
103 if (theDB4oManager == null)
104 theDB4oManager = new DB4oManager();
105 return theDB4oManager;
108 public void initializeDB() {
111 Owner jon = new Owner("Jon");
112 Owner alfredo = new Owner("Alfredo");
113 jon.addRuralHouse("Ezkio", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
114 jon.addRuralHouse("Eskia", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
115 jon.setBankAccount("1349 5677 21 2133567777");
116 alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
118 alfredo.addRuralHouse("Murgoitz", "", "Murgia", 6, 6, 6, 6, 6);
119 alfredo.setBankAccount("4144 0087 23 9700002133");
120 Account jonAcc = new Account("userJon", "passJon", jon);
121 Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
126 db.store(alfredoAcc);
133 public void deleteDB() {
135 if (c.isDatabaseLocal() == false)
141 Owner proto = new Owner(null, null);
142 ObjectSet result = db.queryByExample(proto);
143 Vector<Owner> owners = new Vector<Owner>();
144 while (result.hasNext()) {
145 Owner o = (Owner) result.next();
146 System.out.println("Deleted owner: " + o.toString());
156 @SuppressWarnings("finally")
157 public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
158 Date lastDay, float price) throws RemoteException, Exception {
161 if (c.isDatabaseLocal() == false)
168 RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
169 null, null, null, null);
170 ObjectSet result = db.queryByExample(proto);
171 RuralHouse rh = (RuralHouse) result.next();
172 o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
174 db.store(theDB4oManagerAux); // To store the new value for
179 } catch (com.db4o.ext.ObjectNotStorableException e) {
181 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
188 @SuppressWarnings("finally")
189 public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
190 if (c.isDatabaseLocal() == false)
201 } catch (com.db4o.ext.ObjectNotStorableException e) {
203 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
210 @SuppressWarnings("finally")
211 public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception {
212 if (c.isDatabaseLocal() == false)
223 } catch (com.db4o.ext.ObjectNotStorableException e) {
225 .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
233 * This method creates a book with a corresponding parameters
236 * day, last day, house number and telephone
239 public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
240 Date lastDate, String bookTelephoneNumber)
241 throws OfferCanNotBeBooked {
243 if (c.isDatabaseLocal() == false)
252 if (c.isDatabaseLocal() == false)
255 RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
256 null, ruralHouse.getDescription(), ruralHouse.getTown(),
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
340 new RuralHouse(rh.getHouseName(), null, null,
342 if (rhn.overlapsWith(firstDay, lastDay) != null)
343 throw new OverlappingOfferExists();
351 public static ObjectContainer getContainer() {
355 public void close() {
357 System.out.println("DataBase closed");
360 public String toString() {
361 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
368 * @throws RemoteException
371 public Vector<Account> getAccount(String usr, String pwd)
372 throws RemoteException, Exception {
374 if (c.isDatabaseLocal() == false)
380 Account proto = new Account(usr, pwd, new Owner(null, null));
381 ObjectSet<Account> result = db.queryByExample(proto);
382 Vector<Account> accounts = new Vector<Account>();
383 while (result.hasNext())
384 accounts.add((Account) result.next());
394 public boolean storeRuralHouses(RuralHouse rh) {
396 if (c.isDatabaseLocal() == false)
401 boolean stored = false;
402 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null,
405 ObjectSet<Owner> result = db.queryByExample(house);
406 if (result.isEmpty()) {
418 public void removeHouse(RuralHouse rh, Owner owner) {
420 if (c.isDatabaseLocal() == false)
425 ObjectSet<RuralHouse> result = db.queryByExample(rh);
426 if (!result.isEmpty()) {
427 RuralHouse found = (RuralHouse) result.get(0);
428 // db.delete(found.getOwner());
433 } catch (Exception exc) {
434 exc.printStackTrace();
441 public Vector<RuralHouse> getRuralHousesByTown(String town){
442 RuralHouse rh = new RuralHouse(null,null,null,town,null);
444 if (c.isDatabaseLocal() == false)
450 ObjectSet<RuralHouse> result = db.queryByExample(rh);
451 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
452 while (result.hasNext())
453 ruralHouses.add(result.next());
460 public RuralHouse getRuralHouseByName(String name){
461 RuralHouse rh = new RuralHouse(name,null,null,null,null);
463 if (c.isDatabaseLocal() == false)
469 ObjectSet<RuralHouse> result = db.queryByExample(rh);
470 Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
471 while(result.hasNext())
472 ruralHouses.add(result.next());
474 return ruralHouses.get(0);
475 } catch (NullPointerException e){