implemented lacking GUIs and corrected errors
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManager.java
1 package dataAccess;
2
3 import java.io.File;
4 import java.rmi.RemoteException;
5 import java.util.Date;
6 import java.util.Vector;
7 //import java.util.Enumeration;
8 //import java.util.Vector;
9
10 import businessLogic.BookingManager;
11
12 import com.db4o.*;
13
14 import configuration.Config;
15 import domain.*;
16 import exceptions.OfferCanNotBeBooked;
17
18 public class DB4oManager {
19
20         private static ObjectContainer db;
21         private static DB4oManager theDB4oManager = new DB4oManager();
22
23         /**
24          * 
25          */
26         private DB4oManager() {
27                 Config c = Config.getInstance();
28                 String dataBaseOpenMode = c.getDataBaseOpenMode();
29                 DB4oManager.openDatabase(dataBaseOpenMode);
30         }
31
32         /**
33          * @param mode
34          */
35         public static void openDatabase(String mode) {
36                 Config c = Config.getInstance();
37                 String db4oFileName = c.getDb4oFilename();
38                 if (mode.compareTo("open") == 0) {
39                         db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
40                         db.ext().configure().updateDepth(5);
41
42
43                 } else if (mode.compareTo("initialize") == 0) {
44                         try {
45                                 new File(db4oFileName).delete();
46                                 db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
47                                 db.ext().configure().updateDepth(5);
48                                 Owner jon = new Owner("Jon");
49                                 Owner alfredo = new Owner("Alfredo");
50                                 jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
51                                 jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
52                                 jon.setBankAccount("1349 5677 21 2133567777");
53                                 alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5,
54                                                 5, 5, 5);
55                                 alfredo.addRuralHouse(4,"", "Murgia", 6, 6, 6, 6, 6);
56                                 alfredo.setBankAccount("4144 0087 23 9700002133");
57                                 Account jonAcc = new Account("userJon", "passJon", jon);
58                                 Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
59                                                 alfredo);
60                                 db.store(jon);
61                                 db.store(alfredo);
62                                 db.store(jonAcc);
63                                 db.store(alfredoAcc);
64                                 db.commit();
65                                 System.out.println("DataBase Initialized");
66                         } finally {
67                                 db.close();
68                         }
69
70                 }
71         }
72
73         /**
74          * @return
75          */
76         public static ObjectContainer getContainer() {
77                 return db;
78         }
79
80
81         /**
82          * @param rh
83          */
84         public boolean storeRuralHouses(RuralHouse rh) {
85                 DB4oManager.openDatabase("open");
86                 boolean stored = false;
87                 ObjectContainer db = DB4oManager.getContainer();
88                 RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null,
89                                 null, null);
90                 try {
91                         ObjectSet<Owner> result = db.queryByExample(house);
92                         if (result.isEmpty()) {
93                                 db.store(rh);
94                                 db.commit();
95                                 stored = true;
96                         }
97                 } finally {
98                         db.close();
99                 }
100                 return stored;
101         }
102
103         /**
104          * This method finds all existing owners
105          * 
106          */
107
108         public Vector<Owner> getOwners() throws RemoteException, Exception {
109                 DB4oManager.openDatabase("open");
110                 ObjectContainer db = DB4oManager.getContainer();
111
112                 try {
113                         Owner proto = new Owner(null, null);
114                         ObjectSet<Owner> result = db.queryByExample(proto);
115                         Vector<Owner> owners = new Vector<Owner>();
116                         while (result.hasNext())
117                                 owners.add((Owner) result.next());
118                         return owners;
119                 } finally {
120                         db.close();
121                 }
122
123         }
124
125         /**
126          * @param usr
127          * @param pwd
128          * @return
129          * @throws RemoteException
130          * @throws Exception
131          */
132         public Vector<Account> getAccount(String usr, String pwd)
133                         throws RemoteException, Exception {
134                 DB4oManager.openDatabase("open");
135                 ObjectContainer db = DB4oManager.getContainer();
136
137                 try {
138                         Account proto = new Account(usr, pwd, new Owner(null, null));
139                         ObjectSet<Account> result = db.queryByExample(proto);
140                         Vector<Account> accounts = new Vector<Account>();
141                         while (result.hasNext())
142                                 accounts.add((Account) result.next());
143                         return accounts;
144                 } finally {
145                         db.close();
146                 }
147         }
148
149         /**
150          * @return
151          * @throws RemoteException
152          * @throws Exception
153          */
154         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
155
156                         Exception {
157                 DB4oManager.openDatabase("open");
158                 ObjectContainer db = DB4oManager.getContainer();
159                 try {
160                         RuralHouse proto = new RuralHouse(0, null, null, null, null);
161                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
162                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
163                         while (result.hasNext())
164                                 ruralHouses.add((RuralHouse) result.next());
165                         return ruralHouses;
166                 } finally {
167                         db.close();
168                 }
169         }
170
171         /**
172          * This method creates an offer with a house number, first day, last day and
173          * price precondition: There are no overlapping offers
174          * 
175          * @param House
176          *            number, start day, last day and price
177          * @return None
178          */
179
180         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
181                         Date lastDay, float price) throws RemoteException, Exception {
182                 DB4oManager.openDatabase("open");
183                 ObjectContainer db = DB4oManager.getContainer();
184                 Offer o;
185                 try {
186                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
187                                         null, ruralHouse.getDescription(), ruralHouse.getTown(),
188                                         ruralHouse.getFeatures());
189                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
190                         RuralHouse rh = (RuralHouse) result.next();
191                         o = rh.createOffer(firstDay, lastDay, price);
192                         db.store(o);
193                         db.commit();
194                 } finally {
195                         db.close();
196                 }
197                 return o;
198         }
199
200
201         /**
202          * @param rh
203          * @return
204          */
205         public RuralHouse getRuralHouse(RuralHouse rh) {
206
207                 DB4oManager.openDatabase("open");
208                 try {
209                         ObjectContainer db = DB4oManager.getContainer();
210                         RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
211
212                                         rh.getDescription(), rh.getTown(), rh.getFeatures());
213                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
214                         return rh = (RuralHouse) result.next();
215                 } catch (Exception exc) {
216                         exc.printStackTrace();
217                         return null;
218                 } finally {
219                         db.close();
220                 }
221
222         } 
223
224         /**
225          * @param offer
226          * @param clientTelephoneNumber
227          * @return
228          * @throws OfferCanNotBeBooked
229          */
230         public Booking createBooking(Offer offer, String clientTelephoneNumber)
231                         throws OfferCanNotBeBooked {
232                 DB4oManager.openDatabase("open");
233                 try {
234                         Booking b = null;
235                         if (offer != null) {
236                                 b = offer.createBook(clientTelephoneNumber);
237                                 db.store(b);
238                                 db.store(offer);
239                                 db.commit();
240                         }
241                         return b;
242                 } catch (Exception exc) {
243                         exc.printStackTrace();
244                         return null;
245                 } finally {
246                         db.close();
247                 }
248
249         }
250
251         public void removeHouse(int houseNumber) {
252                 ObjectContainer db = DB4oManager.getContainer();
253                 RuralHouse house = new RuralHouse(houseNumber, null, null, null, null);
254                 try {
255                         ObjectSet<RuralHouse> result = db.queryByExample(house);
256                         if (!result.isEmpty()) {
257                                 RuralHouse found = (RuralHouse) result.get(0);
258                                 db.delete(found);
259                                 db.commit();
260                         }
261                 } catch (Exception exc) {
262                         exc.printStackTrace();
263                 } finally {
264                         db.close();
265                 }
266
267         }
268
269         /**
270          * This method returns the instance of the DB4oManager class
271          * 
272          * @return the db4o manager
273          */
274         public static DB4oManager getInstance() {
275                 return theDB4oManager;
276         }
277
278 }