minor changes
[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(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
114                         jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
115                         jon.setBankAccount("1349 5677 21 2133567777");
116                         alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
117                                         5);
118                         alfredo.addRuralHouse(4, "", "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.getHouseNumber(),
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.getHouseNumber(),
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
238                 return bok;
239         }
240
241         /**
242          * This method existing owners
243          * 
244          */
245         public Vector<Owner> getOwners() throws RemoteException, Exception {
246
247                 if (c.isDatabaseLocal() == false)
248                         openSDB();
249                 else
250                         openDB();
251
252                 try {
253                         Owner proto = new Owner(null, null);
254                         ObjectSet result = db.queryByExample(proto);
255                         Vector<Owner> owners = new Vector<Owner>();
256                         while (result.hasNext())
257                                 owners.add((Owner) result.next());
258                         return owners;
259                 } finally {
260                         db.close();
261                 }
262         }
263
264         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
265                         Exception {
266
267                 if (c.isDatabaseLocal() == false)
268                         openSDB();
269                 else
270                         openDB();
271
272                 try {
273                         RuralHouse proto = new RuralHouse(0, null, null, null, null);
274                         ObjectSet result = db.queryByExample(proto);
275                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
276                         while (result.hasNext())
277                                 ruralHouses.add((RuralHouse) result.next());
278                         return ruralHouses;
279                 } finally {
280                         db.close();
281                 }
282         }
283
284         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
285                         Date lastDay) throws RemoteException, OverlappingOfferExists {
286
287                 if (c.isDatabaseLocal() == false)
288                         openSDB();
289                 else
290                         openDB();
291
292                 try {
293
294                         RuralHouse rhn = (RuralHouse) db
295                                         .queryByExample(
296                                                         new RuralHouse(rh.getHouseNumber(), null, null,
297                                                                         null, null)).next();
298                         if (rhn.overlapsWith(firstDay, lastDay) != null)
299                                 throw new OverlappingOfferExists();
300                         else
301                                 return false;
302                 } finally {
303                         db.close();
304                 }
305         }
306
307         public static ObjectContainer getContainer() {
308                 return db;
309         }
310
311         public void close() {
312                 db.close();
313                 System.out.println("DataBase closed");
314         }
315
316         public String toString() {
317                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
318         }
319
320         /**
321          * @param usr
322          * @param pwd
323          * @return
324          * @throws RemoteException
325          * @throws Exception
326          */
327         public Vector<Account> getAccount(String usr, String pwd)
328                         throws RemoteException, Exception {
329
330                 if (c.isDatabaseLocal() == false)
331                         openSDB();
332                 else
333                         openDB();
334
335                 try {
336                         Account proto = new Account(usr, pwd, new Owner(null, null));
337                         ObjectSet<Account> result = db.queryByExample(proto);
338                         Vector<Account> accounts = new Vector<Account>();
339                         while (result.hasNext())
340                                 accounts.add((Account) result.next());
341                         return accounts;
342                 } finally {
343                         db.close();
344                 }
345         }
346
347         /**
348          * @param rh
349          */
350         public boolean storeRuralHouses(RuralHouse rh) {
351
352                 if (c.isDatabaseLocal() == false)
353                         openSDB();
354                 else
355                         openDB();
356
357                 boolean stored = false;
358                 RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null,
359                                 null, null);
360                 try {
361                         ObjectSet<Owner> result = db.queryByExample(house);
362                         if (result.isEmpty()) {
363                                 db.store(rh);
364                                 db.commit();
365                                 stored = true;
366                         }
367                 } finally {
368                         db.close();
369                 }
370                 return stored;
371         }
372
373         public void removeHouse(int houseNumber) {
374
375                 if (c.isDatabaseLocal() == false)
376                         openSDB();
377                 else
378                         openDB();
379
380                 RuralHouse house = new RuralHouse(houseNumber, null, null, null, null);
381                 try {
382                         ObjectSet<RuralHouse> result = db.queryByExample(house);
383                         if (!result.isEmpty()) {
384                                 RuralHouse found = (RuralHouse) result.get(0);
385                                 db.delete(found);
386                                 db.commit();
387                         }
388                 } catch (Exception exc) {
389                         exc.printStackTrace();
390                 } finally {
391                         db.close();
392                 }
393
394         }
395
396         public Vector<RuralHouse> getRuralHousesByTown(String town) {
397                 RuralHouse rh = new RuralHouse(0, null, null, town, null);
398
399                 if (c.isDatabaseLocal() == false)
400                         openSDB();
401                 else
402                         openDB();
403
404                 try {
405                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
406                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
407                         while (result.hasNext())
408                                 ruralHouses.add(result.next());
409                         return ruralHouses;
410                 } finally {
411                         db.close();
412                 }
413
414         }
415 }