unification with the actual initial project. Some things are new now, but there has...
[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
8 import java.rmi.RemoteException;
9 import java.sql.Date;
10 import java.util.HashSet;
11 import java.util.ListIterator;
12 import java.util.Vector;
13
14 import com.db4o.*;
15 import com.db4o.config.EmbeddedConfiguration;
16 import com.db4o.cs.Db4oClientServer;
17 import com.db4o.cs.config.ClientConfiguration;
18
19 import configuration.ConfigXML;
20 import domain.Account;
21 import domain.Booking;
22 import domain.Offer;
23 //import dataModel.Offer;
24 import domain.Owner;
25 import domain.RuralHouse;
26 import exceptions.OfferCanNotBeBooked;
27 import exceptions.OverlappingOfferExists;
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 serialized
34         private int offerNumber=0;   // if it is "static" then it is not serialized
35         private static DB4oManager theDB4oManager=null;
36
37         private static DB4oManagerAux theDB4oManagerAux;
38         static ConfigXML c;
39
40         private DB4oManager() throws Exception {
41                 theDB4oManagerAux=new DB4oManagerAux(0,0);
42                 c=ConfigXML.getInstance();
43                 System.out.println("Creating DB4oManager instance => isDatabaseLocal: "+c.isDatabaseLocal()+" getDatabBaseOpenMode: "+c.getDataBaseOpenMode());
44
45                 
46                 if ((c.getDataBaseOpenMode().equals("initialize")) && (c.isDatabaseLocal()))
47                                 new File(c.getDb4oFilename()).delete();
48                 
49                 if (c.isDatabaseLocal())
50                 {
51                         configuration = Db4oEmbedded.newConfiguration();
52                         configuration.common().activationDepth(c.getActivationDepth());
53                         configuration.common().updateDepth(c.getUpdateDepth());
54                         db=Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
55                         System.out.println("DataBase opened");
56                 }
57                 else // c.isDatabaseLocal==false
58                 { 
59                         openObjectContainer();
60                 }
61                 if (c.getDataBaseOpenMode().equals("initialize"))
62                 {       initializeDB();
63                         System.out.println("DataBase initialized");
64                 }
65                 else // c.getDataBaseOpenMode().equals("open")
66                 
67                         {       
68                                 ObjectSet res =db.queryByExample(DB4oManagerAux.class);
69                                 ListIterator listIter = res.listIterator();
70                                 if (listIter.hasNext()) theDB4oManagerAux = (DB4oManagerAux) res.next();                
71             }
72                 }
73
74         private static void openDB(){
75                 configuration = Db4oEmbedded.newConfiguration();
76                 configuration.common().activationDepth(c.getActivationDepth());
77                 configuration.common().updateDepth(c.getUpdateDepth());
78                 db=Db4oEmbedded.openFile(configuration, c.getDb4oFilename());
79         }
80                 
81         private void openObjectContainer(){
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                 DB4oManagerAux(int bookingNumber,int offerNumber){
96                         this.bookingNumber=bookingNumber;
97                         this.offerNumber=offerNumber;
98                 }
99         }
100
101         public static DB4oManager getInstance() throws Exception{
102                 if (theDB4oManager==null)
103                         theDB4oManager=new DB4oManager();
104                 return theDB4oManager;
105         }
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,
117                                         5, 5, 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                 }
129                 finally {
130                         //db.close();
131                 }
132         }
133         
134         @SuppressWarnings("finally")
135         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
136                         float price) throws RemoteException, Exception {
137         Offer o = null;
138         try {
139                 
140                 if (c.isDatabaseLocal()==false) openObjectContainer();
141                 
142                 
143                 RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),null,null,null,null);
144                 ObjectSet result = db.queryByExample(proto);
145                 RuralHouse rh=(RuralHouse)result.next();
146                 o=rh.createOffer(theDB4oManagerAux.offerNumber++,firstDay, lastDay, price);
147                 db.store(theDB4oManagerAux); // To store the new value for offerNumber
148                 db.store(o);
149                 db.commit(); 
150                 
151         }
152         catch (com.db4o.ext.ObjectNotStorableException e){
153                 System.out.println("Error: com.db4o.ext.ObjectNotStorableException in createOffer");
154         }
155         finally {
156                 return o;
157         }
158         }
159         
160         public void deleteDB() {
161                 
162                 try {
163                          Owner proto = new Owner(null,null);
164                          ObjectSet result = db.queryByExample(proto);
165                          Vector<Owner> owners=new Vector<Owner>();
166                          while(result.hasNext())
167                          {      Owner o = (Owner)result.next();
168                                 System.out.println("Deleted owner: "+o.toString());
169                                 db.delete(o);
170                          }
171                          db.commit();
172              } finally {
173                 // db.close();
174              }
175         }
176
177         /**
178          * This method creates a book with a corresponding parameters
179          * 
180          * @param First
181          *            day, last day, house number and telephone
182          * @return a book
183          */
184         public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber)
185                         throws OfferCanNotBeBooked {
186                 
187                 try {
188
189                         if (c.isDatabaseLocal()==false) openObjectContainer();
190
191                         RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(),null,ruralHouse.getDescription(),ruralHouse.getTown(), null);
192                          ObjectSet result = db.queryByExample(proto);
193                          RuralHouse rh=(RuralHouse)result.next();
194
195                         Offer offer;
196                         offer = rh.findOffer(firstDate, lastDate);
197
198                         if (offer!=null) {
199                                 offer.createBooking(theDB4oManagerAux.bookingNumber++, bookTelephoneNumber);
200                                 db.store(theDB4oManagerAux); // To store the new value for bookingNumber
201                                 db.store(offer);
202                                 db.commit();
203                                 return offer.getBooking();
204                         }
205                         return null;
206
207                 } catch (com.db4o.ext.ObjectNotStorableException e){
208                                 System.out.println("Error: com.db4o.ext.ObjectNotStorableException in createBooking");
209                                 return null;
210                         }
211                 catch (Exception exc) {
212                         exc.printStackTrace();
213                         return null;
214                 }
215         }
216
217
218         /**
219          * This method existing  owners 
220          * 
221          */
222         public Vector<Owner> getOwners() throws RemoteException,
223                         Exception {
224                 
225                 if (c.isDatabaseLocal()==false) openObjectContainer();
226
227                  try {
228                          Owner proto = new Owner(null,null);
229                          ObjectSet result = db.queryByExample(proto);
230                          Vector<Owner> owners=new Vector<Owner>();
231                          while(result.hasNext())                                 
232                                  owners.add((Owner)result.next());
233                          return owners;
234              } finally {
235                  //db.close();
236              }
237         } 
238         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
239         Exception {
240                 if (c.isDatabaseLocal()==false) openObjectContainer();
241
242                  try {
243                          RuralHouse proto = new RuralHouse(0,null,null,null,null);
244                          ObjectSet result = db.queryByExample(proto);
245                          Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
246                          while(result.hasNext()) 
247                                  ruralHouses.add((RuralHouse)result.next());
248                          return ruralHouses;
249              } finally {
250                 // db.close();
251              }
252         }
253         
254         public boolean existsOverlappingOffer(RuralHouse rh,Date firstDay, Date lastDay) throws RemoteException, OverlappingOfferExists{
255                  try {
256                         if (c.isDatabaseLocal()==false) openObjectContainer();
257
258                         RuralHouse rhn = (RuralHouse) db.queryByExample(new RuralHouse(rh.getHouseNumber(),null,null,null,null)).next();                
259                         if (rhn.overlapsWith(firstDay, lastDay)!=null) throw new OverlappingOfferExists();
260                         else return false; 
261              } finally {
262                  //db.close();
263              }
264         }
265
266         public static ObjectContainer getContainer(){
267                 return db;
268         }
269         
270         public void close(){
271                 db.close();
272                 System.out.println("DataBase closed");
273         }
274         
275         public String toString() {
276                 return "bookingNumber="+bookingNumber+" offerNumber="+offerNumber;
277         }
278
279
280
281         /**
282          * @param usr
283          * @param pwd
284          * @return
285          * @throws RemoteException
286          * @throws Exception
287          */
288         public Vector<Account> getAccount(String usr, String pwd)
289                         throws RemoteException, Exception {
290
291                 try {
292                         Account proto = new Account(usr, pwd, new Owner(null, null));
293                         ObjectSet<Account> result = db.queryByExample(proto);
294                         Vector<Account> accounts = new Vector<Account>();
295                         while (result.hasNext())
296                                 accounts.add((Account) result.next());
297                         return accounts;
298                 } finally {
299                         //db.close();
300                 }
301         }
302
303         /**
304          * @param rh
305          */
306         public boolean storeRuralHouses(RuralHouse rh) {
307                 DB4oManager.openDB();
308                 boolean stored = false;
309                 ObjectContainer db = DB4oManager.getContainer();
310                 RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null,
311                                 null, null);
312                 try {
313                         ObjectSet<Owner> result = db.queryByExample(house);
314                         if (result.isEmpty()) {
315                                 db.store(rh);
316                                 db.commit();
317                                 stored = true;
318                         }
319                 } finally {
320                         db.close();
321                 }
322                 return stored;
323         }
324
325         public void removeHouse(int houseNumber) {
326                 DB4oManager.openDB();
327                 RuralHouse house = new RuralHouse(houseNumber, null, null, null, null);
328                 try {
329                         ObjectSet<RuralHouse> result = db.queryByExample(house);
330                         if (!result.isEmpty()) {
331                                 RuralHouse found = (RuralHouse) result.get(0);
332                                 db.delete(found);
333                                 db.commit();
334                         }
335                 } catch (Exception exc) {
336                         exc.printStackTrace();
337                 } finally {
338                         db.close();
339                 }
340
341         }
342 }
343