Some bugs have been fixed
[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                                 try {
79                                         MailManager.getInstance().Send(b.getClient().getMailAccount(), "Your booking has not been accepted","We are sorry");
80                                 } catch (MessagingException e) {
81                                         e.printStackTrace();
82                                 }
83         }
84
85
86         public void acceptBooking(Booking b) throws RemoteException{
87                 b.getOffer().setBooked(true);
88                 b.getOffer().getBookings().clear();
89                 b.getOffer().getBookings().add(b);
90                 this.dbMngr.acceptBooking(b.getOffer());
91                 try {
92                         MailManager.getInstance().Send(b.getClient().getMailAccount(), "Your booking has been accepted","Here should be the bill");
93                 } catch (MessagingException e) {
94                         e.printStackTrace();
95                 }
96                 
97         }
98         /**
99          * This method creates a book with a corresponding parameters
100          * 
101          * @param First
102          *            day, last day, house number and telephone
103          * @return a book
104          */
105         public Vector<Booking> createBooking(RuralHouse ruralHouse, Date firstDate,
106                         Date lastDate, Client client)
107                         throws OfferCanNotBeBooked,RemoteException {
108
109                 return dbMngr.createBooking(ruralHouse, firstDate, lastDate,
110                                 client);
111         }
112
113 }