GUI added to show list of available houses
[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.sql.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.Offer;
22 //import dataModel.Offer;
23 import domain.Owner;
24 import domain.RuralHouse;
25 import exceptions.OfferCanNotBeBooked;
26 import exceptions.OverlappingOfferExists;
27
28 public class DB4oManager {
29
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
34                                                                         // serialized
35         private int offerNumber = 0; // if it is "static" then it is not serialized
36         private static DB4oManager theDB4oManager = null;
37
38         private static DB4oManagerAux theDB4oManagerAux;
39         static ConfigXML c;
40
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());
47
48                 if ((c.getDataBaseOpenMode().equals("initialize"))
49                                 && (c.isDatabaseLocal()))
50                         new File(c.getDb4oFilename()).delete();
51
52                 if (c.isDatabaseLocal()) {
53                         openDB();
54                         System.out.println("DataBase opened");
55                 } else // c.isDatabaseLocal==false
56                 {
57                         openSDB();
58                         System.out.println("Remote DataBase opened");
59                 }
60                 if (c.getDataBaseOpenMode().equals("initialize")) {
61                         initializeDB();
62                         System.out.println("DataBase initialized");
63                 } else // c.getDataBaseOpenMode().equals("open")
64
65                 {
66                         ObjectSet res = db.queryByExample(DB4oManagerAux.class);
67                         ListIterator listIter = res.listIterator();
68                         if (listIter.hasNext())
69                                 theDB4oManagerAux = (DB4oManagerAux) res.next();
70                 }
71         }
72
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());
79         }
80
81         private void openSDB() {
82
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());
89
90         }
91
92         class DB4oManagerAux {
93                 int bookingNumber;
94                 int offerNumber;
95
96                 DB4oManagerAux(int bookingNumber, int offerNumber) {
97                         this.bookingNumber = bookingNumber;
98                         this.offerNumber = offerNumber;
99                 }
100         }
101
102         public static DB4oManager getInstance() throws Exception {
103                 if (theDB4oManager == null)
104                         theDB4oManager = new DB4oManager();
105                 return theDB4oManager;
106         }
107
108         public void initializeDB() {
109
110                 try {
111                         Owner jon = new Owner("Jon");
112                         Owner alfredo = new Owner("Alfredo");
113                         jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
114                         jon.addRuralHouse("Eskiatze", "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,
117                                         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",
122                                         alfredo);
123                         db.store(jon);
124                         db.store(alfredo);
125                         db.store(jonAcc);
126                         db.store(alfredoAcc);
127                         db.commit();
128                 } finally {
129                         db.close();
130                 }
131         }
132
133         public void deleteDB() {
134
135                 if (c.isDatabaseLocal() == false)
136                         openSDB();
137                 else
138                         openDB();
139
140                 try {
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());
147                                 db.delete(o);
148                         }
149                         db.commit();
150                 } finally {
151                         db.close();
152                 }
153         }
154         
155         
156         @SuppressWarnings("finally")
157         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
158                         Date lastDay, float price) throws RemoteException, Exception {
159                 Offer o = null;
160
161                 if (c.isDatabaseLocal() == false)
162                         openSDB();
163                 else
164                         openDB();
165
166                 try {
167
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,
173                                         lastDay, price);
174                         db.store(theDB4oManagerAux); // To store the new value for
175                                                                                         // offerNumber
176                         db.store(o);
177                         db.commit();
178
179                 } catch (com.db4o.ext.ObjectNotStorableException e) {
180                         System.out
181                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
182                 } finally {
183                         db.close();
184                 }
185                 return o;
186         }
187
188         @SuppressWarnings("finally")
189         public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
190                 if (c.isDatabaseLocal() == false)
191                         openSDB();
192                 else
193                         openDB();
194
195                 try {
196
197                         
198                         db.store(offer);
199                         db.commit();
200
201                 } catch (com.db4o.ext.ObjectNotStorableException e) {
202                         System.out
203                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
204                 } finally {
205                         db.close();
206                 }
207                 return offer;
208         }
209         
210         @SuppressWarnings("finally")
211         public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception {
212                 if (c.isDatabaseLocal() == false)
213                         openSDB();
214                 else
215                         openDB();
216
217                 try {
218
219                         db.store(rh);
220                         db.delete(offer);
221                         db.commit();
222
223                 } catch (com.db4o.ext.ObjectNotStorableException e) {
224                         System.out
225                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
226                 } finally {
227                         db.close();
228                 }
229         }
230         
231
232         /**
233          * This method creates a book with a corresponding parameters
234          * 
235          * @param First
236          *            day, last day, house number and telephone
237          * @return a book
238          */
239         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
240                         Date lastDate, String bookTelephoneNumber)
241                         throws OfferCanNotBeBooked {
242
243                 if (c.isDatabaseLocal() == false)
244                         openSDB();
245                 else
246                         openDB();
247
248                 Booking bok = null;
249
250                 try {
251
252                         if (c.isDatabaseLocal() == false)
253                                 openSDB();
254
255                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
256                                         null, ruralHouse.getDescription(), ruralHouse.getTown(),
257                                         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
339                                         .queryByExample(
340                                                         new RuralHouse(rh.getHouseName(), null, null,
341                                                                         null, null)).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,
403                                 null, 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
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         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                         return ruralHouses.get(0);
475                 } catch (NullPointerException e){
476                         return null;
477                 }
478                 
479                 
480                 
481         }
482 }