295af737588de7e948a311e4f60c458462471719
[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 void storeRuralHouses(RuralHouse rh) {
83                 DB4oManager.openDatabase("open");
84                 ObjectContainer db = DB4oManager.getContainer();
85                 try {
86                         db.store(rh);
87                         db.commit();
88                 } finally {
89                         db.close();
90                 }
91         }
92
93         /**
94          * This method finds all existing owners
95          * 
96          */
97         public Vector<Owner> getOwners() throws RemoteException, Exception {
98                 DB4oManager.openDatabase("open");
99                 ObjectContainer db = DB4oManager.getContainer();
100
101                 try {
102                         Owner proto = new Owner(null, null);
103                         ObjectSet<Owner> result = db.queryByExample(proto);
104                         Vector<Owner> owners = new Vector<Owner>();
105                         while (result.hasNext())
106                                 owners.add((Owner) result.next());
107                         return owners;
108                 } finally {
109                         db.close();
110                 }
111         }
112
113         /**
114          * @param usr
115          * @param pwd
116          * @return
117          * @throws RemoteException
118          * @throws Exception
119          */
120         public Vector<Account> getAccount(String usr, String pwd)
121                         throws RemoteException, Exception {
122                 DB4oManager.openDatabase("open");
123                 ObjectContainer db = DB4oManager.getContainer();
124
125                 try {
126                         Account proto = new Account(usr, pwd, new Owner(null, null));
127                         ObjectSet<Account> result = db.queryByExample(proto);
128                         Vector<Account> accounts = new Vector<Account>();
129                         while (result.hasNext())
130                                 accounts.add((Account) result.next());
131                         return accounts;
132                 } finally {
133                         db.close();
134                 }
135         }
136
137         /**
138          * @return
139          * @throws RemoteException
140          * @throws Exception
141          */
142         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
143                         Exception {
144                 DB4oManager.openDatabase("open");
145                 ObjectContainer db = DB4oManager.getContainer();
146                 try {
147                         RuralHouse proto = new RuralHouse(0, null, null, null, 0, 0, 0, 0,
148                                         0);
149                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
150                         Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
151                         while (result.hasNext())
152                                 ruralHouses.add((RuralHouse) result.next());
153                         return ruralHouses;
154                 } finally {
155                         db.close();
156                 }
157         }
158
159         /**
160          * This method creates an offer with a house number, first day, last day and
161          * price precondition: There are no overlapping offers
162          * 
163          * @param House
164          *            number, start day, last day and price
165          * @return None
166          */
167         public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
168                         Date lastDay, float price) throws RemoteException, Exception {
169                 DB4oManager.openDatabase("open");
170                 ObjectContainer db = DB4oManager.getContainer();
171                 Offer o;
172                 try {
173                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),
174                                         null, ruralHouse.getDescription(), ruralHouse.getTown(), 0,
175                                         0, 0, 0, 0);
176                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
177                         RuralHouse rh = (RuralHouse) result.next();
178                         o = rh.createOffer(firstDay, lastDay, price);
179                         db.store(o);
180                         db.commit();
181                 } finally {
182                         db.close();
183                 }
184                 return o;
185         }
186
187         /**
188          * @param rh
189          * @return
190          */
191         public RuralHouse getRuralHouse(RuralHouse rh) {
192
193                 DB4oManager.openDatabase("open");
194                 try {
195                         ObjectContainer db = DB4oManager.getContainer();
196                         RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
197                                         rh.getDescription(), rh.getTown(), 0, 0, 0, 0, 0);
198                         ObjectSet<RuralHouse> result = db.queryByExample(proto);
199                         return rh = (RuralHouse) result.next();
200                 } catch (Exception exc) {
201                         exc.printStackTrace();
202                         return null;
203                 } finally {
204                         db.close();
205                 }
206         }
207
208         /**
209          * @param offer
210          * @param clientTelephoneNumber
211          * @return
212          * @throws OfferCanNotBeBooked
213          */
214         public Booking createBooking(Offer offer, String clientTelephoneNumber)
215                         throws OfferCanNotBeBooked {
216                 DB4oManager.openDatabase("open");
217                 try {
218                         Booking b = null;
219                         if (offer != null) {
220                                 b = offer.createBook(clientTelephoneNumber);
221                                 db.store(b);
222                                 db.store(offer);
223                                 db.commit();
224                         }
225                         return b;
226                 } catch (Exception exc) {
227                         exc.printStackTrace();
228                         return null;
229                 } finally {
230                         db.close();
231                 }
232         }
233
234         /**
235          * This method returns the instance of the DB4oManager class
236          * 
237          * @return the db4o manager
238          */
239         public static DB4oManager getInstance() {
240                 return theDB4oManager;
241         }
242
243 }