Minor error correction and some bugs, alongside with modify offers quality of life...
[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         
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         @SuppressWarnings("finally")
193         public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
194                 if (c.isDatabaseLocal() == false)
195                         openSDB();
196                 else
197                         openDB();
198
199                 try {
200
201                         db.store(offer);
202                         db.commit();
203
204                 } catch (com.db4o.ext.ObjectNotStorableException e) {
205                         System.out
206                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
207                 } finally {
208                         db.close();
209                 }
210                 return offer;
211         }
212
213         @SuppressWarnings("finally")
214         public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException,
215                         Exception {
216                 if (c.isDatabaseLocal() == false)
217                         openSDB();
218                 else
219                         openDB();
220
221                 try {
222
223                         db.store(rh);
224                         db.delete(offer);
225                         db.commit();
226
227                 } catch (com.db4o.ext.ObjectNotStorableException e) {
228                         System.out
229                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
230                 } finally {
231                         db.close();
232                 }
233         }
234
235         /**
236          * This method creates a book with a corresponding parameters
237          * 
238          * @param First
239          *            day, last day, house number and telephone
240          * @return a book
241          */
242         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
243                         Date lastDate, String bookTelephoneNumber)
244                         throws OfferCanNotBeBooked {
245
246                 if (c.isDatabaseLocal() == false)
247                         openSDB();
248                 else
249                         openDB();
250
251                 Booking bok = null;
252
253                 try {
254
255                         if (c.isDatabaseLocal() == false)
256                                 openSDB();
257
258                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
259                                         ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
260                         ObjectSet result = db.queryByExample(proto);
261                         RuralHouse rh = (RuralHouse) result.next();
262
263                         Offer offer;
264                         offer = rh.findOffer(firstDate, lastDate);
265
266                         if (offer != null) {
267                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
268                                                 bookTelephoneNumber);
269                                 db.store(theDB4oManagerAux); // To store the new value for
270                                                                                                 // bookingNumber
271                                 db.store(offer);
272                                 db.commit();
273                                 bok = offer.getBooking();
274                         }
275
276                 } catch (com.db4o.ext.ObjectNotStorableException e) {
277                         System.out
278                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
279                 } catch (Exception exc) {
280                         exc.printStackTrace();
281                 } finally {
282                         db.close();
283                 }
284                 return bok;
285         }
286
287         /**
288          * This method existing owners
289          * 
290          */
291         public Vector<Owner> getOwners() throws RemoteException, Exception {
292
293                 if (c.isDatabaseLocal() == false)
294                         openSDB();
295                 else
296                         openDB();
297
298                 try {
299                         Owner proto = new Owner(null, null);
300                         ObjectSet result = db.queryByExample(proto);
301                         Vector<Owner> owners = new Vector<Owner>();
302                         while (result.hasNext())
303                                 owners.add((Owner) result.next());
304                         return owners;
305                 } finally {
306                         db.close();
307                 }
308         }
309
310         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
311                         Exception {
312
313                 if (c.isDatabaseLocal() == false)
314                         openSDB();
315                 else
316                         openDB();
317
318                 try {
319                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
320                         ObjectSet result = db.queryByExample(proto);
321                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
322                         while (result.hasNext())
323                                 ruralHouses.add((RuralHouse) result.next());
324                         return ruralHouses;
325                 } finally {
326                         db.close();
327                 }
328         }
329
330         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
331                         Date lastDay) throws RemoteException, OverlappingOfferExists {
332
333                 if (c.isDatabaseLocal() == false)
334                         openSDB();
335                 else
336                         openDB();
337
338                 try {
339
340                         RuralHouse rhn = (RuralHouse) db.queryByExample(
341                                         new RuralHouse(rh.getHouseName(), null, null, null, null))
342                                         .next();
343                         if (rhn.overlapsWith(firstDay, lastDay) != null)
344                                 throw new OverlappingOfferExists();
345                         else
346                                 return false;
347                 } finally {
348                         db.close();
349                 }
350         }
351
352         public static ObjectContainer getContainer() {
353                 return db;
354         }
355
356         public void close() {
357                 db.close();
358                 System.out.println("DataBase closed");
359         }
360
361         public String toString() {
362                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
363         }
364
365         /**
366          * @param usr
367          * @param pwd
368          * @return
369          * @throws RemoteException
370          * @throws Exception
371          */
372         public Vector<Account> getAccount(String usr, String pwd)
373                         throws RemoteException, Exception {
374
375                 if (c.isDatabaseLocal() == false)
376                         openSDB();
377                 else
378                         openDB();
379
380                 try {
381                         Account proto = new Account(usr, pwd, new Owner(null, null));
382                         ObjectSet<Account> result = db.queryByExample(proto);
383                         Vector<Account> accounts = new Vector<Account>();
384                         while (result.hasNext())
385                                 accounts.add((Account) result.next());
386                         return accounts;
387                 } finally {
388                         db.close();
389                 }
390         }
391
392         /**
393          * @param rh
394          */
395         public boolean storeRuralHouses(RuralHouse rh) {
396
397                 if (c.isDatabaseLocal() == false)
398                         openSDB();
399                 else
400                         openDB();
401
402                 boolean stored = false;
403                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
404                                 null);
405                 try {
406                         ObjectSet<Owner> result = db.queryByExample(house);
407                         if (result.isEmpty()) {
408                                 db.store(rh);
409                                 db.commit();
410                                 stored = true;
411                         }
412                 } finally {
413                         db.close();
414                 }
415                 return stored;
416         }
417
418         public void removeHouse(RuralHouse rh, Owner owner) {
419
420                 if (c.isDatabaseLocal() == false)
421                         openSDB();
422                 else
423                         openDB();
424                 try {
425                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
426                         if (!result.isEmpty()) {
427                                 RuralHouse found = (RuralHouse) result.get(0);
428                                 // db.delete(found.getOwner());
429                                 db.store(owner);
430                                 db.delete(found);
431                                 db.commit();
432                         }
433                 } catch (Exception exc) {
434                         exc.printStackTrace();
435                 } finally {
436                         db.close();
437                 }
438
439         }
440
441         public Vector<RuralHouse> getRuralHousesByTown(String town) {
442                 RuralHouse rh = new RuralHouse(null, null, null, town, null);
443
444                 if (c.isDatabaseLocal() == false)
445                         openSDB();
446                 else
447                         openDB();
448
449                 try {
450                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
451                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
452                         while (result.hasNext())
453                                 ruralHouses.add(result.next());
454                         return ruralHouses;
455                 } finally {
456                         db.close();
457                 }
458
459         }
460
461         public RuralHouse getRuralHouseByName(String name) {
462                 RuralHouse rh = new RuralHouse(name, null, null, null, null);
463
464                 if (c.isDatabaseLocal() == false)
465                         openSDB();
466                 else
467                         openDB();
468
469                 try {
470                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
471                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
472                         while (result.hasNext())
473                                 ruralHouses.add(result.next());
474                         db.close();
475                         if (!ruralHouses.isEmpty())
476                                 return ruralHouses.get(0);
477                         else
478                                 return null;
479                 } catch (NullPointerException e) {
480                         return null;
481                 }
482
483         }
484
485         public Vector<RuralHouse> getRuralHouses(String town, int nBed, int nKit,
486                         int nBath, int nPark, int nLiv) {
487                 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
488                 RuralHouse rh = new RuralHouse(null, null, null, town, fea);
489                 if (c.isDatabaseLocal() == false)
490                         openSDB();
491                 else
492                         openDB();
493
494                 try {
495                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
496                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
497                         while (result.hasNext())
498                                 ruralHouses.add(result.next());
499                         db.close();
500                         return ruralHouses;
501                 } catch (NullPointerException e) {
502                         return null;
503                 }
504
505         }
506 }