8388435809773cf6f0b0549f80891a9a97bc7179
[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
16 import domain.Booking;
17 import domain.Offer;
18 import domain.Owner;
19 import domain.RuralHouse;
20 import exceptions.OfferCanNotBeBooked;
21
22 public class DB4oManager { 
23
24         private static ObjectContainer db;
25         private static DB4oManager theDB4oManager=new DB4oManager();
26
27         private DB4oManager() {
28                 Config c=Config.getInstance();
29                 String dataBaseOpenMode=c.getDataBaseOpenMode();
30                 DB4oManager.openDatabase(dataBaseOpenMode);
31         }
32
33         public static void openDatabase(String mode) {
34                 Config c=Config.getInstance();
35                 String db4oFileName=c.getDb4oFilename();
36                 if (mode.compareTo("open")==0) {
37                         db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
38                         db.ext().configure().updateDepth(5);
39                         System.out.println("DataBase Opened");
40
41                 } else if (mode.compareTo("initialize")==0){
42                         new File(db4oFileName).delete();
43                         db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName);
44                         db.ext().configure().updateDepth(5);
45                         Owner jon = new Owner("Jon", "userJon", "passJon");
46                         Owner alfredo = new Owner("Alfredo","userAlfredo", "passAlfredo");
47                         jon.addRuralHouse(1, "Ezkioko etxea","Ezkio");
48                         jon.addRuralHouse(2, "Eskiatzeko etxea","Jaca");
49                         jon.setBankAccount("1349 5677 21 2133567777");
50                         alfredo.addRuralHouse(3, "Casa del abuelo","Pitillas");
51                         alfredo.addRuralHouse(4, "Refugio","Murgia");
52                         alfredo.setBankAccount("4144 0087 23 9700002133");
53
54                         db.store(jon);
55                         db.store(alfredo);
56                         db.commit();
57                         System.out.println("DataBase Initialized");
58                 }
59         }
60
61         public  static ObjectContainer getContainer() {
62                 return db;
63         }
64         public static void close(){
65                 db.close();
66                 System.out.println("DataBase closed");
67         }
68         
69         /**
70          * This method finds all existing  owners 
71          * 
72          */
73         public Vector<Owner> getOwners() throws RemoteException,
74         Exception {
75                 ObjectContainer db=DB4oManager.getContainer();
76
77                 try {
78                         Owner proto = new Owner(null,null,null,null);
79                         ObjectSet result = db.queryByExample(proto);
80                         Vector<Owner> owners=new Vector<Owner>();
81                         while(result.hasNext())                          
82                                 owners.add((Owner)result.next());
83                         return owners;
84                 } finally {
85                         //db.close();
86                 }
87         } 
88         
89         public Vector<Owner> getSingleOwner(String usr, String pwd) throws RemoteException,
90         Exception {
91                 ObjectContainer db=DB4oManager.getContainer();
92
93                 try {
94                         Owner proto = new Owner(null,usr,pwd,null);
95                         ObjectSet result = db.queryByExample(proto);
96                         Vector<Owner> owners=new Vector<Owner>();
97                         while(result.hasNext())                          
98                                 owners.add((Owner)result.next());
99                         return owners;
100                 } finally {
101                         //db.close();
102                 }
103         }
104         
105         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
106         Exception {
107                 ObjectContainer db=DB4oManager.getContainer();
108                 try {
109                         RuralHouse proto = new RuralHouse(0,null,null,null);
110                         ObjectSet result = db.queryByExample(proto);
111                         Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
112                         while(result.hasNext()) 
113                                 ruralHouses.add((RuralHouse)result.next());
114                         return ruralHouses;
115                 } finally {
116                         //db.close();
117                 }
118         }
119
120         /**
121          * This method creates an offer with a house number, first day, last day and price
122          * precondition: There are no overlapping offers
123          * @param House
124          *            number, start day, last day and price
125          * @return None
126          */
127         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
128                         float price) throws RemoteException, Exception {
129                 ObjectContainer db=DB4oManager.getContainer();
130                 RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null, 
131                                 ruralHouse.getDescription(), ruralHouse.getTown());
132                 ObjectSet result = db.queryByExample(proto);
133                 RuralHouse rh=(RuralHouse)result.next();
134                 Offer o=rh.createOffer(firstDay, lastDay, price);
135                 db.store(o);
136                 db.commit(); 
137                 return o;
138         }
139
140         public RuralHouse getRuralHouse(RuralHouse rh){
141                 try {
142                         ObjectContainer db=DB4oManager.getContainer();
143                         RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
144                                         rh.getDescription(), rh.getTown());
145                         ObjectSet result = db.queryByExample(proto);
146                         return  rh=(RuralHouse)result.next();
147                 } catch (Exception exc) {
148                         exc.printStackTrace();
149                         return null;
150                 }
151         }
152
153         public Booking createBooking(Offer offer, String clientTelephoneNumber)
154                         throws OfferCanNotBeBooked {
155                 try {
156                         Booking b=null;
157                         if (offer!=null) {
158                                 b=offer.createBook(clientTelephoneNumber);
159                                 db.store(b);
160                                 db.store(offer);
161                                 db.commit();
162                         }
163                         return b;
164                 } catch (Exception exc) {
165                         exc.printStackTrace();
166                         return null;
167                 }
168         }
169
170         /**
171          * This method returns the instance of the DB4oManager class 
172          * 
173          * @return the db4o manager
174          */
175         public static DB4oManager getInstance()  {
176                 return theDB4oManager;
177         }
178
179 }
180