6e24ecec138b31e9209e62b8e5077ccd4d54fb76
[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.ListIterator;
9 import java.util.Vector;
10
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;
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         
160
161         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
162                         Date lastDay, float price) throws RemoteException, Exception {
163                 Offer o = null;
164
165                 if (c.isDatabaseLocal() == false)
166                         openSDB();
167                 else
168                         openDB();
169
170                 try {
171
172                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
173                                         null, null, null);
174                         ObjectSet result = db.queryByExample(proto);
175                         RuralHouse rh = (RuralHouse) result.next();
176                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
177                                         lastDay, price);
178                         db.store(theDB4oManagerAux); // To store the new value for
179                                                                                         // offerNumber
180                         db.store(o);
181                         db.commit();
182
183                 } catch (com.db4o.ext.ObjectNotStorableException e) {
184                         System.out
185                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
186                 } finally {
187                         db.close();
188                 }
189                 return o;
190         }
191
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         public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException,
213                         Exception {
214                 if (c.isDatabaseLocal() == false)
215                         openSDB();
216                 else
217                         openDB();
218
219                 try {
220
221                         db.store(rh);
222                         db.delete(offer);
223                         db.commit();
224
225                 } catch (com.db4o.ext.ObjectNotStorableException e) {
226                         System.out
227                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
228                 } finally {
229                         db.close();
230                 }
231         }
232
233         /**
234          * This method creates a book with a corresponding parameters
235          * 
236          * @param First
237          *            day, last day, house number and telephone
238          * @return a book
239          */
240         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
241                         Date lastDate, String bookTelephoneNumber)
242                         throws OfferCanNotBeBooked {
243
244                 if (c.isDatabaseLocal() == false)
245                         openSDB();
246                 else
247                         openDB();
248
249                 Booking bok = null;
250
251                 try {
252
253                         if (c.isDatabaseLocal() == false)
254                                 openSDB();
255
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();
260
261                         Offer offer;
262                         offer = rh.findOffer(firstDate, lastDate);
263
264                         if (offer != null) {
265                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
266                                                 bookTelephoneNumber);
267                                 db.store(theDB4oManagerAux); // To store the new value for
268                                                                                                 // bookingNumber
269                                 db.store(offer);
270                                 db.commit();
271                                 bok = offer.getBooking();
272                         }
273
274                 } catch (com.db4o.ext.ObjectNotStorableException e) {
275                         System.out
276                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
277                 } catch (Exception exc) {
278                         exc.printStackTrace();
279                 } finally {
280                         db.close();
281                 }
282                 return bok;
283         }
284
285         /**
286          * This method existing owners
287          * 
288          */
289         public Vector<Owner> getOwners() throws RemoteException, Exception {
290
291                 if (c.isDatabaseLocal() == false)
292                         openSDB();
293                 else
294                         openDB();
295
296                 try {
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());
302                         return owners;
303                 } finally {
304                         db.close();
305                 }
306         }
307
308         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
309                         Exception {
310
311                 if (c.isDatabaseLocal() == false)
312                         openSDB();
313                 else
314                         openDB();
315
316                 try {
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());
322                         return ruralHouses;
323                 } finally {
324                         db.close();
325                 }
326         }
327
328         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
329                         Date lastDay) throws RemoteException, OverlappingOfferExists {
330
331                 if (c.isDatabaseLocal() == false)
332                         openSDB();
333                 else
334                         openDB();
335
336                 try {
337
338                         RuralHouse rhn = (RuralHouse) db.queryByExample(
339                                         new RuralHouse(rh.getHouseName(), null, null, null, null))
340                                         .next();
341                         if (rhn.overlapsWith(firstDay, lastDay) != null)
342                                 throw new OverlappingOfferExists();
343                         else
344                                 return false;
345                 } finally {
346                         db.close();
347                 }
348         }
349
350         public static ObjectContainer getContainer() {
351                 return db;
352         }
353
354         public void close() {
355                 db.close();
356                 System.out.println("DataBase closed");
357         }
358
359         public String toString() {
360                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
361         }
362
363         /**
364          * @param usr
365          * @param pwd
366          * @return
367          * @throws RemoteException
368          * @throws Exception
369          */
370         public Vector<Account> getAccount(String usr, String pwd)
371                         throws RemoteException, Exception {
372
373                 if (c.isDatabaseLocal() == false)
374                         openSDB();
375                 else
376                         openDB();
377
378                 try {
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());
384                         return accounts;
385                 } finally {
386                         db.close();
387                 }
388         }
389
390         /**
391          * @param rh
392          */
393         public boolean storeRuralHouses(RuralHouse rh) {
394
395                 if (c.isDatabaseLocal() == false)
396                         openSDB();
397                 else
398                         openDB();
399
400                 boolean stored = false;
401                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
402                                 null);
403                 try {
404                         ObjectSet<Owner> result = db.queryByExample(house);
405                         if (result.isEmpty()) {
406                                 db.store(rh);
407                                 db.commit();
408                                 stored = true;
409                         }
410                 } finally {
411                         db.close();
412                 }
413                 return stored;
414         }
415
416         public void removeHouse(RuralHouse rh, Owner owner) {
417
418                 if (c.isDatabaseLocal() == false)
419                         openSDB();
420                 else
421                         openDB();
422                 try {
423                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
424                         if (!result.isEmpty()) {
425                                 RuralHouse found = (RuralHouse) result.get(0);
426                                 // db.delete(found.getOwner());
427                                 db.store(owner);
428                                 db.delete(found);
429                                 db.commit();
430                         }
431                 } catch (Exception exc) {
432                         exc.printStackTrace();
433                 } finally {
434                         db.close();
435                 }
436
437         }
438
439         public Vector<RuralHouse> getRuralHousesByTown(String town) {
440                 RuralHouse rh = new RuralHouse(null, null, null, town, null);
441
442                 if (c.isDatabaseLocal() == false)
443                         openSDB();
444                 else
445                         openDB();
446
447                 try {
448                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
449                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
450                         while (result.hasNext())
451                                 ruralHouses.add(result.next());
452                         return ruralHouses;
453                 } finally {
454                         db.close();
455                 }
456
457         }
458
459         public RuralHouse getRuralHouseByName(String name) {
460                 RuralHouse rh = new RuralHouse(name, null, null, null, null);
461
462                 if (c.isDatabaseLocal() == false)
463                         openSDB();
464                 else
465                         openDB();
466
467                 try {
468                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
469                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
470                         while (result.hasNext())
471                                 ruralHouses.add(result.next());
472                         db.close();
473                         if (!ruralHouses.isEmpty())
474                                 return ruralHouses.get(0);
475                         else
476                                 return null;
477                 } catch (NullPointerException e) {
478                         return null;
479                 }
480
481         }
482
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)
488                         openSDB();
489                 else
490                         openDB();
491
492                 try {
493                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
494                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
495                         while (result.hasNext())
496                                 ruralHouses.add(result.next());
497                         db.close();
498                         return ruralHouses;
499                 } catch (NullPointerException e) {
500                         return null;
501                 }
502
503         }
504 }