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