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