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