Booking deletion definite debbuging
[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 import java.rmi.RemoteException;
7 import java.util.Date;
8 import java.util.List;
9 import java.util.ListIterator;
10 import java.util.Vector;
11
12 import com.db4o.Db4oEmbedded;
13 import com.db4o.ObjectContainer;
14 import com.db4o.ObjectSet;
15 import com.db4o.config.EmbeddedConfiguration;
16 import com.db4o.cs.Db4oClientServer;
17 import com.db4o.cs.config.ClientConfiguration;
18 import com.db4o.query.Predicate;
19
20 import configuration.ConfigXML;
21 import domain.Account;
22 import domain.Administrator;
23 import domain.Booking;
24 import domain.Client;
25 import domain.HouseFeatures;
26 import domain.Offer;
27 //import dataModel.Offer;
28 import domain.Owner;
29 import domain.RuralHouse;
30 import exceptions.OfferCanNotBeBooked;
31 import exceptions.OverlappingOfferExists;
32
33 public class DB4oManager {
34
35         private static ObjectContainer db;
36         private static EmbeddedConfiguration configuration;
37         private static ClientConfiguration configurationCS;
38         private int bookingNumber = 0; // if it is "static" then it is not
39                                                                         // serialized
40         private int offerNumber = 0; // if it is "static" then it is not serialized
41         private static DB4oManager theDB4oManager = null;
42
43         private static DB4oManagerAux theDB4oManagerAux;
44
45         static ConfigXML c;
46
47         private DB4oManager() throws Exception {
48                 theDB4oManagerAux = new DB4oManagerAux(0, 0);
49                 c = ConfigXML.getInstance();
50                 System.out.println("Creating DB4oManager instance => isDatabaseLocal: "
51                                 + c.isDatabaseLocal() + " getDatabBaseOpenMode: "
52                                 + c.getDataBaseOpenMode());
53
54                 if ((c.getDataBaseOpenMode().equals("initialize"))
55                                 && (c.isDatabaseLocal()))
56                         new File(c.getDb4oFilename()).delete();
57
58                 if (c.isDatabaseLocal()) {
59                         openDB();
60                         System.out.println("DataBase opened");
61                 } else // c.isDatabaseLocal==false
62                 {
63                         openSDB();
64                         System.out.println("Remote DataBase opened");
65                 }
66                 if (c.getDataBaseOpenMode().equals("initialize")) {
67                         initializeDB();
68                         System.out.println("DataBase initialized");
69                 } else // c.getDataBaseOpenMode().equals("open")
70
71                 {
72                         ObjectSet<DB4oManagerAux> res = db
73                                         .queryByExample(DB4oManagerAux.class);
74                         ListIterator<DB4oManagerAux> listIter = res.listIterator();
75                         if (listIter.hasNext())
76                                 theDB4oManagerAux = (DB4oManagerAux) res.next();
77                 }
78         }
79
80         private static void openDB() {
81                 configuration = Db4oEmbedded.newConfiguration();
82                 configuration.common().activationDepth(c.getActivationDepth());
83                 configuration.common().updateDepth(c.getUpdateDepth());
84                 configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
85                 configuration.common().objectClass(Booking.class).cascadeOnDelete(true);
86                 configuration.common().objectClass(RuralHouse.class)
87                                 .cascadeOnDelete(true);
88                 configuration.common().objectClass(Account.class).cascadeOnDelete(true);
89                 configuration.common().objectClass(Offer.class).cascadeOnDelete(true);
90                 configuration.common().objectClass(Owner.class).cascadeOnUpdate(true);
91                 configuration.common().objectClass(Booking.class).cascadeOnUpdate(true);
92                 configuration.common().objectClass(RuralHouse.class)
93                                 .cascadeOnUpdate(true);
94                 configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
95                 configuration.common().objectClass(Offer.class).cascadeOnUpdate(true);
96                 configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
97                 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
98         }
99
100         private void openSDB() {
101
102                 configurationCS = Db4oClientServer.newClientConfiguration();
103                 configurationCS.common().activationDepth(c.getActivationDepth());
104                 configurationCS.common().updateDepth(c.getUpdateDepth());
105                 configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
106                 db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
107                                 c.getDatabasePort(), c.getUser(), c.getPassword());
108
109         }
110
111         class DB4oManagerAux {
112                 int bookingNumber;
113                 int offerNumber;
114
115                 DB4oManagerAux(int bookingNumber, int offerNumber) {
116                         this.bookingNumber = bookingNumber;
117                         this.offerNumber = offerNumber;
118                 }
119         }
120
121         public static DB4oManager getInstance() throws Exception {
122                 if (theDB4oManager == null)
123                         theDB4oManager = new DB4oManager();
124                 return theDB4oManager;
125         }
126
127         public void initializeDB() {
128
129                 try {
130                         Owner jon = new Owner("Jon", "1349 5677 21 2133567777",
131                                         "Jon@gmail.com");
132                         Owner alfredo = new Owner("Alfredo", "4144 0087 23 9700002133",
133                                         "alfredo@gmail.com");
134                         jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3,
135                                         3, 3);
136                         jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4,
137                                         4, 4, 4);
138                         alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, 5,
139                                         5, 5, 5);
140
141                         alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6);
142                         Account jonAcc = new Account("1", "1", jon);
143                         Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
144                                         alfredo);
145
146                         Account admin = new Account("admin", "admin", true);
147                         db.store(Administrator.getInstance());
148                         db.store(jonAcc);
149                         db.store(alfredoAcc);
150                         db.store(admin);
151                         db.commit();
152                 } finally {
153                         db.close();
154                 }
155         }
156
157         public void deleteDB() {
158
159                 if (c.isDatabaseLocal() == false)
160                         openSDB();
161                 else
162                         openDB();
163
164                 try {
165                         Owner proto = new Owner(null, null, null);
166                         ObjectSet<Owner> result = db.queryByExample(proto);
167                         while (result.hasNext()) {
168                                 Owner o = (Owner) result.next();
169                                 System.out.println("Deleted owner: " + o.toString());
170                                 db.delete(o);
171                         }
172                         db.commit();
173                 } finally {
174                         db.close();
175                 }
176         }
177
178         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
179                         Date lastDay, float price) throws RemoteException, Exception {
180                 Offer o = null;
181
182                 if (c.isDatabaseLocal() == false)
183                         openSDB();
184                 else
185                         openDB();
186
187                 try {
188
189                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
190                                         null, null, null);
191                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
192                         RuralHouse rh = (RuralHouse) result.next();
193                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
194                                         lastDay, price);
195                         db.store(theDB4oManagerAux); // To store the new value for
196                                                                                         // offerNumber
197                         db.store(o);
198                         db.commit();
199
200                 } catch (com.db4o.ext.ObjectNotStorableException e) {
201                         System.out
202                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
203                 } finally {
204                         db.close();
205                 }
206                 return o;
207         }
208
209         public void deleteOffer(Offer offer) throws RemoteException, Exception {
210                 if (c.isDatabaseLocal() == false)
211                         openSDB();
212                 else
213                         openDB();
214
215                 try {
216                         ObjectSet<Offer> of = db.queryByExample(offer);
217                         RuralHouse rh = of.get(0).getRuralHouse();
218                         System.out.println(rh.getAllOffers().remove(of.get(0)));
219                         db.store(rh);
220                         db.commit();
221
222                 } catch (com.db4o.ext.ObjectNotStorableException e) {
223                         System.out
224                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
225                 } finally {
226                         db.close();
227                 }
228         }
229
230         public Vector<Offer> getRHsOffer(String name) {
231                 if (c.isDatabaseLocal() == false)
232                         openSDB();
233                 else
234                         openDB();
235
236                 try {
237                         RuralHouse rh = (RuralHouse) db.queryByExample(
238                                         new RuralHouse(name, null, null, null, null)).get(0);
239                         Offer proto = new Offer(0, rh, null, null, 0);
240                         ObjectSet<Offer> result = db.queryByExample(proto);
241                         return new Vector<Offer>(result);
242                 } finally {
243                         db.close();
244                 }
245         }
246
247         public Administrator getAdminData() {
248
249                 if (c.isDatabaseLocal() == false)
250                         openSDB();
251                 else
252                         openDB();
253
254                 try {
255
256                         List<Administrator> admL = db.query(new Predicate<Administrator>() {
257                                 private static final long serialVersionUID = 1L;
258
259                                 public boolean match(Administrator admin) {
260                                         return true;
261                                 }
262                         });
263
264                         return admL.get(0);
265                 } finally {
266                         db.close();
267
268                 }
269
270         }
271
272         public void storeAdmin() {
273
274                 if (c.isDatabaseLocal() == false)
275                         openSDB();
276                 else
277                         openDB();
278                 try {
279
280                         List<Administrator> admL = db.query(new Predicate<Administrator>() {
281                                 /**
282                                  * 
283                                  */
284                                 private static final long serialVersionUID = 1L;
285
286                                 public boolean match(Administrator admin) {
287                                         return true;
288                                 }
289                         });
290
291                         admL.get(0).setAddRequest(
292                                         Administrator.getInstance().getAddRequest());
293                         admL.get(0).setRemoveRequest(
294                                         Administrator.getInstance().getRemoveRequest());
295                         admL.get(0).setNewOwnerRequest(
296                                         Administrator.getInstance().getNewOwnerRequest());
297
298                         db.commit();
299
300                 } catch (Exception e) {
301
302                 } finally {
303                         db.close();
304                 }
305
306         }
307
308         /**
309          * This method creates a book with a corresponding parameters
310          * 
311          * @param First
312          *            day, last day, house number and telephone
313          * @return a book
314          */
315         public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate,
316                         Date lastDate, Client cl) throws OfferCanNotBeBooked {
317
318                 if (c.isDatabaseLocal() == false)
319                         openSDB();
320                 else
321                         openDB();
322
323                 Vector<Booking> book = new Vector<Booking>();
324
325                 try {
326
327                         if (c.isDatabaseLocal() == false)
328                                 openSDB();
329
330                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
331                                         ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
332                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
333                         RuralHouse rh = (RuralHouse) result.next();
334
335                         Offer offer;
336                         offer = rh.findOffer(firstDate, lastDate);
337
338                         if (offer != null) {
339                                 offer.createBooking(theDB4oManagerAux.bookingNumber++, cl);
340                                 db.store(theDB4oManagerAux); // To store the new value for
341                                                                                                 // bookingNumber
342
343                                 db.store(offer);
344                                 db.commit();
345                                 book = offer.getBookings();
346
347                         }
348
349                 } catch (com.db4o.ext.ObjectNotStorableException e) {
350                         System.out
351                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
352                 } catch (Exception exc) {
353                         exc.printStackTrace();
354                 } finally {
355                         db.close();
356                 }
357                 return book;
358         }
359
360         /**
361          * This method existing owners
362          * 
363          */
364         public Vector<Owner> getOwners() throws RemoteException, Exception {
365
366                 if (c.isDatabaseLocal() == false)
367                         openSDB();
368                 else
369                         openDB();
370
371                 try {
372                         Owner proto = new Owner(null, null, null);
373                         ObjectSet<Owner> result = db.queryByExample(proto);
374                         Vector<Owner> owners = new Vector<Owner>();
375                         while (result.hasNext())
376                                 owners.add((Owner) result.next());
377                         return owners;
378                 } finally {
379                         db.close();
380                 }
381         }
382
383         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
384                         Exception {
385
386                 if (c.isDatabaseLocal() == false)
387                         openSDB();
388                 else
389                         openDB();
390
391                 try {
392                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
393                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
394                         return new Vector<RuralHouse>(result);
395                 } finally {
396                         db.close();
397                 }
398         }
399
400         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
401                         Date lastDay) throws RemoteException, OverlappingOfferExists {
402
403                 if (c.isDatabaseLocal() == false)
404                         openSDB();
405                 else
406                         openDB();
407
408                 try {
409
410                         RuralHouse rhn = (RuralHouse) db.queryByExample(
411                                         new RuralHouse(rh.getHouseName(), null, null, null, null))
412                                         .next();
413                         if (rhn.overlapsWith(firstDay, lastDay) != null)
414                                 throw new OverlappingOfferExists();
415                         else
416                                 return false;
417                 } finally {
418                         db.close();
419                 }
420         }
421
422         public static ObjectContainer getContainer() {
423                 return db;
424         }
425
426         public void close() {
427                 db.close();
428                 System.out.println("DataBase closed");
429         }
430
431         public String toString() {
432                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
433         }
434
435         /**
436          * @param usr
437          * @param ps
438          * @return
439          * @throws RemoteException
440          * @throws Exception
441          */
442         public Vector<Account> getAccount(Account proto) throws RemoteException,
443                         Exception {
444
445                 if (c.isDatabaseLocal() == false)
446                         openSDB();
447                 else
448                         openDB();
449
450                 try {
451                         ObjectSet<Account> result = db.queryByExample(proto);
452                         Vector<Account> accounts = new Vector<Account>();
453                         while (result.hasNext())
454                                 accounts.add((Account) result.next());
455                         return accounts;
456                 } finally {
457                         db.close();
458                 }
459         }
460
461         /**
462          * @param rh
463          */
464         public boolean storeRuralHouses(RuralHouse rh) {
465
466                 if (c.isDatabaseLocal() == false)
467                         openSDB();
468                 else
469                         openDB();
470
471                 boolean stored = false;
472                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
473                                 null);
474                 try {
475                         ObjectSet<RuralHouse> result = db.queryByExample(house);
476                         if (result.isEmpty()) {
477                                 Owner ow = (Owner) db.queryByExample(rh.getOwner()).get(0);
478                                 rh.setOwner(ow);
479                                 ow.addRuralHouse(rh);
480                                 db.store(rh);
481                                 db.commit();
482                                 stored = true;
483                         } else {
484                                 db.delete(result.get(0));
485                                 db.store(rh);
486                                 db.commit();
487                                 stored = true;
488                         }
489                 } finally {
490                         db.close();
491                 }
492                 return stored;
493         }
494
495         public void removeHouse(RuralHouse rh, Owner owner) {
496
497                 if (c.isDatabaseLocal() == false)
498                         openSDB();
499                 else
500                         openDB();
501                 try {
502                         ObjectSet<Owner> result = db.queryByExample(owner);
503                         ObjectSet<RuralHouse> rhs = db.queryByExample(rh);
504                         if (!rhs.isEmpty()) {
505                                 Owner found = result.get(0);
506                                 found.getRuralHouses().remove(rhs.get(0));
507                                 db.delete(rhs.get(0));
508                                 db.store(found);
509                                 db.commit();
510                         }
511                 } catch (Exception exc) {
512                         exc.printStackTrace();
513                 } finally {
514                         db.close();
515                 }
516
517         }
518
519         public Vector<RuralHouse> getRuralHouses(Owner ow, String name,
520                         String town, int nBed, int nKit, int nBath, int nPark, int nLiv) {
521                 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
522                 RuralHouse rh = new RuralHouse(name, ow, null, town, fea);
523                 if (c.isDatabaseLocal() == false)
524                         openSDB();
525                 else
526                         openDB();
527
528                 try {
529                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
530                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
531                         while (result.hasNext())
532                                 ruralHouses.add(result.next());
533                         db.close();
534                         return ruralHouses;
535                 } catch (NullPointerException e) {
536                         return null;
537                 }
538
539         }
540
541         public boolean addAccount(Account acc) {
542                 if (c.isDatabaseLocal() == false)
543                         openSDB();
544                 else
545                         openDB();
546
547                 try {
548                         ObjectSet<Account> result = db.queryByExample(new Account(acc
549                                         .getUsername()));
550                         if (result.isEmpty()) {
551                                 db.store(acc);
552                                 db.commit();
553                                 return true;
554                         }
555                 } catch (Exception exc) {
556                         exc.printStackTrace();
557                 } finally {
558                         db.close();
559                 }
560                 return false;
561         }
562
563         public boolean removeAccount(Owner own) {
564                 if (c.isDatabaseLocal() == false)
565                         openSDB();
566                 else
567                         openDB();
568
569                 try {
570                         ObjectSet<Account> result = db.queryByExample(new Account(own));
571                         if (!result.isEmpty()) {
572                                 db.delete(result.get(0));
573                                 db.commit();
574                                 return true;
575                         }
576                 } catch (Exception exc) {
577                         exc.printStackTrace();
578                 } finally {
579                         db.close();
580                 }
581                 return false;
582         }
583
584         public void acceptBooking(Offer of) {
585                 Offer off = new Offer(of.getOfferNumber(), new RuralHouse(of
586                                 .getRuralHouse().getHouseName(), null, null, null, null), null,
587                                 null, 0);
588                 if (c.isDatabaseLocal() == false)
589                         openSDB();
590                 else
591                         openDB();
592
593                 try {
594                         ObjectSet<Offer> result = db.queryByExample(off);
595                         db.delete(result.get(0));
596                         RuralHouse rh = result.get(0).getRuralHouse();
597                         of.setRuralHouse(rh);
598                         rh.getAllOffers().remove(result.get(0));
599                         rh.getAllOffers().add(of);
600                         db.store(rh);
601                         db.close();
602
603                 } catch (Exception e) {
604                         e.printStackTrace();
605                         ;
606                 }
607         }
608
609         public void removeBooking(Booking b) {
610                 if (c.isDatabaseLocal() == false)
611                         openSDB();
612                 else
613                         openDB();
614                 try {
615                         Booking book = new Booking(b.getBookNumber(), new Offer(b
616                                         .getOffer().getOfferNumber(), new RuralHouse(b.getOffer()
617                                         .getRuralHouse().getHouseName(), null, null, null, null),
618                                         null, null, 0), b.getClient(), b.getBookDate());
619                         book.setOffer(null);
620                         db.delete(book);
621                         db.commit();
622                 } catch (Exception e) {
623                         e.printStackTrace();
624                 } finally {
625                         db.close();
626                 }
627
628         }
629 }