b46eafa30dab7bd2c34429bc6e6d18184e31f1ab
[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("Ezkio", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
114                         jon.addRuralHouse("Eskia", "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         @SuppressWarnings("finally")
134         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
135                         Date lastDay, float price) throws RemoteException, Exception {
136                 Offer o = null;
137
138                 if (c.isDatabaseLocal() == false)
139                         openSDB();
140                 else
141                         openDB();
142
143                 try {
144
145                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
146                                         null, null, null, null);
147                         ObjectSet result = db.queryByExample(proto);
148                         RuralHouse rh = (RuralHouse) result.next();
149                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
150                                         lastDay, price);
151                         db.store(theDB4oManagerAux); // To store the new value for
152                                                                                         // offerNumber
153                         db.store(o);
154                         db.commit();
155
156                 } catch (com.db4o.ext.ObjectNotStorableException e) {
157                         System.out
158                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
159                 } finally {
160                         db.close();
161                 }
162                 return o;
163         }
164
165         public void deleteDB() {
166
167                 if (c.isDatabaseLocal() == false)
168                         openSDB();
169                 else
170                         openDB();
171
172                 try {
173                         Owner proto = new Owner(null, null);
174                         ObjectSet result = db.queryByExample(proto);
175                         Vector<Owner> owners = new Vector<Owner>();
176                         while (result.hasNext()) {
177                                 Owner o = (Owner) result.next();
178                                 System.out.println("Deleted owner: " + o.toString());
179                                 db.delete(o);
180                         }
181                         db.commit();
182                 } finally {
183                         db.close();
184                 }
185         }
186
187         /**
188          * This method creates a book with a corresponding parameters
189          * 
190          * @param First
191          *            day, last day, house number and telephone
192          * @return a book
193          */
194         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
195                         Date lastDate, String bookTelephoneNumber)
196                         throws OfferCanNotBeBooked {
197
198                 if (c.isDatabaseLocal() == false)
199                         openSDB();
200                 else
201                         openDB();
202
203                 Booking bok = null;
204
205                 try {
206
207                         if (c.isDatabaseLocal() == false)
208                                 openSDB();
209
210                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
211                                         null, ruralHouse.getDescription(), ruralHouse.getTown(),
212                                         null);
213                         ObjectSet result = db.queryByExample(proto);
214                         RuralHouse rh = (RuralHouse) result.next();
215
216                         Offer offer;
217                         offer = rh.findOffer(firstDate, lastDate);
218
219                         if (offer != null) {
220                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
221                                                 bookTelephoneNumber);
222                                 db.store(theDB4oManagerAux); // To store the new value for
223                                                                                                 // bookingNumber
224                                 db.store(offer);
225                                 db.commit();
226                                 bok = offer.getBooking();
227                         }
228
229                 } catch (com.db4o.ext.ObjectNotStorableException e) {
230                         System.out
231                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
232                 } catch (Exception exc) {
233                         exc.printStackTrace();
234                 } finally {
235                         db.close();
236                 }
237                 return bok;
238         }
239
240         /**
241          * This method existing owners
242          * 
243          */
244         public Vector<Owner> getOwners() throws RemoteException, Exception {
245
246                 if (c.isDatabaseLocal() == false)
247                         openSDB();
248                 else
249                         openDB();
250
251                 try {
252                         Owner proto = new Owner(null, null);
253                         ObjectSet result = db.queryByExample(proto);
254                         Vector<Owner> owners = new Vector<Owner>();
255                         while (result.hasNext())
256                                 owners.add((Owner) result.next());
257                         return owners;
258                 } finally {
259                         db.close();
260                 }
261         }
262
263         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
264                         Exception {
265
266                 if (c.isDatabaseLocal() == false)
267                         openSDB();
268                 else
269                         openDB();
270
271                 try {
272                         RuralHouse proto = new RuralHouse(null, null, null, null, null);
273                         ObjectSet result = db.queryByExample(proto);
274                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
275                         while (result.hasNext())
276                                 ruralHouses.add((RuralHouse) result.next());
277                         return ruralHouses;
278                 } finally {
279                         db.close();
280                 }
281         }
282
283         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
284                         Date lastDay) throws RemoteException, OverlappingOfferExists {
285
286                 if (c.isDatabaseLocal() == false)
287                         openSDB();
288                 else
289                         openDB();
290
291                 try {
292
293                         RuralHouse rhn = (RuralHouse) db
294                                         .queryByExample(
295                                                         new RuralHouse(rh.getHouseName(), null, null,
296                                                                         null, null)).next();
297                         if (rhn.overlapsWith(firstDay, lastDay) != null)
298                                 throw new OverlappingOfferExists();
299                         else
300                                 return false;
301                 } finally {
302                         db.close();
303                 }
304         }
305
306         public static ObjectContainer getContainer() {
307                 return db;
308         }
309
310         public void close() {
311                 db.close();
312                 System.out.println("DataBase closed");
313         }
314
315         public String toString() {
316                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
317         }
318
319         /**
320          * @param usr
321          * @param pwd
322          * @return
323          * @throws RemoteException
324          * @throws Exception
325          */
326         public Vector<Account> getAccount(String usr, String pwd)
327                         throws RemoteException, Exception {
328
329                 if (c.isDatabaseLocal() == false)
330                         openSDB();
331                 else
332                         openDB();
333
334                 try {
335                         Account proto = new Account(usr, pwd, new Owner(null, null));
336                         ObjectSet<Account> result = db.queryByExample(proto);
337                         Vector<Account> accounts = new Vector<Account>();
338                         while (result.hasNext())
339                                 accounts.add((Account) result.next());
340                         return accounts;
341                 } finally {
342                         db.close();
343                 }
344         }
345
346         /**
347          * @param rh
348          */
349         public boolean storeRuralHouses(RuralHouse rh) {
350
351                 if (c.isDatabaseLocal() == false)
352                         openSDB();
353                 else
354                         openDB();
355
356                 boolean stored = false;
357                 RuralHouse house = new RuralHouse(rh.getHouseName(), null, null,
358                                 null, null);
359                 try {
360                         ObjectSet<Owner> result = db.queryByExample(house);
361                         if (result.isEmpty()) {
362                                 db.store(rh);
363                                 db.commit();
364                                 stored = true;
365                         }
366                 } finally {
367                         db.close();
368                 }
369                 return stored;
370         }
371
372         public void removeHouse(String houseName) {
373
374                 if (c.isDatabaseLocal() == false)
375                         openSDB();
376                 else
377                         openDB();               
378                 RuralHouse house = new RuralHouse(houseName, null, null, null, null);
379                 try {
380                         ObjectSet<RuralHouse> result = db.queryByExample(house);
381                         if (!result.isEmpty()) {
382                                 RuralHouse found = (RuralHouse) result.get(0);
383                                 db.delete(found);
384                                 db.commit();
385                         }
386                 } catch (Exception exc) {
387                         exc.printStackTrace();
388                 } finally {
389                         db.close();
390                 }
391
392         }
393         
394         public Vector<RuralHouse> getRuralHousesByTown(String town){
395                 RuralHouse rh = new RuralHouse(null,null,null,town,null);
396                 
397                 if (c.isDatabaseLocal() == false)
398                         openSDB();
399                 else
400                         openDB();
401
402                 try {
403                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
404                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
405                         while (result.hasNext())
406                                 ruralHouses.add(result.next());
407                         return ruralHouses;
408                 } finally {
409                         db.close();
410                 }
411
412         }
413         public RuralHouse getRuralHouseByName(String name){
414                 RuralHouse rh = new RuralHouse(name,null,null,null,null);
415                 
416                 if (c.isDatabaseLocal() == false)
417                         openSDB();
418                 else
419                         openDB();
420                 
421                 try{
422                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
423                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
424                         while(result.hasNext())
425                                 ruralHouses.add(result.next());
426                         db.close();
427                         return ruralHouses.get(0);
428                 } catch (NullPointerException e){
429                         return null;
430                 }
431                 
432                 
433                 
434         }
435 }