3 import java.rmi.RemoteException;
6 import com.db4o.ObjectContainer;
7 import com.db4o.ObjectSet;
9 import dataAccess.DB4oManager;
10 import domain.Booking;
12 import domain.RuralHouse;
13 import exceptions.BadDates;
14 import exceptions.OfferCanNotBeBooked;
15 import exceptions.OverlappingOfferExists;
17 public final class OfferManager {
19 private int offerNumber = 0;
20 dataAccess.DB4oManager dbMngr;
21 private static OfferManager theOfferManager;
23 public OfferManager() {
25 this.dbMngr = DB4oManager.getInstance();
26 } catch (Exception e) {
31 public static int getNumber() {
32 ObjectContainer db=DB4oManager.getContainer();
33 OfferManager o=getInstance();
41 * This method returns the instance of the OfferManager class
43 * @return the offer manager
45 public static OfferManager getInstance() {
46 ObjectContainer db=DB4oManager.getContainer();
47 OfferManager b = new OfferManager();
48 ObjectSet result = db.queryByExample(b);
49 if (!result.hasNext()){
50 theOfferManager = new OfferManager();
51 db.store(theOfferManager);
53 } else theOfferManager=(OfferManager)result.next();
54 return theOfferManager;
58 * This method creates an offer with a house number, first day, last day and price
61 * number, start day, last day and price
62 * @return the created offer, or null, or an exception
64 public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
65 float price) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
66 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
68 boolean b = dbMngr.existsOverlappingOffer(ruralHouse,firstDay,lastDay); // The ruralHouse object in the client may not be updated
70 ruralHouse.createOffer(offerNumber, firstDay, lastDay, price);
71 return dbMngr.createOffer(ruralHouse,firstDay,lastDay,price);
76 public Offer modifyOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay,
77 float price, Offer offer) throws OverlappingOfferExists, BadDates, RemoteException, Exception {
78 if (firstDay.compareTo(lastDay)>=0) throw new BadDates();
79 offer.setFirstDay(firstDay);
80 offer.setLastDay(lastDay);
81 offer.setPrice(price);
83 return dbMngr.modifyOffer(offer);
86 public void deleteOffer(RuralHouse rh, Offer o) throws RemoteException, Exception{
87 rh.offers.removeElement(o);
88 dbMngr.deleteOffer(rh, o);