package businessLogic; import java.util.Date; import java.util.Vector; import javax.mail.MessagingException; import com.db4o.ObjectContainer; import com.db4o.ObjectSet; import dataAccess.DB4oManager; import domain.Booking; import domain.Client; import domain.RuralHouse; import exceptions.OfferCanNotBeBooked; public final class BookingManager { private int bookingNumber = 0; dataAccess.DB4oManager dbMngr; private static BookingManager theBookingManager; public BookingManager() { try { this.dbMngr = DB4oManager.getInstance(); } catch (Exception e) { e.printStackTrace(); } } /** * This method returns the next Booking number * * @return the book number */ public static int getNumber() { ObjectContainer db = DB4oManager.getContainer(); BookingManager b = getInstance(); b.bookingNumber++; db.store(b); db.commit(); return b.bookingNumber; } /** * This method returns the instance of the BookingManager class * * @return the booking manager */ public static BookingManager getInstance() { ObjectContainer db = DB4oManager.getContainer(); BookingManager b = new BookingManager(); ObjectSet result = db.queryByExample(b); if (!result.hasNext()) { theBookingManager = new BookingManager(); db.store(theBookingManager); db.commit(); } else theBookingManager = (BookingManager) result.next(); return theBookingManager; } public void removeDenyBooking(Booking b){ b.getOffer().getBookings().remove(b); this.dbMngr.removeBooking(b); } public void acceptBooking(Booking b){ b.getOffer().setBooked(true); for(Booking boo : b.getOffer().getBookings()){ if(!boo.equals(b)) b.getOffer().getBookings().remove(b); } this.dbMngr.acceptBooking(b.getOffer()); try { MailManager.getInstance().Send(b.getClient().getMailAccount(), "Your booking has been accepted","Here should be the bill"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * This method creates a book with a corresponding parameters * * @param First * day, last day, house number and telephone * @return a book */ public Vector createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, Client client) throws OfferCanNotBeBooked { return dbMngr.createBooking(ruralHouse, firstDate, lastDate, client); } }