Given code uploaded
[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<RuralHouse> getAllRuralHouses() throws RemoteException,
90         Exception {
91                 ObjectContainer db=DB4oManager.getContainer();
92                 try {
93                         RuralHouse proto = new RuralHouse(0,null,null,null);
94                         ObjectSet result = db.queryByExample(proto);
95                         Vector<RuralHouse> ruralHouses=new Vector<RuralHouse>();
96                         while(result.hasNext()) 
97                                 ruralHouses.add((RuralHouse)result.next());
98                         return ruralHouses;
99                 } finally {
100                         //db.close();
101                 }
102         }
103
104         /**
105          * This method creates an offer with a house number, first day, last day and price
106          * precondition: There are no overlapping offers
107          * @param House
108          *            number, start day, last day and price
109          * @return None
110          */
111         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
112                         float price) throws RemoteException, Exception {
113                 ObjectContainer db=DB4oManager.getContainer();
114                 RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null, 
115                                 ruralHouse.getDescription(), ruralHouse.getTown());
116                 ObjectSet result = db.queryByExample(proto);
117                 RuralHouse rh=(RuralHouse)result.next();
118                 Offer o=rh.createOffer(firstDay, lastDay, price);
119                 db.store(o);
120                 db.commit(); 
121                 return o;
122         }
123
124         public RuralHouse getRuralHouse(RuralHouse rh){
125                 try {
126                         ObjectContainer db=DB4oManager.getContainer();
127                         RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null,
128                                         rh.getDescription(), rh.getTown());
129                         ObjectSet result = db.queryByExample(proto);
130                         return  rh=(RuralHouse)result.next();
131                 } catch (Exception exc) {
132                         exc.printStackTrace();
133                         return null;
134                 }
135         }
136
137         public Booking createBooking(Offer offer, String clientTelephoneNumber)
138                         throws OfferCanNotBeBooked {
139                 try {
140                         Booking b=null;
141                         if (offer!=null) {
142                                 b=offer.createBook(clientTelephoneNumber);
143                                 db.store(b);
144                                 db.store(offer);
145                                 db.commit();
146                         }
147                         return b;
148                 } catch (Exception exc) {
149                         exc.printStackTrace();
150                         return null;
151                 }
152         }
153
154         /**
155          * This method returns the instance of the DB4oManager class 
156          * 
157          * @return the db4o manager
158          */
159         public static DB4oManager getInstance()  {
160                 return theDB4oManager;
161         }
162
163 }
164