15b2ad4ffe282b8f3374c994120e97790ec7a792
[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                         
146
147                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
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                         return o;
163                 }
164         }
165
166         public void deleteDB() {
167
168                 if (c.isDatabaseLocal() == false)
169                         openSDB();
170                 else
171                         openDB();
172                 
173                 try {
174                         Owner proto = new Owner(null, null);
175                         ObjectSet result = db.queryByExample(proto);
176                         Vector<Owner> owners = new Vector<Owner>();
177                         while (result.hasNext()) {
178                                 Owner o = (Owner) result.next();
179                                 System.out.println("Deleted owner: " + o.toString());
180                                 db.delete(o);
181                         }
182                         db.commit();
183                 } finally {
184                         db.close();
185                 }
186         }
187
188         /**
189          * This method creates a book with a corresponding parameters
190          * 
191          * @param First
192          *            day, last day, house number and telephone
193          * @return a book
194          */
195         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
196                         Date lastDate, String bookTelephoneNumber)
197                         throws OfferCanNotBeBooked {
198                 
199                 if (c.isDatabaseLocal() == false)
200                         openSDB();
201                 else
202                         openDB();
203                 
204                 Booking bok = null;
205                 
206                 try {
207
208                         if (c.isDatabaseLocal() == false)
209                                 openSDB();
210
211                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
212                                         null, ruralHouse.getDescription(), ruralHouse.getTown(),
213                                         null);
214                         ObjectSet result = db.queryByExample(proto);
215                         RuralHouse rh = (RuralHouse) result.next();
216
217                         Offer offer;
218                         offer = rh.findOffer(firstDate, lastDate);
219
220                         if (offer != null) {
221                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
222                                                 bookTelephoneNumber);
223                                 db.store(theDB4oManagerAux); // To store the new value for
224                                                                                                 // bookingNumber
225                                 db.store(offer);
226                                 db.commit();
227                                 bok = offer.getBooking();
228                         }
229
230                 } catch (com.db4o.ext.ObjectNotStorableException e) {
231                         System.out
232                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
233                 } catch (Exception exc) {
234                         exc.printStackTrace();
235                 }
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(0, 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
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 }