Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / BookingManager.java
1 package businessLogic;
2
3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.Date;
6 import java.util.Vector;
7
8 import javax.mail.MessagingException;
9
10 import com.db4o.ObjectContainer;
11 import com.db4o.ObjectSet;
12 import common.BookingInterface;
13
14 import dataAccess.DB4oManager;
15 import domain.Booking;
16 import domain.Client;
17 import domain.RuralHouse;
18 import exceptions.OfferCanNotBeBooked;
19
20
21
22 public final class BookingManager extends UnicastRemoteObject implements BookingInterface {
23
24         /**
25          * 
26          */
27         private static final long serialVersionUID = 1L;
28         private int bookingNumber = 0;
29         dataAccess.DB4oManager dbMngr;
30
31         private static BookingManager theBookingManager;
32
33         public BookingManager() throws RemoteException {
34                 super();
35                 try {
36                         this.dbMngr = DB4oManager.getInstance();
37                 } catch (Exception e) {
38                         e.printStackTrace();
39                 }
40         }
41
42         /**
43          * This method returns the next Booking number
44          * 
45          * @return the book number
46          */
47
48     public int getNumber() throws RemoteException{
49                 ObjectContainer db=DB4oManager.getContainer();
50         BookingManager b=getInstance();
51         b.bookingNumber++;
52         db.store(b);
53         db.commit();
54         return b.bookingNumber;
55     }
56         
57         /**
58          * This method returns the instance of the BookingManager class
59          * 
60          * @return the booking manager
61          * @throws RemoteException 
62          */
63
64         public BookingManager getInstance() throws RemoteException  {
65                 ObjectContainer db=DB4oManager.getContainer();
66             BookingManager b = new BookingManager();
67             ObjectSet<BookingManager> result = db.queryByExample(b);
68             if (!result.hasNext()){
69                 theBookingManager = new BookingManager();
70                 db.store(theBookingManager);
71                 db.commit();
72             } else theBookingManager=(BookingManager)result.next();
73                 return theBookingManager;
74         }
75
76         public void denyBooking(Booking b) throws RemoteException{
77                                 this.dbMngr.removeBooking(b);
78         }
79
80
81         public void acceptBooking(Booking b) throws RemoteException{
82                 b.getOffer().setBooked(true);
83                 b.getOffer().getBookings().clear();
84                 b.getOffer().getBookings().add(b);
85                 this.dbMngr.acceptBooking(b.getOffer());
86                 try {
87                         MailManager.getInstance().Send(b.getClient().getMailAccount(), "Your booking has been accepted","Here should be the bill");
88                 } catch (MessagingException e) {
89                         e.printStackTrace();
90                 }
91                 
92         }
93         /**
94          * This method creates a book with a corresponding parameters
95          * 
96          * @param First
97          *            day, last day, house number and telephone
98          * @return a book
99          */
100         public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate,
101                         Date lastDate, Client client)
102                         throws OfferCanNotBeBooked,RemoteException {
103
104                 return dbMngr.createBooking(ruralHouse, firstDate, lastDate,
105                                 client);
106         }
107
108 }