9f377d76ae966889d311aae8a107d45d416c7178
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / __FacadeImplementation.java
1 package businessLogic;
2
3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.sql.Date;
6 import java.sql.SQLException;
7 import java.util.Vector;
8
9 import dataAccess.DB4oManager;
10 import domain.Booking;
11 import domain.Offer;
12 import domain.Owner;
13 import domain.RuralHouse;
14 import exceptions.BadDates;
15 import exceptions.DB4oManagerCreationException;
16 import exceptions.OfferCanNotBeBooked;
17 import exceptions.OverlappingOfferExists;
18
19
20 public class __FacadeImplementation extends UnicastRemoteObject implements __ApplicationFacadeInterface {
21
22         /**
23          * 
24          */
25         private static final long serialVersionUID = 1L;
26
27         Vector<Owner> owners;
28         Vector<RuralHouse> ruralHouses;
29         DB4oManager dB4oManager;
30  
31
32         public __FacadeImplementation() throws RemoteException, InstantiationException,
33                         IllegalAccessException, ClassNotFoundException, SQLException, DB4oManagerCreationException {
34                 owners=null;
35                 ruralHouses=null;
36                 try{
37                         dB4oManager=DB4oManager.getInstance();
38                 }
39                 catch (com.db4o.ext.DatabaseFileLockedException e) {
40                         System.out.println("Error in FacadeImplementation: "+e.toString());
41                         throw e;
42                 }
43                 catch (Exception e) {
44                         System.out.println("Error in FacadeImplementation: "+e.toString());
45                         throw new DB4oManagerCreationException();
46                 }
47         }
48         
49
50         /**
51          * This method creates an offer with a house number, first day, last day and price
52          * 
53          * @param House
54          *            number, start day, last day and price
55          * @return the created offer, or null, or an exception
56          */
57         public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
58                         float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
59                 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
60                 ruralHouses=null;
61                 owners=null;
62                 boolean b = dB4oManager.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
63                 if (!b) return dB4oManager.createOffer(ruralHouse,firstDay,lastDay,price);                      
64                 return null;
65         }
66
67         /**
68          * This method creates a book with a corresponding parameters
69          * 
70          * @param First
71          *            day, last day, house number and telephone
72          * @return a book
73          */
74         public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber)
75                         throws OfferCanNotBeBooked {
76                 ruralHouses=null;
77                 owners=null;
78                 return dB4oManager.createBooking(ruralHouse,firstDate,lastDate,bookTelephoneNumber);
79         }
80
81
82         /**
83          * This method existing  owners 
84          * 
85          */
86         public Vector<Owner> getOwners() throws RemoteException,
87                         Exception {
88                 
89                 if (owners!=null) { System.out.println("Owners obtained directly from business logic layer");
90                                                         return owners; }
91                 else return owners=dB4oManager.getOwners();
92         }
93                 
94         public Vector<RuralHouse> getAllRuralHouses() throws RemoteException,
95         Exception {
96                 
97                 if (ruralHouses!=null) { System.out.println("RuralHouses obtained directly from business logic layer");
98                                                                  return ruralHouses; }
99                 else return ruralHouses=dB4oManager.getAllRuralHouses();
100
101         }
102         
103         public void close() throws RemoteException{
104                 dB4oManager.close();
105
106         }
107
108         }
109