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