Some minor changes and the logic for searching rural houses by the name of the town...
[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                         configuration = Db4oEmbedded.newConfiguration();
54                         configuration.common().activationDepth(c.getActivationDepth());
55                         configuration.common().updateDepth(c.getUpdateDepth());
56                         db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
57                         System.out.println("DataBase opened");
58                 } else // c.isDatabaseLocal==false
59                 {
60                         openObjectContainer();
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                 db = Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
80         }
81
82         private void openObjectContainer() {
83
84                 configurationCS = Db4oClientServer.newClientConfiguration();
85                 configurationCS.common().activationDepth(c.getActivationDepth());
86                 configurationCS.common().updateDepth(c.getUpdateDepth());
87                 configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
88                 db = Db4oClientServer.openClient(configurationCS, c.getDatabaseNode(),
89                                 c.getDatabasePort(), c.getUser(), c.getPassword());
90
91         }
92
93         class DB4oManagerAux {
94                 int bookingNumber;
95                 int offerNumber;
96
97                 DB4oManagerAux(int bookingNumber, int offerNumber) {
98                         this.bookingNumber = bookingNumber;
99                         this.offerNumber = offerNumber;
100                 }
101         }
102
103         public static DB4oManager getInstance() throws Exception {
104                 if (theDB4oManager == null)
105                         theDB4oManager = new DB4oManager();
106                 return theDB4oManager;
107         }
108
109         public void initializeDB() {
110
111                 try {
112                         Owner jon = new Owner("Jon");
113                         Owner alfredo = new Owner("Alfredo");
114                         jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
115                         jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
116                         jon.setBankAccount("1349 5677 21 2133567777");
117                         alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
118                                         5);
119                         alfredo.addRuralHouse(4, "", "Murgia", 6, 6, 6, 6, 6);
120                         alfredo.setBankAccount("4144 0087 23 9700002133");
121                         Account jonAcc = new Account("userJon", "passJon", jon);
122                         Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
123                                         alfredo);
124                         db.store(jon);
125                         db.store(alfredo);
126                         db.store(jonAcc);
127                         db.store(alfredoAcc);
128                         db.commit();
129                 } finally {
130                         // db.close();
131                 }
132         }
133
134         @SuppressWarnings("finally")
135         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
136                         Date lastDay, float price) throws RemoteException, Exception {
137                 Offer o = null;
138                 try {
139
140                         if (c.isDatabaseLocal() == false)
141                                 openObjectContainer();
142
143                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
144                                         null, null, null, null);
145                         ObjectSet result = db.queryByExample(proto);
146                         RuralHouse rh = (RuralHouse) result.next();
147                         o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
148                                         lastDay, price);
149                         db.store(theDB4oManagerAux); // To store the new value for
150                                                                                         // offerNumber
151                         db.store(o);
152                         db.commit();
153
154                 } catch (com.db4o.ext.ObjectNotStorableException e) {
155                         System.out
156                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
157                 } finally {
158                         return o;
159                 }
160         }
161
162         public void deleteDB() {
163
164                 try {
165                         Owner proto = new Owner(null, null);
166                         ObjectSet result = db.queryByExample(proto);
167                         Vector<Owner> owners = new Vector<Owner>();
168                         while (result.hasNext()) {
169                                 Owner o = (Owner) result.next();
170                                 System.out.println("Deleted owner: " + o.toString());
171                                 db.delete(o);
172                         }
173                         db.commit();
174                 } finally {
175                         // db.close();
176                 }
177         }
178
179         /**
180          * This method creates a book with a corresponding parameters
181          * 
182          * @param First
183          *            day, last day, house number and telephone
184          * @return a book
185          */
186         public Booking createBooking(RuralHouse ruralHouse, Date firstDate,
187                         Date lastDate, String bookTelephoneNumber)
188                         throws OfferCanNotBeBooked {
189
190                 try {
191
192                         if (c.isDatabaseLocal() == false)
193                                 openObjectContainer();
194
195                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
196                                         null, ruralHouse.getDescription(), ruralHouse.getTown(),
197                                         null);
198                         ObjectSet result = db.queryByExample(proto);
199                         RuralHouse rh = (RuralHouse) result.next();
200
201                         Offer offer;
202                         offer = rh.findOffer(firstDate, lastDate);
203
204                         if (offer != null) {
205                                 offer.createBooking(theDB4oManagerAux.bookingNumber++,
206                                                 bookTelephoneNumber);
207                                 db.store(theDB4oManagerAux); // To store the new value for
208                                                                                                 // bookingNumber
209                                 db.store(offer);
210                                 db.commit();
211                                 return offer.getBooking();
212                         }
213                         return null;
214
215                 } catch (com.db4o.ext.ObjectNotStorableException e) {
216                         System.out
217                                         .println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
218                         return null;
219                 } catch (Exception exc) {
220                         exc.printStackTrace();
221                         return null;
222                 }
223         }
224
225         /**
226          * This method existing owners
227          * 
228          */
229         public Vector<Owner> getOwners() throws RemoteException, Exception {
230
231                 if (c.isDatabaseLocal() == false)
232                         openObjectContainer();
233
234                 try {
235                         Owner proto = new Owner(null, null);
236                         ObjectSet result = db.queryByExample(proto);
237                         Vector<Owner> owners = new Vector<Owner>();
238                         while (result.hasNext())
239                                 owners.add((Owner) result.next());
240                         return owners;
241                 } finally {
242                         // db.close();
243                 }
244         }
245
246         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
247                         Exception {
248                 if (c.isDatabaseLocal() == false)
249                         openObjectContainer();
250
251                 try {
252                         RuralHouse proto = new RuralHouse(0, null, null, null, null);
253                         ObjectSet result = db.queryByExample(proto);
254                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
255                         while (result.hasNext())
256                                 ruralHouses.add((RuralHouse) result.next());
257                         return ruralHouses;
258                 } finally {
259                         // db.close();
260                 }
261         }
262
263         public boolean existsOverlappingOffer(RuralHouse rh, Date firstDay,
264                         Date lastDay) throws RemoteException, OverlappingOfferExists {
265                 try {
266                         if (c.isDatabaseLocal() == false)
267                                 openObjectContainer();
268
269                         RuralHouse rhn = (RuralHouse) db
270                                         .queryByExample(
271                                                         new RuralHouse(rh.getHouseNumber(), null, null,
272                                                                         null, null)).next();
273                         if (rhn.overlapsWith(firstDay, lastDay) != null)
274                                 throw new OverlappingOfferExists();
275                         else
276                                 return false;
277                 } finally {
278                         // db.close();
279                 }
280         }
281
282         public static ObjectContainer getContainer() {
283                 return db;
284         }
285
286         public void close() {
287                 db.close();
288                 System.out.println("DataBase closed");
289         }
290
291         public String toString() {
292                 return "bookingNumber=" + bookingNumber + " offerNumber=" + offerNumber;
293         }
294
295         /**
296          * @param usr
297          * @param pwd
298          * @return
299          * @throws RemoteException
300          * @throws Exception
301          */
302         public Vector<Account> getAccount(String usr, String pwd)
303                         throws RemoteException, Exception {
304                 if (c.isDatabaseLocal() == false)
305                         openObjectContainer();
306                 try {
307                         Account proto = new Account(usr, pwd, new Owner(null, null));
308                         ObjectSet<Account> result = db.queryByExample(proto);
309                         Vector<Account> accounts = new Vector<Account>();
310                         while (result.hasNext())
311                                 accounts.add((Account) result.next());
312                         return accounts;
313                 } finally {
314                         // db.close();
315                 }
316         }
317
318         /**
319          * @param rh
320          */
321         public boolean storeRuralHouses(RuralHouse rh) {
322 //              DB4oManager.openDB();
323                 if (c.isDatabaseLocal() == false)
324                         openObjectContainer();
325                 boolean stored = false;
326                 RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null,
327                                 null, null);
328                 try {
329                         ObjectSet<Owner> result = db.queryByExample(house);
330                         if (result.isEmpty()) {
331                                 db.store(rh);
332                                 db.commit();
333                                 stored = true;
334                         }
335                 } finally {
336 //                      db.close();
337                 }
338                 return stored;
339         }
340
341         public void removeHouse(int houseNumber) {
342 //              DB4oManager.openDB();
343                 if (c.isDatabaseLocal() == false)
344                         openObjectContainer();
345                 RuralHouse house = new RuralHouse(houseNumber, null, null, null, null);
346                 try {
347                         ObjectSet<RuralHouse> result = db.queryByExample(house);
348                         if (!result.isEmpty()) {
349                                 RuralHouse found = (RuralHouse) result.get(0);
350                                 db.delete(found);
351                                 db.commit();
352                         }
353                 } catch (Exception exc) {
354                         exc.printStackTrace();
355                 } finally {
356 //                      db.close();
357                 }
358
359         }
360         
361         public Vector<RuralHouse> getRuralHousesByTown(String town){
362                 RuralHouse rh = new RuralHouse(0,null,null,town,null);
363                 if (c.isDatabaseLocal() == false)
364                         openObjectContainer();
365                 try{
366                         ObjectSet<RuralHouse> result = db.queryByExample(rh);
367                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
368                         while(result.hasNext())
369                                 ruralHouses.add(result.next());
370                         return ruralHouses;
371                 }finally{
372                         //db.close();
373                 }
374                 
375         }
376 }