error correction. minor error
[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.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", "Ezkio", 3, 3, 3, 3, 3);
116                         jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
117                         jon.setBankAccount("1349 5677 21 2133567777");
118                         alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
119                                         5);
120                         alfredo.addRuralHouse("Murgoitz", "", "Murgia", 6, 6, 6, 6, 6);
121                         alfredo.setBankAccount("4144 0087 23 9700002133");
122                         Account jonAcc = new Account("userJon", "passJon", jon);
123                         Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
124                                         alfredo);
125                         db.store(jon);
126                         db.store(alfredo);
127                         db.store(jonAcc);
128                         db.store(alfredoAcc);
129                         db.commit();
130                 } finally {
131                         db.close();
132                 }
133         }
134
135         public void deleteDB() {
136
137                 if (c.isDatabaseLocal() == false)
138                         openSDB();
139                 else
140                         openDB();
141
142                 try {
143                         Owner proto = new Owner(null, null);
144                         ObjectSet result = db.queryByExample(proto);
145                         Vector<Owner> owners = new Vector<Owner>();
146                         while (result.hasNext()) {
147                                 Owner o = (Owner) result.next();
148                                 System.out.println("Deleted owner: " + o.toString());
149                                 db.delete(o);
150                         }
151                         db.commit();
152                 } finally {
153                         db.close();
154                 }
155         }
156         
157         
158         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
159                         Date lastDay, float price) throws RemoteException, Exception {
160                 Offer o = null;
161
162                 if (c.isDatabaseLocal() == false)
163                         openSDB();
164                 else
165                         openDB();
166
167                 try {
168
169                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
170                                         null, null, null, null);
171                         ObjectSet result = db.queryByExample(proto);
172                         RuralHouse rh = (RuralHouse) result.next();
173                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
174                                         lastDay, price);
175                         db.store(theDB4oManagerAux); // To store the new value for
176                                                                                         // offerNumber
177                         db.store(o);
178                         db.commit();
179
180                 } catch (com.db4o.ext.ObjectNotStorableException e) {
181                         System.out
182                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
183                 } finally {
184                         db.close();
185                 }
186                 return o;
187         }
188
189         @SuppressWarnings("finally")
190         public Offer modifyOffer(Offer offer) throws RemoteException, Exception {
191                 if (c.isDatabaseLocal() == false)
192                         openSDB();
193                 else
194                         openDB();
195
196                 try {
197
198                         
199                         db.store(offer);
200                         db.commit();
201
202                 } catch (com.db4o.ext.ObjectNotStorableException e) {
203                         System.out
204                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
205                 } finally {
206                         db.close();
207                 }
208                 return offer;
209         }
210         
211         @SuppressWarnings("finally")
212         public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception {
213                 if (c.isDatabaseLocal() == false)
214                         openSDB();
215                 else
216                         openDB();
217
218                 try {
219
220                         db.store(rh);
221                         db.delete(offer);
222                         db.commit();
223
224                 } catch (com.db4o.ext.ObjectNotStorableException e) {
225                         System.out
226                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
227                 } finally {
228                         db.close();
229                 }
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(),
257                                         null, ruralHouse.getDescription(), ruralHouse.getDistrict(),
258                                         null);
259                         ObjectSet result = db.queryByExample(proto);
260                         RuralHouse rh = (RuralHouse) result.next();
261
262                         Offer offer;
263                         offer = rh.findOffer(firstDate, lastDate);
264
265                         if (offer != null) {
266                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
267                                                 bookTelephoneNumber);
268                                 db.store(theDB4oManagerAux); // To store the new value for
269                                                                                                 // bookingNumber
270                                 db.store(offer);
271                                 db.commit();
272                                 bok = offer.getBooking();
273                         }
274
275                 } catch (com.db4o.ext.ObjectNotStorableException e) {
276                         System.out
277                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
278                 } catch (Exception exc) {
279                         exc.printStackTrace();
280                 } finally {
281                         db.close();
282                 }
283                 return bok;
284         }
285
286         /**
287          * This method existing owners
288          * 
289          */
290         public Vector<Owner> getOwners() throws RemoteException, Exception {
291
292                 if (c.isDatabaseLocal() == false)
293                         openSDB();
294                 else
295                         openDB();
296
297                 try {
298                         Owner proto = new Owner(null, null);
299                         ObjectSet result = db.queryByExample(proto);
300                         Vector<Owner> owners = new Vector<Owner>();
301                         while (result.hasNext())
302                                 owners.add((Owner) result.next());
303                         return owners;
304                 } finally {
305                         db.close();
306                 }
307         }
308
309         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
310                         Exception {
311
312                 if (c.isDatabaseLocal() == false)
313                         openSDB();
314                 else
315                         openDB();
316
317                 try {
318                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
319                         ObjectSet result = db.queryByExample(proto);
320                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
321                         while (result.hasNext())
322                                 ruralHouses.add((RuralHouse) result.next());
323                         return ruralHouses;
324                 } finally {
325                         db.close();
326                 }
327         }
328
329         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
330                         Date lastDay) throws RemoteException, OverlappingOfferExists {
331
332                 if (c.isDatabaseLocal() == false)
333                         openSDB();
334                 else
335                         openDB();
336
337                 try {
338
339                         RuralHouse rhn = (RuralHouse) db
340                                         .queryByExample(
341                                                         new RuralHouse(rh.getHouseName(), null, null,
342                                                                         null, null)).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,
404                                 null, 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
419         public void removeHouse(RuralHouse rh, Owner owner) {
420
421                 if (c.isDatabaseLocal() == false)
422                         openSDB();
423                 else
424                         openDB();               
425                 try {
426                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
427                         if (!result.isEmpty()) {
428                                 RuralHouse found = (RuralHouse) result.get(0);
429 //                              db.delete(found.getOwner());
430                                 db.store(owner);
431                                 db.delete(found);
432                                 db.commit();
433                         }
434                 } catch (Exception exc) {
435                         exc.printStackTrace();
436                 } finally {
437                         db.close();
438                 }
439
440         }
441         
442         public Vector<RuralHouse> getRuralHousesByTown(String town){
443                 RuralHouse rh = new RuralHouse(null,null,null,town,null);
444                 
445                 if (c.isDatabaseLocal() == false)
446                         openSDB();
447                 else
448                         openDB();
449
450                 try {
451                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
452                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
453                         while (result.hasNext())
454                                 ruralHouses.add(result.next());
455                         return ruralHouses;
456                 } finally {
457                         db.close();
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                         return ruralHouses.get(0);
476                 } catch (NullPointerException e){
477                         return null;
478                 }
479                 
480                 
481                 
482         }
483         
484         public Vector<RuralHouse> getRuralHouses(String town,int nBed , int nKit, 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                 
505         }
506 }