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