Started creating the booking interface for the owners.
[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.LinkedList;
9 import java.util.List;
10 import java.util.ListIterator;
11 import java.util.Vector;
12
13 import com.db4o.Db4oEmbedded;
14 import com.db4o.ObjectContainer;
15 import com.db4o.ObjectSet;
16 import com.db4o.config.EmbeddedConfiguration;
17 import com.db4o.cs.Db4oClientServer;
18 import com.db4o.cs.config.ClientConfiguration;
19 import com.db4o.query.Predicate;
20
21 import configuration.ConfigXML;
22 import domain.Account;
23 import domain.Administrator;
24 import domain.Booking;
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.queryByExample(DB4oManagerAux.class);
73                         ListIterator<DB4oManagerAux> listIter = res.listIterator();
74                         if (listIter.hasNext())
75                                 theDB4oManagerAux = (DB4oManagerAux) res.next();
76                 }
77         }
78
79         private static void openDB() {
80                 configuration = Db4oEmbedded.newConfiguration();
81                 configuration.common().activationDepth(c.getActivationDepth());
82                 configuration.common().updateDepth(c.getUpdateDepth());
83                 configuration.common().objectClass(Owner.class).cascadeOnUpdate(true);
84                 configuration.common().objectClass(Owner.class).cascadeOnDelete(true);
85                 configuration.common().objectClass(RuralHouse.class).cascadeOnUpdate(true);
86                 configuration.common().objectClass(Account.class).cascadeOnUpdate(true);
87                 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
88         }
89
90         private void openSDB() {
91
92                 configurationCS = Db4oClientServer.newClientConfiguration();
93                 configurationCS.common().activationDepth(c.getActivationDepth());
94                 configurationCS.common().updateDepth(c.getUpdateDepth());
95                 configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
96                 db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
97                                 c.getDatabasePort(), c.getUser(), c.getPassword());
98
99         }
100
101         class DB4oManagerAux {
102                 int bookingNumber;
103                 int offerNumber;
104
105                 DB4oManagerAux(int bookingNumber, int offerNumber) {
106                         this.bookingNumber = bookingNumber;
107                         this.offerNumber = offerNumber;
108                 }
109         }
110
111         public static DB4oManager getInstance() throws Exception {
112                 if (theDB4oManager == null)
113                         theDB4oManager = new DB4oManager();
114                 return theDB4oManager;
115         }
116
117         public void initializeDB() {
118
119                 try {
120                         Owner jon = new Owner("Jon");
121                         Owner alfredo = new Owner("Alfredo");
122                         jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3,
123                                         3, 3);
124                         jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4,
125                                         4, 4, 4);
126                         jon.setBankAccount("1349 5677 21 2133567777");
127                         alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, 5,
128                                         5, 5, 5);
129                         
130                         alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6);
131                         alfredo.setBankAccount("4144 0087 23 9700002133");
132                         Account jonAcc = new Account("1", "1", jon);
133                         Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
134                                         alfredo);
135
136                         Account admin = new Account("admin","admin", true);
137                         db.store(Administrator.giveAdmin());
138                         db.store(jonAcc);
139                         db.store(alfredoAcc);
140                         db.store(admin);
141                         db.commit();
142                 } finally {
143                         db.close();     
144                 }
145         }
146
147         public void deleteDB() {
148
149                 if (c.isDatabaseLocal() == false)
150                         openSDB();
151                 else
152                         openDB();
153
154                 try {
155                         Owner proto = new Owner(null, null);
156                         ObjectSet<Owner> result = db.queryByExample(proto);
157                         while (result.hasNext()) {
158                                 Owner o = (Owner) result.next();
159                                 System.out.println("Deleted owner: " + o.toString());
160                                 db.delete(o);
161                         }
162                         db.commit();
163                 } finally {
164                         db.close();
165                 }
166         }
167
168         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
169                         Date lastDay, float price) throws RemoteException, Exception {
170                 Offer o = null;
171
172                 if (c.isDatabaseLocal() == false)
173                         openSDB();
174                 else
175                         openDB();
176
177                 try {
178
179                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
180                                         null, null, null);
181                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
182                         RuralHouse rh = (RuralHouse) result.next();
183                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
184                                         lastDay, price);
185                         db.store(theDB4oManagerAux); // To store the new value for
186                                                                                         // offerNumber
187                         db.store(o);
188                         db.commit();
189
190                 } catch (com.db4o.ext.ObjectNotStorableException e) {
191                         System.out
192                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
193                 } finally {
194                         db.close();
195                 }
196                 return o;
197         }
198
199         
200         public void deleteOffer(Offer offer) throws RemoteException,
201                         Exception {
202                 if (c.isDatabaseLocal() == false)
203                         openSDB();
204                 else
205                         openDB();
206
207                 try {
208                         ObjectSet<Offer> of = db.queryByExample(offer);
209                         RuralHouse rh =of.get(0).getRuralHouse();
210                         System.out.println(rh.offers.remove(of.get(0)));
211                         db.store(rh);
212                         db.commit();
213
214                 } catch (com.db4o.ext.ObjectNotStorableException e) {
215                         System.out
216                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
217                 } finally {
218                         db.close();
219                 }
220         }
221
222         
223         @SuppressWarnings("static-access")
224         public LinkedList<RuralHouse>[] getAdminData(){
225                 if (c.isDatabaseLocal() == false)
226                         openSDB();
227                 else
228                         openDB();
229                 @SuppressWarnings("unchecked")
230                 LinkedList<RuralHouse>[] ret =  new LinkedList[2];
231                 
232                 try {
233                         
234                         List<Administrator> admL = db.query(new Predicate<Administrator>(){
235                                 /**
236                                  * 
237                                  */
238                                 private static final long serialVersionUID = 1L;
239
240                                 public boolean match(Administrator admin){
241                                         return true;
242                                 }
243                         });
244                         
245                         ret[0] = admL.get(0).getAddRequest();
246                         ret[1] = admL.get(0).getRemoveRequest();
247                         return ret;
248                 }
249                 finally {
250                         db.close();
251                         
252                 }
253                 
254         }
255         
256         @SuppressWarnings("static-access")
257         public void storeAdmin() {
258                 if (c.isDatabaseLocal() == false)
259                         openSDB();
260                 else
261                         openDB();               
262                 try {
263                         
264                         List<Administrator> admL = db.query(new Predicate<Administrator>(){
265                                 /**
266                                  * 
267                                  */
268                                 private static final long serialVersionUID = 1L;
269
270                                 public boolean match(Administrator admin){
271                                         return true;
272                                 }
273                         });
274                         
275                         admL.get(0).setAddRequest(Administrator.getAddRequest());
276                         admL.get(0).setRemoveRequest(Administrator.getRemoveRequest());
277                         db.commit();
278                         
279                 }
280                 catch(Exception e){
281                         
282                 }
283                 finally {
284                         db.close();
285                 }
286                 
287                 
288         }
289         
290         
291         /**
292          * This method creates a book with a corresponding parameters
293          * 
294          * @param First
295          *            day, last day, house number and telephone
296          * @return a book
297          */
298         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
299                         Date lastDate, String bookTelephoneNumber)
300                         throws OfferCanNotBeBooked {
301
302                 if (c.isDatabaseLocal() == false)
303                         openSDB();
304                 else
305                         openDB();
306
307                 Booking bok = null;
308                 
309
310                 try {
311
312                         if (c.isDatabaseLocal() == false)
313                                 openSDB();
314
315                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
316                                         ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
317                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
318                         RuralHouse rh = (RuralHouse) result.next();
319
320                         Offer offer;
321                         offer = rh.findOffer(firstDate, lastDate);
322
323                         if (offer != null) {
324                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
325                                                 bookTelephoneNumber);
326                                 db.store(theDB4oManagerAux); // To store the new value for
327                                                                                                 // bookingNumber
328                                 db.store(offer);
329                                 db.commit();
330                                 bok = offer.getBooking();
331                         }
332
333                 } catch (com.db4o.ext.ObjectNotStorableException e) {
334                         System.out
335                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
336                 } catch (Exception exc) {
337                         exc.printStackTrace();
338                 } finally {
339                         db.close();
340                 }
341                 return bok;
342         }
343
344         /**
345          * This method existing owners
346          * 
347          */
348         public Vector<Owner> getOwners() throws RemoteException, Exception {
349
350                 if (c.isDatabaseLocal() == false)
351                         openSDB();
352                 else
353                         openDB();
354
355                 try {
356                         Owner proto = new Owner(null, null);
357                         ObjectSet<Owner> result = db.queryByExample(proto);
358                         Vector<Owner> owners = new Vector<Owner>();
359                         while (result.hasNext())
360                                 owners.add((Owner) result.next());
361                         return owners;
362                 } finally {
363                         db.close();
364                 }
365         }
366
367         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
368                         Exception {
369
370                 if (c.isDatabaseLocal() == false)
371                         openSDB();
372                 else
373                         openDB();
374
375                 try {
376                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
377                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
378                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
379                         while (result.hasNext())
380                                 ruralHouses.add((RuralHouse) result.next());
381                         return ruralHouses;
382                 } finally {
383                         db.close();
384                 }
385         }
386
387         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
388                         Date lastDay) throws RemoteException, OverlappingOfferExists {
389
390                 if (c.isDatabaseLocal() == false)
391                         openSDB();
392                 else
393                         openDB();
394
395                 try {
396
397                         RuralHouse rhn = (RuralHouse) db.queryByExample(
398                                         new RuralHouse(rh.getHouseName(), null, null, null, null))
399                                         .next();
400                         if (rhn.overlapsWith(firstDay, lastDay) != null)
401                                 throw new OverlappingOfferExists();
402                         else
403                                 return false;
404                 } finally {
405                         db.close();
406                 }
407         }
408
409         public static ObjectContainer getContainer() {
410                 return db;
411         }
412
413         public void close() {
414                 db.close();
415                 System.out.println("DataBase closed");
416         }
417
418         public String toString() {
419                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
420         }
421
422         /**
423          * @param usr
424          * @param pwd
425          * @return
426          * @throws RemoteException
427          * @throws Exception
428          */
429         public Vector<Account> getAccount(String usr, String pwd)
430                         throws RemoteException, Exception {
431
432                 if (c.isDatabaseLocal() == false)
433                         openSDB();
434                 else
435                         openDB();
436
437                 try {
438                         Account proto = new Account(usr, pwd, null);
439                         ObjectSet<Account> result = db.queryByExample(proto);
440                         Vector<Account> accounts = new Vector<Account>();
441                         while (result.hasNext())
442                                 accounts.add((Account) result.next());
443                         return accounts;
444                 } finally {
445                         db.close();
446                 }
447         }
448
449         /**
450          * @param rh
451          */
452         public boolean storeRuralHouses(RuralHouse rh) {
453
454                 if (c.isDatabaseLocal() == false)
455                         openSDB();
456                 else
457                         openDB();
458
459                 boolean stored = false;
460                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
461                                 null);
462                 try {
463                         ObjectSet<RuralHouse> result = db.queryByExample(house);
464                         if (result.isEmpty()) {
465                                 Owner ow = (Owner) db.queryByExample(rh.getOwner()).get(0);
466                                 rh.setOwner(ow);
467                                 ow.addRuralHouse(rh);
468                                 db.store(rh);
469                                 db.commit();
470                                 stored = true;
471                         } else {
472                                 db.delete(result.get(0));
473                                 db.store(rh);
474                                 db.commit();
475                                 stored = true;
476                         }
477                 } finally {
478                         db.close();
479                 }
480                 return stored;
481         }
482
483         public void removeHouse(RuralHouse rh, Owner owner) {
484
485                 if (c.isDatabaseLocal() == false)
486                         openSDB();
487                 else
488                         openDB();
489                 try {
490                         ObjectSet<Owner> result = db.queryByExample(owner);
491                         ObjectSet<RuralHouse> rhs = db.queryByExample(rh);
492                         if (!rhs.isEmpty()) {
493                                 Owner found =  result.get(0);
494                                 found.getRuralHouses().remove(rhs.get(0));
495                                 db.delete(rhs.get(0));
496                                 db.store(found);
497                                 db.commit();
498                         }
499                 } catch (Exception exc) {
500                         exc.printStackTrace();
501                 } finally {
502                         db.close();
503                 }
504
505         }
506
507
508         public Vector<RuralHouse> getRuralHouses(String name, String town,
509                         int nBed, int nKit, int nBath, int nPark, int nLiv) {
510                 HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
511                 RuralHouse rh = new RuralHouse(name, null, null, town, fea);
512                 if (c.isDatabaseLocal() == false)
513                         openSDB();
514                 else
515                         openDB();
516
517                 try {
518                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
519                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
520                         while (result.hasNext())
521                                 ruralHouses.add(result.next());
522                         db.close();
523                         return ruralHouses;
524                 } catch (NullPointerException e) {
525                         return null;
526                 }
527
528         }
529
530         
531 }