New GUI for booking and querying availability
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
1 package dataAccess;
2
3 import java.io.File;
4 //import java.util.Enumeration;
5 //import java.util.Vector;
6
7 import java.rmi.RemoteException;
8 import java.util.Date;
9 import java.util.HashSet;
10 import java.util.ListIterator;
11 import java.util.Vector;
12
13 import com.db4o.*;
14 import com.db4o.config.EmbeddedConfiguration;
15 import com.db4o.cs.Db4oClientServer;
16 import com.db4o.cs.config.ClientConfiguration;
17
18 import configuration.ConfigXML;
19 import domain.Account;
20 import domain.Booking;
21 import domain.HouseFeatures;
22 import domain.Offer;
23 //import dataModel.Offer;
24 import domain.Owner;
25 import domain.RuralHouse;
26 import exceptions.OfferCanNotBeBooked;
27 import exceptions.OverlappingOfferExists;
28
29 public class DB4oManager {
30
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
35                                                                         // serialized
36         private int offerNumber = 0; // if it is "static" then it is not serialized
37         private static DB4oManager theDB4oManager = null;
38
39         private static DB4oManagerAux theDB4oManagerAux;
40
41         static ConfigXML c;
42
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());
49
50                 if ((c.getDataBaseOpenMode().equals("initialize"))
51                                 && (c.isDatabaseLocal()))
52                         new File(c.getDb4oFilename()).delete();
53
54                 if (c.isDatabaseLocal()) {
55                         openDB();
56                         System.out.println("DataBase opened");
57                 } else // c.isDatabaseLocal==false
58                 {
59                         openSDB();
60                         System.out.println("Remote DataBase opened");
61                 }
62                 if (c.getDataBaseOpenMode().equals("initialize")) {
63                         initializeDB();
64                         System.out.println("DataBase initialized");
65                 } else // c.getDataBaseOpenMode().equals("open")
66
67                 {
68                         ObjectSet res = db.queryByExample(DB4oManagerAux.class);
69                         ListIterator listIter = res.listIterator();
70                         if (listIter.hasNext())
71                                 theDB4oManagerAux = (DB4oManagerAux) res.next();
72                 }
73         }
74
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());
81         }
82
83         private void openSDB() {
84
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());
91
92         }
93
94         class DB4oManagerAux {
95                 int bookingNumber;
96                 int offerNumber;
97
98                 DB4oManagerAux(int bookingNumber, int offerNumber) {
99                         this.bookingNumber = bookingNumber;
100                         this.offerNumber = offerNumber;
101                 }
102         }
103
104         public static DB4oManager getInstance() throws Exception {
105                 if (theDB4oManager == null)
106                         theDB4oManager = new DB4oManager();
107                 return theDB4oManager;
108         }
109
110         public void initializeDB() {
111
112                 try {
113                         Owner jon = new Owner("Jon");
114                         Owner alfredo = new Owner("Alfredo");
115                         jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3, 3,
116                                         3);
117                         jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4, 4,
118                                         4, 4);
119                         jon.setBankAccount("1349 5677 21 2133567777");
120                         alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5,
121                                         5, 5, 5, 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",
126                                         alfredo);
127                         db.store(jon);
128                         db.store(alfredo);
129                         db.store(jonAcc);
130                         db.store(alfredoAcc);
131                         db.commit();
132                 } finally {
133                         db.close();
134                 }
135         }
136
137         public void deleteDB() {
138
139                 if (c.isDatabaseLocal() == false)
140                         openSDB();
141                 else
142                         openDB();
143
144                 try {
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());
151                                 db.delete(o);
152                         }
153                         db.commit();
154                 } finally {
155                         db.close();
156                 }
157         }
158
159         @SuppressWarnings("finally")
160         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
161                         Date lastDay, float price) throws RemoteException, Exception {
162                 Offer o = null;
163
164                 if (c.isDatabaseLocal() == false)
165                         openSDB();
166                 else
167                         openDB();
168
169                 try {
170
171                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
172                                         null, null, null);
173                         ObjectSet result = db.queryByExample(proto);
174                         RuralHouse rh = (RuralHouse) result.next();
175                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
176                                         lastDay, price);
177                         db.store(theDB4oManagerAux); // To store the new value for
178                                                                                         // offerNumber
179                         db.store(o);
180                         db.commit();
181
182                 } catch (com.db4o.ext.ObjectNotStorableException e) {
183                         System.out
184                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
185                 } finally {
186                         db.close();
187                 }
188                 return o;
189         }
190
191         @SuppressWarnings("finally")
192         public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
193                 if (c.isDatabaseLocal() == false)
194                         openSDB();
195                 else
196                         openDB();
197
198                 try {
199
200                         db.store(offer);
201                         db.commit();
202
203                 } catch (com.db4o.ext.ObjectNotStorableException e) {
204                         System.out
205                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
206                 } finally {
207                         db.close();
208                 }
209                 return offer;
210         }
211
212         @SuppressWarnings("finally")
213         public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException,
214                         Exception {
215                 if (c.isDatabaseLocal() == false)
216                         openSDB();
217                 else
218                         openDB();
219
220                 try {
221
222                         db.store(rh);
223                         db.delete(offer);
224                         db.commit();
225
226                 } catch (com.db4o.ext.ObjectNotStorableException e) {
227                         System.out
228                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
229                 } finally {
230                         db.close();
231                 }
232         }
233
234         /**
235          * This method creates a book with a corresponding parameters
236          * 
237          * @param First
238          *            day, last day, house number and telephone
239          * @return a book
240          */
241         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
242                         Date lastDate, String bookTelephoneNumber)
243                         throws OfferCanNotBeBooked {
244
245                 if (c.isDatabaseLocal() == false)
246                         openSDB();
247                 else
248                         openDB();
249
250                 Booking bok = null;
251
252                 try {
253
254                         if (c.isDatabaseLocal() == false)
255                                 openSDB();
256
257                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
258                                         ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
259                         ObjectSet result = db.queryByExample(proto);
260                         RuralHouse rh = (RuralHouse) result.next();
261
262                         Offer offer;
263                         offer = rh.findOffer(firstDate, lastDate);
264
265                         if (offer != null) {
266                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
267                                                 bookTelephoneNumber);
268                                 db.store(theDB4oManagerAux); // To store the new value for
269                                                                                                 // bookingNumber
270                                 db.store(offer);
271                                 db.commit();
272                                 bok = offer.getBooking();
273                         }
274
275                 } catch (com.db4o.ext.ObjectNotStorableException e) {
276                         System.out
277                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
278                 } catch (Exception exc) {
279                         exc.printStackTrace();
280                 } finally {
281                         db.close();
282                 }
283                 return bok;
284         }
285
286         /**
287          * This method existing owners
288          * 
289          */
290         public Vector<Owner> getOwners() throws RemoteException, Exception {
291
292                 if (c.isDatabaseLocal() == false)
293                         openSDB();
294                 else
295                         openDB();
296
297                 try {
298                         Owner proto = new Owner(null, null);
299                         ObjectSet result = db.queryByExample(proto);
300                         Vector<Owner> owners = new Vector<Owner>();
301                         while (result.hasNext())
302                                 owners.add((Owner) result.next());
303                         return owners;
304                 } finally {
305                         db.close();
306                 }
307         }
308
309         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
310                         Exception {
311
312                 if (c.isDatabaseLocal() == false)
313                         openSDB();
314                 else
315                         openDB();
316
317                 try {
318                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
319                         ObjectSet result = db.queryByExample(proto);
320                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
321                         while (result.hasNext())
322                                 ruralHouses.add((RuralHouse) result.next());
323                         return ruralHouses;
324                 } finally {
325                         db.close();
326                 }
327         }
328
329         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
330                         Date lastDay) throws RemoteException, OverlappingOfferExists {
331
332                 if (c.isDatabaseLocal() == false)
333                         openSDB();
334                 else
335                         openDB();
336
337                 try {
338
339                         RuralHouse rhn = (RuralHouse) db.queryByExample(
340                                         new RuralHouse(rh.getHouseName(), null, null, null, null))
341                                         .next();
342                         if (rhn.overlapsWith(firstDay, lastDay) != null)
343                                 throw new OverlappingOfferExists();
344                         else
345                                 return false;
346                 } finally {
347                         db.close();
348                 }
349         }
350
351         public static ObjectContainer getContainer() {
352                 return db;
353         }
354
355         public void close() {
356                 db.close();
357                 System.out.println("DataBase closed");
358         }
359
360         public String toString() {
361                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
362         }
363
364         /**
365          * @param usr
366          * @param pwd
367          * @return
368          * @throws RemoteException
369          * @throws Exception
370          */
371         public Vector<Account> getAccount(String usr, String pwd)
372                         throws RemoteException, Exception {
373
374                 if (c.isDatabaseLocal() == false)
375                         openSDB();
376                 else
377                         openDB();
378
379                 try {
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());
385                         return accounts;
386                 } finally {
387                         db.close();
388                 }
389         }
390
391         /**
392          * @param rh
393          */
394         public boolean storeRuralHouses(RuralHouse rh) {
395
396                 if (c.isDatabaseLocal() == false)
397                         openSDB();
398                 else
399                         openDB();
400
401                 boolean stored = false;
402                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
403                                 null);
404                 try {
405                         ObjectSet<Owner> result = db.queryByExample(house);
406                         if (result.isEmpty()) {
407                                 db.store(rh);
408                                 db.commit();
409                                 stored = true;
410                         }
411                 } finally {
412                         db.close();
413                 }
414                 return stored;
415         }
416
417         public void removeHouse(RuralHouse rh, Owner owner) {
418
419                 if (c.isDatabaseLocal() == false)
420                         openSDB();
421                 else
422                         openDB();
423                 try {
424                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
425                         if (!result.isEmpty()) {
426                                 RuralHouse found = (RuralHouse) result.get(0);
427                                 // db.delete(found.getOwner());
428                                 db.store(owner);
429                                 db.delete(found);
430                                 db.commit();
431                         }
432                 } catch (Exception exc) {
433                         exc.printStackTrace();
434                 } finally {
435                         db.close();
436                 }
437
438         }
439
440         public Vector<RuralHouse> getRuralHousesByTown(String town) {
441                 RuralHouse rh = new RuralHouse(null, null, null, town, null);
442
443                 if (c.isDatabaseLocal() == false)
444                         openSDB();
445                 else
446                         openDB();
447
448                 try {
449                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
450                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
451                         while (result.hasNext())
452                                 ruralHouses.add(result.next());
453                         return ruralHouses;
454                 } finally {
455                         db.close();
456                 }
457
458         }
459
460         public RuralHouse getRuralHouseByName(String name) {
461                 RuralHouse rh = new RuralHouse(name, null, null, null, null);
462
463                 if (c.isDatabaseLocal() == false)
464                         openSDB();
465                 else
466                         openDB();
467
468                 try {
469                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
470                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
471                         while (result.hasNext())
472                                 ruralHouses.add(result.next());
473                         db.close();
474                         if (!ruralHouses.isEmpty())
475                                 return ruralHouses.get(0);
476                         else
477                                 return null;
478                 } catch (NullPointerException e) {
479                         return null;
480                 }
481
482         }
483
484         public Vector<RuralHouse> getRuralHouses(String town, int nBed, int nKit,
485                         int nBath, int nPark, int nLiv) {
486                 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
487                 RuralHouse rh = new RuralHouse(null, null, null, town, fea);
488                 if (c.isDatabaseLocal() == false)
489                         openSDB();
490                 else
491                         openDB();
492
493                 try {
494                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
495                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
496                         while (result.hasNext())
497                                 ruralHouses.add(result.next());
498                         db.close();
499                         return ruralHouses;
500                 } catch (NullPointerException e) {
501                         return null;
502                 }
503
504         }
505 }